[英]How to send ethers to a contract?
I have written the FarihaToken which is ERC20 standard complaint, the code is below: 我已经写了FarihaToken,这是ERC20标准投诉,代码如下:
contract FarihaToken is ERC20Interface{
using SafeMath for uint;
string public symbol;
string public name;
uint8 public decimals;
uint public _totalSupply;
mapping(address => uint) balances;
mapping(address => mapping(address => uint)) allowed;
// ------------------------------------------------------------------------
// Constructor
// ------------------------------------------------------------------------
function FarihaToken() public{
symbol = "FTC";
name = "Fariha Token";
decimals = 18;
_totalSupply = totalSupply();
balances[msg.sender] = _totalSupply;
Transfer(address(0),msg.sender,_totalSupply);
}
function totalSupply() public constant returns (uint){
return 1000000 * 10**uint(decimals);
}
// ------------------------------------------------------------------------
// Get the token balance for account `tokenOwner`
// ------------------------------------------------------------------------
function balanceOf(address tokenOwner) public constant returns (uint balance) {
return balances[tokenOwner];
}
// ------------------------------------------------------------------------
// Transfer the balance from token owner's account to `to` account
// - Owner's account must have sufficient balance to transfer
// - 0 value transfers are allowed
// ------------------------------------------------------------------------
function transfer(address to, uint tokens) public returns (bool success){
balances[msg.sender] = balances[msg.sender].sub(tokens);
balances[to] = balances[to].add(tokens);
Transfer(msg.sender,to,tokens);
return true;
}
// ------------------------------------------------------------------------
// Token owner can approve for `spender` to transferFrom(...) `tokens`
// from the token owner's account
//
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
// recommends that there are no checks for the approval double-spend attack
// as this should be implemented in user interfaces
// ------------------------------------------------------------------------
function approve(address spender, uint tokens) public returns (bool success){
allowed[msg.sender][spender] = tokens;
Approval(msg.sender,spender,tokens);
return true;
}
// ------------------------------------------------------------------------
// Transfer `tokens` from the `from` account to the `to` account
//
// The calling account must already have sufficient tokens approve(...)-d
// for spending from the `from` account and
// - From account must have sufficient balance to transfer
// - Spender must have sufficient allowance to transfer
// - 0 value transfers are allowed
// ------------------------------------------------------------------------
function transferFrom(address from, address to, uint tokens) public returns (bool success){
balances[from] = balances[from].sub(tokens);
allowed[from][to] = allowed[from][to].sub(tokens);
balances[to] = balances[to].add(tokens);
Transfer(msg.sender,to,tokens);
return true;
}
// ------------------------------------------------------------------------
// Returns the amount of tokens approved by the owner that can be
// transferred to the spender's account
// ------------------------------------------------------------------------
function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
return allowed[tokenOwner][spender];
}
}
contract PurchaseToken is FarihaToken{
// Accept ETH and return FTC
function() public payable {
var amount = msg.value/2 ;
require(balanceOf(this) >= amount);
transfer(msg.sender,amount);
}
}
Use case is that when some one sends 2 ethers it'll send back 1 token. 用例是,当有人发送2个以太币时,它将回送1个令牌。 I'm using truffle as dev framework, I'm newbie to this so I have some questions to ask if someone can help me with them: 我使用松露作为开发框架,对此是新手,所以我有一些问题要问是否有人可以帮助我:
I want to send ethers. 我想寄醚。 How can I do that? 我怎样才能做到这一点? which account to use? 使用哪个帐户? a truffle command will be useful. 松露命令将很有用。
I want to make sure that the account that send ethers have received tokens as well. 我要确保发送以太币的帐户也已收到令牌。 Command would be useful. 命令会很有用。
Is there a way I can see my ethers in account using truffle ? 有什么办法可以使用松露查看账户中的以太币吗?
PS: I tried running this on truffle but it reverts the action. PS:我尝试在松露上运行此命令,但它会还原操作。
PurchaseToken.deployed().then(function(D){ff=D;}); PurchaseToken.deployed()。then(function(D){ff = D;}); ff.send(2) ff.send(2)
Transfer of tokens means you will deduct the tokens from contract account and add these token to msg.sender. 令牌转移意味着您将从合同帐户中扣除令牌,并将这些令牌添加到msg.sender。 Please go through the following contract which consists of the functionality- https://github.com/ganeshdipdumbare/coin-with-whitelisting/blob/master/GCoinAdvance.sol 请通过以下包含功能的合同-https: //github.com/ganeshdipdumbare/coin-with-whitelisting/blob/master/GCoinAdvance.sol
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.