Skip to content

Commit acd7563

Browse files
Add tests to js-pro
1 parent 54228d3 commit acd7563

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1283
-24
lines changed

.changeset/beige-books-battle.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@aptos-labs/js-pro": patch
3+
"@aptos-labs/react": patch
4+
---
5+
6+
Add default empty objects to js-pro queries

packages/js-pro/src/client.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,12 @@ export class AptosJSProClient {
251251
clientConfig?: AptosSettings,
252252
network: NetworkInfo = this.state.network
253253
): Sdk | undefined => {
254-
const indexerUrl = this.createAptos(
255-
clientConfig,
256-
network
257-
).config.getRequestUrl(AptosApiType.INDEXER);
254+
let indexerUrl: string | undefined;
255+
try {
256+
indexerUrl = this.createAptos(clientConfig, network).config.getRequestUrl(
257+
AptosApiType.INDEXER
258+
);
259+
} catch {}
258260

259261
if (indexerUrl === undefined) return undefined;
260262

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, expect } from "vitest";
2+
import { test } from "../../tests/fixtures";
3+
4+
describe("buildTransaction", async () => {
5+
test("should build a simple transaction", async ({ devnet }) => {
6+
const transaction = await devnet.buildTransaction({
7+
data: {
8+
function: "0x1::aptos_account::transfer",
9+
functionArguments: ["0x1", 100],
10+
},
11+
});
12+
13+
expect(transaction).toBeDefined();
14+
});
15+
16+
test("should build a multi-agent transaction", async ({ devnet }) => {
17+
const transaction = await devnet.buildTransaction({
18+
data: {
19+
function: "0x1::aptos_account::transfer",
20+
functionArguments: ["0x1", 100],
21+
},
22+
secondarySignerAddresses: ["0x2"],
23+
});
24+
25+
expect(transaction).toBeDefined();
26+
});
27+
28+
test("should throw an error if no sender is provided", async ({ devnet }) => {
29+
devnet.setAccount(undefined);
30+
31+
await expect(
32+
devnet.buildTransaction({
33+
data: {
34+
function: "0x1::aptos_account::transfer",
35+
functionArguments: ["0x1", 100],
36+
},
37+
})
38+
).rejects.toThrowError();
39+
});
40+
});
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
});

packages/js-pro/src/mutations/signAndSubmitTransaction.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export async function signAndSubmitTransaction(
4747
} else {
4848
transaction = params.transaction;
4949
}
50+
5051
return signer.signAndSubmitTransaction({
5152
aptos,
5253
transaction,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { beforeAll, describe, expect } from "vitest";
2+
import { setupClient, test } from "../../tests/fixtures";
3+
import { AnyRawTransaction, Account } from "@aptos-labs/ts-sdk";
4+
import { convertAptosAccountToSigner } from "../utils";
5+
6+
describe("signTransaction", async () => {
7+
const account = Account.generate();
8+
let transaction: AnyRawTransaction;
9+
10+
beforeAll(async () => {
11+
const testnet = setupClient();
12+
13+
await testnet.aptos.fundAccount({
14+
accountAddress: account.accountAddress,
15+
amount: 1000000000,
16+
});
17+
18+
transaction = await testnet.buildTransaction({
19+
data: {
20+
function: "0x1::aptos_account::transfer",
21+
functionArguments: [account.accountAddress, 100],
22+
},
23+
});
24+
});
25+
26+
test("should sign the transaction", async ({ testnet }) => {
27+
testnet.setSigner(convertAptosAccountToSigner(account));
28+
29+
const signedTransaction = await testnet.signTransaction({ transaction });
30+
31+
expect(signedTransaction).toBeDefined();
32+
});
33+
34+
test("should throw error if signer is not available", async ({ testnet }) => {
35+
testnet.setSigner(undefined);
36+
37+
await expect(
38+
testnet.signTransaction({ transaction })
39+
).rejects.toThrowError();
40+
});
41+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { beforeAll, describe, expect } from "vitest";
2+
import { setupClient, test } from "../../tests/fixtures";
3+
import { Account } from "@aptos-labs/ts-sdk";
4+
import { convertAptosAccountToAccountInfo } from "../utils";
5+
6+
describe("simulateTransaction", async () => {
7+
const account = Account.generate();
8+
const account2 = Account.generate();
9+
10+
beforeAll(async () => {
11+
const devnet = setupClient();
12+
13+
await devnet.aptos.fundAccount({
14+
accountAddress: account.accountAddress,
15+
amount: 1000000000,
16+
});
17+
});
18+
19+
test("should simulate a simple transaction", async ({ devnet }) => {
20+
devnet.setAccount(convertAptosAccountToAccountInfo(account));
21+
22+
const transaction = await devnet.buildTransaction({
23+
data: {
24+
function: "0x1::aptos_account::transfer",
25+
functionArguments: [account.accountAddress, 100],
26+
},
27+
});
28+
29+
const result = await devnet.simulateTransaction({ transaction });
30+
31+
expect(result).toBeDefined();
32+
});
33+
34+
test("should simulate a multi-agent transaction", async ({ devnet }) => {
35+
devnet.setAccount(convertAptosAccountToAccountInfo(account));
36+
37+
const transaction = await devnet.buildTransaction({
38+
data: {
39+
function: "0x1::aptos_account::transfer",
40+
functionArguments: [account.accountAddress, 100],
41+
},
42+
sender: account.accountAddress,
43+
secondarySignerAddresses: [account2.accountAddress],
44+
});
45+
46+
const result = await devnet.simulateTransaction({
47+
transaction,
48+
secondarySignersPublicKeys: [account2.publicKey],
49+
});
50+
51+
expect(result).toBeDefined();
52+
});
53+
});

packages/js-pro/src/mutations/simulateTransaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function simulateTransaction(
3535
): Promise<SimulateTransactionResult> {
3636
const { aptos } = this.getClients({ network: params.network });
3737

38-
if ("secondarySignerAddresses" in params) {
38+
if ("secondarySignersPublicKeys" in params) {
3939
return (await aptos.transaction.simulate.multiAgent(params))[0];
4040
}
4141

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { beforeAll, describe, expect } from "vitest";
2+
import { setupClient, test } from "../../tests/fixtures";
3+
import {
4+
Account,
5+
Deserializer,
6+
generateSignedTransaction,
7+
SignedTransaction,
8+
} from "@aptos-labs/ts-sdk";
9+
import {
10+
convertAptosAccountToAccountInfo,
11+
convertAptosAccountToSigner,
12+
} from "../utils";
13+
14+
describe("submitTransaction", async () => {
15+
const account = Account.generate();
16+
17+
beforeAll(async () => {
18+
const devnet = setupClient();
19+
20+
await devnet.aptos.fundAccount({
21+
accountAddress: account.accountAddress,
22+
amount: 1000000000,
23+
});
24+
});
25+
26+
test("should submit the transaction with SignedTransaction", async ({
27+
devnet,
28+
}) => {
29+
devnet.setAccount(convertAptosAccountToAccountInfo(account));
30+
devnet.setSigner(convertAptosAccountToSigner(account));
31+
32+
const transaction = await devnet.buildTransaction({
33+
data: {
34+
function: "0x1::aptos_account::transfer",
35+
functionArguments: [account.accountAddress, 100],
36+
},
37+
sender: account.accountAddress,
38+
});
39+
40+
const signedTransaction = await devnet.signTransaction({ transaction });
41+
42+
const result = await devnet.submitTransaction({
43+
transaction: SignedTransaction.deserialize(
44+
new Deserializer(
45+
generateSignedTransaction({
46+
senderAuthenticator: signedTransaction.authenticator,
47+
transaction,
48+
})
49+
)
50+
),
51+
});
52+
53+
expect(result).toBeDefined();
54+
});
55+
56+
test.sequential(
57+
"should submit the transaction with senderAuthenticator",
58+
async ({ devnet }) => {
59+
devnet.setAccount(convertAptosAccountToAccountInfo(account));
60+
devnet.setSigner(convertAptosAccountToSigner(account));
61+
62+
const transaction = await devnet.buildTransaction({
63+
data: {
64+
function: "0x1::aptos_account::transfer",
65+
functionArguments: [account.accountAddress, 100],
66+
},
67+
sender: account.accountAddress,
68+
});
69+
70+
const signedTransaction = await devnet.signTransaction({ transaction });
71+
72+
const result = await devnet.submitTransaction({
73+
senderAuthenticator: signedTransaction.authenticator,
74+
transaction,
75+
});
76+
77+
expect(result).toBeDefined();
78+
}
79+
);
80+
});

0 commit comments

Comments
 (0)