How does bakeryswap.js work?

Time:2021-10-25

1 compilation contract
Enter the project directory at the terminal and execute the following command to compile bakeryswap and the test token contract provided by the development package:

~$ cd ~/bakeryswap.js
~/bakeryswap.js$ bin/build-contracts

The results are as follows:

Insert picture description here

2 deployment contract
First, start the development private chain at another terminal:

~$ ganache-cli -d

Then enter the project demo directory and execute the following command to deploy the contract:

~$ cd ~/bakeryswap.js/demo
~/bakeryswap.js/demo$ node deploy-contracts.js

The results are as follows:

Insert picture description here

Note: if you need to deploy the above contracts in the BSC main chain or test chain, or start Ganache cli with other parameters, you need to modify the account configuration in config.js.

3 token authorization
The code demo / token-approve.js demonstrates how to authorize the bakeryswap routing contract agent to operate the hapy token and usdt token of the current account.

Enter the project demo directory at the terminal and execute the following commands:

~$ cd ~/bakeryswap.js/demo
~/bakeryswap.js/demo$ node token-approve.js

The results are as follows:

Insert picture description here

4 bep20 / bep20 transaction pair add liquidity
The code demo / add-liquidity.js demonstrates how to add liquidity to bep20 / bep20 transaction pairs.

Enter the project demo directory at the terminal and execute the following commands:

~$ cd ~/bakeryswap.js/demo
~/bakeryswap.js/demo$ node add-liquidity.js

The results are as follows:

Insert picture description here

The bakeryswap agreement stipulates that when adding liquidity for the first time, a transaction pair will be automatically created, and then when adding liquidity again, the demonstration code will first display the current position information. For example, execute the following command again:

~/bakeryswap.js/demo$ node add-liquidity.js

The results are as follows:

Insert picture description here

5 bep20 / bep20 transaction pair remove liquidity
The code demo / remove-liquidity.js demonstrates how to remove the liquidity of a specified bep20 / bep20 transaction pair.

Enter the project demo directory at the terminal and execute the following commands:

~$ cd ~/bakeryswap.js/demo
~/bakeryswap.js/demo$ node remove-liquidity.js

The results are as follows:

Insert picture description here

6 bep20 / bep20 exchange: Based on the number of tokens entered
The code demo / swap-exact-tokens-for-tokens.js demonstrates how to implement bep20 / bep20 exchange based on the number of input tokens.

Enter the project demo directory at the terminal and execute the following commands:

~$ cd ~/bakeryswap.js/demo
~/bakeryswap.js/demo$ node swap-exact-tokens-for-tokens.js

The results are as follows:

Insert picture description here

7 bep20 / bep20 exchange: Based on the number of output tokens
The code demo / swap-tokens-for-exact-tokens.js demonstrates how to implement bep20 / bep20 exchange based on the number of output tokens.

Enter the project demo directory at the terminal and execute the following commands:

~$ cd ~/bakeryswap.js/demo
~/bakeryswap.js/demo$ node swap-tokens-for-exact-tokens.js

The results are as follows:

Insert picture description here

8 BNB / bep20 transaction pair add liquidity
The code demo / add-liquidty-bnb.js demonstrates how to add liquidity to BNB / bep20 or bep20 / BNB transaction pairs.

Enter the project demo directory at the terminal and execute the following commands:

~$ cd ~/bakeryswap.js/demo
~/bakeryswap.js/demo$ node add-liquidity-bnb.js

The results are as follows:

Insert picture description here

Adding liquidity for the first time will automatically create a transaction pair, and adding it again will first display the current position information. For example, execute the following command again:

~/bakeryswap.js/demo$ node add-liquidity-bnb.js

The results are as follows:

Insert picture description here

9 BNB / bep20 transaction pair remove liquidity
The code demo / remove-liquidity-bnb.js demonstrates how to remove the liquidity of BNB / bep20 or bep20 / BNB transaction pairs.

Enter the project demo directory at the terminal and execute the following commands:

~$ cd ~/bakeryswap.js/demo
~/bakeryswap.js/demo$ node remove-liquidity-bnb.js

The results are as follows:

Insert picture description here

10 BNB / bep20 exchange: Based on the input BNB quantity
The code demo / swap-exact-bnb-for-tokens.js demonstrates how to convert a specified number of bnbs into bep20 tokens.

Enter the project demo game directory on the terminal and execute the following commands:

~$ cd ~/bakeryswap.js/demo
~/bakeryswap.js/demo$ node swap-exact-bnb-for-tokens.js

The results are as follows:

Insert picture description here

11 BNB / bep20 exchange: Based on the number of output tokens
The code demo / swap-bnb-for-exact-tokens.js demonstrates how to convert BNB into a specified number of bep20 tokens.

Enter the project demo directory at the terminal and execute the following commands:

~$ cd ~/bakeryswap.js/demo
~/bakeryswap.js/demo$ node swap-bnb-for-exact-tokens.js

The results are as follows:

Insert picture description here

12 bep20 / BNB exchange: Based on the number of tokens entered
The code demo / swap-exact-tokens-for-bnb.js demonstrates how to convert a specified number of bep20 tokens to www.cungun.combnb.

Enter the project demo directory at the terminal and execute the following commands:

~$ cd ~/bakeryswap.js/demo
~/bakeryswap.js/demo$ node swap-exact-tokens-for-bnb.js

The results are as follows:

Insert picture description here

13 bep20 / BNB exchange: Based on the output BNB quantity
The code demo / swap-tokens-for-exact-bnb.js demonstrates how to convert bep20 tokens to a specified number of bnbs.

Enter the project demo directory at the terminal and execute the following commands:

~$ cd ~/bakeryswap.js/demo
~/bakeryswap.js/demo$ node swap-tokens-for-exact-bnb.js

The results are as follows:

Insert picture description here

14 instantiate swapkit
Swapkit instantiation needs to pass in three parameters:

Currency security smart chain node URL
Bakeryswap routing contract address
The private key of the currency security smart chain account used to perform contract interaction.
For example, the following code creates a swapkit instance that accesses the bakeryswap protocol of the BSC main network:

const { SwapKit } = require(‘bakeryswap.js’)

const kit = new SwapKit(
‘ https://bsc-dataseed.binance…. //Currency security smart chain node URL
‘0xcde540d7eafe93ac5fe6233bee57e1270d3e330f’, / / BSc main chain bakeryswap routing contract address
‘0x4f3e… 3b1d’ / / the private key of the account is executed by default
)

15 generate liquidity addition / removal delegation
Since the bakeryswap protocol involves transaction sliding point processing and price pre calculation, bakeryswap.js provides a special class orderbuilder for generating liquidity addition / removal entrustment orders. The implementation of orderbuilder has built-in automatic price calculation and sliding point calculation, so the caller only needs to specify the basic data to generate the liquidity addition / removal order that can be submitted to the bakeryswap contract.

Use the orderbuilder attribute of the swapkit object to obtain the pre created orderbuilder object, and call the asynchronous method build () of orderbuilder to generate the entrustment. For example:

Const order = await kit.orderbuilder / / the orderbuilder object is returned

... // parameter setting
                . build() // generate and return the entrustment form

The orderbuilder object provides the following methods to adjust the behavior of the Builder:

Ordertype (type): sets the type of entrustment order. Currently, two types are supported:
Order.LIQUIDITY_ Add – liquidity addition order
Order.LIQUIDITY_ Remove – liquidity removal order
Tokena (addr) / tokenb (addr): two token addresses of the transaction pair
Amounta (BN) / amountb (BN): the added quantity of two tokens. It needs to be set only when adding flow
Liquidity (BN): the number of LP tokens, which needs to be set only when removing liquidity
Slippage (BN): transaction slip point setting. The default value is 5, indicating a slip point tolerance range of 0.5%
To (addr): the address to which the delegation order execution result is received
For example, the following code generates a liquidity addition delegation:

// const { SwapKit, Order, bn } = require(‘bakeryswap.js’)

Const order = await kit.orderbuilder / / obtain the consignment order generator object

. ordertype (order. Liquidity_add) // add liquidity
                . tokena ('0x... ') // transaction pair tokena
                . tokenb ('0x... ') // transaction pair tokenb
                . amounta (BN ('10000000000000000000000 ') // based on the tokena quantity, the tokenb quantity is automatically calculated according to the market price
                . slippage (BN ('10 ') // slip point tolerance range 1%
                . to (kit. Owneraddress) // set the receiving address of LP token
                . build() // generate a delegation form

console.log(amountA => ${order.amountA})/ / tokena set quantity
console.log(amountB => ${order.amountB})/ / tokenb calculated quantity
console.log(amountAMin => ${order.amountAMin})/ / minimum acceptable number of tokena after sliding point processing
console.log(amountBMin => ${order.amountBMin})/ / minimum acceptable number of tokenb after sliding point processing

be careful:

Use a special swapkit.eth_ Token indicates BNB in the transaction pair, and the value is 0x000000000000000000000000000000000000000000000000eeeee
BN (numstr) is a shortcut function provided by bakeryswap.js to create BigNumber object, and its parameter is hexadecimal string.
The number of tokens is specified in the smallest unit
Similarly, the following code generates a liquidity removal delegation:

// const { SwapKit, Order, bn } = require(‘bakeryswap.js’)

Const order = await kit.orderbuilder / / obtain the consignment order generator object

. ordertype (order. Liquidity_remove) // remove liquidity  
                . tokena ('0x... ') // transaction pair tokena address 
                . tokenb ('0x... ') // transaction pair tokenb address 
                . liquidity (BN ('1000000000000000000000 ') // amount of liquidity to be removed
                . slippage (BN ('10 ') // slip point tolerance range 1%
                . to (kit. Owneraddress) // token receiving address
                . build() // generate a delegation form

console.log(amountAMin => ${order.amountAMin})/ / the minimum number of tokena that should be received after sliding point processing
console.log(amountBMin => ${order.amountAMin})/ / minimum number of tokenbs to be received after sliding point processing

The following values provided by the order object help to provide users with better feedback before executing the entrustment order:

Amount a / amount B: tokena or tokenb ideal value calculated by market price
Amountamin / amountamax: the minimum / maximum value of tokena after sliding point processing
Amountbmin / amountbmax: the minimum / maximum value of tokenb after sliding point processing
16. Execute liquidity entrustment form
Use the executeorder() method of swapkit to execute the generated liquidity entrustment order and finally complete the liquidity addition / removal transaction. For example:

const tx = await kit->executeOrder($trade); // Execution entrustment
console.log(txid => ${tx.hash})/ / display transaction ID

By default, the executeorder () method automatically estimates the gas limit and gas price required for the transaction, but you can pass in additional parameters to manually set these two values.

For example, the following code executes the mobility maintenance order with the specified gas parameters:

// const { bn } = require(‘bakeryswap.js’)

const opts = {
gasLimit: bn(‘4000000’), //4 million
gasPrice: bn(‘200000000000’) //200 gwei
}
Const TX = await kit. Executeorder (order, opts) / / execute the delegation order

17 query bin information
Use the getliquidityinfo () method of swapkit to query the position information of the specified account in the specified transaction pair. For example, to query the position of an address:

const info = await kit.getLiquidityInfo(‘0x90F8…c9C1’); // Query position information

console.log(total => ${info.totalSupply})/ / total LP
console.log(balance => ${info.balance})/ / number of positions
console.log(share % => ${info.share})/ / position proportion

18 generate exchange trading partner
Similar to the addition and removal of liquidity, the exchange of transaction pairs in bakeryswap also has the problems of automatic price calculation and sliding point processing. Therefore, the bakeryswap.js development package provides a special class tradebuilder to simplify this operation. Tradebuilder has built-in automatic price calculation and sliding point processing, so the caller only needs to set basic parameters.

Use the tradebuilder property of the swapkit object to obtain the pre created tradebuilder object, for example:

Const TB = await kit. Tradebuilder / / obtain the pre created exchange transaction generator

... // parameter setting
        . build() // generate exchange transaction object

Tradebuilder provides the following methods to adjust the behavior of the Builder:

Tradetype (type): exchange type. The optional values are:
Trade.EXACT_ Input – based on the number of tokenins
Trade.EXACT_ Output – based on the number of tokenout
Tokenin (addr) – enter the address of the token
Tokenout (addr) – the address of the output token
Amountin (BN) – enter the quantity, only if the exchange type is act_ Input needs to be set
Amountout (BN) – output quantity, only when the exchange type is execute_ Output needs to be set
Slippage (BN) – transaction slip point setting. The default value is 5, indicating that 0.5% slip point is allowed
To ($addr) – output token receiving address