@aptos-labs/gas-station-client
v2.0.3
Published
Client for the Aptos Gas Station API
Readme
Gas Station Client
The Aptos Build gas station client lets you submit transactions to be sponsored by the gas station.
Version Management
This package uses changesets for version management. To make changes:
- Make your code changes
- Run
pnpm changesetin the root directory - Follow the prompts to describe your changes
- Commit the generated changeset file along with your changes
- When ready to release, run
pnpm version-packagesto update versions - Run
pnpm releaseto publish the new version
Installation
pnpm install @aptos-labs/gas-station-clientThis package contains a client for the Gas Station Admin API. It is primarily intended for use by Aptos Build. That's why we publish this to our private registry.
Usage
Recommended Usage
The easiest way to use the gas station client is with the TS SDK's plugin interface:
const network = Network.MAINNET;
const gasStationClient = new GasStationTransactionSubmitter({
network,
apiKey: "aptoslabs_21edbqbxyPrS_6fu9i83mYmjid4MVs3Z3XmCeATQ8tkSdd",
});
const config = new AptosConfig({
network,
pluginSettings: {
TRANSACTION_SUBMITTER: gasStationClient,
},
});
const aptos = new Aptos(config);From this point you can use the client like normal and it will submit transactions via the gas station instead of the default submitter:
const transaction = await aptos.transaction.build.simple({
sender: senderAccount.accountAddress,
// IMPORTANT: Must set withFeePayer: true for gas station transactions.
withFeePayer: true,
data: {
function: "0x1::aptos_account::transfer",
functionArguments: [receiverAccount.accountAddress.toString(), 100],
},
});
const response = await aptos.signAndSubmitTransaction({
signer: senderAccount,
transaction,
});To provide a recaptcha token or other plugin parameters:
const response = await aptos.signAndSubmitTransaction({
signer: senderAccount,
transaction,
pluginParams: {
recaptchaToken: "your-recaptcha-token",
},
});Ignoring the Plugin
You can temporarily bypass the plugin for a single transaction if needed:
const response = await aptos.signAndSubmitTransaction({
signer: senderAccount,
transaction,
// Set this to null to ignore the plugin.
transactionSubmitter: null,
});You can also totally disable / re-enable the plugin like this:
aptos.setIgnoreTransactionSubmitter(true);
// Transactions will now go through normal submission (not gas station)
aptos.setIgnoreTransactionSubmitter(false);
// Re-enable the pluginDirect Usage
If you wish, you can use the client directly:
const network = Network.MAINNET;
const config = new AptosConfig({network});
const aptos = new Aptos(config);
// Create the client.
const client = new GasStationClient({
network,
apiKey: "aptoslabs_21edbqbxyPrS_6fu9i83mYmjid4MVs3Z3XmCeATQ8tkSdd",
});
// Build the transaction.
const transaction = await aptos.transaction.build.simple({
sender: alice.accountAddress,
// Make sure that this is set to true.
withFeePayer: true,
data: {
function: "0x1::aptos_account::transfer",
functionArguments: [bob.accountAddress.toString(), 100],
},
});
// Sign it and get an authenticator.
const senderAuthenticator = aptos.transaction.sign({ signer: alice, transaction });
// Submit it to the gas station.
const { transactionHash } = await client.signAndSubmitTransaction({
transaction,
senderAuthenticator,
});
// Wait for the transaction to be executed.
const executedTransaction = await aptos.waitForTransaction({
transactionHash,
options: { checkSuccess: true },
});
console.log("Transaction executed successfully", executedTransaction.hash);