|
| 1 | +import { beforeAll, describe, expect, it } from "vitest"; |
| 2 | +import { setupClient, test } from "../../tests/fixtures"; |
| 3 | +import { |
| 4 | + AccountAddress, |
| 5 | + AnyRawTransaction, |
| 6 | + Account, |
| 7 | + InputGenerateTransactionPayloadData, |
| 8 | +} from "@aptos-labs/ts-sdk"; |
| 9 | +import { |
| 10 | + convertAptosAccountToAccountInfo, |
| 11 | + convertAptosAccountToSigner, |
| 12 | +} from "../utils"; |
| 13 | +import { AdapterSignerClient } from "../types"; |
| 14 | + |
| 15 | +describe("signAndSubmitTransaction", async () => { |
| 16 | + const account = Account.generate(); |
| 17 | + |
| 18 | + let transaction: AnyRawTransaction; |
| 19 | + let data: InputGenerateTransactionPayloadData; |
| 20 | + |
| 21 | + beforeAll(async () => { |
| 22 | + const devnet = setupClient(); |
| 23 | + |
| 24 | + await devnet.aptos.fundAccount({ |
| 25 | + accountAddress: account.accountAddress, |
| 26 | + amount: 1000000000, |
| 27 | + }); |
| 28 | + |
| 29 | + data = { |
| 30 | + function: "0x1::aptos_account::transfer", |
| 31 | + functionArguments: [account.accountAddress, 100], |
| 32 | + }; |
| 33 | + |
| 34 | + transaction = await devnet.buildTransaction({ |
| 35 | + data, |
| 36 | + sender: account.accountAddress, |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + test.sequential( |
| 41 | + "should sign and submit the transaction with transaction", |
| 42 | + async ({ devnet }) => { |
| 43 | + devnet.setSigner(convertAptosAccountToSigner(account)); |
| 44 | + |
| 45 | + const signedTransaction = await devnet.signAndSubmitTransaction({ |
| 46 | + transaction, |
| 47 | + }); |
| 48 | + await devnet.waitForTransaction({ hash: signedTransaction.hash }); |
| 49 | + |
| 50 | + expect(signedTransaction).toBeDefined(); |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | + test.sequential( |
| 55 | + "should sign and submit the transaction with data for account signer", |
| 56 | + async ({ devnet }) => { |
| 57 | + devnet.setSigner(convertAptosAccountToSigner(account)); |
| 58 | + devnet.setAccount(convertAptosAccountToAccountInfo(account)); |
| 59 | + |
| 60 | + const signedTransaction = await devnet.signAndSubmitTransaction({ data }); |
| 61 | + await devnet.waitForTransaction({ hash: signedTransaction.hash }); |
| 62 | + |
| 63 | + expect(signedTransaction).toBeDefined(); |
| 64 | + } |
| 65 | + ); |
| 66 | + |
| 67 | + test.sequential( |
| 68 | + "should sign and submit the transaction with transaction and data for adapter signer", |
| 69 | + async ({ devnet }) => { |
| 70 | + devnet.setSigner({ |
| 71 | + type: "adapter", |
| 72 | + async signAndSubmitTransaction({ aptos, payload }) { |
| 73 | + if (!payload) throw new Error(); |
| 74 | + return aptos.signAndSubmitTransaction({ |
| 75 | + signer: account, |
| 76 | + transaction: await aptos.transaction.build.simple({ |
| 77 | + sender: account.accountAddress, |
| 78 | + data: payload.data, |
| 79 | + }), |
| 80 | + }); |
| 81 | + }, |
| 82 | + } as AdapterSignerClient); |
| 83 | + devnet.setAccount(convertAptosAccountToAccountInfo(account)); |
| 84 | + |
| 85 | + const signedTransaction = await devnet.signAndSubmitTransaction({ data }); |
| 86 | + await devnet.waitForTransaction({ hash: signedTransaction.hash }); |
| 87 | + |
| 88 | + expect(signedTransaction).toBeDefined(); |
| 89 | + } |
| 90 | + ); |
| 91 | + |
| 92 | + test("should throw error if signer is not available", async ({ devnet }) => { |
| 93 | + devnet.setSigner(undefined); |
| 94 | + |
| 95 | + await expect( |
| 96 | + devnet.signAndSubmitTransaction({ transaction }) |
| 97 | + ).rejects.toThrowError(); |
| 98 | + }); |
| 99 | +}); |
0 commit comments