MVPWorkshop / dot-acp-ui

DOT ACP Frontend
https://app.dotacp.mvpworkshop.co/
3 stars 4 forks source link

Map palet communication #13

Closed gagi13 closed 7 months ago

gagi13 commented 10 months ago

Go through https://github.com/paritytech/substrate-dex and map out how to best communicate with the developed pallet. Connect with pallet.

kaliman93 commented 10 months ago

Working with asset Conversion pallet.

We can use polkadot js library (specifically @polkadot/api) to communicate with asset conversion pallet

Useful links

Rust docs of the pallet Try it out in UI

Polkadot.js installation and initialisation

Installation:

yarn add @polkadot/api

Initialisation


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; 
    ...

Usage

Pool

Get list of all the pools:

    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}'
  ], ...

Create a new Pool

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); 

Add Liquidity to Pool

    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);

Remove Liquidity from Pool

    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);

Swap

Swap exact tokens to tokens

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))

  1. Swap Native Coin for Asset in Asset Hub
    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);
  2. Swap Asset(ID: 32) -> Asset(ID: 4200) from Asset Hub
    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);

Swap tokens to exact tokens

Useful when user wants get exact amount of output token (wants to get exactly 1 BTC for some amount of token2 (with max defined))

  1. Swap Native Coin for Asset in Asset Hub
    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);
  2. Swap Asset(ID: 32) -> Asset(ID: 4200) from Asset Hub
    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);

Prices (TODO)