API Reference

Compute Swap Results

You can use the computeSwapResults() function to retrieve the amount of coinB to be received when swapped for coinA

Let's assume we're trading on the SUI-USDC pool:

aToBbyAmountInResult
TRUETRUETrading SUI for USDC by specifying the amount of SUI to put in
TRUEFALSETrading SUI for USDC by specifying the amount of USDC you require
FALSETRUETrading USDC for SUI by specifying the amount USDC to put in
FALSEFALSETrading USDC for SUI by specifying the amount of SUI you require

Example

import { OnChainCalls, QueryChain, ISwapParams } from "@firefly-exchange/library-sui/dist/src/spot"
import { Ed25519Keypair, toBigNumber, SuiClient } from "@firefly-exchange/library-sui";
import { mainnet } from './config'

const client = new SuiClient({ url: "https://fullnode.mainnet.sui.io:443" });

/// Parameters:
/// - privateKey        : The private key of the user making the blockchain call
/// - poolID            : The id of the the pool ex: 0x3b585786b13af1d8ea067ab37101b6513a05d2f90cfe60e8b1d9e1b46a63c4fa 
/// - amount            : The amount of coinA you're swapping
/// - aToB              : If true, then the swap is coinA -> coinB
///                       if false then the swap is coinB -> coinA
/// - byAmountIn        : If true, then you're specifying the amount you're putting in
///                       If false, then you're specifying the amount you're getting back
/// - slippage			    : The difference between the expected price of a trade and the actual price at which it is executed.
///						            This should be a number between 0 and 1, eg: 0.2     

async function computeSwapResults(privateKey: string, poolID: string, amount : number, aToB : boolean, byAmountIn: boolean, slippage: number){
    const keyPair = Ed25519Keypair.fromSecretKey(Buffer.from(privateKey, 'hex'));

    let oc = new OnChainCalls(client,mainnet, {signer: keyPair});
    let qc = new QueryChain(client);

    let poolState = await qc.getPool(poolID);

    let iSwapParams : ISwapParams = {
        pool: poolState, 
        amountIn : byAmountIn == true ? toBigNumber(amount, (aToB == true ? poolState.coin_a.decimals : poolState.coin_b.decimals))  : 0,
        amountOut: byAmountIn == true ? 0 : toBigNumber(amount, (aToB == true ? poolState.coin_b.decimals : poolState.coin_a.decimals)),
        aToB: aToB,
        byAmountIn: byAmountIn,
        slippage: slippage
    }
    
    let resp = await oc.computeSwapResults(iSwapParams);
    return resp
}

computeSwapResults("<private_key>","0x3b585786b13af1d8ea067ab37101b6513a05d2f90cfe60e8b1d9e1b46a63c4fa", 1, true, true, 0.1,)
    .then((resp) => console.log(JSON.stringify(resp)))
    .catch((err) => console.error("Error: ", err));

Response

{
   "effects":{
      "messageVersion":"v1",
      "status":{
         "status":"success"
      },
      "executedEpoch":"569",
      "gasUsed":{
         "computationCost":"750000",
         "storageCost":"988000",
         "storageRebate":"0",
         "nonRefundableStorageFee":"0"
      },
      "modifiedAtVersions":[
         {
            "objectId":"0x16397cfb8b38d97a99fdbf1ba77e2c4874c80758d8565d1b507beebd1e83880e",
            "sequenceNumber":"1"
         }
      ],
      "sharedObjects":[
         {
            "objectId":"0x3b585786b13af1d8ea067ab37101b6513a05d2f90cfe60e8b1d9e1b46a63c4fa",
            "version":410525748,
            "digest":"Zi6o3C3bt9UsWoodvEaJNsr7Z5qikvnmAgkgx6mLUA6"
         }
      ],
      "transactionDigest":"29dJE3L3p1maJMWXKUQxEatEQN8aS47n8K4BbWnkt2jm",
      "mutated":[
         {
            "owner":{
               "AddressOwner":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269"
            },
            "reference":{
               "objectId":"0x16397cfb8b38d97a99fdbf1ba77e2c4874c80758d8565d1b507beebd1e83880e",
               "version":410525749,
               "digest":"AosJyzTwgMqcMtjShKKosDxK4EyGA33BYcsfd5vapttY"
            }
         }
      ],
      "gasObject":{
         "owner":{
            "AddressOwner":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269"
         },
         "reference":{
            "objectId":"0x16397cfb8b38d97a99fdbf1ba77e2c4874c80758d8565d1b507beebd1e83880e",
            "version":410525749,
            "digest":"AosJyzTwgMqcMtjShKKosDxK4EyGA33BYcsfd5vapttY"
         }
      },
      "eventsDigest":"BoASCqsemz7HQGPiWVFWuvSqjHDXFAdepiSuW9Cdmsi9",
      "dependencies":[
         "49puDQZwHRnu7zYoARCjco1dFKMiK7LVSS8B5Si1yVdh",
         "7DkJSrf5sPWGxqHbH2X1wXwFcftRm6myhJJnuTJbaMYr",
         "Atpejs9MU4EGVAzBETZqppC8jUtpc8cmozj7yEHGB73g",
         "CUQmrEssKtB8WxBYj3YQZJCrFTTDLqz3UWq1nRiWGCLS"
      ]
   },
   "events":[
      {
         "id":{
            "txDigest":"29dJE3L3p1maJMWXKUQxEatEQN8aS47n8K4BbWnkt2jm",
            "eventSeq":"0"
         },
         "packageId":"0xb104ecc75397f3a65735ef26c85a037da1d197e26f4f275a9990a577ba0e6c4c",
         "transactionModule":"pool",
         "sender":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269",
         "type":"0x3492c874c1e3b3e2984e8c41b589e642d4d0a5d6459e5a9cfc2d52fd7c89c267::pool::SwapResult",
         "parsedJson":{
            "a2b":true,
            "amount_calculated":"3812923",
            "amount_specified":"2000000000",
            "amount_specified_remaining":"0",
            "by_amount_in":true,
            "current_tick_index":{
               "bits":4294904687
            },
            "end_sqrt_price":"806240314785727840",
            "fee_amount":"3000000",
            "fee_growth_global":"11821604056053703",
            "is_exceed":false,
            "liquidity":"4777281631688",
            "protocol_fee":"1000000",
            "start_sqrt_price":"806255037806392276",
            "starting_liquidity":"4777281631688",
            "step_results":[
               {
                  "amount_in":"1996000000",
                  "amount_out":"3812923",
                  "fee_amount":"3000000",
                  "initialized":true,
                  "remaining_amount":"0",
                  "sqrt_price_next":"804965766547694465",
                  "sqrt_price_start":"806255037806392276",
                  "tick_index_next":{
                     "bits":4294904656
                  }
               }
            ],
            "steps":"1"
         },
         "bcs":"CDGqC8m4u1KYvjeeJ4caTTshgFgyFffmLesSHHiv9ewHmjkeyumLRy5K7TV14Ly2dM9FruG7fkKYbgZPtqvUpVaqh5FdKbJA4Zyq2Epk1qHMkJxWV4nVa2YNXoQnyE1MNheNpQhhQhqAHhcDye8tRig7jQ3ri5jJQGuhLDgoAuFMy3CscaPS2bd8wRqi1eyDFqhPGpWrTmuFLsBDRwuaHaGyj16WcYU1bVgVHPzH6w8NahxNQ8zpQ81c2NeZAAQGMEuU4KJwSd59j8PVHpZKrz3"
      }
   ],
   "results":[
      {
         "returnValues":[
            [
               [
                  1,
                  1,
                  0,
                  148,
                  53,
                  119,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  59,
                  46,
                  58,
                  0,
                  0,
                  0,
                  0,
                  0,
                  199,
                  183,
                  111,
                  174,
                  175,
                  255,
                  41,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  192,
                  198,
                  45,
                  0,
                  0,
                  0,
                  0,
                  0,
                  64,
                  66,
                  15,
                  0,
                  0,
                  0,
                  0,
                  0,
                  212,
                  179,
                  209,
                  102,
                  175,
                  100,
                  48,
                  11,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  96,
                  5,
                  103,
                  110,
                  75,
                  87,
                  48,
                  11,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  111,
                  11,
                  255,
                  255,
                  0,
                  200,
                  181,
                  44,
                  76,
                  88,
                  4,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  200,
                  181,
                  44,
                  76,
                  88,
                  4,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  1,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  1,
                  80,
                  11,
                  255,
                  255,
                  1,
                  212,
                  179,
                  209,
                  102,
                  175,
                  100,
                  48,
                  11,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  129,
                  227,
                  108,
                  139,
                  25,
                  208,
                  43,
                  11,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  139,
                  248,
                  118,
                  0,
                  0,
                  0,
                  0,
                  59,
                  46,
                  58,
                  0,
                  0,
                  0,
                  0,
                  0,
                  192,
                  198,
                  45,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0,
                  0
               ],
               "0x3492c874c1e3b3e2984e8c41b589e642d4d0a5d6459e5a9cfc2d52fd7c89c267::pool::SwapResult"
            ]
         ]
      }
   ]
}