gpt4 book ai didi

blockchain - 在 Solidity 中实现买卖

转载 作者:行者123 更新时间:2023-12-05 03:42:06 29 4
gpt4 key购买 nike

所以我希望能够买卖代币,但也希望用户能够将 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 才能从用户那里 repo 代币。

你可以扩展这段代码来实现

  • 有权更改价格的契约(Contract)所有者
  • 买卖价格不同
  • 费用
  • 最小/最大金额(每笔交易、每位用户、每天……)
  • 根据 msg.value 计算代币数量(用户不必知道确切数量)
  • 等...

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

关于blockchain - 在 Solidity 中实现买卖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67409550/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com