Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

blockchain - Is it possible to reserve an address on Solidity smart contract creation for taxes collection?

I am playing with solidity programming language and trying to implement a contract that every made transaction is deducted with tax fee and this tax fee should be transferred to some specific address of a contract which is being created. Is that possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, it's possible. All Ethereum token standards (ERC-20, ERC-721, ...) only define an interface and few other points (such as when to emit events). So you're free to implement the methods however you need.


Let's say you have a very simple transfer() implementation without the fees.

Note: This doesn't follow the ERC-* standards and is vulnerable to integer overflow in Solidity <= 0.7.6. I've simplified it to better show the calculations.

function transfer(address _to, uint256 _amount) external {
    balances[msg.sender] -= _amount;
    balances[_to] += _amount;
}

Adding the fees is just a matter of a small calculation:

address admin = address(0x123);

function transfer(address _to, uint256 _amount) external returns (bool) {
    uint256 fee = (_amount / 100) * 3; // Calculate 3% fee

    balances[msg.sender] -= _amount; // subtract the full amount
    balances[admin] += fee; // add the fee to the admin balance
    balances[_to] += (_amount - fee); // add the remainder to the recipient balance
}

Note: This is to demonstrate the basics, and doesn't take into account few cases such as _amount having value not divisible by 100 (the fee is not going to be precisely 3% in that case).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...