UNPKG

aftermath-ts-sdk

Version:
168 lines (167 loc) 7.91 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.CoinApi = void 0; const coin_1 = require("../coin"); const helpers_1 = require("../../../general/utils/helpers"); const transactionsApiHelpers_1 = require("../../../general/apiHelpers/transactionsApiHelpers"); // import { ethers, Networkish } from "ethers"; class CoinApi { // ========================================================================= // Constructor // ========================================================================= constructor(Provider) { this.Provider = Provider; // ========================================================================= // Transaction Builders // ========================================================================= this.fetchCoinWithAmountTx = (inputs) => __awaiter(this, void 0, void 0, function* () { const { tx, walletAddress, coinType, coinAmount, isSponsoredTx } = inputs; tx.setSender(walletAddress); const coinData = yield this.fetchCoinsWithAtLeastAmount(inputs); return CoinApi.coinWithAmountTx({ tx, coinData, coinAmount, coinType, isSponsoredTx, }); }); this.fetchCoinsWithAmountTx = (inputs) => __awaiter(this, void 0, void 0, function* () { const { tx, walletAddress, coinTypes, coinAmounts, isSponsoredTx } = inputs; tx.setSender(walletAddress); const allCoinsData = yield Promise.all(coinTypes.map((coinType, index) => __awaiter(this, void 0, void 0, function* () { return this.fetchCoinsWithAtLeastAmount(Object.assign(Object.assign({}, inputs), { coinAmount: coinAmounts[index], coinType })); }))); let coinArgs = []; for (const [index, coinData] of allCoinsData.entries()) { const coinArg = CoinApi.coinWithAmountTx({ tx, coinData, coinAmount: coinAmounts[index], coinType: coinTypes[index], isSponsoredTx, }); coinArgs = [...coinArgs, coinArg]; } return coinArgs; }); this.fetchCoinsWithAtLeastAmount = (inputs) => __awaiter(this, void 0, void 0, function* () { let allCoinData = []; let cursor = undefined; do { const paginatedCoins = yield this.Provider.provider.getCoins(Object.assign(Object.assign({}, inputs), { owner: inputs.walletAddress, cursor })); const coinData = paginatedCoins.data; allCoinData = [...allCoinData, ...coinData]; if (paginatedCoins.data.length === 0 || !paginatedCoins.hasNextPage || !paginatedCoins.nextCursor) { allCoinData.sort((b, a) => Number(BigInt(a.balance) - BigInt(b.balance))); let coinDatas = []; let sum = BigInt(0); for (const coinData of allCoinData) { coinDatas.push(coinData); sum += BigInt(coinData.balance); if (sum >= inputs.coinAmount) return coinDatas; } throw new Error("wallet does not have coins of sufficient balance"); } cursor = paginatedCoins.nextCursor; } while (true); }); // fetchCoinsUntilAmountReachedOrEnd this.fetchAllCoins = (inputs) => __awaiter(this, void 0, void 0, function* () { let allCoinData = []; let cursor = undefined; do { const paginatedCoins = yield this.Provider.provider.getCoins(Object.assign(Object.assign({}, inputs), { owner: inputs.walletAddress, cursor })); // const coinData = paginatedCoins.data.filter( // (data) => BigInt(data.balance) > BigInt(0) // ); const coinData = paginatedCoins.data; allCoinData = [...allCoinData, ...coinData]; // const totalAmount = Helpers.sumBigInt( // allCoinData.map((data) => BigInt(data.balance)) // ); // if (totalAmount >= inputs.coinAmount) return allCoinData; if (paginatedCoins.data.length === 0 || !paginatedCoins.hasNextPage || !paginatedCoins.nextCursor) return allCoinData.sort((b, a) => Number(BigInt(b.coinObjectId) - BigInt(a.coinObjectId))); cursor = paginatedCoins.nextCursor; } while (true); }); } } exports.CoinApi = CoinApi; // ========================================================================= // Private Static Methods // ========================================================================= CoinApi.coinWithAmountTx = (inputs) => { const { tx, coinData, coinAmount, coinType, isSponsoredTx } = inputs; if (coinData.length <= 0) throw new Error("wallet does not have coins of sufficient balance"); const isSuiCoin = coin_1.Coin.isSuiCoin(coinData[0].coinType); const totalCoinBalance = helpers_1.Helpers.sumBigInt(coinData.map((data) => BigInt(data.balance))); if (totalCoinBalance < coinAmount) throw new Error("wallet does not have coins of sufficient balance"); if (!isSponsoredTx && isSuiCoin) { tx.setGasPayment(coinData.map((obj) => { return Object.assign(Object.assign({}, obj), { objectId: obj.coinObjectId }); })); return tx.splitCoins(tx.gas, [coinAmount]); // return Helpers.transactions.splitCoinsTx({ // tx, // coinId: tx.gas, // amounts: [coinAmount], // coinType, // }); } const coinObjectIds = coinData.map((data) => data.coinObjectId); const mergedCoinObjectId = coinObjectIds[0]; if (coinObjectIds.length > 1) { // TODO: fix this (v1) if (isSponsoredTx) { tx.add({ $kind: "MergeCoins", MergeCoins: { destination: tx.object(mergedCoinObjectId), sources: [ ...coinObjectIds .slice(1) .map((coinId) => tx.object(coinId)), ], }, }); } else { tx.mergeCoins(tx.object(mergedCoinObjectId), [ ...coinObjectIds .slice(1) .map((coinId) => tx.object(coinId)), ]); } } // return tx.add({ // kind: "SplitCoins", // coin: tx.object(mergedCoinObjectId), // amounts: [tx.pure(coinAmount)], // }); return isSponsoredTx ? transactionsApiHelpers_1.TransactionsApiHelpers.splitCoinTx({ tx, coinId: mergedCoinObjectId, amount: coinAmount, coinType, }) : tx.splitCoins(mergedCoinObjectId, [coinAmount]); };