UNPKG

aftermath-ts-sdk

Version:
251 lines (250 loc) 13.9 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NftAmmApi = void 0; const nftAmmApiCasting_1 = require("./nftAmmApiCasting"); const utils_1 = require("../../../general/utils"); const coin_1 = require("../../coin/coin"); const pools_1 = require("../../pools/pools"); const transactions_1 = require("@mysten/sui/transactions"); class NftAmmApi { // ========================================================================= // Constructor // ========================================================================= constructor(Provider) { this.Provider = Provider; // ========================================================================= // Public Methods // ========================================================================= // ========================================================================= // Objects // ========================================================================= this.fetchNftsInMarketTable = (inputs) => __awaiter(this, void 0, void 0, function* () { return yield this.Provider.DynamicFields().fetchCastDynamicFieldsOfTypeWithCursor(Object.assign(Object.assign({}, inputs), { parentObjectId: inputs.marketTableObjectId, objectsFromObjectIds: (objectIds) => this.Provider.Nfts().fetchNfts({ objectIds }) })); }); this.fetchMarket = (inputs) => __awaiter(this, void 0, void 0, function* () { return this.Provider.Objects().fetchCastObject(Object.assign(Object.assign({}, inputs), { objectFromSuiObjectResponse: nftAmmApiCasting_1.NftAmmApiCasting.marketObjectFromSuiObject })); }); this.fetchMarkets = (inputs) => __awaiter(this, void 0, void 0, function* () { return this.Provider.Objects().fetchCastObjectBatch(Object.assign(Object.assign({}, inputs), { objectFromSuiObjectResponse: nftAmmApiCasting_1.NftAmmApiCasting.marketObjectFromSuiObject })); }); // ========================================================================= // Transaction Builders // ========================================================================= this.fetchBuildBuyTx = (inputs) => __awaiter(this, void 0, void 0, function* () { const tx = new transactions_1.Transaction(); tx.setSender(inputs.walletAddress); const { market } = inputs; const marketObject = market.market; const expectedAssetCoinAmountIn = market.getBuyAssetCoinAmountIn({ nftsCount: inputs.nftObjectIds.length, referral: inputs.referrer !== undefined, }); const assetCoin = yield this.Provider.Coin().fetchCoinWithAmountTx({ tx, walletAddress: inputs.walletAddress, coinType: marketObject.assetCoinType, coinAmount: expectedAssetCoinAmountIn, }); this.buyTx(Object.assign(Object.assign({ tx }, inputs), { marketObjectId: marketObject.objectId, genericTypes: NftAmmApi.genericTypesForMarket({ market }), assetCoin, expectedAssetCoinAmountIn, withTransfer: true })); return tx; }); this.fetchBuildSellTx = (inputs) => __awaiter(this, void 0, void 0, function* () { const tx = new transactions_1.Transaction(); tx.setSender(inputs.walletAddress); const { market } = inputs; const marketObject = market.market; const expectedAssetCoinAmountOut = market.getSellAssetCoinAmountOut({ nftsCount: inputs.nftObjectIds.length, referral: inputs.referrer !== undefined, }); this.sellTx(Object.assign(Object.assign({}, inputs), { tx, nfts: inputs.nftObjectIds, marketObjectId: marketObject.objectId, genericTypes: NftAmmApi.genericTypesForMarket({ market }), expectedAssetCoinAmountOut, withTransfer: true })); return tx; }); this.fetchBuildDepositTx = (inputs) => __awaiter(this, void 0, void 0, function* () { const tx = new transactions_1.Transaction(); tx.setSender(inputs.walletAddress); const { market } = inputs; const marketObject = market.market; const { lpRatio } = market.getDepositLpCoinAmountOut({ assetCoinAmountIn: inputs.assetCoinAmountIn, referral: inputs.referrer !== undefined, }); // // TODO: move this somewhere else and into its own func const expectedLpRatio = utils_1.Casting.numberToFixedBigInt(lpRatio); const assetCoin = yield this.Provider.Coin().fetchCoinWithAmountTx({ tx, walletAddress: inputs.walletAddress, coinType: marketObject.assetCoinType, coinAmount: inputs.assetCoinAmountIn, }); this.depositTx(Object.assign(Object.assign({ tx }, inputs), { marketObjectId: marketObject.objectId, genericTypes: NftAmmApi.genericTypesForMarket({ market }), expectedLpRatio, assetCoin, withTransfer: true })); return tx; }); this.fetchBuildWithdrawTx = (inputs) => __awaiter(this, void 0, void 0, function* () { const tx = new transactions_1.Transaction(); tx.setSender(inputs.walletAddress); const { market } = inputs; const marketObject = market.market; const fractionalizedCoinAmountOut = market.getWithdrawFractionalizedCoinAmountOut({ lpCoinAmount: inputs.lpCoinAmount, referral: inputs.referrer !== undefined, }); const { balances: coinAmountsOut } = coin_1.Coin.coinsAndBalancesOverZero({ [marketObject.fractionalizedCoinType]: fractionalizedCoinAmountOut, }); const expectedAssetCoinAmountOut = coinAmountsOut[0]; const lpCoin = yield this.Provider.Coin().fetchCoinWithAmountTx({ tx, walletAddress: inputs.walletAddress, coinType: marketObject.lpCoinType, coinAmount: inputs.lpCoinAmount, }); this.addWithdrawCommandToTransaction(Object.assign(Object.assign({ tx }, inputs), { marketObjectId: marketObject.objectId, genericTypes: NftAmmApi.genericTypesForMarket({ market }), expectedAssetCoinAmountOut, lpCoin, withTransfer: true })); return tx; }); // ========================================================================= // Transaction Commands // ========================================================================= this.buyTx = (inputs) => { const { tx, assetCoin, genericTypes, nftObjectIds } = inputs; return tx.moveCall({ target: utils_1.Helpers.transactions.createTxTarget(this.addresses.packages.nftAmm, inputs.withTransfer ? NftAmmApi.constants.moduleNames.interface : NftAmmApi.constants.moduleNames.actions, "buy"), typeArguments: genericTypes, arguments: [ tx.object(inputs.marketObjectId), tx.object(this.addresses.objects.protocolFeeVault), tx.object(this.addresses.objects.treasury), tx.object(this.addresses.objects.insuranceFund), tx.object(this.addresses.objects.referralVault), typeof assetCoin === "string" ? tx.object(assetCoin) : assetCoin, tx.makeMoveVec({ elements: nftObjectIds.map((id) => tx.object(id)), type: "ID", }), tx.pure.u64(inputs.expectedAssetCoinAmountIn.toString()), tx.pure.u64(pools_1.Pools.normalizeInvertSlippage(inputs.slippage)), ], }); }; this.sellTx = (inputs) => { const { tx, genericTypes, nfts } = inputs; return tx.moveCall({ target: utils_1.Helpers.transactions.createTxTarget(this.addresses.packages.nftAmm, inputs.withTransfer ? NftAmmApi.constants.moduleNames.interface : NftAmmApi.constants.moduleNames.actions, "sell"), typeArguments: genericTypes, arguments: [ tx.object(inputs.marketObjectId), tx.object(this.addresses.objects.protocolFeeVault), tx.object(this.addresses.objects.treasury), tx.object(this.addresses.objects.insuranceFund), tx.object(this.addresses.objects.referralVault), tx.makeMoveVec({ elements: utils_1.Helpers.isArrayOfStrings(nfts) ? nfts.map((nft) => tx.object(nft)) : nfts, type: genericTypes[3], }), tx.pure.u64(inputs.expectedAssetCoinAmountOut.toString()), tx.pure.u64(pools_1.Pools.normalizeInvertSlippage(inputs.slippage)), ], }); }; this.depositTx = (inputs) => { const { tx, assetCoin, genericTypes, nfts } = inputs; return tx.moveCall({ target: utils_1.Helpers.transactions.createTxTarget(this.addresses.packages.nftAmm, inputs.withTransfer ? NftAmmApi.constants.moduleNames.interface : NftAmmApi.constants.moduleNames.actions, "deposit"), typeArguments: genericTypes, arguments: [ tx.object(inputs.marketObjectId), tx.object(this.addresses.objects.protocolFeeVault), tx.object(this.addresses.objects.treasury), tx.object(this.addresses.objects.insuranceFund), tx.object(this.addresses.objects.referralVault), typeof assetCoin === "string" ? tx.object(assetCoin) : assetCoin, tx.makeMoveVec({ elements: utils_1.Helpers.isArrayOfStrings(nfts) ? nfts.map((nft) => tx.object(nft)) : nfts, type: genericTypes[3], }), tx.pure.u64(inputs.expectedLpRatio.toString()), tx.pure.u64(pools_1.Pools.normalizeInvertSlippage(inputs.slippage)), ], }); }; this.addWithdrawCommandToTransaction = (inputs) => { const { tx, lpCoin, genericTypes, nftObjectIds } = inputs; return tx.moveCall({ target: utils_1.Helpers.transactions.createTxTarget(this.addresses.packages.nftAmm, inputs.withTransfer ? NftAmmApi.constants.moduleNames.interface : NftAmmApi.constants.moduleNames.actions, "withdraw"), typeArguments: genericTypes, arguments: [ tx.object(inputs.marketObjectId), tx.object(this.addresses.objects.protocolFeeVault), tx.object(this.addresses.objects.treasury), tx.object(this.addresses.objects.insuranceFund), tx.object(this.addresses.objects.referralVault), typeof lpCoin === "string" ? tx.object(lpCoin) : lpCoin, tx.makeMoveVec({ elements: nftObjectIds.map((id) => tx.object(id)), type: "ID", }), tx.pure.u64(inputs.expectedAssetCoinAmountOut.toString()), tx.pure.u64(pools_1.Pools.normalizeInvertSlippage(inputs.slippage)), ], }); }; const addresses = this.Provider.addresses.nftAmm; if (!addresses) throw new Error("not all required addresses have been set in provider"); this.addresses = addresses; } } exports.NftAmmApi = NftAmmApi; // ========================================================================= // Constants // ========================================================================= NftAmmApi.constants = { moduleNames: { interface: "interface", actions: "actions", market: "market", }, }; // ========================================================================= // Private Methods // ========================================================================= // ========================================================================= // Helpers // ========================================================================= NftAmmApi.genericTypesForMarket = (inputs) => { const marketObject = inputs.market.market; return [ marketObject.lpCoinType, marketObject.fractionalizedCoinType, marketObject.assetCoinType, marketObject.nftType, ]; };