如何使用python/solidity生成钱包并向其转移/铸造代币



我制作了一个简单的bep 20代币,我正试图将我的钱包地址铸造为代币供应量的50%,并将剩余的50%均匀分布在我想用代码生成的10个不同钱包之间。我不确定这是可以在合同本身内完成,还是必须在合同部署后通过python单独完成。

这是坚固性代码:


contract Token {
mapping(address => uint) public balances;
mapping(address => mapping(address => uint)) public allowance;
uint public totalSupply = 10000000000;
string public name = 'TestToken';
string public symbol = 'TEST';
uint public decimals = 9;

event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owener, address indexed spender, uint value);

constructor() {
balances[msg.sender] = totalSupply;
}
function balanceOf(address owner) public view returns(uint) {
return balances[owner];
}

function transfer(address to, uint value) public returns(bool) {
require(balanceOf(msg.sender)>= value, 'You are broke lol');
balances[to] += value;
balances[msg.sender] -= value;
emit Transfer(msg.sender, to, value);
return true;
}

function transferFrom(address from, address to, uint value) public returns(bool) {
require(balanceOf(from) >= value, 'You broke');
require(allowance[from][msg.sender] >= value, 'allowance too low');
balances[to] += value;
balances[from] -= value;
emit Transfer(from, to, value);
return true;
}

function approve(address spender, uint value) public returns(bool) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
}```

您可以实现mint()函数,将前50%分配给msg.sender(或硬编码地址(,将另50%分配给函数参数中传递的10个地址。

您的python脚本(或任何其他客户端、钱包软件、JS脚本等(生成10个地址并在参数中传递。JS中使用web3库的示例:

const Web3 = require('web3');
const web3 = new Web3(providerUrl); // TODO fill your value
const contract = new web3.eth.Contract(abiJson, contractAddress); // TODO fill your values
const mintTokens = async () {
const amount = web3.utils.toWei('1', 'ether');
const addresses = await getRandomAddresses();
await contract.methods.mint(amount, addresses).send();
}
const getRandomAddresses = async () {
let addresses = [];
for (let i = 0; i < 10; i++) {
const account = web3.eth.accounts.create();
addresses.push(account.address);
}
return addresses;
}
mintTokens();

请注意,这个例子要求_amount可以被20整除。否则,它将导致轻微的错误结果(总计高达5%(。

另外,在我的例子中没有授权,所以任何人都可以执行这个函数。您可能还想实现授权(例如,使用ownable模式(,或者将函数标记为internal(这样任何人都不能执行它(,并仅从constructor()调用它。

pragma solidity ^0.8.4;
contract Token {
mapping(address => uint) public balances;
event Transfer(address indexed from, address indexed to, uint value);

function mint(uint256 _amount, address[10] memory _receivers) external {
// mint 50% of the _amount to one address
balances[msg.sender] += _amount / 2;
emit Transfer(address(0x0), msg.sender, _amount / 2);

// mint the rest (another 50%) evenly to each receiver
// i.e. each gets 5%
for (uint i = 0; i < 10; i++) {
balances[_receivers[i]] += _amount / 20;
emit Transfer(address(0x0), _receivers[i], _amount / 20);
}
}
}

相关内容

  • 没有找到相关文章

最新更新