Cannot transfer tokens from contract to wallet

I am deploying the contract successfully with a fixed amount of tokens. Now I wish to transfer them to my wallet either at deployment or after, but none of my functions work.

:computer: Environment
Env: Remix pragma solidity ^0.6.0;
Contracts: https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/token/ERC777

When I create a function to transfer the funds to my wallet, the function attempts to transfer the balance from my wallet to the contract.

+++Trying to do it at deployment

constructor(
    string memory name,
    string memory symbol,
    uint256 startSupply,
    address[] memory defaultOperators
) public {
    _name = name;
    _symbol = symbol;
    _totalSupply = startSupply;
    _balances[msg.sender] = _totalSupply;
    _defaultOperatorsArray = defaultOperators;
    for (uint256 i = 0; i < _defaultOperatorsArray.length; i++) {
        _defaultOperators[_defaultOperatorsArray[i]] = true;
    }

    // register interfaces
    _ERC1820_REGISTRY.setInterfaceImplementer(address(this), keccak256("ERC777Token"), address(this));
    _ERC1820_REGISTRY.setInterfaceImplementer(address(this), keccak256("ERC20Token"), address(this));
}

+++Trying to do it as a function

function testtoken() public {
_balances[msg.sender]  = _totalSupply        // Give the creator all initial tokens;
}
1 Like

Hi @DuckSoldier,

Welcome to the community :wave:

To use OpenZeppelin Contracts you should import the contracts. In Remix you can import via GitHub.

To keep your system secure, you should always use the installed code as-is, and neither copy-paste it from online sources, nor modify it yourself.

See the ERC20 example using OpenZeppelin Contracts 2.5:

Also see Remix documentation for importing via GitHub:
https://remix-ide.readthedocs.io/en/latest/import.html#importing-from-github

The contracts in the OpenZeppelin Contracts repository master branch are part of the upcoming OpenZeppelin Contracts 3.0 (currently at OpenZeppelin Contracts v3.0 final release candidate) and use Solidity 0.6.

I have a simple ERC777 token (using OpenZeppelin 2.5) which can be deployed using OpenZeppelin CLI or Truffle (I would need to put together an example that works in Remix):

Hi @abcoathup

Thanks for the nice welcome!

I have originally deployed the ERC777 contract as is from github, but was unable to create the initial supply or transfer any amount to my own wallet in the Rinkbey Test environment. Have I misunderstood how the contract works? Would appreciate your guidance.

1 Like

Hi @DuckSoldier,

I suggest deploying the simple ERC20 token in Remix and playing with that.

I will have to try putting together a similar example for ERC777 that can be used in Remix (I just tried to do it now but had issues deploying, so will need to spend a bit more time on it in the next day or so).

OK, thanks for the quick answer and generous help. I will play around with ERC20 in the meantime. Where will you post the update once it has been modified for REMIX? Also, can you recommend some beginner level guide for truffle, would appreciate it?

1 Like

Hi @DuckSoldier,

When deploying to a local testnet we need to have the ERC1820 registry deployed.
Which is why I had issues last night deploying to a JavaScript VM, so instead deployed to a public testnet.

Below are versions of the Simple777Token that can be used on Remix.

:warning: Simple777Token has not been tested nor audited

Simple777Token.sol

Using OpenZeppelin Contracts 3.0

pragma solidity ^0.6.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.0/contracts/token/ERC777/ERC777.sol";

/**
 * @title Simple777Token
 * @dev Very simple ERC777 Token example, where all tokens are pre-assigned to the creator.
 * Note they can later distribute these tokens as they wish using `transfer` and other
 * `ERC20` or `ERC777` functions.
 * Based on https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/examples/SimpleToken.sol
 */
contract Simple777Token is ERC777 {

    /**
     * @dev Constructor that gives msg.sender all of existing tokens.
     */
    constructor () public ERC777("Simple777Token", "S7", new address[](0)) {
        _mint(msg.sender, 10000 * 10 ** 18, "", "");
    }
}

Simple777Token.sol

Using OpenZeppelin Contracts 2.5

pragma solidity ^0.5.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.0/contracts/token/ERC777/ERC777.sol";

/**
 * @title Simple777Token
 * @dev Very simple ERC777 Token example, where all tokens are pre-assigned to the creator.
 * Note they can later distribute these tokens as they wish using `transfer` and other
 * `ERC20` or `ERC777` functions.
 * Based on https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/examples/SimpleToken.sol
 */
contract Simple777Token is ERC777 {

    /**
     * @dev Constructor that gives msg.sender all of existing tokens.
     */
    constructor () public ERC777("Simple777Token", "S7", new address[](0)) {
        _mint(msg.sender, msg.sender, 10000 * 10 ** 18, "", "");
    }
}

Answered here: