@ledgerhq/coin-aptos 2.0.0-nightly.5 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/.unimportedrc.json +18 -6
- package/CHANGELOG.md +152 -34
- package/jest.config.js +2 -1
- package/jest.integ.config.js +8 -0
- package/lib/__tests__/api/index.integ.test.d.ts +2 -0
- package/lib/__tests__/api/index.integ.test.d.ts.map +1 -0
- package/lib/__tests__/api/index.integ.test.js +52 -0
- package/lib/__tests__/api/index.integ.test.js.map +1 -0
- package/lib/__tests__/api/index.test.js +48 -404
- package/lib/__tests__/api/index.test.js.map +1 -1
- package/lib/__tests__/bridge/broadcast.test.js +5 -5
- package/lib/__tests__/bridge/broadcast.test.js.map +1 -1
- package/lib/__tests__/bridge/buildTransaction.test.js +60 -5
- package/lib/__tests__/bridge/buildTransaction.test.js.map +1 -1
- package/lib/__tests__/bridge/createTransaction.test.js +1 -1
- package/lib/__tests__/bridge/createTransaction.test.js.map +1 -1
- package/lib/__tests__/bridge/getFeesForTransaction.test.js +115 -14
- package/lib/__tests__/bridge/getFeesForTransaction.test.js.map +1 -1
- package/lib/__tests__/bridge/getTransactionStatus.test.js +175 -37
- package/lib/__tests__/bridge/getTransactionStatus.test.js.map +1 -1
- package/lib/__tests__/bridge/logic.test.js +889 -115
- package/lib/__tests__/bridge/logic.test.js.map +1 -1
- package/lib/__tests__/bridge/prepareTransaction.test.js +3 -3
- package/lib/__tests__/bridge/prepareTransaction.test.js.map +1 -1
- package/lib/__tests__/bridge/signOperation.test.js +131 -4
- package/lib/__tests__/bridge/signOperation.test.js.map +1 -1
- package/lib/__tests__/bridge/synchronisation.test.js +1215 -68
- package/lib/__tests__/bridge/synchronisation.test.js.map +1 -1
- package/lib/__tests__/network/client.test.d.ts +2 -0
- package/lib/__tests__/network/client.test.d.ts.map +1 -0
- package/lib/__tests__/network/client.test.js +520 -0
- package/lib/__tests__/network/client.test.js.map +1 -0
- package/lib/api/index.d.ts +4 -30
- package/lib/api/index.d.ts.map +1 -1
- package/lib/api/index.js +24 -170
- package/lib/api/index.js.map +1 -1
- package/lib/bridge/bridge.fixture.d.ts +2 -0
- package/lib/bridge/bridge.fixture.d.ts.map +1 -1
- package/lib/bridge/bridge.fixture.js +85 -13
- package/lib/bridge/bridge.fixture.js.map +1 -1
- package/lib/bridge/broadcast.d.ts +3 -2
- package/lib/bridge/broadcast.d.ts.map +1 -1
- package/lib/bridge/broadcast.js +4 -3
- package/lib/bridge/broadcast.js.map +1 -1
- package/lib/bridge/buildTransaction.d.ts +1 -1
- package/lib/bridge/buildTransaction.d.ts.map +1 -1
- package/lib/bridge/buildTransaction.js +29 -3
- package/lib/bridge/buildTransaction.js.map +1 -1
- package/lib/bridge/createTransaction.js +3 -3
- package/lib/bridge/createTransaction.js.map +1 -1
- package/lib/bridge/estimateMaxSpendable.d.ts.map +1 -1
- package/lib/bridge/estimateMaxSpendable.js +6 -5
- package/lib/bridge/estimateMaxSpendable.js.map +1 -1
- package/lib/bridge/getFeesForTransaction.d.ts +1 -1
- package/lib/bridge/getFeesForTransaction.d.ts.map +1 -1
- package/lib/bridge/getFeesForTransaction.js +19 -13
- package/lib/bridge/getFeesForTransaction.js.map +1 -1
- package/lib/bridge/getTransactionStatus.d.ts.map +1 -1
- package/lib/bridge/getTransactionStatus.js +28 -14
- package/lib/bridge/getTransactionStatus.js.map +1 -1
- package/lib/bridge/index.d.ts +1 -1
- package/lib/bridge/index.d.ts.map +1 -1
- package/lib/bridge/logic.d.ts +13 -11
- package/lib/bridge/logic.d.ts.map +1 -1
- package/lib/bridge/logic.js +152 -57
- package/lib/bridge/logic.js.map +1 -1
- package/lib/bridge/prepareTransaction.d.ts.map +1 -1
- package/lib/bridge/prepareTransaction.js +5 -4
- package/lib/bridge/prepareTransaction.js.map +1 -1
- package/lib/bridge/signOperation.d.ts.map +1 -1
- package/lib/bridge/signOperation.js +20 -6
- package/lib/bridge/signOperation.js.map +1 -1
- package/lib/bridge/synchronisation.d.ts +15 -0
- package/lib/bridge/synchronisation.d.ts.map +1 -1
- package/lib/bridge/synchronisation.js +129 -6
- package/lib/bridge/synchronisation.js.map +1 -1
- package/lib/config.d.ts +13 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +9 -0
- package/lib/config.js.map +1 -0
- package/lib/constants.d.ts +9 -1
- package/lib/constants.d.ts.map +1 -1
- package/lib/constants.js +13 -2
- package/lib/constants.js.map +1 -1
- package/lib/network/client.d.ts +36 -0
- package/lib/network/client.d.ts.map +1 -0
- package/lib/network/client.js +247 -0
- package/lib/network/client.js.map +1 -0
- package/lib/network/graphql/queries.d.ts.map +1 -0
- package/lib/{api → network}/graphql/queries.js +6 -6
- package/lib/network/graphql/queries.js.map +1 -0
- package/lib/{api → network}/graphql/types.d.ts +9 -9
- package/lib/network/graphql/types.d.ts.map +1 -0
- package/lib/{api → network}/graphql/types.js.map +1 -1
- package/lib/network/index.d.ts +2 -1
- package/lib/network/index.d.ts.map +1 -1
- package/lib/network/index.js +15 -0
- package/lib/network/index.js.map +1 -1
- package/lib/test/bot-specs.d.ts.map +1 -1
- package/lib/test/bot-specs.js +40 -1
- package/lib/test/bot-specs.js.map +1 -1
- package/lib/test/bridgeDatasetTest.d.ts.map +1 -1
- package/lib/test/bridgeDatasetTest.js +44 -62
- package/lib/test/bridgeDatasetTest.js.map +1 -1
- package/lib/test/speculos-deviceActions.d.ts +1 -0
- package/lib/test/speculos-deviceActions.d.ts.map +1 -1
- package/lib/test/speculos-deviceActions.js +37 -5
- package/lib/test/speculos-deviceActions.js.map +1 -1
- package/lib/types/assets.d.ts +12 -0
- package/lib/types/assets.d.ts.map +1 -0
- package/lib/types/assets.js +3 -0
- package/lib/types/assets.js.map +1 -0
- package/lib/types/index.d.ts +18 -7
- package/lib/types/index.d.ts.map +1 -1
- package/lib-es/__tests__/api/index.integ.test.d.ts +2 -0
- package/lib-es/__tests__/api/index.integ.test.d.ts.map +1 -0
- package/lib-es/__tests__/api/index.integ.test.js +50 -0
- package/lib-es/__tests__/api/index.integ.test.js.map +1 -0
- package/lib-es/__tests__/api/index.test.js +52 -408
- package/lib-es/__tests__/api/index.test.js.map +1 -1
- package/lib-es/__tests__/bridge/broadcast.test.js +2 -2
- package/lib-es/__tests__/bridge/broadcast.test.js.map +1 -1
- package/lib-es/__tests__/bridge/buildTransaction.test.js +60 -5
- package/lib-es/__tests__/bridge/buildTransaction.test.js.map +1 -1
- package/lib-es/__tests__/bridge/createTransaction.test.js +1 -1
- package/lib-es/__tests__/bridge/createTransaction.test.js.map +1 -1
- package/lib-es/__tests__/bridge/getFeesForTransaction.test.js +112 -11
- package/lib-es/__tests__/bridge/getFeesForTransaction.test.js.map +1 -1
- package/lib-es/__tests__/bridge/getTransactionStatus.test.js +177 -39
- package/lib-es/__tests__/bridge/getTransactionStatus.test.js.map +1 -1
- package/lib-es/__tests__/bridge/logic.test.js +891 -117
- package/lib-es/__tests__/bridge/logic.test.js.map +1 -1
- package/lib-es/__tests__/bridge/prepareTransaction.test.js +2 -2
- package/lib-es/__tests__/bridge/prepareTransaction.test.js.map +1 -1
- package/lib-es/__tests__/bridge/signOperation.test.js +131 -4
- package/lib-es/__tests__/bridge/signOperation.test.js.map +1 -1
- package/lib-es/__tests__/bridge/synchronisation.test.js +1213 -69
- package/lib-es/__tests__/bridge/synchronisation.test.js.map +1 -1
- package/lib-es/__tests__/network/client.test.d.ts +2 -0
- package/lib-es/__tests__/network/client.test.d.ts.map +1 -0
- package/lib-es/__tests__/network/client.test.js +515 -0
- package/lib-es/__tests__/network/client.test.js.map +1 -0
- package/lib-es/api/index.d.ts +4 -30
- package/lib-es/api/index.d.ts.map +1 -1
- package/lib-es/api/index.js +22 -168
- package/lib-es/api/index.js.map +1 -1
- package/lib-es/bridge/bridge.fixture.d.ts +2 -0
- package/lib-es/bridge/bridge.fixture.d.ts.map +1 -1
- package/lib-es/bridge/bridge.fixture.js +82 -12
- package/lib-es/bridge/bridge.fixture.js.map +1 -1
- package/lib-es/bridge/broadcast.d.ts +3 -2
- package/lib-es/bridge/broadcast.d.ts.map +1 -1
- package/lib-es/bridge/broadcast.js +4 -3
- package/lib-es/bridge/broadcast.js.map +1 -1
- package/lib-es/bridge/buildTransaction.d.ts +1 -1
- package/lib-es/bridge/buildTransaction.d.ts.map +1 -1
- package/lib-es/bridge/buildTransaction.js +30 -4
- package/lib-es/bridge/buildTransaction.js.map +1 -1
- package/lib-es/bridge/createTransaction.js +1 -1
- package/lib-es/bridge/createTransaction.js.map +1 -1
- package/lib-es/bridge/estimateMaxSpendable.d.ts.map +1 -1
- package/lib-es/bridge/estimateMaxSpendable.js +4 -3
- package/lib-es/bridge/estimateMaxSpendable.js.map +1 -1
- package/lib-es/bridge/getFeesForTransaction.d.ts +1 -1
- package/lib-es/bridge/getFeesForTransaction.d.ts.map +1 -1
- package/lib-es/bridge/getFeesForTransaction.js +14 -8
- package/lib-es/bridge/getFeesForTransaction.js.map +1 -1
- package/lib-es/bridge/getTransactionStatus.d.ts.map +1 -1
- package/lib-es/bridge/getTransactionStatus.js +28 -14
- package/lib-es/bridge/getTransactionStatus.js.map +1 -1
- package/lib-es/bridge/index.d.ts +1 -1
- package/lib-es/bridge/index.d.ts.map +1 -1
- package/lib-es/bridge/logic.d.ts +13 -11
- package/lib-es/bridge/logic.d.ts.map +1 -1
- package/lib-es/bridge/logic.js +146 -55
- package/lib-es/bridge/logic.js.map +1 -1
- package/lib-es/bridge/prepareTransaction.d.ts.map +1 -1
- package/lib-es/bridge/prepareTransaction.js +5 -4
- package/lib-es/bridge/prepareTransaction.js.map +1 -1
- package/lib-es/bridge/signOperation.d.ts.map +1 -1
- package/lib-es/bridge/signOperation.js +18 -4
- package/lib-es/bridge/signOperation.js.map +1 -1
- package/lib-es/bridge/synchronisation.d.ts +15 -0
- package/lib-es/bridge/synchronisation.d.ts.map +1 -1
- package/lib-es/bridge/synchronisation.js +124 -4
- package/lib-es/bridge/synchronisation.js.map +1 -1
- package/lib-es/config.d.ts +13 -0
- package/lib-es/config.d.ts.map +1 -0
- package/lib-es/config.js +4 -0
- package/lib-es/config.js.map +1 -0
- package/lib-es/constants.d.ts +9 -1
- package/lib-es/constants.d.ts.map +1 -1
- package/lib-es/constants.js +9 -1
- package/lib-es/constants.js.map +1 -1
- package/lib-es/network/client.d.ts +36 -0
- package/lib-es/network/client.d.ts.map +1 -0
- package/lib-es/network/client.js +240 -0
- package/lib-es/network/client.js.map +1 -0
- package/lib-es/network/graphql/queries.d.ts.map +1 -0
- package/lib-es/{api → network}/graphql/queries.js +6 -6
- package/lib-es/network/graphql/queries.js.map +1 -0
- package/lib-es/{api → network}/graphql/types.d.ts +9 -9
- package/lib-es/network/graphql/types.d.ts.map +1 -0
- package/lib-es/{api → network}/graphql/types.js.map +1 -1
- package/lib-es/network/index.d.ts +2 -1
- package/lib-es/network/index.d.ts.map +1 -1
- package/lib-es/network/index.js +1 -0
- package/lib-es/network/index.js.map +1 -1
- package/lib-es/test/bot-specs.d.ts.map +1 -1
- package/lib-es/test/bot-specs.js +41 -2
- package/lib-es/test/bot-specs.js.map +1 -1
- package/lib-es/test/bridgeDatasetTest.d.ts.map +1 -1
- package/lib-es/test/bridgeDatasetTest.js +44 -59
- package/lib-es/test/bridgeDatasetTest.js.map +1 -1
- package/lib-es/test/speculos-deviceActions.d.ts +1 -0
- package/lib-es/test/speculos-deviceActions.d.ts.map +1 -1
- package/lib-es/test/speculos-deviceActions.js +36 -4
- package/lib-es/test/speculos-deviceActions.js.map +1 -1
- package/lib-es/types/assets.d.ts +12 -0
- package/lib-es/types/assets.d.ts.map +1 -0
- package/lib-es/types/assets.js +2 -0
- package/lib-es/types/assets.js.map +1 -0
- package/lib-es/types/index.d.ts +18 -7
- package/lib-es/types/index.d.ts.map +1 -1
- package/package.json +12 -9
- package/src/__tests__/api/index.integ.test.ts +58 -0
- package/src/__tests__/api/index.test.ts +60 -477
- package/src/__tests__/bridge/broadcast.test.ts +2 -2
- package/src/__tests__/bridge/buildTransaction.test.ts +87 -5
- package/src/__tests__/bridge/createTransaction.test.ts +1 -1
- package/src/__tests__/bridge/getFeesForTransaction.test.ts +146 -11
- package/src/__tests__/bridge/getTransactionStatus.test.ts +217 -38
- package/src/__tests__/bridge/logic.test.ts +922 -118
- package/src/__tests__/bridge/prepareTransaction.test.ts +2 -2
- package/src/__tests__/bridge/signOperation.test.ts +147 -5
- package/src/__tests__/bridge/synchronisation.test.ts +1265 -71
- package/src/__tests__/network/client.test.ts +618 -0
- package/src/api/index.ts +31 -223
- package/src/bridge/bridge.fixture.ts +91 -12
- package/src/bridge/broadcast.ts +7 -7
- package/src/bridge/buildTransaction.ts +40 -7
- package/src/bridge/createTransaction.ts +1 -1
- package/src/bridge/estimateMaxSpendable.ts +4 -3
- package/src/bridge/getFeesForTransaction.ts +16 -10
- package/src/bridge/getTransactionStatus.ts +35 -13
- package/src/bridge/index.ts +1 -1
- package/src/bridge/logic.ts +202 -67
- package/src/bridge/prepareTransaction.ts +7 -4
- package/src/bridge/signOperation.ts +20 -4
- package/src/bridge/synchronisation.ts +171 -4
- package/src/config.ts +19 -0
- package/src/constants.ts +17 -1
- package/src/network/client.ts +315 -0
- package/src/{api → network}/graphql/queries.ts +6 -6
- package/src/{api → network}/graphql/types.ts +9 -9
- package/src/network/index.ts +2 -1
- package/src/test/bot-specs.ts +63 -3
- package/src/test/bridgeDatasetTest.ts +46 -59
- package/src/test/speculos-deviceActions.ts +40 -4
- package/src/types/assets.ts +15 -0
- package/src/types/index.ts +15 -1
- package/lib/api/graphql/queries.d.ts.map +0 -1
- package/lib/api/graphql/queries.js.map +0 -1
- package/lib/api/graphql/types.d.ts.map +0 -1
- package/lib-es/api/graphql/queries.d.ts.map +0 -1
- package/lib-es/api/graphql/queries.js.map +0 -1
- package/lib-es/api/graphql/types.d.ts.map +0 -1
- /package/lib/{api → network}/graphql/queries.d.ts +0 -0
- /package/lib/{api → network}/graphql/types.js +0 -0
- /package/lib-es/{api → network}/graphql/queries.d.ts +0 -0
- /package/lib-es/{api → network}/graphql/types.js +0 -0
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
import { ApolloClient } from "@apollo/client";
|
|
2
|
+
import {
|
|
3
|
+
AccountAddress,
|
|
4
|
+
Aptos,
|
|
5
|
+
ChainId,
|
|
6
|
+
Ed25519PublicKey,
|
|
7
|
+
InputEntryFunctionData,
|
|
8
|
+
RawTransaction,
|
|
9
|
+
Serializable,
|
|
10
|
+
post,
|
|
11
|
+
} from "@aptos-labs/ts-sdk";
|
|
12
|
+
import network from "@ledgerhq/live-network";
|
|
13
|
+
import BigNumber from "bignumber.js";
|
|
14
|
+
import { AptosAPI } from "../../network";
|
|
15
|
+
import { AptosAsset, AptosExtra, AptosSender } from "../../types/assets";
|
|
16
|
+
import { TransactionIntent } from "@ledgerhq/coin-framework/api/types";
|
|
17
|
+
import { TokenCurrency } from "@ledgerhq/types-cryptoassets";
|
|
18
|
+
|
|
19
|
+
jest.mock("@aptos-labs/ts-sdk");
|
|
20
|
+
jest.mock("@apollo/client");
|
|
21
|
+
let mockedAptos: jest.Mocked<any>;
|
|
22
|
+
let mockedApolloClient: jest.Mocked<any>;
|
|
23
|
+
let mockedPost = jest.fn();
|
|
24
|
+
|
|
25
|
+
jest.mock("@ledgerhq/live-network/network");
|
|
26
|
+
const mockedNetwork = jest.mocked(network);
|
|
27
|
+
|
|
28
|
+
describe("Aptos API", () => {
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
mockedAptos = jest.mocked(Aptos);
|
|
31
|
+
mockedApolloClient = jest.mocked(ApolloClient);
|
|
32
|
+
mockedPost = jest.mocked(post);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterEach(() => jest.clearAllMocks());
|
|
36
|
+
|
|
37
|
+
it("builds the client properly for mainnet", () => {
|
|
38
|
+
const api = new AptosAPI("aptos");
|
|
39
|
+
|
|
40
|
+
expect(api.broadcast).toBeDefined();
|
|
41
|
+
expect(typeof api.broadcast).toBe("function");
|
|
42
|
+
expect(api.estimateGasPrice).toBeDefined();
|
|
43
|
+
expect(typeof api.estimateGasPrice).toBe("function");
|
|
44
|
+
expect(api.generateTransaction).toBeDefined();
|
|
45
|
+
expect(typeof api.generateTransaction).toBe("function");
|
|
46
|
+
expect(api.getAccount).toBeDefined();
|
|
47
|
+
expect(typeof api.getAccount).toBe("function");
|
|
48
|
+
expect(api.getAccountInfo).toBeDefined();
|
|
49
|
+
expect(typeof api.getAccountInfo).toBe("function");
|
|
50
|
+
expect(api.simulateTransaction).toBeDefined();
|
|
51
|
+
expect(typeof api.simulateTransaction).toBe("function");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("builds the client properly for testnet", () => {
|
|
55
|
+
const api = new AptosAPI("aptos_testnet");
|
|
56
|
+
|
|
57
|
+
expect(api.broadcast).toBeDefined();
|
|
58
|
+
expect(typeof api.broadcast).toBe("function");
|
|
59
|
+
expect(api.estimateGasPrice).toBeDefined();
|
|
60
|
+
expect(typeof api.estimateGasPrice).toBe("function");
|
|
61
|
+
expect(api.generateTransaction).toBeDefined();
|
|
62
|
+
expect(typeof api.generateTransaction).toBe("function");
|
|
63
|
+
expect(api.getAccount).toBeDefined();
|
|
64
|
+
expect(typeof api.getAccount).toBe("function");
|
|
65
|
+
expect(api.getAccountInfo).toBeDefined();
|
|
66
|
+
expect(typeof api.getAccountInfo).toBe("function");
|
|
67
|
+
expect(api.simulateTransaction).toBeDefined();
|
|
68
|
+
expect(typeof api.simulateTransaction).toBe("function");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe("getAccount", () => {
|
|
72
|
+
it("calls getAccountInfo", async () => {
|
|
73
|
+
const mockGetAccountInfo = jest.fn();
|
|
74
|
+
mockedAptos.mockImplementation(() => ({
|
|
75
|
+
getAccountInfo: mockGetAccountInfo,
|
|
76
|
+
}));
|
|
77
|
+
|
|
78
|
+
const mockGetAccountSpy = jest.spyOn({ getAccount: mockGetAccountInfo }, "getAccount");
|
|
79
|
+
|
|
80
|
+
const api = new AptosAPI("aptos");
|
|
81
|
+
await api.getAccount("address");
|
|
82
|
+
|
|
83
|
+
expect(mockGetAccountSpy).toHaveBeenCalledWith({
|
|
84
|
+
accountAddress: "address",
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe("getBalance", () => {
|
|
90
|
+
let token: TokenCurrency;
|
|
91
|
+
|
|
92
|
+
beforeEach(() => {
|
|
93
|
+
token = {
|
|
94
|
+
type: "TokenCurrency",
|
|
95
|
+
id: "aptos_token",
|
|
96
|
+
name: "Aptos Token",
|
|
97
|
+
ticker: "APT",
|
|
98
|
+
units: [{ name: "APT", code: "APT", magnitude: 6 }],
|
|
99
|
+
contractAddress: "APTOS_1_ADDRESS",
|
|
100
|
+
tokenType: "fungible_asset",
|
|
101
|
+
parentCurrency: {
|
|
102
|
+
type: "CryptoCurrency",
|
|
103
|
+
id: "aptos",
|
|
104
|
+
name: "Aptos",
|
|
105
|
+
ticker: "APT",
|
|
106
|
+
units: [{ name: "APT", code: "APT", magnitude: 6 }],
|
|
107
|
+
color: "#000000",
|
|
108
|
+
family: "aptos",
|
|
109
|
+
scheme: "aptos",
|
|
110
|
+
explorerViews: [],
|
|
111
|
+
managerAppName: "Aptos",
|
|
112
|
+
coinType: 637,
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("get coin balance", async () => {
|
|
118
|
+
mockedAptos.mockImplementation(() => ({
|
|
119
|
+
view: jest.fn().mockReturnValue(["1234"]),
|
|
120
|
+
}));
|
|
121
|
+
|
|
122
|
+
token.tokenType = "coin";
|
|
123
|
+
const api = new AptosAPI("aptos");
|
|
124
|
+
const balance = await api.getBalance("address", token);
|
|
125
|
+
|
|
126
|
+
expect(balance).toEqual(new BigNumber(1234));
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("get fungible assets balance", async () => {
|
|
130
|
+
mockedAptos.mockImplementation(() => ({
|
|
131
|
+
view: jest.fn().mockReturnValue(["12345"]),
|
|
132
|
+
}));
|
|
133
|
+
|
|
134
|
+
token.tokenType = "fungible_asset";
|
|
135
|
+
|
|
136
|
+
const api = new AptosAPI("aptos");
|
|
137
|
+
const balance = await api.getBalance("address", token);
|
|
138
|
+
|
|
139
|
+
expect(balance).toEqual(new BigNumber(12345));
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("return 0 balace if could not retrieve proper balance of fungible assets", async () => {
|
|
143
|
+
mockedAptos.mockImplementation(() => ({
|
|
144
|
+
view: jest.fn().mockImplementation(() => {
|
|
145
|
+
throw new Error("error");
|
|
146
|
+
}),
|
|
147
|
+
}));
|
|
148
|
+
|
|
149
|
+
token.tokenType = "fungible_asset";
|
|
150
|
+
|
|
151
|
+
const api = new AptosAPI("aptos");
|
|
152
|
+
const balance = await api.getBalance("address", token);
|
|
153
|
+
|
|
154
|
+
expect(balance).toEqual(new BigNumber(0));
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe("getAccountInfo", () => {
|
|
159
|
+
it("calls getCoinBalance, fetchTransactions and getHeight", async () => {
|
|
160
|
+
mockedAptos.mockImplementation(() => ({
|
|
161
|
+
view: jest.fn().mockReturnValue(["123"]),
|
|
162
|
+
getTransactionByVersion: jest.fn().mockReturnValue({
|
|
163
|
+
type: "user_transaction",
|
|
164
|
+
version: "v1",
|
|
165
|
+
}),
|
|
166
|
+
getBlockByVersion: jest.fn().mockReturnValue({
|
|
167
|
+
block_height: "1",
|
|
168
|
+
block_hash: "83ca6d",
|
|
169
|
+
}),
|
|
170
|
+
}));
|
|
171
|
+
|
|
172
|
+
mockedNetwork.mockResolvedValue(
|
|
173
|
+
Promise.resolve({
|
|
174
|
+
data: {
|
|
175
|
+
account: {
|
|
176
|
+
account_number: 1,
|
|
177
|
+
sequence: 0,
|
|
178
|
+
pub_key: { key: "k", "@type": "type" },
|
|
179
|
+
base_account: {
|
|
180
|
+
account_number: 2,
|
|
181
|
+
sequence: 42,
|
|
182
|
+
pub_key: { key: "k2", "@type": "type2" },
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
block_height: "999",
|
|
186
|
+
},
|
|
187
|
+
status: 200,
|
|
188
|
+
headers: {} as any,
|
|
189
|
+
statusText: "",
|
|
190
|
+
config: {
|
|
191
|
+
headers: {} as any,
|
|
192
|
+
},
|
|
193
|
+
}),
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
mockedApolloClient.mockImplementation(() => ({
|
|
197
|
+
query: async () => ({
|
|
198
|
+
data: {
|
|
199
|
+
account_transactions: [{ transaction_version: 1 }],
|
|
200
|
+
},
|
|
201
|
+
loading: false,
|
|
202
|
+
networkStatus: 7,
|
|
203
|
+
}),
|
|
204
|
+
}));
|
|
205
|
+
|
|
206
|
+
const api = new AptosAPI("aptos");
|
|
207
|
+
const accountInfo = await api.getAccountInfo("APTOS_1_ADDRESS", "1");
|
|
208
|
+
|
|
209
|
+
expect(accountInfo.balance).toEqual(new BigNumber(123));
|
|
210
|
+
expect(accountInfo.transactions).toEqual([
|
|
211
|
+
{
|
|
212
|
+
type: "user_transaction",
|
|
213
|
+
version: "v1",
|
|
214
|
+
block: {
|
|
215
|
+
height: 1,
|
|
216
|
+
hash: "83ca6d",
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
]);
|
|
220
|
+
expect(accountInfo.blockHeight).toEqual(999);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("return balance = 0 if it fails to fetch balance", async () => {
|
|
224
|
+
mockedAptos.mockImplementation(() => ({
|
|
225
|
+
view: jest.fn().mockImplementation(() => {
|
|
226
|
+
throw new Error("error");
|
|
227
|
+
}),
|
|
228
|
+
getTransactionByVersion: jest.fn().mockReturnValue({
|
|
229
|
+
type: "user_transaction",
|
|
230
|
+
version: "v1",
|
|
231
|
+
}),
|
|
232
|
+
getBlockByVersion: jest.fn().mockReturnValue({
|
|
233
|
+
block_height: "1",
|
|
234
|
+
block_hash: "83ca6d",
|
|
235
|
+
}),
|
|
236
|
+
}));
|
|
237
|
+
|
|
238
|
+
mockedNetwork.mockResolvedValue(
|
|
239
|
+
Promise.resolve({
|
|
240
|
+
data: {
|
|
241
|
+
account: {
|
|
242
|
+
account_number: 1,
|
|
243
|
+
sequence: 0,
|
|
244
|
+
pub_key: { key: "k", "@type": "type" },
|
|
245
|
+
base_account: {
|
|
246
|
+
account_number: 2,
|
|
247
|
+
sequence: 42,
|
|
248
|
+
pub_key: { key: "k2", "@type": "type2" },
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
block_height: "999",
|
|
252
|
+
},
|
|
253
|
+
status: 200,
|
|
254
|
+
headers: {} as any,
|
|
255
|
+
statusText: "",
|
|
256
|
+
config: {
|
|
257
|
+
headers: {} as any,
|
|
258
|
+
},
|
|
259
|
+
}),
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
mockedApolloClient.mockImplementation(() => ({
|
|
263
|
+
query: async () => ({
|
|
264
|
+
data: {
|
|
265
|
+
account_transactions: [{ transaction_version: 1 }],
|
|
266
|
+
},
|
|
267
|
+
loading: false,
|
|
268
|
+
networkStatus: 7,
|
|
269
|
+
}),
|
|
270
|
+
}));
|
|
271
|
+
|
|
272
|
+
const api = new AptosAPI("aptos");
|
|
273
|
+
const accountInfo = await api.getAccountInfo("APTOS_1_ADDRESS", "1");
|
|
274
|
+
|
|
275
|
+
expect(accountInfo.balance).toEqual(new BigNumber(0));
|
|
276
|
+
expect(accountInfo.transactions).toEqual([
|
|
277
|
+
{
|
|
278
|
+
type: "user_transaction",
|
|
279
|
+
version: "v1",
|
|
280
|
+
block: {
|
|
281
|
+
height: 1,
|
|
282
|
+
hash: "83ca6d",
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
]);
|
|
286
|
+
expect(accountInfo.blockHeight).toEqual(999);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it("returns no transactions if it the address is empty", async () => {
|
|
290
|
+
mockedAptos.mockImplementation(() => ({
|
|
291
|
+
view: jest.fn().mockReturnValue(["123"]),
|
|
292
|
+
getTransactionByVersion: jest.fn().mockReturnValue({
|
|
293
|
+
type: "user_transaction",
|
|
294
|
+
version: "v1",
|
|
295
|
+
}),
|
|
296
|
+
getBlockByVersion: jest.fn().mockReturnValue({
|
|
297
|
+
block_height: "1",
|
|
298
|
+
block_hash: "83ca6d",
|
|
299
|
+
}),
|
|
300
|
+
}));
|
|
301
|
+
|
|
302
|
+
mockedNetwork.mockResolvedValue(
|
|
303
|
+
Promise.resolve({
|
|
304
|
+
data: {
|
|
305
|
+
account: {
|
|
306
|
+
account_number: 1,
|
|
307
|
+
sequence: 0,
|
|
308
|
+
pub_key: { key: "k", "@type": "type" },
|
|
309
|
+
base_account: {
|
|
310
|
+
account_number: 2,
|
|
311
|
+
sequence: 42,
|
|
312
|
+
pub_key: { key: "k2", "@type": "type2" },
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
block_height: "999",
|
|
316
|
+
},
|
|
317
|
+
status: 200,
|
|
318
|
+
headers: {} as any,
|
|
319
|
+
statusText: "",
|
|
320
|
+
config: {
|
|
321
|
+
headers: {} as any,
|
|
322
|
+
},
|
|
323
|
+
}),
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
mockedApolloClient.mockImplementation(() => ({
|
|
327
|
+
query: async () => ({
|
|
328
|
+
data: {
|
|
329
|
+
account_transactions: [{ transaction_version: 1 }],
|
|
330
|
+
},
|
|
331
|
+
loading: false,
|
|
332
|
+
networkStatus: 7,
|
|
333
|
+
}),
|
|
334
|
+
}));
|
|
335
|
+
|
|
336
|
+
const api = new AptosAPI("aptos");
|
|
337
|
+
const accountInfo = await api.getAccountInfo("", "1");
|
|
338
|
+
|
|
339
|
+
expect(accountInfo.balance).toEqual(new BigNumber(123));
|
|
340
|
+
expect(accountInfo.transactions).toEqual([]);
|
|
341
|
+
expect(accountInfo.blockHeight).toEqual(999);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it("returns a null transaction if it fails to getTransactionByVersion", async () => {
|
|
345
|
+
mockedAptos.mockImplementation(() => ({
|
|
346
|
+
view: jest.fn().mockReturnValue(["123"]),
|
|
347
|
+
getTransactionByVersion: jest.fn().mockImplementation(() => {
|
|
348
|
+
throw new Error("error");
|
|
349
|
+
}),
|
|
350
|
+
getBlockByVersion: jest.fn().mockReturnValue({
|
|
351
|
+
block_height: "1",
|
|
352
|
+
block_hash: "83ca6d",
|
|
353
|
+
}),
|
|
354
|
+
}));
|
|
355
|
+
|
|
356
|
+
mockedNetwork.mockResolvedValue(
|
|
357
|
+
Promise.resolve({
|
|
358
|
+
data: {
|
|
359
|
+
account: {
|
|
360
|
+
account_number: 1,
|
|
361
|
+
sequence: 0,
|
|
362
|
+
pub_key: { key: "k", "@type": "type" },
|
|
363
|
+
base_account: {
|
|
364
|
+
account_number: 2,
|
|
365
|
+
sequence: 42,
|
|
366
|
+
pub_key: { key: "k2", "@type": "type2" },
|
|
367
|
+
},
|
|
368
|
+
},
|
|
369
|
+
block_height: "999",
|
|
370
|
+
},
|
|
371
|
+
status: 200,
|
|
372
|
+
headers: {} as any,
|
|
373
|
+
statusText: "",
|
|
374
|
+
config: {
|
|
375
|
+
headers: {} as any,
|
|
376
|
+
},
|
|
377
|
+
}),
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
mockedApolloClient.mockImplementation(() => ({
|
|
381
|
+
query: async () => ({
|
|
382
|
+
data: {
|
|
383
|
+
account_transactions: [{ transaction_version: 1 }],
|
|
384
|
+
},
|
|
385
|
+
loading: false,
|
|
386
|
+
networkStatus: 7,
|
|
387
|
+
}),
|
|
388
|
+
}));
|
|
389
|
+
|
|
390
|
+
const api = new AptosAPI("aptos");
|
|
391
|
+
const accountInfo = await api.getAccountInfo("APTOS_1_ADDRESS", "1");
|
|
392
|
+
|
|
393
|
+
expect(accountInfo.balance).toEqual(new BigNumber(123));
|
|
394
|
+
expect(accountInfo.transactions).toEqual([null]);
|
|
395
|
+
expect(accountInfo.blockHeight).toEqual(999);
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
describe("estimateGasPrice", () => {
|
|
400
|
+
it("estimates the gas price", async () => {
|
|
401
|
+
const gasEstimation = { gas_estimate: 100 };
|
|
402
|
+
mockedAptos.mockImplementation(() => ({
|
|
403
|
+
getGasPriceEstimation: jest.fn().mockReturnValue(gasEstimation),
|
|
404
|
+
}));
|
|
405
|
+
|
|
406
|
+
const api = new AptosAPI("aptos");
|
|
407
|
+
const gasPrice = await api.estimateGasPrice();
|
|
408
|
+
|
|
409
|
+
expect(gasPrice.gas_estimate).toEqual(100);
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
describe("generateTransaction", () => {
|
|
414
|
+
const payload: InputEntryFunctionData = {
|
|
415
|
+
function: "0x1::coin::transfer",
|
|
416
|
+
functionArguments: ["0x13", 1],
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
it("generates a transaction without expireTimestamp", async () => {
|
|
420
|
+
const options = {
|
|
421
|
+
maxGasAmount: "100",
|
|
422
|
+
gasUnitPrice: "50",
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
const mockSimple = jest.fn().mockImplementation(async () => ({
|
|
426
|
+
rawTransaction: null,
|
|
427
|
+
}));
|
|
428
|
+
mockedAptos.mockImplementation(() => ({
|
|
429
|
+
transaction: {
|
|
430
|
+
build: {
|
|
431
|
+
simple: mockSimple,
|
|
432
|
+
},
|
|
433
|
+
},
|
|
434
|
+
}));
|
|
435
|
+
|
|
436
|
+
const mockSimpleSpy = jest.spyOn({ simple: mockSimple }, "simple");
|
|
437
|
+
|
|
438
|
+
const api = new AptosAPI("aptos");
|
|
439
|
+
await api.generateTransaction("APTOS_1_ADDRESS", payload, options);
|
|
440
|
+
|
|
441
|
+
expect(mockSimpleSpy).toHaveBeenCalledWith({
|
|
442
|
+
data: payload,
|
|
443
|
+
options: {
|
|
444
|
+
maxGasAmount: Number(options.maxGasAmount),
|
|
445
|
+
gasUnitPrice: Number(options.gasUnitPrice),
|
|
446
|
+
},
|
|
447
|
+
sender: "APTOS_1_ADDRESS",
|
|
448
|
+
});
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it("generates a transaction with default expireTimestamp", async () => {
|
|
452
|
+
const options = {
|
|
453
|
+
maxGasAmount: "100",
|
|
454
|
+
gasUnitPrice: "50",
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
const mockSimple = jest.fn().mockImplementation(async () => ({
|
|
458
|
+
rawTransaction: null,
|
|
459
|
+
}));
|
|
460
|
+
const mockGetLedgerInfo = jest.fn().mockImplementation(async () => ({
|
|
461
|
+
ledger_timestamp: "0",
|
|
462
|
+
}));
|
|
463
|
+
mockedAptos.mockImplementation(() => ({
|
|
464
|
+
transaction: {
|
|
465
|
+
build: {
|
|
466
|
+
simple: mockSimple,
|
|
467
|
+
},
|
|
468
|
+
},
|
|
469
|
+
getLedgerInfo: mockGetLedgerInfo,
|
|
470
|
+
}));
|
|
471
|
+
|
|
472
|
+
const mockSimpleSpy = jest.spyOn({ simple: mockSimple }, "simple");
|
|
473
|
+
|
|
474
|
+
const api = new AptosAPI("aptos");
|
|
475
|
+
await api.generateTransaction("APTOS_1_ADDRESS", payload, options);
|
|
476
|
+
|
|
477
|
+
expect(mockSimpleSpy).toHaveBeenCalledWith({
|
|
478
|
+
data: payload,
|
|
479
|
+
options: {
|
|
480
|
+
maxGasAmount: Number(options.maxGasAmount),
|
|
481
|
+
gasUnitPrice: Number(options.gasUnitPrice),
|
|
482
|
+
expireTimestamp: 120,
|
|
483
|
+
},
|
|
484
|
+
sender: "APTOS_1_ADDRESS",
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
it("throws an error when failing to build a transaction", async () => {
|
|
489
|
+
const options = {
|
|
490
|
+
maxGasAmount: "100",
|
|
491
|
+
gasUnitPrice: "50",
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
const mockSimple = jest.fn().mockImplementation(async () => null);
|
|
495
|
+
mockedAptos.mockImplementation(() => ({
|
|
496
|
+
transaction: {
|
|
497
|
+
build: {
|
|
498
|
+
simple: mockSimple,
|
|
499
|
+
},
|
|
500
|
+
},
|
|
501
|
+
}));
|
|
502
|
+
|
|
503
|
+
const api = new AptosAPI("aptos");
|
|
504
|
+
expect(
|
|
505
|
+
async () => await api.generateTransaction("APTOS_1_ADDRESS", payload, options),
|
|
506
|
+
).rejects.toThrow();
|
|
507
|
+
});
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
describe("simulateTransaction", () => {
|
|
511
|
+
it("simulates a transaction with the correct options", async () => {
|
|
512
|
+
const mockSimple = jest.fn().mockImplementation(async () => ({
|
|
513
|
+
rawTransaction: null,
|
|
514
|
+
}));
|
|
515
|
+
mockedAptos.mockImplementation(() => ({
|
|
516
|
+
transaction: {
|
|
517
|
+
simulate: {
|
|
518
|
+
simple: mockSimple,
|
|
519
|
+
},
|
|
520
|
+
},
|
|
521
|
+
}));
|
|
522
|
+
|
|
523
|
+
const mockSimpleSpy = jest.spyOn({ simple: mockSimple }, "simple");
|
|
524
|
+
|
|
525
|
+
const api = new AptosAPI("aptos");
|
|
526
|
+
const address = new Ed25519PublicKey("APTOS_1_ADDRESS");
|
|
527
|
+
const tx = new RawTransaction(
|
|
528
|
+
new AccountAddress(Uint8Array.from(Buffer.from("APTOS_2_ADDRESS"))),
|
|
529
|
+
BigInt(1),
|
|
530
|
+
"" as unknown as Serializable,
|
|
531
|
+
BigInt(100),
|
|
532
|
+
BigInt(50),
|
|
533
|
+
BigInt(1),
|
|
534
|
+
{ chainId: 1 } as ChainId,
|
|
535
|
+
);
|
|
536
|
+
await api.simulateTransaction(address, tx);
|
|
537
|
+
|
|
538
|
+
expect(mockSimpleSpy).toHaveBeenCalledWith({
|
|
539
|
+
signerPublicKey: address,
|
|
540
|
+
transaction: { rawTransaction: tx },
|
|
541
|
+
options: {
|
|
542
|
+
estimateGasUnitPrice: true,
|
|
543
|
+
estimateMaxGasAmount: true,
|
|
544
|
+
estimatePrioritizedGasUnitPrice: false,
|
|
545
|
+
},
|
|
546
|
+
});
|
|
547
|
+
});
|
|
548
|
+
});
|
|
549
|
+
describe("broadcast", () => {
|
|
550
|
+
it("broadcasts the transaction", async () => {
|
|
551
|
+
mockedPost.mockImplementation(async () => ({ data: { hash: "ok" } }));
|
|
552
|
+
const mockedPostSpy = jest.spyOn({ post: mockedPost }, "post");
|
|
553
|
+
|
|
554
|
+
mockedAptos.mockImplementation(() => ({
|
|
555
|
+
config: "config",
|
|
556
|
+
}));
|
|
557
|
+
|
|
558
|
+
const api = new AptosAPI("aptos");
|
|
559
|
+
await api.broadcast("signature");
|
|
560
|
+
|
|
561
|
+
expect(mockedPostSpy).toHaveBeenCalledWith({
|
|
562
|
+
contentType: "application/x.aptos.signed_transaction+bcs",
|
|
563
|
+
aptosConfig: "config",
|
|
564
|
+
body: Uint8Array.from(Buffer.from("signature", "hex")),
|
|
565
|
+
path: "transactions",
|
|
566
|
+
type: "Fullnode",
|
|
567
|
+
originMethod: "",
|
|
568
|
+
});
|
|
569
|
+
});
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
describe("estimateFees", () => {
|
|
573
|
+
it("estimates the fees", async () => {
|
|
574
|
+
const gasEstimation = { gas_estimate: 100 };
|
|
575
|
+
mockedAptos.mockImplementation(() => ({
|
|
576
|
+
getLedgerInfo: jest.fn().mockResolvedValue({
|
|
577
|
+
ledger_timestamp: Date.now(),
|
|
578
|
+
}),
|
|
579
|
+
transaction: {
|
|
580
|
+
build: {
|
|
581
|
+
simple: jest.fn().mockResolvedValue({ rawTransaction: {} }),
|
|
582
|
+
},
|
|
583
|
+
simulate: {
|
|
584
|
+
simple: jest.fn().mockResolvedValue([
|
|
585
|
+
{
|
|
586
|
+
gas_used: 10,
|
|
587
|
+
gas_unit_price: 4,
|
|
588
|
+
},
|
|
589
|
+
]),
|
|
590
|
+
},
|
|
591
|
+
},
|
|
592
|
+
getGasPriceEstimation: jest.fn().mockReturnValue(gasEstimation),
|
|
593
|
+
}));
|
|
594
|
+
|
|
595
|
+
const amount = BigInt(100);
|
|
596
|
+
const sender: AptosSender = {
|
|
597
|
+
xpub: "xpub",
|
|
598
|
+
freshAddress: "address1",
|
|
599
|
+
};
|
|
600
|
+
const recipient = "address2";
|
|
601
|
+
|
|
602
|
+
const api = new AptosAPI("aptos");
|
|
603
|
+
const transactionIntent: TransactionIntent<AptosAsset, AptosExtra, AptosSender> = {
|
|
604
|
+
asset: {
|
|
605
|
+
type: "native",
|
|
606
|
+
},
|
|
607
|
+
type: "send",
|
|
608
|
+
sender,
|
|
609
|
+
amount,
|
|
610
|
+
recipient,
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
const fees = await api.estimateFees(transactionIntent);
|
|
614
|
+
|
|
615
|
+
expect(fees.value.toString()).toEqual("40");
|
|
616
|
+
});
|
|
617
|
+
});
|
|
618
|
+
});
|