Uniswap docking tutorial [DeFi]

Uniswap is a decentralized digital encrypted currency transaction protocol based on Ethereum. It provides a simple interface for token holders to exchange one ERC20 token for another, and the gas cost required for the transaction is very low. Blockchain developers can access Uniswap with their own ERC20 tokens to enhance their liquidity. In this tutorial, we will learn how to connect an ERC20 token to the Uniswap protocol, and provide the complete implementation source code at the end of the tutorial.

Use their own familiar language learning Ethernet Square DApp development : the Java | Php | Python | .Net / C # | golang | Node.JS | Flutter / Dart

1. Overview of Uniswap architecture

First of all, we need to understand the structure of the Uniswap protocol. It mainly contains two types of components-factory contracts and token exchange contracts:

Insert picture description here

Plant contract / Factory Contract to manage token exchange contracts, which is responsible for creating a new generation of currency swaps and maintain a token ERC20 each have their own tokens from the address mapping table token exchange contracts instance tokens Exchange Contract , which is responsible for completing the exchange of two tokens.

From a developer's point of view, the process of connecting an ERC20 token to the Uniswap protocol is as follows:

  1. Deploy the ERC20 token protocol to the Ethereum network
  2. Use Uniswap's factory contract to create an exchange contract for the previously deployed ERC20 tokens
  3. Use exchange contracts to exchange your own ERC20 tokens with other tokens

Each token exchange contract is intelligently used to exchange the corresponding ERC20 token with Ether. In order to complete the exchange of two different ERC20 tokens, Uniswap uses Ether as an intermediate bridge, for example, first exchange Token A into Ether, and then exchange Ether into Token B.

Insert picture description here

It is worth pointing out that the Uniswap protocol currently only supports Ethereum, and can only exchange ERC20 tokens on the same Ethereum network.

For demonstration purposes, in this tutorial we use the Rinkeby testnet to learn the access process, which is also applicable on the mainnet. Tutorial demonstrates the following code uses the software stack is completed: Truffle v5.0.3, Node.js v8.11.1, Web3.js-1.0.

2. ERC20 token contract development and deployment

First, let us write a simple ERC20 token contract for testing:

Insert picture description here

After saving, use the truffle compilecommand to compile the above contract:

$ truffle compile

Then modify truffle-config.jsto connect to the Rinkeby network:

rinkeby: {
     provider: function() {
     return new HDWalletProvider(process.env.NMEMORIC, "https://rinkeby.infura.io/")
     },
     network_id: '4',
     websockets: true,
     gas: 6000000,
     gasPrice: 10000000000
 },

Finally, use the truffle deploycommand to deploy the contract to the Rinkeby testnet:

$ truffle deploy --network rinkeby 
...
2_token.js
=================   Replacing 'Token'
   ----------------------
   > transaction hash:    0xd17a0dc69f54f78aa9a158682dc5bd24c281de959e3a7545ecb7d3c60cdadeab
   > Blocks: 0            Seconds: 12
   > contract address:    0xCC4d8eCFa6a5c1a84853EC5c0c08Cc54Cb177a6A
   > account:             0x0E364EB0Ad6EB5a4fC30FC3D2C2aE8EBe75F245c
   > balance:             2.57090605
   > gas used:            1214459
   > gas price:           10 gwei
   > value sent:          0 ETH
   > total cost:          0.01214459 ETH   > Saving artifacts
   -------------------------------------
   > Total cost:          0.01214459 ETH

You can see that our ERC20 tokens have been deployed to the following addresses on the Rinkeby testnet. Of course, your results should be different from this:

0xCC4d8eCFa6a5c1a84853EC5c0c08Cc54Cb177a6A

3. Create a token exchange contract in Uniswap

Uniswap's factory contract has been officially deployed to the following address of the Rinkeby testnet:

0xf5d915570bc477f9b8d6c0e980aa81757a3aac36

The ABI and source code of Factory Contract/Factory Contract can be viewed on Etherscan:

Insert picture description here

In order to create the Uniswap token exchange contract corresponding to our ERC20 token, we need to call the factory contract createExchange()method deployed by Uniswap . For example, in the following code we use Infura to send a signed transaction that calls this method:

Insert picture description here

Run the above code with node.js:

$ node 1.create.exchange.js

The results are similar to the following:

Insert picture description here

Note that if it prompts an error during runtime, the following error message:

Node error:{"code":-32601, "message": "The method eth_sendTransaction does not exist/is not available"},

It means that Infura does not support the eth_sendTransaction method, which means that you can only submit signed transactions to Infura, but cannot ask Infura to sign for you.

Now our ERC20 token exchange contract has been created inside Uniswap. Now we can use the following command to query the deployment address of this contract:

script/2.get.exchange.address.js

The output is as follows:

Insert picture description here

Also use node.js to run:

$ node script/2.get.exchange.address.js
the exchange address for ERC20 token is: 0x416F1Ac032D1eEE743b18296aB958743B1E61E81

This shows that we have successfully created the Uniswap exchange protocol for our ERC20 token contract.

4. Add liquidity to the Uniswap token exchange protocol

Now we can access the token exchange protocol. Here are the important contract addresses we will use:

  • Our ERC20 token contract address: 0xCC4d8eCFa6a5c1a84853EC5c0c08Cc54Cb177a6A
  • Uniswap exchange contract address: 0x416F1Ac032D1eEE743b18296aB958743B1E61E81

The Uniswap token exchange contract needs to deposit some tokens in order to provide initial liquidity. To this end, we first need to authorize the token exchange contract to operate the ERC20 tokens in our wallet, and then call the token exchange contract addLiquidity()method to deposit ERC20 tokens and ether at the same time:

  1. token.approve(exchange_address, TOKEN_RESERVE)
  2. exchange.addLiquidity(0, TOKEN_RESERVE, DEADLINE, transact={‘value’: ETH_RESERVE})

First, we run a command $node cript/3.approve.exchange.jsto authorize the token exchange contract to access
ERC20 tokens on our wallet address:

Insert picture description here

Next, we run the command $node script/4.add.liquidity.jsto deposit 15 ERC20 tokens and 0.1 Ether to the token exchange contract to provide initial liquidity:

Insert picture description here
You can use etherscan to check the account balance of the verification contract:

Insert picture description here

5. Realize decentralized transactions with Uniswap

Now we can use Uniswap to realize the exchange between Ether/ERC20 tokens or two ERC20 tokens.

In the first test, we send Ether to the Uniswap token exchange contract, which will be automatically converted to ERC20 tokens. You can execute commands $node script/5.eth2erc20.swap.jsto test, the code is as follows:
Insert picture description here

On Etherscan, you can see that 0.05 Ether has been deposited in the Uniswap token exchange contract and converted into 5 ERC20 tokens and sent back to the buyer's wallet:

Insert picture description here

In the second test, we tried to exchange two different ERC20 tokens, such as converting LINK tokens into our deployed ERC20 tokens:

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-rEbU0yMO-1593388918029)(uniswap-developer-guide/erc20-swap-code.jpeg)]

In the above code, we deposit 10 LINK tokens and get 0.0015 self-deployed tokens:

Insert picture description here

6 Conclusion

In this tutorial, we learned how to create an ERC20 token and connect it to the Uniswap decentralized exchange protocol to increase or decrease the liquidity of the token. You can download the complete experimental code here: https://github.com/oceanprotocol/Nautilus/tree/master/3-uniswap.


Original link: Uniswap Access Development Guide — Huizhi.com

Guess you like

Origin blog.csdn.net/shebao3333/article/details/107012482