Paymaster SDK Tutorial (Typescript)
Install the paymaster SDK
npm install @kanalabs/paymaster-sdk
or
yarn add @kanalabs/paymaster-sdk
Initialize SDK with Private Key and projectKey
import { PaymasterSdk } from "@kanalabs/paymaster-sdk";
const sdk = new PaymasterSdk(
{
privateKey:
"user private key", // Optional
},
{
projectKey: "your project key",
network: Network.TESTNET // default MAINNET
}
);
Initialize SDK without Private Key
import { PaymasterSdk } from "@kanalabs/paymaster-sdk";
const sdk = new PaymasterSdk({}, { projectKey: testProjectKey, network: Network.TESTNET })
Check If user already whitelisted,
const isWhitelisted = await sdk.isWhitelisted();
// Response format
{
success: boolean;
message: string;
}
It will return { success: true, message: 'whitelisted' }
if already whitelisted.
It will return { success: true, message: 'not whitelisted' }
if it is not whitelisted.
To add user to whitelist
const whitelist = await sdk.addToWhitelist();
// Response format
{
success: boolean;
message: string;
}
It will return { success: true, message: 'Successfully added' }
if successfully whitelisted.
It will return { success: true, message: 'already whitelisted' }
if user already whitelisted.
Initialize user account
if that account is not available in Aptos mainnet you can initialize account with the following function.
const initAccount = await sdk.initAccount();
// Response format
{
success: boolean;
message: string;
}
It will return { success: true, message: 'account initialized' }
if successfully initialized.
To make sponsored transactons you can build they required payload and pass it to the sponsoredTxn
const payload: TransactionPayload = {
function: "0x1::aptos_account::transfer_coins",
functionArguments: [
"0xa197f0ffe941bf5cfca7af28438c8692464316fd8075baf6145c26051bc85d4d",
0,
],
typeArguments: ["0x1::aptos_coin::AptosCoin"],
};
const options: TransactionOptions = { gasUnitPrice: 100, maxGasAmount: 2000 };
try {
const txn = await sdk.sponsoredTxn({
data: payload,
options: options
});
console.log(txn);
const txnReceipt = await sdk.aptosClient.waitForTransaction({
transactionHash: txn.hash,
options: {
checkSuccess: true,
},
});
console.log("Txn status", txnReceipt.success);
} catch (error: any) {
console.log("error", error);
}
Sponsor Transaction with Sender Auth and Transaction
const config = new AptosConfig({ network: Network.MAINNET });
const aptosClient = new Aptos(config);
const senderAccount = Account.generate();
const payload: TransactionPayload = {
function: "0x1::aptos_account::transfer_coins",
functionArguments: [
"0x0b4b8ef78fb296f89006f1936f01427a3a7e0eadd11dd4998c6bf438a0c8ce6b",
0,
],
typeArguments: ["0x1::aptos_coin::AptosCoin"],
};
const options: TransactionOptions = { gasUnitPrice: 100, maxGasAmount: 2000 };
const transaction = await aptosClient.transaction.build.simple({
sender: senderAccount.accountAddress.toString(),
data: payload,
options: options,
withFeePayer: true,
});
const senderAuth = aptosClient.transaction.sign({
signer: senderAccount,
transaction: transaction,
});
try {
const txn = await sdk.sponsoredTxnWithSenderAuth({
transaction: transaction,
senderAuth: senderAuth,
});
console.log(txn);
const txnReceipt = await sdk.aptosClient.waitForTransaction({
transactionHash: txn.hash,
options: {
checkSuccess: true,
},
});
console.log("Txn status", txnReceipt.success);
Last updated