Skip to content

Commit

Permalink
add server as sponsor example
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmaayan committed Jan 24, 2024
1 parent 3371085 commit 5b46e7e
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 7 deletions.
7 changes: 4 additions & 3 deletions examples/typescript-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
"type": "module",
"scripts": {
"build": "pnpm _build:esm",
"_build:esm": "tsup simple_transfer.ts simple_sponsored_transaction.ts simple_digital_asset.ts multisig_v2.ts front_to_back_txn.ts --platform node --format esm --dts --out-dir dist",
"_build:esm": "tsup simple_transfer.ts sponsored_transactions/simple_sponsored_transaction.ts sponsored_transactions/server_as_sponsor.ts simple_digital_asset.ts multisig_v2.ts sponsored_transactions/server_signs_and_submit.ts --platform node --format esm --dts --out-dir dist",
"simple_transfer": "ts-node --esm dist/simple_transfer.js",
"simple_sponsored_transaction": "ts-node --esm dist/simple_sponsored_transaction.js",
"simple_sponsored_transaction": "ts-node --esm dist/sponsored_transactions/simple_sponsored_transaction.js",
"simple_digital_asset": "pnpm run build && ts-node --esm dist/simple_digital_asset.js",
"multisig_v2": "pnpm run build && ts-node --esm dist/multisig_v2.js",
"front_to_back_txn": "pnpm run build && ts-node dist/front_to_back_txn.js",
"server_signs_and_submit": "pnpm run build && ts-node dist/sponsored_transactions/server_signs_and_submit.js",
"server_as_sponsor": "pnpm run build && ts-node dist/sponsored_transactions/server_as_sponsor.js",
"test": "run-s build simple_transfer simple_sponsored_transaction"
},
"keywords": [],
Expand Down
112 changes: 112 additions & 0 deletions examples/typescript-esm/sponsored_transactions/server_as_sponsor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* eslint-disable no-console */

/**
* This example demostrates how one can use an external server
* as a sponsor to sign a transaction to eventually pay the gas fees.
*
* A server (for example, frontend) generates a transaction, have it serialized and send
* it to another server (for example, backend) to deserialize and signs as the sponsor,
* then sends it back to the other server (the frontend server) to submit the transaction.
*/

import {
Account,
AccountAuthenticator,
Aptos,
AptosConfig,
Deserializer,
Network,
NetworkToNetworkName,
RawTransaction,
} from "@aptos-labs/ts-sdk";

const INITIAL_BALANCE = 100_000_000;
const TRANSFER_AMOUNT = 100;

// Default to devnet, but allow for overriding
const APTOS_NETWORK: Network = NetworkToNetworkName[process.env.APTOS_NETWORK] || Network.DEVNET;
// Setup the client
const config = new AptosConfig({ network: APTOS_NETWORK });
const aptos = new Aptos(config);

// The sponsor server gets the serialized transaction to sign as the fee payer
const sendToTheSponsorServer = async (rawTransactionBytes: Uint8Array) => {
const sponsor = Account.generate();
console.log(`Sponsor's address is: ${sponsor.accountAddress}`);
await aptos.fundAccount({ accountAddress: sponsor.accountAddress, amount: INITIAL_BALANCE });

// deserialize raw transaction
const deserializer = new Deserializer(rawTransactionBytes);
const rawTransaction = RawTransaction.deserialize(deserializer);

// Sponsor signs
const sponsorAuth = aptos.transaction.signAsFeePayer({
signer: sponsor,
transaction: { rawTransaction, feePayerAddress: sponsor.accountAddress },
});

const sponsorAuthBytes = sponsorAuth.bcsToBytes();

return { sponsorAuthBytes, feepayerAddress: sponsor.accountAddress };
};

const example = async () => {
// Create two accounts
const alice = Account.generate();
const bob = Account.generate();

console.log("=== Addresses ===\n");
console.log(`Alice's address is: ${alice.accountAddress}`);
console.log(`Bob's address is: ${bob.accountAddress}`);

// Fund the accounts
console.log("\n=== Funding accounts ===\n");

await aptos.faucet.fundAccount({
accountAddress: alice.accountAddress,
amount: INITIAL_BALANCE,
});

console.log("\n=== Accounts funded ===\n");

const transaction = await aptos.transaction.build.simple({
sender: alice.accountAddress,
withFeePayer: true,
data: {
function: "0x1::aptos_account::transfer",
functionArguments: [bob.accountAddress, TRANSFER_AMOUNT],
},
});

// Alice signs
const senderAuth = aptos.transaction.sign({ signer: alice, transaction });

// Serialize data
/**
* the generated `transaction` is of a simple type and can't be
* bcs serialized. Therefore we serialize the `rawTransaction` and
* send it to the server.
*/
const rawTransactionBytes = transaction.rawTransaction.bcsToBytes();

// Send the transaction to the sponsor server to sign
const { sponsorAuthBytes, feepayerAddress } = await sendToTheSponsorServer(rawTransactionBytes);

// deserialize fee payer authenticator
const deserializer3 = new Deserializer(sponsorAuthBytes);
const feePayerAuthenticator = AccountAuthenticator.deserialize(deserializer3);

const response = await aptos.transaction.submit.simple({
transaction: {
rawTransaction: transaction.rawTransaction,
feePayerAddress: feepayerAddress,
},
senderAuthenticator: senderAuth,
feePayerAuthenticator,
});

const executedTransaction = await aptos.waitForTransaction({ transactionHash: response.hash });
console.log("executed transaction", executedTransaction.hash);
};

example();
6 changes: 2 additions & 4 deletions examples/typescript-esm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
"target": "es2020",
"pretty": true
},
"include": [
"*.ts"
],
"include": ["*.ts", "sponsored_transactions/*.ts"],
"paths": {
"@aptos/*": "../..",
"@aptos/*": "../.."
}
}

0 comments on commit 5b46e7e

Please sign in to comment.