You can use the swapAssets()
function to swap an asset for another asset on a specific pool.
Let's assume we're trading on the SUI-USDC pool:
aToB | byAmountIn | Result |
---|---|---|
TRUE | TRUE | Trading SUI for USDC by specifying the amount of SUI to put in |
TRUE | FALSE | Trading SUI for USDC by specifying the amount of USDC you require |
FALSE | TRUE | Trading USDC for SUI by specifying the amount USDC to put in |
FALSE | FALSE | Trading 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 swapAssets(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.swapAssets(iSwapParams);
return resp
}
swapAssets("<private_key>","0x0c89fd0320b406311c05f1ed8c4656b4ab7ed14999a992cc6c878c2fad405140", 1, true, true, 0.1)
.then((resp) => console.log(JSON.stringify(resp)))
.catch((err) => console.error("Error: ", err));
Response
{
"digest":"FbDvEuFHTabTYtRbvgEjPNgW9YCv8FPSSb79tFQ7VCR4",
"transaction":{
"data":{
"messageVersion":"v1",
"transaction":{
"kind":"ProgrammableTransaction",
"inputs":[
{
"type":"pure",
"valueType":"u64",
"value":"200000000"
},
{
"type":"object",
"objectType":"sharedObject",
"objectId":"0x0000000000000000000000000000000000000000000000000000000000000006",
"initialSharedVersion":"1",
"mutable":false
},
{
"type":"object",
"objectType":"sharedObject",
"objectId":"0x03db251ba509a8d5d8777b6338836082335d93eecbdd09a11e190a1cff51c352",
"initialSharedVersion":"406496849",
"mutable":false
},
{
"type":"object",
"objectType":"sharedObject",
"objectId":"0x3b585786b13af1d8ea067ab37101b6513a05d2f90cfe60e8b1d9e1b46a63c4fa",
"initialSharedVersion":"406731547",
"mutable":true
},
{
"type":"pure",
"valueType":"bool",
"value":true
},
{
"type":"pure",
"valueType":"bool",
"value":true
},
{
"type":"pure",
"valueType":"u64",
"value":"200000000"
},
{
"type":"pure",
"valueType":"u64",
"value":"0"
},
{
"type":"pure",
"valueType":"u128",
"value":"764880688336009915"
}
],
"transactions":[
{
"SplitCoins":[
"GasCoin",
[
{
"Input":0
}
]
]
},
{
"MoveCall":{
"package":"0x0000000000000000000000000000000000000000000000000000000000000002",
"module":"coin",
"function":"zero",
"type_arguments":[
"0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC"
]
}
},
{
"MoveCall":{
"package":"0xb104ecc75397f3a65735ef26c85a037da1d197e26f4f275a9990a577ba0e6c4c",
"module":"gateway",
"function":"swap_assets",
"type_arguments":[
"0x2::sui::SUI",
"0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC"
],
"arguments":[
{
"Input":1
},
{
"Input":2
},
{
"Input":3
},
{
"Result":0
},
{
"Result":1
},
{
"Input":4
},
{
"Input":5
},
{
"Input":6
},
{
"Input":7
},
{
"Input":8
}
]
}
}
]
},
"sender":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269",
"gasData":{
"payment":[
{
"objectId":"0x10611c2158f9d0456e6eb47b8b4168fd0fcab2102a1338cededb8cfbfc764367",
"version":410525748,
"digest":"4MZ7i2J8ymMzTBhYpbbu7AkGMezTHoGTRsRSccZdSbTB"
},
{
"objectId":"0x4682287b2248eceacecd263e9c6239b278a8b05de0dcde171602b6c88e3ebfe8",
"version":410525748,
"digest":"2XbVKjQ2EUzvRfTL2aH5iHiKrso8NXEuj55awAU4YAoA"
}
],
"owner":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269",
"price":"750",
"budget":"3879028"
}
},
"txSignatures":[
"AI0J6M5uQ6rddmz8Bpdca3ETWlX4syawwW9yri1vokgN2Ce8u5J62fKoBVEVIUNDTufT305iKs0FcReNL48h3gVwIesVczXTTL/3Q5yIJFnOVC24GMSqzrdNJIY1vK3z9w=="
]
},
"effects":{
"messageVersion":"v1",
"status":{
"status":"success"
},
"executedEpoch":"569",
"gasUsed":{
"computationCost":"750000",
"storageCost":"9173200",
"storageRebate":"8750412",
"nonRefundableStorageFee":"88388"
},
"modifiedAtVersions":[
{
"objectId":"0x10611c2158f9d0456e6eb47b8b4168fd0fcab2102a1338cededb8cfbfc764367",
"sequenceNumber":"410525748"
},
{
"objectId":"0x3b585786b13af1d8ea067ab37101b6513a05d2f90cfe60e8b1d9e1b46a63c4fa",
"sequenceNumber":"410525748"
},
{
"objectId":"0x4682287b2248eceacecd263e9c6239b278a8b05de0dcde171602b6c88e3ebfe8",
"sequenceNumber":"410525748"
}
],
"sharedObjects":[
{
"objectId":"0x3b585786b13af1d8ea067ab37101b6513a05d2f90cfe60e8b1d9e1b46a63c4fa",
"version":410525748,
"digest":"Zi6o3C3bt9UsWoodvEaJNsr7Z5qikvnmAgkgx6mLUA6"
},
{
"objectId":"0x0000000000000000000000000000000000000000000000000000000000000006",
"version":161661005,
"digest":"D3FAuD9cAtYf5d7WS18PsQky71sY3oEYwPRhXxnThLnb"
},
{
"objectId":"0x03db251ba509a8d5d8777b6338836082335d93eecbdd09a11e190a1cff51c352",
"version":409452196,
"digest":"cHY3mzy38rXW9RSBJZFvBuGA95wpNTjZet4NR136suv"
}
],
"transactionDigest":"FbDvEuFHTabTYtRbvgEjPNgW9YCv8FPSSb79tFQ7VCR4",
"created":[
{
"owner":{
"AddressOwner":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269"
},
"reference":{
"objectId":"0x66243ddef46317b0041ef8228d992f50e4ab7171f59a49032a40328ab11a68aa",
"version":410525749,
"digest":"4sPiM6KwTudeTBqpUa8tJYS1UsVCLxaBBbvjfmjgb4GB"
}
}
],
"mutated":[
{
"owner":{
"AddressOwner":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269"
},
"reference":{
"objectId":"0x10611c2158f9d0456e6eb47b8b4168fd0fcab2102a1338cededb8cfbfc764367",
"version":410525749,
"digest":"2UDbfZPJDLTnMGnxN1WmiJ51nNkLp2VA5pCkesE2RU5m"
}
},
{
"owner":{
"Shared":{
"initial_shared_version":406731547
}
},
"reference":{
"objectId":"0x3b585786b13af1d8ea067ab37101b6513a05d2f90cfe60e8b1d9e1b46a63c4fa",
"version":410525749,
"digest":"6VKtD7GKQCzCaKqB3VDkLNh6DMaAkR61xyWhJQJ9DNsv"
}
}
],
"deleted":[
{
"objectId":"0x4682287b2248eceacecd263e9c6239b278a8b05de0dcde171602b6c88e3ebfe8",
"version":410525749,
"digest":"7gyGAp71YXQRoxmFBaHxofQXAipvgHyBKPyxmdSJxyvz"
}
],
"gasObject":{
"owner":{
"AddressOwner":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269"
},
"reference":{
"objectId":"0x10611c2158f9d0456e6eb47b8b4168fd0fcab2102a1338cededb8cfbfc764367",
"version":410525749,
"digest":"2UDbfZPJDLTnMGnxN1WmiJ51nNkLp2VA5pCkesE2RU5m"
}
},
"eventsDigest":"BtDmDYjfzNKgStRHSfiRMUm1UtUUS3fvhnVLYnXE5diu",
"dependencies":[
"2mo91Mn8imFuBYrsoWTMgtpa2cCWjMXdPE9mJeQtvf3a",
"49puDQZwHRnu7zYoARCjco1dFKMiK7LVSS8B5Si1yVdh",
"7DkJSrf5sPWGxqHbH2X1wXwFcftRm6myhJJnuTJbaMYr",
"Atpejs9MU4EGVAzBETZqppC8jUtpc8cmozj7yEHGB73g",
"CUQmrEssKtB8WxBYj3YQZJCrFTTDLqz3UWq1nRiWGCLS",
"HzzEA9X29bCpPE7AJqoxN8zUgefPHztVJu1c73ScgxGa"
]
},
"events":[
{
"id":{
"txDigest":"FbDvEuFHTabTYtRbvgEjPNgW9YCv8FPSSb79tFQ7VCR4",
"eventSeq":"0"
},
"packageId":"0xb104ecc75397f3a65735ef26c85a037da1d197e26f4f275a9990a577ba0e6c4c",
"transactionModule":"gateway",
"sender":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269",
"type":"0x3492c874c1e3b3e2984e8c41b589e642d4d0a5d6459e5a9cfc2d52fd7c89c267::events::AssetSwap",
"parsedJson":{
"a2b":true,
"after_liquidity":"4777281631688",
"after_sqrt_price":"806253565480128302",
"amount_in":"200000000",
"amount_out":"381298",
"before_liquidity":"4777281631688",
"before_sqrt_price":"806255037806392276",
"current_tick":{
"bits":4294904687
},
"exceeded":false,
"fee":"300000",
"pool_coin_a_amount":"2919491509461",
"pool_coin_b_amount":"1597778886",
"pool_id":"0x3b585786b13af1d8ea067ab37101b6513a05d2f90cfe60e8b1d9e1b46a63c4fa",
"sequence_number":"3957"
},
"bcs":"6NWSg8Zmcjfa6V4AXKfpNAeFVvJpChrkBZmegqXQSn5pjga88KhRoUXS9uVEcRZR1oVHJcLpEzKhNrDHhKL3a7kgefGhw7BTFE8zcAZBjzcFTrur2PzuhRoL7tMAbmX3Wz5J9ETUWCUMwuXXPTyyHNEBSGymfpWiY8gBRKihLoWNE3r81Fo7DxCqywr4YbChDRT712CoBwCkpW4guiF7YgKy"
}
],
"objectChanges":[
{
"type":"mutated",
"sender":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269",
"owner":{
"AddressOwner":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269"
},
"objectType":"0x2::coin::Coin<0x2::sui::SUI>",
"objectId":"0x10611c2158f9d0456e6eb47b8b4168fd0fcab2102a1338cededb8cfbfc764367",
"version":"410525749",
"previousVersion":"410525748",
"digest":"2UDbfZPJDLTnMGnxN1WmiJ51nNkLp2VA5pCkesE2RU5m"
},
{
"type":"mutated",
"sender":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269",
"owner":{
"Shared":{
"initial_shared_version":406731547
}
},
"objectType":"0x3492c874c1e3b3e2984e8c41b589e642d4d0a5d6459e5a9cfc2d52fd7c89c267::pool::Pool<0x2::sui::SUI, 0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC>",
"objectId":"0x3b585786b13af1d8ea067ab37101b6513a05d2f90cfe60e8b1d9e1b46a63c4fa",
"version":"410525749",
"previousVersion":"410525748",
"digest":"6VKtD7GKQCzCaKqB3VDkLNh6DMaAkR61xyWhJQJ9DNsv"
},
{
"type":"created",
"sender":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269",
"owner":{
"AddressOwner":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269"
},
"objectType":"0x2::coin::Coin<0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC>",
"objectId":"0x66243ddef46317b0041ef8228d992f50e4ab7171f59a49032a40328ab11a68aa",
"version":"410525749",
"digest":"4sPiM6KwTudeTBqpUa8tJYS1UsVCLxaBBbvjfmjgb4GB"
},
{
"type":"deleted",
"sender":"0xa2d6fcd5ed2ae13fe527a96c96100120d519900599f33e38ee112d80e51f9269",
"objectType":"0x2::coin::Coin<0x2::sui::SUI>",
"objectId":"0x4682287b2248eceacecd263e9c6239b278a8b05de0dcde171602b6c88e3ebfe8",
"version":"410525749"
}
],
"confirmedLocalExecution":false
}