UNPKG

@firefly-exchange/library-sui

Version:

Sui library housing helper methods, classes to interact with Bluefin protocol(s) deployed on Sui

83 lines (82 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserCalls = void 0; const classes_1 = require("../../classes"); const library_1 = require("../../library"); const types_1 = require("../../types"); const on_chain_calls_1 = require("./on-chain-calls"); const utils_1 = require("../../spot/utils"); class UserCalls extends on_chain_calls_1.OnChainCalls { /** * Allows users to deposit coins into the external bank * @param assetSymbol name of the asset to be deposited * @param amountE9 quantity of USDC to be deposited. Should be in 6 decimal places for USDC * @param options Optional tx execution params AND * - asset: the name of the asset to be deposited - defaults to USDC * - account: the address of the account to deposit money into. `Alice` can deposit funds to `Bob`'s account * - coinId: the id of supported USDC coin to be used for deposits. Please ensure that the coin has enough balance. * @returns OnChainCallResponse */ async depositToAssetBank(assetSymbol, amount, options) { const account = options?.account || this.walletAddress; const coinType = this.parser.getAssetCoinType(assetSymbol); const txb = options?.txBlock || new types_1.TransactionBlock(); let depositToken; let splitCoin; let mergedCoin; if (options?.coinId) { depositToken = options?.coinId; } else { const [split, merged] = await classes_1.CoinUtils.createCoinWithBalance(this.suiClient, txb, (0, library_1.bigNumber)(amount).toFixed(0), coinType, this.walletAddress); depositToken = split; splitCoin = split; mergedCoin = merged; } this.txBuilder.depositToAssetBank(assetSymbol, account, (0, library_1.bigNumber)(amount).toFixed(0), depositToken, { ...options, txBlock: txb }); if (mergedCoin) { txb.transferObjects([mergedCoin], this.walletAddress); } if (splitCoin) { txb.transferObjects([splitCoin], this.walletAddress); } return this.execCall(txb, options); } /** * Allows users to deposit provided coin into the external bank (This function is used for PTB) * @param assetSymbol name of the asset to be deposited * @param coin the coin to be deposited * @param options Optional tx execution params AND * - asset: the name of the asset to be deposited - defaults to USDC * - account: the address of the account to deposit money into. `Alice` can deposit funds to `Bob`'s account * @returns OnChainCallResponse */ async depositAllFromCoinToAssetBank(assetSymbol, coin, options) { const account = options?.account || this.walletAddress; const coinType = this.parser.getAssetCoinType(assetSymbol); const txb = options?.txBlock || new types_1.TransactionBlock(); const depositToken = coin; const amountArg = txb.moveCall({ target: "0x2::coin::value", arguments: [coin], typeArguments: [coinType] }); txb.moveCall({ arguments: [ txb.object(this.parser.getExternalDataStore()), txb.pure.string(assetSymbol), txb.pure.address(account), amountArg, txb.object(depositToken) ], typeArguments: [this.parser.getAssetCoinType(assetSymbol)], target: `${this.parser.getPackageId()}::exchange::deposit_to_asset_bank` }); (0, utils_1.transferOrDestroyZeroCoin)(txb, this.parser.getAssetCoinType(assetSymbol), coin, account); return this.execCall(txb, options); } } exports.UserCalls = UserCalls;