whales-otc-pre-market-aptos-sdk
Version:
TypeScript SDK for Whales OTC Pre-Market trading on Aptos blockchain
58 lines (57 loc) • 2.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkCoinToFa = void 0;
const ts_sdk_1 = require("@aptos-labs/ts-sdk");
const checkCoinToFa = async (aptos, sender, exToken) => {
if (!(0, ts_sdk_1.isValidFunctionInfo)(exToken) &&
standardizeMoveTypeString(exToken) !== standardizeMoveTypeString(ts_sdk_1.APTOS_FA)) {
return {
useCoin: false,
coinType: exToken,
};
}
const coinType = standardizeMoveTypeString(exToken) === standardizeMoveTypeString(ts_sdk_1.APTOS_FA)
? ts_sdk_1.APTOS_COIN
: exToken;
const faAddress = standardizeMoveTypeString(coinType) === ts_sdk_1.APTOS_COIN
? ts_sdk_1.AccountAddress.A
: (0, ts_sdk_1.createObjectAddress)(ts_sdk_1.AccountAddress.A, standardizeMoveTypeString(coinType));
const [faBalance] = await aptos.view({
payload: {
function: "0x1::primary_fungible_store::balance",
typeArguments: ["0x1::fungible_asset::Metadata"],
functionArguments: [sender, faAddress],
},
});
const [coinBalance] = await aptos.view({
payload: {
function: "0x1::coin::balance",
typeArguments: [coinType],
functionArguments: [sender],
},
});
if (coinBalance === faBalance) {
return {
useCoin: false,
coinType: exToken,
};
}
return {
useCoin: true,
coinType: coinType,
};
};
exports.checkCoinToFa = checkCoinToFa;
/**
* Helper function to standardize Move type string by converting all addresses to short form,
* including addresses within nested type parameters
*/
function standardizeMoveTypeString(input) {
// Regular expression to match addresses in the type string, including those within type parameters
// This regex matches "0x" followed by hex digits, handling both standalone addresses and those within <>
const addressRegex = /0x[0-9a-fA-F]+/g;
return input.replace(addressRegex, (match) => {
// Use AccountAddress to handle the address
return ts_sdk_1.AccountAddress.from(match, { maxMissingChars: 63 }).toStringShort();
});
}