Transfers and approval of ERC-20 tokens from a solidity smart contract
In the previous tutorial we studied the anatomy of an ERC-20 token in Solidity on the Ethereum blockchain. In this article we’ll see how we can use a smart contract to interact with a token using the Solidity language.
For this smart contract, we’ll create a really dummy decentralized exchange where a user can trade Ethereum with our newly deployed ERC-20 token.
For this tutorial we’ll use the code we wrote in the previous tutorial as a base. Our DEX will instantiate an instance of the contract in its constructor and perform the operations of:
- exchanging tokens to ether
- exchanging ether to tokens
We’ll start our Decentralized exchange code by adding our simple ERC20 codebase:
1pragma solidity ^0.6.0;23interface IERC20 {45 function totalSupply() external view returns (uint256);6 function balanceOf(address account) external view returns (uint256);7 function allowance(address owner, address spender) external view returns (uint256);89 function transfer(address recipient, uint256 amount) external returns (bool);10 function approve(address spender, uint256 amount) external returns (bool);11 function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);121314 event Transfer(address indexed from, address indexed to, uint256 value);15 event Approval(address indexed owner, address indexed spender, uint256 value);16}171819contract ERC20Basic is IERC20 {2021 string public constant name = "ERC20Basic";22 string public constant symbol = "ERC";23 uint8 public constant decimals = 18;242526 event Approval(address indexed tokenOwner, address indexed spender, uint tokens);27 event Transfer(address indexed from, address indexed to, uint tokens);282930 mapping(address => uint256) balances;3132 mapping(address => mapping (address => uint256)) allowed;3334 uint256 totalSupply_ = 100 ether;3536 using SafeMath for uint256;3738 constructor(uint256 total) public {39 balances[msg.sender] = totalSupply_;40 }4142 function totalSupply() public override view returns (uint256) {43 return totalSupply_;44 }4546 function balanceOf(address tokenOwner) public override view returns (uint256) {47 return balances[tokenOwner];48 }4950 function transfer(address receiver, uint256 numTokens) public override returns (bool) {51 require(numTokens <= balances[msg.sender]);52 balances[msg.sender] = balances[msg.sender].sub(numTokens);53 balances[receiver] = balances[receiver].add(numTokens);54 emit Transfer(msg.sender, receiver, numTokens);55 return true;56 }5758 function approve(address delegate, uint256 numTokens) public override returns (bool) {59 allowed[msg.sender][delegate] = numTokens;60 emit Approval(msg.sender, delegate, numTokens);61 return true;62 }6364 function allowance(address owner, address delegate) public override view returns (uint) {65 return allowed[owner][delegate];66 }6768 function transferFrom(address owner, address buyer, uint256 numTokens) public override returns (bool) {69 require(numTokens <= balances[owner]);70 require(numTokens <= allowed[owner][msg.sender]);7172 balances[owner] = balances[owner].sub(numTokens);73 allowed[owner][msg.sender] = allowed[owner][msg.sender].sub(numTokens);74 balances[buyer] = balances[buyer].add(numTokens);75 emit Transfer(owner, buyer, numTokens);76 return true;77 }78}7980library SafeMath {81 function sub(uint256 a, uint256 b) internal pure returns (uint256) {82 assert(b <= a);83 return a - b;84 }8586 function add(uint256 a, uint256 b) internal pure returns (uint256) {87 uint256 c = a + b;88 assert(c >= a);89 return c;90 }91}92Alles zeigenKopieren
Our new DEX smart contract will deploy the ERC-20 and get all the supplied:
1contract DEX {23 IERC20 public token;45 event Bought(uint256 amount);6 event Sold(uint256 amount);78 constructor() public {9 token = new ERC20Basic();10 }1112 function buy() payable public {13 // TODO14 }1516 function sell(uint256 amount) public {17 // TODO18 }1920}21Alles zeigenKopieren
So we now have our DEX and it has all the token reserve available. The contract has two functions:
buy
: The user can send ether and get tokens in exchangesell
: The user can decide to send tokens to get ether back
The buy function
Let’s code the buy function. We’ll first need to check the amount of ether the message contains and verify that the contracts own enough tokens and that the message has some ether in it. If the contract owns enough tokens it’ll send the number of tokens to the user and emit the Bought
event.
Note that if we call the require function in the case of an error the ether sent will directly be reverted and given back to the user.
To keep things simple, we just exchange 1 token for 1 ether.
1function buy() payable public {2 uint256 amountTobuy = msg.value;3 uint256 dexBalance = token.balanceOf(address(this));4 require(amountTobuy > 0, "You need to send some ether");5 require(amountTobuy <= dexBalance, "Not enough tokens in the reserve");6 token.transfer(msg.sender, amountTobuy);7 emit Bought(amountTobuy);8}9Kopieren
In the case where the buy is successful we should see two events in the transaction: The token Transfer
and the Bought
event.
The sell function
The function responsible for the sell will first require the user to have approved the amount by calling the approve function beforehand. Then when the sell function is called, we’ll check if the transfer from the caller address to the contract address was successful and then send the Ethers back to the caller address.
1function sell(uint256 amount) public {2 require(amount > 0, "You need to sell at least some tokens");3 uint256 allowance = token.allowance(msg.sender, address(this));4 require(allowance >= amount, "Check the token allowance");5 token.transferFrom(msg.sender, address(this), amount);6 msg.sender.transfer(amount);7 emit Sold(amount);8}9Kopieren
If everything works you should see 2 events (a Transfer
and Sold
) in the transaction and your token balance and Ethereum balance updated.
From this tutorial we saw how to check the balance and allowance of an ERC-20 token and also how to call Transfer
and TransferFrom
of an ERC20 smart contract using the interface.
Once you make a transaction we have a Javascript tutorial to wait and get details about the transactions that were made to your contract and a tutorial to decode events generated by token transfers or any other events as long as you have the ABI.
Here is the complete code for the tutorial:
1pragma solidity ^0.6.0;23interface IERC20 {45 function totalSupply() external view returns (uint256);6 function balanceOf(address account) external view returns (uint256);7 function allowance(address owner, address spender) external view returns (uint256);89 function transfer(address recipient, uint256 amount) external returns (bool);10 function approve(address spender, uint256 amount) external returns (bool);11 function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);121314 event Transfer(address indexed from, address indexed to, uint256 value);15 event Approval(address indexed owner, address indexed spender, uint256 value);16}171819contract ERC20Basic is IERC20 {2021 string public constant name = "ERC20Basic";22 string public constant symbol = "ERC";23 uint8 public constant decimals = 18;242526 event Approval(address indexed tokenOwner, address indexed spender, uint tokens);27 event Transfer(address indexed from, address indexed to, uint tokens);282930 mapping(address => uint256) balances;3132 mapping(address => mapping (address => uint256)) allowed;3334 uint256 totalSupply_ = 10 ether;3536 using SafeMath for uint256;3738 constructor() public {39 balances[msg.sender] = totalSupply_;40 }4142 function totalSupply() public override view returns (uint256) {43 return totalSupply_;44 }4546 function balanceOf(address tokenOwner) public override view returns (uint256) {47 return balances[tokenOwner];48 }4950 function transfer(address receiver, uint256 numTokens) public override returns (bool) {51 require(numTokens <= balances[msg.sender]);52 balances[msg.sender] = balances[msg.sender].sub(numTokens);53 balances[receiver] = balances[receiver].add(numTokens);54 emit Transfer(msg.sender, receiver, numTokens);55 return true;56 }5758 function approve(address delegate, uint256 numTokens) public override returns (bool) {59 allowed[msg.sender][delegate] = numTokens;60 emit Approval(msg.sender, delegate, numTokens);61 return true;62 }6364 function allowance(address owner, address delegate) public override view returns (uint) {65 return allowed[owner][delegate];66 }6768 function transferFrom(address owner, address buyer, uint256 numTokens) public override returns (bool) {69 require(numTokens <= balances[owner]);70 require(numTokens <= allowed[owner][msg.sender]);7172 balances[owner] = balances[owner].sub(numTokens);73 allowed[owner][msg.sender] = allowed[owner][msg.sender].sub(numTokens);74 balances[buyer] = balances[buyer].add(numTokens);75 emit Transfer(owner, buyer, numTokens);76 return true;77 }78}7980library SafeMath {81 function sub(uint256 a, uint256 b) internal pure returns (uint256) {82 assert(b <= a);83 return a - b;84 }8586 function add(uint256 a, uint256 b) internal pure returns (uint256) {87 uint256 c = a + b;88 assert(c >= a);89 return c;90 }91}9293contract DEX {9495 event Bought(uint256 amount);96 event Sold(uint256 amount);979899 IERC20 public token;100101 constructor() public {102 token = new ERC20Basic();103 }104105 function buy() payable public {106 uint256 amountTobuy = msg.value;107 uint256 dexBalance = token.balanceOf(address(this));108 require(amountTobuy > 0, "You need to send some ether");109 require(amountTobuy <= dexBalance, "Not enough tokens in the reserve");110 token.transfer(msg.sender, amountTobuy);111 emit Bought(amountTobuy);112 }113114 function sell(uint256 amount) public {115 require(amount > 0, "You need to sell at least some tokens");116 uint256 allowance = token.allowance(msg.sender, address(this));117 require(allowance >= amount, "Check the token allowance");118 token.transferFrom(msg.sender, address(this), amount);119 msg.sender.transfer(amount);120 emit Sold(amount);121 }122123}124Alles zeigenKopieren