Skip to content

Commit

Permalink
Use local network with tests (#26)
Browse files Browse the repository at this point in the history
use local network with tests
  • Loading branch information
0xmaayan authored Oct 13, 2023
1 parent 606c136 commit 41047d1
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 187 deletions.
1 change: 1 addition & 0 deletions src/utils/apiEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const NetworkToIndexerAPI: Record<string, string> = {
mainnet: "https://indexer.mainnet.aptoslabs.com/v1/graphql",
testnet: "https://indexer-testnet.staging.gcp.aptosdev.com/v1/graphql",
devnet: "https://indexer-devnet.staging.gcp.aptosdev.com/v1/graphql",
local: "http://127.0.0.1:8090/v1/graphql",
};

export const NetworkToNodeAPI: Record<string, string> = {
Expand Down
9 changes: 6 additions & 3 deletions tests/e2e/api/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Account, Aptos, AptosConfig, Network } from "../../../src";
import { U64 } from "../../../src/bcs/serializable/move-primitives";
import { SigningScheme } from "../../../src/types";
import { sleep } from "../../../src/utils/helpers";

describe("account api", () => {
const FUND_AMOUNT = 100_000_000;
Expand Down Expand Up @@ -111,7 +112,7 @@ describe("account api", () => {
});

test("it fetches account transactions count", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const senderAccount = Account.generate({ scheme: SigningScheme.Ed25519 });
const response = await aptos.fundAccount({
Expand All @@ -127,7 +128,7 @@ describe("account api", () => {
});

test("it fetches account coins data", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const senderAccount = Account.generate({ scheme: SigningScheme.Ed25519 });
const response = await aptos.fundAccount({
Expand All @@ -136,6 +137,8 @@ describe("account api", () => {
});

await aptos.waitForTransaction({ txnHash: response });
// to help with indexer latency
await sleep(1000);
const accountCoinData = await aptos.getAccountCoinsData({
accountAddress: senderAccount.accountAddress.toString(),
});
Expand All @@ -144,7 +147,7 @@ describe("account api", () => {
});

test("it fetches account coins count", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const senderAccount = Account.generate({ scheme: SigningScheme.Ed25519 });
const response = await aptos.fundAccount({
Expand Down
10 changes: 6 additions & 4 deletions tests/e2e/api/coin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { waitForTransaction } from "../../../src/internal/transaction";
import { RawTransaction, TransactionPayloadEntryFunction } from "../../../src/transactions/instances";
import { TypeTagStruct } from "../../../src/transactions/typeTag/typeTag";
import { SigningScheme } from "../../../src/types";
import { sleep } from "../../../src/utils/helpers";

describe("coin", () => {
test("it generates a transfer coin transaction with AptosCoin coin type", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const sender = Account.generate({ scheme: SigningScheme.Ed25519 });
const recipient = Account.generate({ scheme: SigningScheme.Ed25519 });
Expand All @@ -27,7 +28,7 @@ describe("coin", () => {
});

test("it generates a transfer coin transaction with a custom coin type", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const sender = Account.generate({ scheme: SigningScheme.Ed25519 });
const recipient = Account.generate({ scheme: SigningScheme.Ed25519 });
Expand All @@ -49,7 +50,7 @@ describe("coin", () => {
});

test("it transfers APT coin aomunt from sender to recipient", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const sender = Account.generate({ scheme: SigningScheme.Ed25519 });
const recipient = Account.generate({ scheme: SigningScheme.Ed25519 });
Expand All @@ -65,7 +66,8 @@ describe("coin", () => {
const response = await aptos.signAndSubmitTransaction({ signer: sender, transaction });

await waitForTransaction({ aptosConfig: config, txnHash: response.hash });

// to help with indexer latency
await sleep(1000);
const recipientCoins = await aptos.getAccountCoinsData({ accountAddress: recipient.accountAddress.toString() });
const senderCoinsAfter = await aptos.getAccountCoinsData({ accountAddress: sender.accountAddress.toString() });

Expand Down
112 changes: 36 additions & 76 deletions tests/e2e/api/transaction_submission.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Account, AptosConfig, Ed25519PrivateKey, Network, Aptos, Deserializer } from "../../../src";
import { U64 } from "../../../src/bcs/serializable/move-primitives";
import { MoveObject } from "../../../src/bcs/serializable/move-structs";
import { waitForTransaction } from "../../../src/internal/transaction";
import { AccountAuthenticator, AccountAuthenticatorEd25519 } from "../../../src/transactions/authenticator/account";
import { RawTransaction } from "../../../src/transactions/instances";
import {
Expand All @@ -16,13 +17,10 @@ import { SigningScheme } from "../../../src/types";
describe("transaction submission", () => {
describe("generateTransaction", () => {
test("it generates a script transaction", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const alice = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey({
hexInput: "0x5aba8dab1c523be32bd4dafe2cc612f7f8050ce42a3322b60216ef67dc97768c",
}),
});
const alice = Account.generate({ scheme: SigningScheme.Ed25519 });
await aptos.fundAccount({ accountAddress: alice.accountAddress.toString(), amount: 1000000000 });
const rawTxn = await aptos.generateTransaction({
sender: alice.accountAddress.toString(),
data: {
Expand All @@ -39,13 +37,10 @@ describe("transaction submission", () => {
});

test("it generates a multi sig transaction", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const alice = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey({
hexInput: "0x5aba8dab1c523be32bd4dafe2cc612f7f8050ce42a3322b60216ef67dc97768c",
}),
});
const alice = Account.generate({ scheme: SigningScheme.Ed25519 });
await aptos.fundAccount({ accountAddress: alice.accountAddress.toString(), amount: 1000000000 });
const bob = Account.generate({ scheme: SigningScheme.Ed25519 });
const rawTxn = await aptos.generateTransaction({
sender: alice.accountAddress.toString(),
Expand All @@ -64,13 +59,10 @@ describe("transaction submission", () => {
});

test("it generates an entry function transaction", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const alice = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey({
hexInput: "0x5aba8dab1c523be32bd4dafe2cc612f7f8050ce42a3322b60216ef67dc97768c",
}),
});
const alice = Account.generate({ scheme: SigningScheme.Ed25519 });
await aptos.fundAccount({ accountAddress: alice.accountAddress.toString(), amount: 1000000000 });
const bob = Account.generate({ scheme: SigningScheme.Ed25519 });
const rawTxn = await aptos.generateTransaction({
sender: alice.accountAddress.toString(),
Expand All @@ -89,18 +81,12 @@ describe("transaction submission", () => {
});
describe("simulateTransaction", () => {
test("it simulates a multi agent script transaction", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const alice = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey({
hexInput: "0x5aba8dab1c523be32bd4dafe2cc612f7f8050ce42a3322b60216ef67dc97768c",
}),
});
const bob = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey({
hexInput: "0xc1e06f150f7fa79c1ca0ad0f8d0b26d83abaf1bed0fc31de24b500d81a2ff924",
}),
});
const alice = Account.generate({ scheme: SigningScheme.Ed25519 });
await aptos.fundAccount({ accountAddress: alice.accountAddress.toString(), amount: 1000000000 });
const bob = Account.generate({ scheme: SigningScheme.Ed25519 });
await aptos.fundAccount({ accountAddress: bob.accountAddress.toString(), amount: 1000000000 });
const rawTxn = await aptos.generateTransaction({
sender: alice.accountAddress.toString(),
secondarySignerAddresses: [bob.accountAddress.toString()],
Expand Down Expand Up @@ -128,13 +114,10 @@ describe("transaction submission", () => {
});
describe("signTransaction", () => {
test("it signs a script transaction", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const alice = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey({
hexInput: "0x5aba8dab1c523be32bd4dafe2cc612f7f8050ce42a3322b60216ef67dc97768c",
}),
});
const alice = Account.generate({ scheme: SigningScheme.Ed25519 });
await aptos.fundAccount({ accountAddress: alice.accountAddress.toString(), amount: 1000000000 });
const rawTxn = await aptos.generateTransaction({
sender: alice.accountAddress.toString(),
data: {
Expand All @@ -154,13 +137,10 @@ describe("transaction submission", () => {
});

test("it signs a multi sig transaction", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const alice = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey({
hexInput: "0x5aba8dab1c523be32bd4dafe2cc612f7f8050ce42a3322b60216ef67dc97768c",
}),
});
const alice = Account.generate({ scheme: SigningScheme.Ed25519 });
await aptos.fundAccount({ accountAddress: alice.accountAddress.toString(), amount: 1000000000 });
const bob = Account.generate({ scheme: SigningScheme.Ed25519 });
const rawTxn = await aptos.generateTransaction({
sender: alice.accountAddress.toString(),
Expand All @@ -182,13 +162,10 @@ describe("transaction submission", () => {
});

test("it signs an entry function transaction", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const alice = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey({
hexInput: "0x5aba8dab1c523be32bd4dafe2cc612f7f8050ce42a3322b60216ef67dc97768c",
}),
});
const alice = Account.generate({ scheme: SigningScheme.Ed25519 });
await aptos.fundAccount({ accountAddress: alice.accountAddress.toString(), amount: 1000000000 });
const bob = Account.generate({ scheme: SigningScheme.Ed25519 });
const rawTxn = await aptos.generateTransaction({
sender: alice.accountAddress.toString(),
Expand All @@ -210,18 +187,12 @@ describe("transaction submission", () => {
});
describe("submitTransaction", () => {
test("it submits a script transaction", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const alice = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey({
hexInput: "0x5aba8dab1c523be32bd4dafe2cc612f7f8050ce42a3322b60216ef67dc97768c",
}),
});
const bob = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey({
hexInput: "0xc1e06f150f7fa79c1ca0ad0f8d0b26d83abaf1bed0fc31de24b500d81a2ff924",
}),
});
const alice = Account.generate({ scheme: SigningScheme.Ed25519 });
await aptos.fundAccount({ accountAddress: alice.accountAddress.toString(), amount: 1000000000 });
const bob = Account.generate({ scheme: SigningScheme.Ed25519 });
await aptos.fundAccount({ accountAddress: bob.accountAddress.toString(), amount: 1000000000 });
const rawTxn = await aptos.generateTransaction({
sender: alice.accountAddress.toString(),
secondarySignerAddresses: [bob.accountAddress.toString()],
Expand Down Expand Up @@ -253,25 +224,14 @@ describe("transaction submission", () => {
additionalSignersAuthenticators: [bobauthenticator],
},
});
expect(response).toHaveProperty("hash");
await waitForTransaction({ aptosConfig: config, txnHash: response.hash });
});

// Currently this test fails because we don't wait for the previous transaction to be executed
// and we end up with transaction is already in mempool. This is because we are still missing
// waitForTransaction query and/or the fund query (so we can create an account and then fund it
// to create it on chain) - Anyhow, I tested each test individually and it works.
// The whole test flow should work once we have the option to wait for transaction and/or fund
// an account to create it on chain
// FIXME: Fix this test
/*test("it submits an entry function transaction", async () => {
const config = new AptosConfig({ network: Network.DEVNET });
test("it submits an entry function transaction", async () => {
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const alice = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey({
hexInput:
"0x5aba8dab1c523be32bd4dafe2cc612f7f8050ce42a3322b60216ef67dc97768c",
}),
});
const alice = Account.generate({ scheme: SigningScheme.Ed25519 });
await aptos.fundAccount({ accountAddress: alice.accountAddress.toString(), amount: 1000000000 });
const bob = Account.generate({ scheme: SigningScheme.Ed25519 });
const rawTxn = await aptos.generateTransaction({
sender: alice.accountAddress.toString(),
Expand All @@ -289,7 +249,7 @@ describe("transaction submission", () => {
transaction: rawTxn,
senderAuthenticator: authenticator,
});
expect(response).toHaveProperty("hash");
});*/
await waitForTransaction({ aptosConfig: config, txnHash: response.hash });
});
});
});
2 changes: 1 addition & 1 deletion tests/unit/aptos_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("aptos config", () => {
expect(aptosConfig.network).toEqual("local");
expect(aptosConfig.getRequestUrl(AptosApiType.FULLNODE)).toBe(NetworkToNodeAPI[Network.LOCAL]);
expect(aptosConfig.getRequestUrl(AptosApiType.FAUCET)).toBe(NetworkToFaucetAPI[Network.LOCAL]);
expect(aptosConfig.getRequestUrl(AptosApiType.INDEXER)).toBeUndefined();
expect(aptosConfig.getRequestUrl(AptosApiType.INDEXER)).toBe(NetworkToIndexerAPI[Network.LOCAL]);
});

test("it should set urls based on a given network", async () => {
Expand Down
Loading

0 comments on commit 41047d1

Please sign in to comment.