Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Tutorials for using BakerySwap.js package

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "BakerySwap.js development package use tutorial", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn the "BakerySwap.js development package use tutorial" bar!

The BakerySwap.js development kit is suitable for rapidly increasing the support for the currency security smart chain BakerySwap protocol for Web front-ends or Node.js applications. It not only supports application scenarios with self-deployed BSC blockchain nodes, but also supports lightweight deployment scenarios using third-party nodes. Official download address: bakeryswap.js development package.

1. Overview of the development package

The BakerySwap.js development package mainly includes the following features:

Deploy BakerySwap protocol with one click to facilitate rapid development and testing

Supports all interfaces of the BakerySwap protocol and provides developer-friendly API

Supports liquidity addition, removal and exchange transactions for various trading pairs such as BEP20/BEP20, BNB/BEP20, etc.

Support automatic market-making price calculation and slip point calculation

Support automatic estimation and manual setting of transaction gas usage and gas price

Support EIP712 signature authorization, complete liquidity maintenance within a single transaction

The BakerySwap.js package runs in the JavaScript environment, and the current version 1.0.0. The main classes / interfaces and relationships are shown in the following figure:

For a list of the main code files of the BakerySwap.js development package, see the official website description: http://sc.hubwiz.com/codebag/bakeryswap-js/

2. Compile the contract with sample code 2.1

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 implementation results are as follows:

2.2 deployment contract

First, start the development private chain on another terminal:

~ $ganache-cli-d

Then go to 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 implementation results are as follows:

Note: if you need to deploy the above contract 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.

2.3 Token Authorization

The code demo/token-approve.js demonstrates how to authorize the bakeryswap routing contract agent to operate on the HAPY token and USDT token of the current account.

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

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

The implementation results are as follows:

2.4 BEP20/BEP20 trading pairs add liquidity

The code demo/add-liquidity.js demonstrates how to add liquidity to a BEP20/BEP20 trading pair.

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

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

The implementation results are as follows:

The bakeryswap agreement stipulates that trading pairs will be automatically created the first time liquidity is added, and when liquidity is added again, the demo code will first display the current position information. For example, execute the following command again:

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

The implementation results are as follows:

2.5 BEP20/BEP20 trading pair removes liquidity

The code demo/remove-liquidity.js demonstrates how to remove liquidity from a specified BEP20/BEP20 trading pair.

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

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

The implementation results are as follows:

2.6 BEP20/BEP20 exchange: based on the quantity of input token

The code demo/swap-exact-tokens-for-tokens.js demonstrates how to implement BEP20/BEP20 conversion based on the number of input token.

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

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

The implementation results are as follows:

2.7BEP20/BEP20 exchange: based on the quantity of exported token

The code demo/swap-tokens-for-exact-tokens.js demonstrates how to implement BEP20/BEP20 conversion based on the number of output token.

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

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

The implementation results are as follows:

2.8 BNB/BEP20 trading pairs add liquidity

The code demo/add-liquidty-bnb.js demonstrates how to add liquidity to a BNB/BEP20 or BEP20/BNB trading pair.

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

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

The implementation results are as follows:

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

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

The implementation results are as follows:

2.9 BNB/BEP20 trading pair removes liquidity

The code demo/remove-liquidity-bnb.js demonstrates how to remove liquidity from a BNB/BEP20 or BEP20/BNB trading pair.

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

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

The implementation results are as follows:

2.10 BNB/BEP20 exchange: based on the input quantity of BNB

The code demo/swap-exact-bnb-for-tokens.js demonstrates how to convert a specified amount of BNB into BEP20 token.

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

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

The implementation results are as follows:

2.11 BNB/BEP20 exchange: based on the quantity of exported token

The code demo/swap-bnb-for-exact-tokens.js demonstrates how to convert BNB into a specified number of BEP20 token.

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

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

The implementation results are as follows:

2.12 BEP20/BNB exchange: based on the input quantity of token

The code demo/swap-exact-tokens-for-bnb.js demonstrates how to convert a specified amount of BEP20 token into BNB.

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

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

The implementation results are as follows:

2.13 BEP20/BNB exchange: based on the quantity of exported BNB

The code demo/swap-tokens-for-exact-bnb.js demonstrates how to convert BEP20 token into a specified number of BNB.

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

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

The implementation results are as follows:

3. Use the bakeryswap.js development package

SwapKit is the entrance to the development package, and using this class, you can quickly implement the following functions:

Liquidity addition and removal: support BEP20/BEP20, BEP20/BNB, BNB/BEP20 and other trading pairs

Creation and execution of exchange transactions: supports various trading pairs such as BEP20/BEP20, BEP20/BNB, BNB/BEP20, etc., and supports input or output prices.

User position query: query the position of the specified account in the specified trading pair, such as the number of positions, proportion, and so on.

3.1 instantiate SwapKit

Three parameters are passed in to instantiate SwapKit:

Money Security Intelligent chain Node URL

Bakeryswap routing contract address

The private key of the currency security smart chain account used to perform the 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.org', / / coin security smart chain node URL '0xCDe540d7eAFE93aC5fE6233Bee57E1270D3E330F' / / BSC main chain bakeryswap routing contract address' 0x4f3e.. 3b1d' / default execution account private key) 3.2 generate liquidity add / remove order

Because the transaction slip point processing and price precalculation are involved in the bakeryswap protocol, bakeryswap.js provides a special class OrderBuilder for generating liquidity add / remove orders. The implementation of OrderBuilder has built-in automatic price calculation and slip point calculation, so callers only need to specify basic data to generate liquidity add / remove proxy orders that can be submitted to bakeryswap contracts.

Use the orderBuilder property of the SwapKit object to get the pre-created OrderBuilder object, and call the asynchronous method build () of OrderBuilder to generate the delegate. For example:

Const order = await kit.orderBuilder / / returns the OrderBuilder object. / / Parameter setting. Build () / / build and return the order.

The OrderBuilder object provides the following methods for adjusting the behavior of the generator:

OrderType (type): sets the type of delegation order. Currently, two types are supported:

Order.LIQUIDITY_ADD-liquidity add order

Order.LIQUIDITY_REMOVE-liquidity removal order

TokenA (addr) / tokenB (addr): two Token addresses of the transaction pair

AmountA (bn) / amountB (bn): the number of two token added, which needs to be set only when adding flows

Liquidity (bn): number of LP token, which needs to be set only when removing liquidity

Slippage (bn): set the transaction slip point. The default value is 5, which indicates a 0.5% slip point tolerance range.

To (addr): address for receiving the result of execution of the entrustment order

For example, the following code generates a liquidity add delegation order:

/ / const {SwapKit, Order Bn} = require ('bakeryswap.js') const order = await kit.orderBuilder / / get delegation order generator object .orderType (Order.LIQUIDITY_ADD) / / add liquidity .tokenA (' 0x..') / / transaction pair TokenA .tokenB ('0x..') / / transaction pair TokenB .amount A (bn ('100000000000000000000000')) / / automatically calculate the TokenB quantity at market price based on the quantity of TokenA. Slippage (bn ('10')) / / slippage tolerance range 1% .to (kit.ownerAddress) / / set the receiving address of the LP Token. Build () / generate the proxy console.log (`build = > ${order.amountA} `) / / TokenA set the number of console.log (`amountB = > ${order.amountB}) `) / / TokenB calculated quantity console.log (` amountAMin = > ${order.amountAMin} `) / / minimum acceptable quantity of TokenA after sliding point processing console.log (` amountBMin = > ${order.amountBMin} `) / / minimum acceptable quantity of TokenB after sliding point processing

Note:

Use a special SwapKit.ETH_TOKEN to represent the BNB in the transaction pair. The value is: 0x00000000000000000000000000000000000eeeee.

Bn (numstr) is a shortcut function provided by bakeryswap.js to create a BigNumber object, whose argument is a decimal string.

The quantity of token is specified in the smallest unit.

Similarly, the following code generates a liquidity removal order:

/ / const {SwapKit, Order Bn} = require ('bakeryswap.js') const order = await kit.orderBuilder / / get delegated order generator object .orderType (Order.LIQUIDITY_REMOVE) / / remove liquidity .tokenA (' 0x..') / / transaction pair TokenA address .tokenB ('0x..') / / transaction pair TokenB address. Liquidity (bn ('10000000000000000000000000')) / the amount of liquidity to be removed. slippage (bn ('10')) / / slippage tolerance range 1% .to (kit.ownerAddress) / / token receive address .build () / / generate proxy console.log (`TokenA = > ${order.amountAMin} `) / / the minimum number of TokenA that should be received after slippery point processing console.log (`amountBMin = > ${order.amountAMin}`) / / the minimum number of TokenB that should be received after sliding point processing

The following values provided by the Order object help to provide better feedback to the user before executing the delegate:

AmountA/amountB: the ideal value of tokenA or tokenB calculated at market price

AmountAMin/amountAMax: minimum / maximum value of tokenA after slip point processing

AmountBMin/amountBMax: minimum / maximum value of tokenB after slip point processing

3.3 execution of liquidity order

The generated liquidity order can be executed using the executeOrder () method of SwapKit, and the liquidity add / remove transaction is finally completed. For example:

Const tx = await kit- > executeOrder ($trade); / / execute the order console.log (`txid = > ${tx.hash} `) / / display the transaction ID

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

For example, the following code executes the liquidity maintenance order with the specified gas parameter:

/ / const {bn} = require ('bakeryswap.js') const opts = {gasLimit: bn (' 40000000'), / / 4 million gasPrice: bn ('2000000000000') / / 200gwei} const tx = await kit.executeOrder (order, opts) / / execute order 3.4 to query position information

Use the getLiquidityInfo () method of SwapKit to query the position information of a specified account in a specified trading pair. For example, query the position of an address:

Const info = await kit.getLiquidityInfo ('0x90F8.. c9C1') / / query position information console.log (`share = > ${info.totalSupply} `) / / Total LP console.log (`share = > ${info.balance}`) / / position quantity console.log (`position% = > ${info.share} `) / / generate exchange transaction object

Similar to the addition and removal of liquidity, the transaction exchange in bakeryswap also has the problem of automatic price calculation and slip point processing. To this end, the bakeryswap.js development package provides a special class TradeBuilder to simplify this operation. TradeBuilder has built-in automatic price calculation and slip point processing, so callers only need to set basic parameters.

Use the tradeBuilder property of the SwapKit object to get the pre-created TradeBuilder object, for example:

Const tb = await kit.tradeBuilder / / get the pre-created exchange transaction generator. / / Parameter setting .build () / / generate exchange transaction object

TradeBuilder provides the following methods to adjust the behavior of the generator:

TradeType (type): exchange type. Available values are:

Trade.EXACT_INPUT-based on the number of tokenIn

Trade.EXACT_OUTPUT-based on the number of tokenOut

TokenIn (addr)-enter the address of the Token

TokenOut (addr)-address of the output Token

AmountIn (bn)-enter the quantity, which needs to be set only if the exchange type is EXACT_INPUT

AmountOut (bn)-output quantity, which needs to be set only if the exchange type is EXACT_OUTPUT

Slippage (bn)-transaction slip point setting. The default value is 5, which means 0.5% slip point is allowed.

To ($addr)-output token receive address

For example, the following code generates an exchange transaction object based on the number of token entered:

/ / const {SwapKit, Trade Bn} = require ('bakeryswap.js') const trade = await kit.tradeBuilder / / get the exchange transaction generator .tradeType (Trade.EXACT_INPUT) / / based on the number of token entered. Tokenin (' 0x..') / / enter token .tokenOut ('0x..') / / output token .amount in (bn ('10000000000000000000000000')) / / enter the number of token .slippage (bn ('10')) / / slippage tolerance range 1% .to (kit.ownerAddress) / / output the receiving address of the token. Build () / generate the exchange transaction object console.log (`amountIn = > ${trade.amountIn}`) / / enter the set number of token console.log (`build = > ${trade.amountOut} `) / / the automatic market-making algorithm The number of output token console.log (`amountOutMin = > ${trade.amountOutMin}`) / / the minimum number of output token that should be received after slippery point processing 3.6execute the exchange transaction

Executes the specified exchange transaction object using the executeTrade () method of the SwapKit object. For example:

Const tx = await kit.executeTrade (trade) / / execute the exchange transaction console.log (`txid = > ${tx.hash} `) / / display the transaction ID

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

For example, the following code performs an exchange transaction using the set gas parameter:

/ / const {bn} = require ('bakeryswap.js') const opts = {gasLimit: bn (' 40000000'), / / 4 million gasPrice: bn ('2000000000000') / / 200gwei} const tx = await kit.executeTrade (trade, opts) / / execute the transaction

Official download link for bakeryswap.js: http://sc.hubwiz.com/codebag/bakeryswap-js/

Thank you for reading, the above is the content of the "BakerySwap.js package use tutorial", after the study of this article, I believe you have a deeper understanding of the BakerySwap.js package use tutorial, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report