在Solidity中执行买卖



因此,我希望能够购买/出售代币,但也希望用户能够将eth发送到我的合同钱包并接收我的代币作为交换。我相信我已经准备好了供买家和卖家一起进行交易的代码,但我不认为我有足够的代码让别人接收发送给我以太坊的代币。我想这样做,所以一开始人们会给我寄一些以基本值设置的硬币的eth

pragma solidity 0.4.22;
contract ERC20Basic {
string public constant name = "Community Token";
string public constant symbol = "COMM";
uint8 public constant decimals = 1;  

event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
event Transfer(address indexed from, address indexed to, uint tokens);

mapping(address => uint256) balances;
mapping(address => mapping (address => uint256)) allowed;

uint256 totalSupply_;
using SafeMath for uint256;

constructor(uint256 total) public {  
totalSupply_ = total;
balances[msg.sender] = totalSupply_;
}  
function totalSupply() public view returns (uint256) {
return totalSupply_;
}

function balanceOf(address tokenOwner) public view returns (uint) {
return balances[tokenOwner];
}
function transfer(address receiver, uint numTokens) public returns (bool) {
require(numTokens <= balances[msg.sender]);
balances[msg.sender] = balances[msg.sender].sub(numTokens);
balances[receiver] = balances[receiver].add(numTokens);
emit Transfer(msg.sender, receiver, numTokens);
return true;
}
function approve(address delegate, uint numTokens) public returns (bool) {
allowed[msg.sender][delegate] = numTokens;
emit Approval(msg.sender, delegate, numTokens);
return true;
}
function allowance(address owner, address delegate) public view returns (uint) {
return allowed[owner][delegate];
}
function transferFrom(address owner, address buyer, uint numTokens) public returns (bool) {
require(numTokens <= balances[owner]);    
require(numTokens <= allowed[owner][msg.sender]);

balances[owner] = balances[owner].sub(numTokens);
allowed[owner][msg.sender] = allowed[owner][msg.sender].sub(numTokens);
balances[buyer] = balances[buyer].add(numTokens);
emit Transfer(owner, buyer, numTokens);
return true;
}
}
library SafeMath { 
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}

function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}

买卖的一个非常基本的例子:

pragma solidity ^0.8;
contract ERC20Basic{
uint256 public constant tokenPrice = 5; // 1 token for 5 wei

function buy(uint256 _amount) external payable {
// e.g. the buyer wants 100 tokens, needs to send 500 wei
require(msg.value == _amount * tokenPrice, 'Need to send exact amount of wei');

/*
* sends the requested amount of tokens
* from this contract address
* to the buyer
*/
transfer(msg.sender, _amount);
}

function sell(uint256 _amount) external {
// decrement the token balance of the seller
balances[msg.sender] -= _amount;
increment the token balance of this contract
balances[address(this)] += _amount;
/*
* don't forget to emit the transfer event
* so that external apps can reflect the transfer
*/
emit Transfer(msg.sender, address(this), _amount);

// e.g. the user is selling 100 tokens, send them 500 wei
payable(msg.sender).transfer(amount * tokenPrice);
}
}

这将允许任何用户从您的合同中购买或出售代币。您的合同需要拥有这些代币才能将其出售给用户。此外,您的合同需要有足够的ETH才能从用户那里回购代币。

您可以扩展此代码以实现

  • 有权更改价格的合同所有者
  • 买卖价格不同
  • 费用
  • 最小/最大金额(每个交易、每个用户、每天…(
  • 基于msg.value计算代币数量(用户不必有确切的数量(
  • 等等

请注意,我的代码段使用的是Solidity 0.8,其中可以自动防止整数溢出。问题是使用了不推荐使用的Solidity 0.4,因此您需要使用SafeMath,使用require/assert检查值,或者升级到Solidity 0.8以获得相同的结果。

最新更新