Skip to content

Instantly share code, notes, and snippets.

@mgild
Created December 28, 2023 20:05
Show Gist options
  • Save mgild/d121571ffec1d98d6b5093c270165984 to your computer and use it in GitHub Desktop.
Save mgild/d121571ffec1d98d6b5093c270165984 to your computer and use it in GitHub Desktop.
async pop(account: AptosAccount, pop_idx?: number): Promise<string> {
return await sendAptosTx(
this.client,
account,
`${this.switchboardAddress}::crank_pop_action::run`,
[HexString.ensure(this.address).hex(), pop_idx ?? 0],
[this.coinType]
);
}
export async function sendAptosTx(
client: AptosClient,
signer: AptosAccount,
method: EntryFunctionId,
args: Array<any>,
type_args: Array<string> = [],
maxGasPrice: number = 2000
): Promise<string> {
const payload = {
type: "entry_function_payload",
function: method,
type_arguments: type_args,
arguments: args,
};
let txnRequest = await client.generateTransaction(signer.address(), payload);
try {
const simulation = (
await client.simulateTransaction(signer, txnRequest, {
estimateGasUnitPrice: true,
estimateMaxGasAmount: true, // @ts-ignore
estimatePrioritizedGasUnitPrice: true,
})
)[0];
if (Number(simulation.gas_unit_price) > maxGasPrice) {
throw Error(
`Estimated gas price from simulation ${simulation.gas_unit_price} above maximum (${maxGasPrice}).`
);
}
txnRequest = await client.generateTransaction(signer.address(), payload, {
gas_unit_price: simulation.gas_unit_price,
max_gas_amount: Math.ceil(Number(simulation.gas_used) * 1.2).toString(),
});
if (simulation.success === false) {
throw new AptosSimulationError(simulation.vm_status);
}
const signedTxn = await client.signTransaction(signer, txnRequest);
const transactionRes = await client.submitTransaction(signedTxn);
await client.waitForTransaction(transactionRes.hash);
return transactionRes.hash;
} catch (error) {
const switchboardError = handleError(error);
if (switchboardError) {
throw switchboardError;
}
throw error;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment