【发布时间】:2022-01-06 01:02:03
【问题描述】:
我正在使用truffle console 来部署在Ganache 网络上的交互式智能合约。并使用以下代码将硬币发送到其他帐户:
truffle(development)> let instance = await MetaCoin.deployed()
truffle(development)> let accounts = await web3.eth.getAccounts()
instance.sendCoin(accounts[1], 500)
这样做之后,我看到交易从account[0] 到account[1]。我不明白的是为什么它选择account[0] 作为来源。这是默认行为吗?如何选择其他帐户?
合约代码为:
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.25 <0.7.0;
import "./ConvertLib.sol";
// This is just a simple example of a coin-like contract.
// It is not standards compatible and cannot be expected to talk to other
// coin/token contracts. If you want to create a standards-compliant
// token, see: https://github.com/ConsenSys/Tokens. Cheers!
contract MetaCoin {
mapping (address => uint) balances;
event Transfer(address indexed _from, address indexed _to, uint256 _value);
constructor() public {
balances[tx.origin] = 10000;
}
function sendCoin(address receiver, uint amount) public returns(bool sufficient) {
if (balances[msg.sender] < amount) return false;
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Transfer(msg.sender, receiver, amount);
return true;
}
function getBalanceInEth(address addr) public view returns(uint){
return ConvertLib.convert(getBalance(addr),2);
}
function getBalance(address addr) public view returns(uint) {
return balances[addr];
}
}
【问题讨论】:
标签: blockchain ethereum solidity truffle ganache