@ledgerhq/coin-aptos
Version:
Ledger Aptos Coin integration
38 lines • 1.67 kB
JavaScript
import buildTransaction, { isTokenType } from "./buildTransaction";
import createTransaction from "./createTransaction";
import BigNumber from "bignumber.js";
import { APTOS_ASSET_ID } from "../constants";
export async function craftTransaction(aptosClient, transactionIntent) {
const newTx = createTransaction();
newTx.amount = BigNumber(transactionIntent.amount.toString());
newTx.recipient = transactionIntent.recipient;
newTx.mode = transactionIntent.type;
newTx.useAllAmount = transactionIntent.amount === BigInt(0);
const account = {
freshAddress: transactionIntent.sender,
xpub: transactionIntent.senderPublicKey,
subAccounts: new Array(),
};
let tokenType;
const contractAddress = getContractAddress(transactionIntent);
let balance;
if (newTx.useAllAmount === true) {
const balances = await aptosClient.getBalances(transactionIntent.sender);
balance = balances?.find(b => b.contractAddress.toLowerCase() === contractAddress?.toLowerCase());
if (balance !== undefined) {
newTx.amount = BigNumber(balance.amount.toString());
}
}
if (transactionIntent.asset.type === "token") {
tokenType = transactionIntent.asset.standard;
}
const aptosTx = await buildTransaction(account, newTx, aptosClient, contractAddress, tokenType ?? undefined);
return aptosTx.bcsToHex().toString();
}
function getContractAddress(txIntent) {
if (txIntent.asset.type === "token" && isTokenType(txIntent.asset.standard)) {
return txIntent.asset.contractAddress;
}
return APTOS_ASSET_ID;
}
//# sourceMappingURL=craftTransaction.js.map