Closed gagi13 closed 7 months ago
We can use polkadot js library (specifically @polkadot/api) to communicate with asset conversion pallet
Rust docs of the pallet Try it out in UI
yarn add @polkadot/api
const main = async () => {
const wsProvider = new WsProvider('wss://westend-asset-hub-rpc.polkadot.io'); // Connect to 3rd Party RPC (OnFinality)
const api = await ApiPromise.create({ provider: wsProvider });
await api.isReady;
...
const pools = await api.query.assetConversion.pools.entries();
console.log(pools.map(([key, value]) => [key.args[0].toString(), value.toString()]));
// Log output:
`[
[
// [Asset1 (native), Asset2] MultiLocation
'[{"parents":0,"interior":{"here":null}},{"parents":0,"interior":{"x2":[{"palletInstance":50},{"generalIndex":2511}]}}]',
'{"lpToken":4}'
],
[
'[{"parents":0,"interior":{"here":null}},{"parents":0,"interior":{"x2":[{"palletInstance":50},{"generalIndex":1977}]}}]',
'{"lpToken":0}'
], ...
We can only create Native Coin -> Asset pools, not Asset1 -> Asset2
const result = await api.tx.assetConversion.createPool(
{"parents":0,"interior":{"here":null}}, // Native Coin MultiLocation
{"parents":0,"interior":{"x2":[{"palletInstance":50},{"generalIndex":420420}]}} // Asset in Assethub MultiLocation
).signAndSend(wallet);
const result = await api.tx.assetConversion.addLiquidity(
{"parents":0,"interior":{"here":null}},
{"parents":0,"interior":{"x2":[{"palletInstance":50},{"generalIndex":420420}]}},
10000, // desired amount of token1 to provide as liquidity (calculations happen when tx in executed)
10000, // desired amount of token2 to provide as liquidity
100, // minimum amount of token1 to provide as liquidity
100, // minimum amount of token2 to provide as liquidity
"5ERLqAatjG1r5YyotuZevxC59hNNrb81soVKnd5BXLRwiUDb", // address to mint LP tokens
).signAndSend(wallet);
const result = await api.tx.assetConversion.removeLiquidity(
{"parents":0,"interior":{"here":null}},
{"parents":0,"interior":{"x2":[{"palletInstance":50},{"generalIndex":420420}]}},
100, // amount of Lp token to burn
1000, // minimum amount of token1 to provide as liquidity
124444, // minimum amount of token2 to provide as liquidity
"5ERLqAatjG1r5YyotuZevxC59hNNrb81soVKnd5BXLRwiUDb", // token withdrawal address
).signAndSend(wallet);
Useful when user wants to swap exact amount of input token (wants to exchange 100USD for however he gets of other token (with min defined))
const result = await api.tx.assetConversion.swapExactTokensForTokens(
[
{"parents":0,"interior":{"here":null}},
{"parents":0,"interior":{"x2":[{"palletInstance":50},{"generalIndex":4200}]}}
], // path array
12, // amount of tokens to swap
150, // minimum amount of token2 user wants to receive
"5ERLqAatjG1r5YyotuZevxC59hNNrb81soVKnd5BXLRwiUDb", // address to receive swapped tokens
false // Keep alive parameter
).signAndSend(wallet);
const result = await api.tx.assetConversion.swapExactTokensForTokens(
[
{"parents":0,"interior":{"x2":[{"palletInstance":50},{"generalIndex":32}]}}
{"parents":0,"interior":{"here":null}},
{"parents":0,"interior":{"x2":[{"palletInstance":50},{"generalIndex":4200}]}}
], // path array (path includes middle Native Coin Multilocation)
12, // amount of tokens to swap
150, // minimum amount of token2 user wants to receive
"5ERLqAatjG1r5YyotuZevxC59hNNrb81soVKnd5BXLRwiUDb", // address to receive swapped tokens
false // Keep alive parameter
).signAndSend(wallet);
Useful when user wants get exact amount of output token (wants to get exactly 1 BTC for some amount of token2 (with max defined))
const result = await api.tx.assetConversion.swapTokensForExactTokens(
[
{"parents":0,"interior":{"here":null}},
{"parents":0,"interior":{"x2":[{"palletInstance":50},{"generalIndex":4200}]}}
], // path array
12, // amount of tokens to get
150, // maximum amount of tokens to spend
"5ERLqAatjG1r5YyotuZevxC59hNNrb81soVKnd5BXLRwiUDb", // address to receive swapped tokens
false // Keep alive parameter
).signAndSend(wallet);
const result = await api.tx.assetConversion.swapTokensForExactTokens(
[
{"parents":0,"interior":{"x2":[{"palletInstance":50},{"generalIndex":32}]}}
{"parents":0,"interior":{"here":null}},
{"parents":0,"interior":{"x2":[{"palletInstance":50},{"generalIndex":4200}]}}
], // path array (path includes middle Native Coin Multilocation)
12, // amount of tokens to get
150, // maximum amount of tokens to spend
"5ERLqAatjG1r5YyotuZevxC59hNNrb81soVKnd5BXLRwiUDb", // address to receive swapped tokens
false // Keep alive parameter
).signAndSend(wallet);
Go through https://github.com/paritytech/substrate-dex and map out how to best communicate with the developed pallet. Connect with pallet.