@layerzerolabs/lz-aptos-sdk-v2
Version:
572 lines (564 loc) • 26.5 kB
JavaScript
;
var tsSdk = require('@aptos-labs/ts-sdk');
var invariant = require('tiny-invariant');
var lzCorekitAptos = require('@layerzerolabs/lz-corekit-aptos');
var lzDefinitions = require('@layerzerolabs/lz-definitions');
var lzMovevmSdkV2 = require('@layerzerolabs/lz-movevm-sdk-v2');
var moveDefinitions = require('@layerzerolabs/move-definitions');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var invariant__default = /*#__PURE__*/_interopDefault(invariant);
// src/index.ts
// src/generated/addresses.ts
var ENDPOINT_V2_ADDRESS = {
"aptos-sandbox-local": "0x824f76b2794de0a0bf25384f2fde4db5936712e6c5c45cf2c3f9ef92e75709c",
"aptos-testnet": "0x7f03103b83c51c8b09be1751a797a65ac6e755f72947ecdecffc203d32d816c6",
"aptos-mainnet": "0xe60045e20fc2c99e869c1c34a65b9291c020cd12a0d37a00a53ac1348af4f43c",
"movement-sandbox-local": "0x824f76b2794de0a0bf25384f2fde4db5936712e6c5c45cf2c3f9ef92e75709c",
"movement-testnet": "0x7f03103b83c51c8b09be1751a797a65ac6e755f72947ecdecffc203d32d816c6",
"movement-mainnet": "0xe60045e20fc2c99e869c1c34a65b9291c020cd12a0d37a00a53ac1348af4f43c"
};
var SIMPLE_MESSAGE_LIB_ADDRESS = {
"aptos-sandbox-local": "0xbb29964fac328dc2cd1dbf03982e51fad9de67e2a525c35d6e52cde4b66e7997",
"aptos-testnet": "0x98ab4d5f6f5ae0d3bc9d9785cfd63244fffc4652bf54f8971cb7035589a256eb",
"aptos-mainnet": "0x52d5c6f8dcb20ed8ace8dbaa7cc09a98eb1dbec0f184720795310c031ace5111",
"movement-sandbox-local": "0xbb29964fac328dc2cd1dbf03982e51fad9de67e2a525c35d6e52cde4b66e7997",
"movement-testnet": "0x98ab4d5f6f5ae0d3bc9d9785cfd63244fffc4652bf54f8971cb7035589a256eb",
"movement-mainnet": "0x52d5c6f8dcb20ed8ace8dbaa7cc09a98eb1dbec0f184720795310c031ace5111"
};
var ULN_MESSAGE_LIB_ADDRESS = {
"aptos-sandbox-local": "0x3f2714ef2d63f1128f45e4a3d31b354c1c940ccdb38aca697c9797ef95e7a09f",
"aptos-testnet": "0xcc1c03aed42e2841211865758b5efe93c0dde2cb7a2a5dc6cf25a4e33ad23690",
"aptos-mainnet": "0xc33752e0220faf79e45385dd73fb28d681dcd9f1569a1480725507c1f3c3aba9",
"movement-sandbox-local": "0x3f2714ef2d63f1128f45e4a3d31b354c1c940ccdb38aca697c9797ef95e7a09f",
"movement-testnet": "0xcc1c03aed42e2841211865758b5efe93c0dde2cb7a2a5dc6cf25a4e33ad23690",
"movement-mainnet": "0xc33752e0220faf79e45385dd73fb28d681dcd9f1569a1480725507c1f3c3aba9"
};
var COUNTER_V2_ADDRESS = {
"aptos-sandbox-local": "0xbf1258595a80969371696c798d481a47dd0a1e8fe087c1c8e2a94e17801bec6e",
"aptos-testnet": "0x0b8815df49c1f36000581fbe37b39f703f112ff83081004302c2effb5f6305f5",
"aptos-mainnet": "0x3dd555e720e29ffd45c7ba6f3dcacc3a467730a900e3de81b791f5b3863579f1",
"movement-sandbox-local": "0xbf1258595a80969371696c798d481a47dd0a1e8fe087c1c8e2a94e17801bec6e",
"movement-testnet": "0xb8815df49c1f36000581fbe37b39f703f112ff83081004302c2effb5f6305f5",
"movement-mainnet": "0x3dd555e720e29ffd45c7ba6f3dcacc3a467730a900e3de81b791f5b3863579f1"
};
var OFT_ADDRESS = {
"aptos-sandbox-local": "0x40cf3d168e49851d6423b919338aba1c42f018a9996395e777548a7e9ce956c9",
"aptos-testnet": "0x732ab32f721cc9dea67db94e1bbfc9641f7c6c45bcfc277757559bb6d501340f",
"aptos-mainnet": "0xd0ef5cf11188041d9e688d9fbf0f0205e3d156b4b4325e6268256a6e51c9fab9",
"movement-sandbox-local": "0x40cf3d168e49851d6423b919338aba1c42f018a9996395e777548a7e9ce956c9",
"movement-testnet": "0x732ab32f721cc9dea67db94e1bbfc9641f7c6c45bcfc277757559bb6d501340f",
"movement-mainnet": "0xd0ef5cf11188041d9e688d9fbf0f0205e3d156b4b4325e6268256a6e51c9fab9"
};
var LAYERZERO_VIEWS_ADDRESS = {
"aptos-sandbox-local": "0x9f03c64333c99c9d319ffc034c13618d309a8fa6f18636261d7e5d3e21e2d8d8",
"aptos-testnet": "0x8a2453373b206a7d3b470a3fd62a1c7185f8ea0f7072e4ab65dd709f0f0467ff",
"aptos-mainnet": "0x1cc729cf1cb5491d9dd3f0ad004884cbeb8d1bc9df87bb3aa9a4917e7ffa1aee",
"movement-sandbox-local": "0x9f03c64333c99c9d319ffc034c13618d309a8fa6f18636261d7e5d3e21e2d8d8",
"movement-testnet": "0x8a2453373b206a7d3b470a3fd62a1c7185f8ea0f7072e4ab65dd709f0f0467ff",
"movement-mainnet": "0x1cc729cf1cb5491d9dd3f0ad004884cbeb8d1bc9df87bb3aa9a4917e7ffa1aee"
};
var EXECUTOR_ADDRESS = {
"aptos-sandbox-local": "0x806020afea680a0c0f32431acdfcf1a7e31ace28ce81b73da4f27c5898155590",
"aptos-testnet": "0x93353700091200ef9fdc536ce6a86182cc7e62da25f94356be9421c6310b9585",
"aptos-mainnet": "0x15a5bbf1eb7998a22c9f23810d424abe40bd59ddd8e6ab7e59529853ebed41c4",
"movement-sandbox-local": "0x806020afea680a0c0f32431acdfcf1a7e31ace28ce81b73da4f27c5898155590",
"movement-testnet": "0x93353700091200ef9fdc536ce6a86182cc7e62da25f94356be9421c6310b9585",
"movement-mainnet": "0x15a5bbf1eb7998a22c9f23810d424abe40bd59ddd8e6ab7e59529853ebed41c4"
};
var EXECUTOR_ULN301_ADDRESS = {
"aptos-sandbox-local": "0xdb02463fcf441ade02733f535f276b04e8220b8f7fbe8da5197ba4141aa0c248",
"aptos-testnet": "0x6c405a040328f466a9edfbc0c272731e9cdeb2d26fa6643e618614fbb120748",
"aptos-mainnet": "0x956f942b024d04b09b0bc40e283b3c8a247bca2671cc227e1af72f7dcae24e05",
"movement-sandbox-local": "0xdb02463fcf441ade02733f535f276b04e8220b8f7fbe8da5197ba4141aa0c248",
"movement-testnet": "0x0",
"movement-mainnet": "0x0"
};
var DVN_ADDRESS = {
"aptos-sandbox-local": "0x1f79b324153abe0ca18a279822f3b561acbaabb4d68d47ed3639b5a53e4d3470",
"aptos-testnet": "0x756f8ab056688d22687740f4a9aeec3b361170b28d08b719e28c4d38eed1043e",
"aptos-mainnet": "0xdf8f0a53b20f1656f998504b81259698d126523a31bdbbae45ba1e8a3078d8da",
"movement-sandbox-local": "0x1f79b324153abe0ca18a279822f3b561acbaabb4d68d47ed3639b5a53e4d3470",
"movement-testnet": "0x756f8ab056688d22687740f4a9aeec3b361170b28d08b719e28c4d38eed1043e",
"movement-mainnet": "0xdf8f0a53b20f1656f998504b81259698d126523a31bdbbae45ba1e8a3078d8da"
};
var DVN_301_ADDRESS = {
"aptos-sandbox-local": "0x1f79b324153abe0ca18a279822f3b561acbaabb4d68d47ed3639b5a53e4d3470",
"aptos-testnet": "0x60072f90836f8142e4c87820ba9e27c4e84dffcff2a0117e70ee85240f609af4",
"aptos-mainnet": "0xf3f0a412626edba5ddd3613d91109b241893873ac5479ade231cf0b3130572b5",
"movement-sandbox-local": "0x1f79b324153abe0ca18a279822f3b561acbaabb4d68d47ed3639b5a53e4d3470",
"movement-testnet": "0x0",
"movement-mainnet": "0x0"
};
var DVN_UPGRADER_ADDRESS = {
"aptos-sandbox-local": "0xbb0fbbe978dcf5e658e1a447d3e28977a92b276fe0e14d60081b7a2af8b3c61f",
"aptos-testnet": "0xf4aca0f32edb994ada0dcfa8fc51afcca9e5a70b98cdd00bfb39befcf33ef68",
"aptos-mainnet": "0xfbe3700ccc2db708a5412f678c7ba0a65c500799e93b4e758303a513e0f88ec",
"movement-sandbox-local": "0xbb0fbbe978dcf5e658e1a447d3e28977a92b276fe0e14d60081b7a2af8b3c61f",
"movement-testnet": "0x0",
"movement-mainnet": "0x0"
};
var WORKER_COMMON = {
"aptos-sandbox-local": "0x9d8a2cc4cd5563028107b792fd3c7f4068064405ef1bc4fce1cbc3af916032e8",
"aptos-testnet": "0xfe0b685e4cc9e77d91d008ef4161de68f7d7646c3bf67079fd4c2f0356631be8",
"aptos-mainnet": "0x1bffc83ec332cb9de738e8f0c27dd2230ee57bdbc71473047fcfe8bfaa21fab7",
"movement-sandbox-local": "0x9d8a2cc4cd5563028107b792fd3c7f4068064405ef1bc4fce1cbc3af916032e8",
"movement-testnet": "0xfe0b685e4cc9e77d91d008ef4161de68f7d7646c3bf67079fd4c2f0356631be8",
"movement-mainnet": "0x1bffc83ec332cb9de738e8f0c27dd2230ee57bdbc71473047fcfe8bfaa21fab7"
};
var PRICE_FEED = {
"aptos-sandbox-local": "0x894473c4f48d05a65d5bfb106ab91bc0881c7a1c7e9c66fbea2859c2ba9bff83",
"aptos-testnet": "0xa762d65f42c852e0a7f6240ec6441694ea1b4786392cdfa5351dee8364c868fc",
"aptos-mainnet": "0xad0f7141f626c07db99a7fe5b864fde080bc4966c144d88f6f14ac4af391f30",
"movement-sandbox-local": "0x894473c4f48d05a65d5bfb106ab91bc0881c7a1c7e9c66fbea2859c2ba9bff83",
"movement-testnet": "0xa762d65f42c852e0a7f6240ec6441694ea1b4786392cdfa5351dee8364c868fc",
"movement-mainnet": "0xad0f7141f626c07db99a7fe5b864fde080bc4966c144d88f6f14ac4af391f30"
};
var TREASURY_ADDRESS = {
"aptos-sandbox-local": "0xd83cb5c494daec692964a3599c3b36d4bd618dc54e7bcb2bbe444a7f9732f740",
"aptos-testnet": "0x3a9902a21eabd3552edfc104cb4a6ce1ac4fe5af6aa24a56037969e1a0db3d93",
"aptos-mainnet": "0x77c941e60b8e2c8d784de2ee456fd497283edfe1e15704c99a192ff795fc38b7",
"movement-sandbox-local": "0xd83cb5c494daec692964a3599c3b36d4bd618dc54e7bcb2bbe444a7f9732f740",
"movement-testnet": "0x3a9902a21eabd3552edfc104cb4a6ce1ac4fe5af6aa24a56037969e1a0db3d93",
"movement-mainnet": "0x77c941e60b8e2c8d784de2ee456fd497283edfe1e15704c99a192ff795fc38b7"
};
var ENDPOINT_V2_COMMON = {
"aptos-sandbox-local": "0xf8ce9f0030cb1fa5fb48f481d9b2da3909cd922992e7085e76a196653a707bbf",
"aptos-testnet": "0x3bc8cbd74c2e1929c287a0063206fbb126314976146934bae12283f6120e99e9",
"aptos-mainnet": "0xe1dc2a62b445403bea0dbd73df8cee03b3ead0a06b003e72e401c030a810a133",
"movement-sandbox-local": "0xf8ce9f0030cb1fa5fb48f481d9b2da3909cd922992e7085e76a196653a707bbf",
"movement-testnet": "0x3bc8cbd74c2e1929c287a0063206fbb126314976146934bae12283f6120e99e9",
"movement-mainnet": "0xe1dc2a62b445403bea0dbd73df8cee03b3ead0a06b003e72e401c030a810a133"
};
var DVN_FEE_LIB = {
"aptos-sandbox-local": "0xf63f6b01ea6688189e09e2be035bef512049288ff1d307c2470a913f3831330f",
"aptos-testnet": "0x9b77c6ad73d3e642f4c59ff191e1b460a7f4a16a67558edba1a744b4d6a88127",
"aptos-mainnet": "0x349c43bc506cbbe7b754b164867bd1751763410b6458a798c25bb6f3c3e9e487",
"movement-sandbox-local": "0xf63f6b01ea6688189e09e2be035bef512049288ff1d307c2470a913f3831330f",
"movement-testnet": "0x9b77c6ad73d3e642f4c59ff191e1b460a7f4a16a67558edba1a744b4d6a88127",
"movement-mainnet": "0x349c43bc506cbbe7b754b164867bd1751763410b6458a798c25bb6f3c3e9e487"
};
var EXECUTOR_FEE_LIB = {
"aptos-sandbox-local": "0xff72f529a579e922e0edba30f384b4c9c86d05020cbd2401a23fe708f29c6664",
"aptos-testnet": "0x4123c50265067995272e998193638aaf876d75454b2aba50d55d950b2236ff4e",
"aptos-mainnet": "0xbbb5d80871b10c4a7c10b9bbc636fdca4faa05feb3b03dc27e3018a7bfcbd8cb",
"movement-sandbox-local": "0xff72f529a579e922e0edba30f384b4c9c86d05020cbd2401a23fe708f29c6664",
"movement-testnet": "0x4123c50265067995272e998193638aaf876d75454b2aba50d55d950b2236ff4e",
"movement-mainnet": "0xbbb5d80871b10c4a7c10b9bbc636fdca4faa05feb3b03dc27e3018a7bfcbd8cb"
};
var DEPLOY_TO_OBJECT = {
"aptos-sandbox-local": "0x68b674103753edaa1c3a38ef06ba92e038ff5b0fd76e066a57ad7fecd19815a6",
"aptos-testnet": "0x4ea1efbda3414034610c9e460aca244a8559605f328866a9e36aa892184f45c",
"aptos-mainnet": "0x472157dc175fa089e50fc97bbdaa7647afbca155eb2ef2c0edd1395c34f109ff",
"movement-sandbox-local": "0x68b674103753edaa1c3a38ef06ba92e038ff5b0fd76e066a57ad7fecd19815a6",
"movement-testnet": "0x4ea1efbda3414034610c9e460aca244a8559605f328866a9e36aa892184f45c",
"movement-mainnet": "0x472157dc175fa089e50fc97bbdaa7647afbca155eb2ef2c0edd1395c34f109ff"
};
// src/utils.ts
var GAS_LIMIT_SAFETY_BPS = 2e3;
function applyGasLimitSafety(gasUsed) {
return BigInt(gasUsed) * BigInt(1e4 + GAS_LIMIT_SAFETY_BPS) / BigInt(1e4);
}
// src/index.ts
function assertAptosProvider(provider) {
if (!(provider instanceof lzCorekitAptos.AptosProvider)) {
throw new Error("Invalid provider");
}
}
function getSDKFromProvider(provider, stage) {
const client = provider.native;
const url = client.nodeUrl;
assertAptosProvider(provider);
const indexerUrl = provider.indexer;
const faucetUrl = provider.faucet;
const aptosConfig = new tsSdk.AptosConfig({ network: tsSdk.Network.LOCAL, fullnode: url, indexer: indexerUrl, faucet: faucetUrl });
const aptos = new tsSdk.Aptos(aptosConfig);
return new SDK({ provider: aptos, stage });
}
function isOptionValue(value) {
return typeof value === "object" && value != null && "vec" in value;
}
var SDK = class {
/**
* Creates an instance of the SDK.
*
* @param {SdkOptions} options - The SDK options.
*/
constructor(options) {
this.chain = options.chain ?? lzDefinitions.Chain.APTOS;
this.stage = options.stage ?? lzDefinitions.Stage.SANDBOX;
const network = lzDefinitions.chainAndStageToNetwork(this.chain, this.stage, this.stage === lzDefinitions.Stage.SANDBOX);
this.accounts = options.accounts ?? {
endpoint_v2: tsSdk.AccountAddress.from(ENDPOINT_V2_ADDRESS[network] ?? "0x0").toString(),
counter_v2: tsSdk.AccountAddress.from(COUNTER_V2_ADDRESS[network] ?? "0x0").toString(),
oft: tsSdk.AccountAddress.from(OFT_ADDRESS[network] ?? "0x0").toString(),
layerzero_views: tsSdk.AccountAddress.from(LAYERZERO_VIEWS_ADDRESS[network] ?? "0x0").toString(),
simple_message_lib: tsSdk.AccountAddress.from(SIMPLE_MESSAGE_LIB_ADDRESS[network] ?? "0x0").toString(),
uln_message_lib: tsSdk.AccountAddress.from(ULN_MESSAGE_LIB_ADDRESS[network] ?? "0x0").toString(),
executor: tsSdk.AccountAddress.from(EXECUTOR_ADDRESS[network] ?? "0x0").toString(),
dvn: tsSdk.AccountAddress.from(DVN_ADDRESS[network] ?? "0x0").toString(),
dvn_upgrader: tsSdk.AccountAddress.from(DVN_UPGRADER_ADDRESS[network] ?? "0x0").toString(),
worker_common: tsSdk.AccountAddress.from(WORKER_COMMON[network] ?? "0x0").toString(),
price_feed: tsSdk.AccountAddress.from(PRICE_FEED[network] ?? "0x0").toString(),
treasury: tsSdk.AccountAddress.from(TREASURY_ADDRESS[network] ?? "0x0").toString(),
endpoint_v2_common: tsSdk.AccountAddress.from(ENDPOINT_V2_COMMON[network] ?? "0x0").toString(),
deploy_to_object: tsSdk.AccountAddress.from(DEPLOY_TO_OBJECT[network] ?? "0x0").toString()
};
this.client = options.provider;
this.LayerzeroModule = new lzMovevmSdkV2.LayerZeroModulesSdk(this);
}
/**
* Gets the Aptos client.
*
* @returns {Aptos} The Aptos client.
*/
getAptosClient() {
return this.client;
}
/**
* Sends and confirms an authenticated transaction.
*
* @param {Object} args - The arguments for the transaction.
* @param {SimpleTransaction} args.txn - The transaction to send.
* @param {AccountAuthenticator} args.auth - The authenticator for the transaction.
* @returns {Promise<CommittedTransactionResponse>} The response of the committed transaction.
*/
async sendAndConfirmAuthTransaction(args) {
const res = await this.client.transaction.submit.simple({
transaction: args.txn,
senderAuthenticator: args.auth
});
return this.waitAndGetTransaction(res.hash);
}
isMoveFunctionId(name) {
return name.split("::").length === 3;
}
/**
* Sends and confirms a transaction with the given signer and payload.
*
* @param {Ed25519Account} signer - The signer of the transaction.
* @param {InputEntryFunctionData} payload - The payload of the transaction.
* @param {GasOptions} [gasOptions] - The gas options for the transaction.
* @returns {Promise<CommittedTransactionResponse>} The response of the committed transaction.
* @private
*/
async sendAndConfirmTransaction_(signer, payload, gasOptions) {
this.assertValidGasOptions(gasOptions);
const options = await this.estimateGas(signer, payload);
const txnRequest = await this.client.transaction.build.simple({
sender: signer.accountAddress,
data: payload,
options: {
gasUnitPrice: Number(gasOptions?.gasPrice !== void 0 ? gasOptions.gasPrice : options.gas_unit_price),
maxGasAmount: Number(gasOptions?.gasLimit !== void 0 ? gasOptions.gasLimit : options.max_gas_amount)
}
});
const auth = new tsSdk.AccountAuthenticatorEd25519(signer.publicKey, signer.signTransaction(txnRequest));
return this.sendAndConfirmAuthTransaction({ txn: txnRequest, auth });
}
/**
* Handles errors and returns appropriate error instances based on the error type.
*
* @param {unknown} e - The error to handle.
* @returns {unknown} The handled error, which could be an instance of MoveAbortError, MoveResourceNotFoundError, MoveTableItemNotFoundError, UnclassifiedError, or the original error.
*/
handleError(e) {
if (e instanceof tsSdk.AptosApiError) {
const errorData = e.data;
const errorString = errorData.message ?? "";
const errorCode = errorData.error_code ?? "";
const majorStatusMatch = errorString.match(/major_status:\s([A-Z_]+)/);
if (majorStatusMatch !== null) {
const major_status = majorStatusMatch[1];
if (major_status === "ABORTED") {
const subStatusMatch = errorString.match(/sub_status:\sSome\((\d+)\)/);
if (subStatusMatch === null) {
return new moveDefinitions.MoveAbortError(Number.NEGATIVE_INFINITY, errorString);
} else {
return new moveDefinitions.MoveAbortError(Number(subStatusMatch[1]), errorString);
}
} else if (major_status === "MISSING_DATA") {
return new moveDefinitions.MoveResourceNotFoundError(errorString);
}
} else if (errorCode === "resource_not_found") {
return new moveDefinitions.MoveResourceNotFoundError(errorString);
} else if (errorCode === "table_item_not_found") {
return new moveDefinitions.MoveTableItemNotFoundError(errorString);
} else if (errorString.includes("Move abort")) {
const regex = /\((0x[0-9a-fA-F]+)\)/;
const match = errorString.match(regex);
if (match) {
const abortCode = match[1];
return new moveDefinitions.MoveAbortError(Number.parseInt(abortCode, 16), errorString);
}
}
return new moveDefinitions.UnclassifiedError(`statusCode:${e.status},status:${e.statusText},errorString:${errorString}`);
}
return e;
}
/**
* Executes a view function and returns the result.
* @implements {MoveSdkImpl<Ed25519Account>}
*
* @param {Object} args - The arguments for the view function.
* @param {MoveFunction} args.functionName - The name of the function.
* @param {EntryFunctionArgumentTypes[]} args.functionArgs - The arguments for the function.
* @param {string[]} [args.typeArgs] - The type arguments for the function.
* @returns {Promise<MoveValue[]>} The result of the view function.
*/
async viewFunction(args) {
const payload = {
function: args.functionName,
functionArguments: args.functionArgs,
typeArguments: args.typeArgs ?? []
};
try {
let view = await this.client.view({ payload });
view = view.map((item) => {
if (isOptionValue(item)) {
if (item.vec.length !== 0) {
return item.vec[0];
} else {
return null;
}
}
return item;
});
if (view.length > 1) {
view = [view];
}
return view;
} catch (e) {
throw this.handleError(e);
}
}
/**
* Normalizes the signer to an Ed25519Account.
* @implements {MoveSdkImpl<Ed25519Account>}
*
* @param {Ed25519Account | PrivateKey | MnemonicAndPath} signer - The signer to normalize.
* @returns {Ed25519Account} The normalized signer.
* @throws {Error} If the signer is invalid.
*/
normalizeSigner(signer) {
if (signer instanceof tsSdk.Ed25519Account) {
return signer;
} else if (moveDefinitions.isMnemonicAndPath(signer)) {
return tsSdk.Ed25519Account.fromDerivationPath(signer);
} else if (moveDefinitions.isPrivateKey(signer)) {
return new tsSdk.Ed25519Account({ privateKey: new tsSdk.Ed25519PrivateKey(signer) });
}
throw new Error(`Invalid signer`);
}
/**
* Converts an account to an address string.
* @implements {MoveSdkImpl<Ed25519Account>}
*
* @param {Ed25519Account} account - The account to convert.
* @returns {string} The address of the account.
*/
accountToAddress(account) {
return account.accountAddress.toString();
}
/**
* Sends a transaction and waits for confirmation.
* @implements {MoveSdkImpl<Ed25519Account>}
*
* @param {Ed25519Account | PrivateKey | MnemonicAndPath} signer - The signer of the transaction.
* @param {MoveFunction} func - The function to call.
* @param {EntryFunctionArgumentTypes[] | SimpleEntryFunctionArgumentTypes[]} args - The arguments for the function.
* @param {string[]} [_] - Unused parameter.
* @param {GasOptions} [gasOptions] - The gas options.
* @param {string[]} [typeArguments] - The type arguments.
* @returns {Promise<TransactionResponse>} The transaction response.
*/
async sendAndConfirmTransaction(signer, func, args, _, gasOptions, typeArguments) {
try {
signer = this.normalizeSigner(signer);
let transactionData;
if (this.isMoveFunctionId(func)) {
transactionData = {
function: func,
functionArguments: args,
typeArguments
};
} else {
transactionData = {
bytecode: func,
functionArguments: args,
typeArguments
};
}
const committedTransactionResponse = await this.sendAndConfirmTransaction_(
signer,
transactionData,
gasOptions
);
return {
hash: committedTransactionResponse.hash,
sender: signer.accountAddress.toString(),
raw: committedTransactionResponse
};
} catch (e) {
throw this.handleError(e);
}
}
/**
* Asserts that the given gas options are valid.
*
* @param {GasOptions} [gasOptions] - The gas options to validate.
* @throws {Error} If the gas options are invalid.
*/
assertValidGasOptions(gasOptions) {
if (gasOptions === void 0) {
return;
}
if (gasOptions.gasPrice !== void 0) {
invariant__default.default(
Number.isInteger(Number(gasOptions.gasPrice)),
`Invalid gas price: ${gasOptions.gasPrice}, must be an integer`
);
}
if (gasOptions.gasLimit !== void 0) {
invariant__default.default(
Number.isInteger(Number(gasOptions.gasLimit)),
`Invalid gas limit: ${gasOptions.gasLimit}, must be an integer`
);
}
}
/**
* Estimates the gas required for a transaction.
*
* @param {Ed25519Account} signer - The signer of the transaction.
* @param {InputGenerateTransactionPayloadData} payload - The payload of the transaction.
* @returns {Promise<{ max_gas_amount: string; gas_unit_price: string }>} The estimated gas amount and gas unit price.
*/
async estimateGas(signer, payload) {
const txnRequest = await this.client.transaction.build.simple({
sender: signer.accountAddress,
data: payload
});
const sim = await this.client.transaction.simulate.simple({
signerPublicKey: signer.publicKey,
transaction: txnRequest,
options: {
estimateGasUnitPrice: true,
estimateMaxGasAmount: true
}
});
const tx = sim[0];
invariant__default.default(tx.success, `Transaction failed: ${tx.vm_status}}`);
const max_gas_amount = applyGasLimitSafety(tx.gas_used).toString();
return {
max_gas_amount,
gas_unit_price: tx.gas_unit_price
};
}
/**
* Gets an account resource.
* @implements {MoveSdkImpl<Ed25519Account>}
*
* @param {Object} args - The arguments for getting the account resource.
* @param {AccountAddressInput} args.accountAddress - The account address.
* @param {MoveStructId} args.resourceType - The type of the resource.
* @param {LedgerVersionArg} [args.options] - The options for getting the resource.
* @returns {Promise<T>} The account resource.
* @template T - The type of the resource.
* @throws {Error} If the request fails.
*/
async getAccountResource(args) {
try {
return await this.client.getAccountResource(args);
} catch (e) {
throw this.handleError(e);
}
}
/**
* Gets a table item.
*
* @param {Object} args - The arguments for getting the table item.
* @param {string} args.handle - The table handle.
* @param {TableItemRequest} args.data - The key of the item.
* @param {LedgerVersionArg} [args.options] - The options for getting the item.
* @returns {Promise<T>} The table item.
* @template T - The type of the item.
* @throws {Error} If the request fails.
*/
async getTableItem(args) {
try {
return await this.client.getTableItem(args);
} catch (e) {
throw this.handleError(e);
}
}
/**
* Waits for a transaction to be confirmed and returns the transaction response.
*
* @param {string} txnHash - The hash of the transaction.
* @returns {Promise<CommittedTransactionResponse>} The response of the committed transaction.
* @throws {Error} If the transaction fails.
* @private
*/
async waitAndGetTransaction(txnHash) {
const tx = await this.client.waitForTransaction({ transactionHash: txnHash });
invariant__default.default(tx.success, `Transaction failed: ${tx.vm_status}`);
return tx;
}
/**
* Normalizes an address to a string.
* @implements {MoveSdkImpl<Ed25519Account>}
*
* @param {string} address - The address to normalize.
* @returns {string} The normalized address.
*/
normalizeAddress(address) {
return tsSdk.AccountAddress.from(address).toString();
}
/**
* Checks if the SDK supports coins.
* @implements {MoveSdkImpl<Ed25519Account>}
*
* @returns {boolean} True if the SDK supports coins, false otherwise.
*/
supportsCoin() {
return true;
}
/**
* Checks if the given address is a valid object address.
* @param address
*/
async isObjectAddress(address) {
try {
await this.getAccountResource({
accountAddress: address,
resourceType: "0x1::object::ObjectCore"
});
return true;
} catch (e) {
return false;
}
}
};
exports.COUNTER_V2_ADDRESS = COUNTER_V2_ADDRESS;
exports.DEPLOY_TO_OBJECT = DEPLOY_TO_OBJECT;
exports.DVN_301_ADDRESS = DVN_301_ADDRESS;
exports.DVN_ADDRESS = DVN_ADDRESS;
exports.DVN_FEE_LIB = DVN_FEE_LIB;
exports.DVN_UPGRADER_ADDRESS = DVN_UPGRADER_ADDRESS;
exports.ENDPOINT_V2_ADDRESS = ENDPOINT_V2_ADDRESS;
exports.ENDPOINT_V2_COMMON = ENDPOINT_V2_COMMON;
exports.EXECUTOR_ADDRESS = EXECUTOR_ADDRESS;
exports.EXECUTOR_FEE_LIB = EXECUTOR_FEE_LIB;
exports.EXECUTOR_ULN301_ADDRESS = EXECUTOR_ULN301_ADDRESS;
exports.GAS_LIMIT_SAFETY_BPS = GAS_LIMIT_SAFETY_BPS;
exports.LAYERZERO_VIEWS_ADDRESS = LAYERZERO_VIEWS_ADDRESS;
exports.OFT_ADDRESS = OFT_ADDRESS;
exports.PRICE_FEED = PRICE_FEED;
exports.SDK = SDK;
exports.SIMPLE_MESSAGE_LIB_ADDRESS = SIMPLE_MESSAGE_LIB_ADDRESS;
exports.TREASURY_ADDRESS = TREASURY_ADDRESS;
exports.ULN_MESSAGE_LIB_ADDRESS = ULN_MESSAGE_LIB_ADDRESS;
exports.WORKER_COMMON = WORKER_COMMON;
exports.applyGasLimitSafety = applyGasLimitSafety;
exports.getSDKFromProvider = getSDKFromProvider;
//# sourceMappingURL=index.cjs.map
//# sourceMappingURL=index.cjs.map