Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix example sponsor transaction error #413

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions apps/nextjs-example/src/components/transactionFlows/Sponsor.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { aptosClient, isSendableNetwork } from "@/utils";
import { aptosClient, isSendableNetwork } from '@/utils';
import {
AccountAddress,
Account,
AccountAuthenticator,
AnyRawTransaction,
} from "@aptos-labs/ts-sdk";
import { useWallet } from "@aptos-labs/wallet-adapter-react";
import { useState } from "react";
import { TransactionHash } from "../TransactionHash";
import { Button } from "../ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "../ui/card";
import { useToast } from "../ui/use-toast";
} from '@aptos-labs/ts-sdk';
import { useWallet } from '@aptos-labs/wallet-adapter-react';
import { useState } from 'react';
import { TransactionHash } from '../TransactionHash';
import { Button } from '../ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '../ui/card';
import { useToast } from '../ui/use-toast';

const APTOS_COIN = "0x1::aptos_coin::AptosCoin";
const APTOS_COIN = '0x1::aptos_coin::AptosCoin';

export function Sponsor() {
const { toast } = useToast();
Expand All @@ -24,22 +24,25 @@ export function Sponsor() {
useState<AccountAuthenticator>();
const [feepayerAuthenticator, setFeepayerAuthenticator] =
useState<AccountAuthenticator>();
const [feepayerAddress, setFeepayerAddress] = useState<AccountAddress>();

let sendable = isSendableNetwork(connected, network?.name);

// create sponsor account
const SPONSOR_INITIAL_BALANCE = 100_000_000;
const sponsor = Account.generate();

// Generate a raw transaction using the SDK
const generateTransaction = async (): Promise<AnyRawTransaction> => {
if (!account) {
throw new Error("no account");
throw new Error('no account');
}
const transactionToSign = await aptosClient(
network,
network
).transaction.build.simple({
sender: account.address,
withFeePayer: true,
data: {
function: "0x1::coin::transfer",
function: '0x1::coin::transfer',
typeArguments: [APTOS_COIN],
functionArguments: [account.address, 1], // 1 is in Octas
},
Expand All @@ -61,36 +64,43 @@ export function Sponsor() {

const onSignTransactionAsSponsor = async () => {
if (!transactionToSubmit) {
throw new Error("No Transaction to sign");
throw new Error('No Transaction to sign');
}
try {
const authenticator = await signTransaction(transactionToSubmit, true);
await aptosClient(network).fundAccount({
accountAddress: sponsor.accountAddress,
amount: SPONSOR_INITIAL_BALANCE,
});
const authenticator = await aptosClient(
network
).transaction.signAsFeePayer({
signer: sponsor,
transaction: transactionToSubmit,
});
setFeepayerAuthenticator(authenticator);
setFeepayerAddress(AccountAddress.from(account!.address));
} catch (error) {
console.error(error);
}
};

const onSubmitTransaction = async () => {
if (!transactionToSubmit) {
throw new Error("No Transaction to sign");
throw new Error('No Transaction to sign');
0xRichBot marked this conversation as resolved.
Show resolved Hide resolved
}
if (!senderAuthenticator) {
throw new Error("No senderAuthenticator");
throw new Error('No senderAuthenticator');
}
if (!feepayerAuthenticator) {
throw new Error("No feepayerAuthenticator");
throw new Error('No feepayerAuthenticator');
}
transactionToSubmit.feePayerAddress = feepayerAddress;
try {
const response = await submitTransaction({
transaction: transactionToSubmit,
senderAuthenticator: senderAuthenticator,
feePayerAuthenticator: feepayerAuthenticator,
});
toast({
title: "Success",
title: 'Success',
description: <TransactionHash hash={response.hash} network={network} />,
});
} catch (error) {
Expand Down
21 changes: 15 additions & 6 deletions apps/nuxt-example/components/TransactonFlows/Sponsor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AccountAddress,
AccountAuthenticator,
AnyRawTransaction,
Account,
} from "@aptos-labs/ts-sdk";
import TransactionHash from "~/components/TransactionHash.vue";
import { useToast } from "~/components/ui/toast";
Expand All @@ -24,6 +25,10 @@ const isSendable = computed(() =>
isSendableNetwork(connected.value, network.value?.name || undefined),
);

// create sponsor account
const SPONSOR_INITIAL_BALANCE = 100_000_000;
const sponsor = Account.generate();

// Generate a raw transaction using the SDK
const generateTransaction = async (): Promise<AnyRawTransaction> => {
if (!account.value) {
Expand Down Expand Up @@ -59,11 +64,16 @@ const onSignTransactionAsSponsor = async () => {
throw new Error("No Transaction to sign");
}
try {
feepayerAuthenticator.value = await signTransaction(
transactionToSubmit.value,
true,
);
feepayerAddress.value = AccountAddress.from(account.value!.address);
await aptosClient(network.value).fundAccount({
accountAddress: sponsor.accountAddress,
amount: SPONSOR_INITIAL_BALANCE,
});
feepayerAuthenticator.value = await aptosClient(
network.value
).transaction.signAsFeePayer({
signer: sponsor,
transaction: transactionToSubmit.value,
});
} catch (error) {
console.error(error);
}
Expand All @@ -79,7 +89,6 @@ const onSubmitTransaction = async () => {
if (!feepayerAuthenticator.value) {
throw new Error("No feepayerAuthenticator");
}
transactionToSubmit.value.feePayerAddress = feepayerAddress!;
try {
const response = await submitTransaction({
transaction: transactionToSubmit.value,
Expand Down
Loading