让我们假设ERC20令牌合同并检查不同的用例。
首先需要注意的是,所有转移只能通过调用合同来完成,而不是通过直接相互通信来完成。现在用例...
的 令牌持有者A - >令牌持有人B: 强> 在这种情况下,持有人A想要将一些令牌转移到持有人B.如果您使用持有人A的私钥签署交易,那么您只需致电 transfer 合同履行职责 contractAddress 。这就是你通过在下面的代码中发送交易所做的事情(如果是这样的话会有效) myAddress 上面有令牌的余额 noOfTokens 在合同中 contractAddress )
transfer
contractAddress
myAddress
noOfTokens
var rawTransaction = { "from": myAddress, "nonce": "0x" + lastCountOfTransaction.toString(16), "gasPrice": web3.utils.toHex(1 * 1e9), //1 can be changed to n gwei "gasLimit": web3.utils.toHex(1000000), // 1000000 can be to set to any n number "to": contractAddress, "value": "0x0", "data": ContractObject.methods.transfer(userAddress, noOfTokens).encodeABI(), "chainId": chainId };
的 令牌持有者A - >令牌持有者B通过地址C: 强> 在这种情况下,地址C希望代表持有人A进行交易。因此,要调用的标准函数是 transferFrom 。假设从持有人A到地址C已经给予某种批准,那就是 myAddress 指地址C,可以使用下面的交易
transferFrom
var rawTransaction = { "from": myAddress, // refers to Address C "nonce": "0x" + lastCountOfTransaction.toString(16), "gasPrice": web3.utils.toHex(1 * 1e9), //1 can be changed to n gwei "gasLimit": web3.utils.toHex(1000000), // 1000000 can be to set to any n number "to": contractAddress, "value": "0x0", "data": ContractObject.methods.transferFrom(userAddressA, userAddressB, noOfTokens).encodeABI(), "chainId": chainId };
就ERC20而言。参考 https://theethereum.wiki/w/index.php/ERC20_Token_Standard 了解更多信息。 你在做什么感觉像铸造,而ERC20并没有标准化。 你在这里可以做的是设置合同的所有者并使用像这样的修饰符 onlyOwner 为一个 mint 合同中的功能。这样的 mint 功能可以看起来像
onlyOwner
mint
function mint(address _to, uint256 _amount) onlyOwner canMint returns (bool) { totalSupply = totalSupply.add(_amount); balances[_to] = balances[_to].add(_amount); Mint(_to, _amount); return true; }
这样你就可以打电话了 mint 功能来自 myAddress (如果该地址是所有者)并通过 userAddress 谁应该收到代币。你也可以选择保持一个上限,并通过引入检查使标记上限 mint 功能或添加一个额外的修饰符(如 canMint )它。你可以看看 https://solidity.readthedocs.io/en/develop/contracts.html#function-modifiers 有关修饰符的更多信息。
userAddress
canMint