@ledgerhq/coin-aptos 2.2.0-next.1 → 2.2.0-nightly.1
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/CHANGELOG.md +9 -15
- package/lib/__tests__/api/craftTransaction.unit.test.js +16 -31
- package/lib/__tests__/api/craftTransaction.unit.test.js.map +1 -1
- package/lib/__tests__/api/getBalance.unit.test.js +68 -28
- package/lib/__tests__/api/getBalance.unit.test.js.map +1 -1
- package/lib/__tests__/api/index.integ.test.js +30 -11
- package/lib/__tests__/api/index.integ.test.js.map +1 -1
- package/lib/__tests__/api/index.test.js.map +1 -1
- package/lib/__tests__/bridge/synchronisation.test.js +15 -6
- package/lib/__tests__/bridge/synchronisation.test.js.map +1 -1
- package/lib/__tests__/logic/getBalances.test.d.ts +2 -0
- package/lib/__tests__/logic/getBalances.test.d.ts.map +1 -0
- package/lib/__tests__/logic/getBalances.test.js +63 -0
- package/lib/__tests__/logic/getBalances.test.js.map +1 -0
- package/lib/__tests__/network/client.test.js +69 -66
- package/lib/__tests__/network/client.test.js.map +1 -1
- package/lib/api/index.d.ts +3 -3
- package/lib/api/index.d.ts.map +1 -1
- package/lib/api/index.js +2 -2
- package/lib/api/index.js.map +1 -1
- package/lib/bridge/synchronisation.d.ts.map +1 -1
- package/lib/bridge/synchronisation.js +7 -2
- package/lib/bridge/synchronisation.js.map +1 -1
- package/lib/logic/craftTransaction.d.ts +2 -2
- package/lib/logic/craftTransaction.d.ts.map +1 -1
- package/lib/logic/craftTransaction.js +3 -3
- package/lib/logic/craftTransaction.js.map +1 -1
- package/lib/logic/getBalances.d.ts +5 -0
- package/lib/logic/getBalances.d.ts.map +1 -0
- package/lib/logic/getBalances.js +21 -0
- package/lib/logic/getBalances.js.map +1 -0
- package/lib/network/client.d.ts +3 -7
- package/lib/network/client.d.ts.map +1 -1
- package/lib/network/client.js +28 -62
- package/lib/network/client.js.map +1 -1
- package/lib/types/assets.d.ts +0 -4
- package/lib/types/assets.d.ts.map +1 -1
- package/lib-es/__tests__/api/craftTransaction.unit.test.js +16 -31
- package/lib-es/__tests__/api/craftTransaction.unit.test.js.map +1 -1
- package/lib-es/__tests__/api/getBalance.unit.test.js +69 -26
- package/lib-es/__tests__/api/getBalance.unit.test.js.map +1 -1
- package/lib-es/__tests__/api/index.integ.test.js +30 -11
- package/lib-es/__tests__/api/index.integ.test.js.map +1 -1
- package/lib-es/__tests__/api/index.test.js.map +1 -1
- package/lib-es/__tests__/bridge/synchronisation.test.js +15 -6
- package/lib-es/__tests__/bridge/synchronisation.test.js.map +1 -1
- package/lib-es/__tests__/logic/getBalances.test.d.ts +2 -0
- package/lib-es/__tests__/logic/getBalances.test.d.ts.map +1 -0
- package/lib-es/__tests__/logic/getBalances.test.js +61 -0
- package/lib-es/__tests__/logic/getBalances.test.js.map +1 -0
- package/lib-es/__tests__/network/client.test.js +69 -66
- package/lib-es/__tests__/network/client.test.js.map +1 -1
- package/lib-es/api/index.d.ts +3 -3
- package/lib-es/api/index.d.ts.map +1 -1
- package/lib-es/api/index.js +2 -2
- package/lib-es/api/index.js.map +1 -1
- package/lib-es/bridge/synchronisation.d.ts.map +1 -1
- package/lib-es/bridge/synchronisation.js +4 -2
- package/lib-es/bridge/synchronisation.js.map +1 -1
- package/lib-es/logic/craftTransaction.d.ts +2 -2
- package/lib-es/logic/craftTransaction.d.ts.map +1 -1
- package/lib-es/logic/craftTransaction.js +3 -3
- package/lib-es/logic/craftTransaction.js.map +1 -1
- package/lib-es/logic/getBalances.d.ts +5 -0
- package/lib-es/logic/getBalances.d.ts.map +1 -0
- package/lib-es/logic/getBalances.js +17 -0
- package/lib-es/logic/getBalances.js.map +1 -0
- package/lib-es/network/client.d.ts +3 -7
- package/lib-es/network/client.d.ts.map +1 -1
- package/lib-es/network/client.js +28 -62
- package/lib-es/network/client.js.map +1 -1
- package/lib-es/types/assets.d.ts +0 -4
- package/lib-es/types/assets.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/__tests__/api/craftTransaction.unit.test.ts +23 -38
- package/src/__tests__/api/getBalance.unit.test.ts +82 -28
- package/src/__tests__/api/index.integ.test.ts +36 -15
- package/src/__tests__/api/index.test.ts +4 -6
- package/src/__tests__/bridge/synchronisation.test.ts +21 -6
- package/src/__tests__/logic/getBalances.test.ts +80 -0
- package/src/__tests__/network/client.test.ts +83 -89
- package/src/api/index.ts +6 -8
- package/src/bridge/synchronisation.ts +4 -2
- package/src/logic/craftTransaction.ts +6 -8
- package/src/logic/getBalances.ts +27 -0
- package/src/network/client.ts +31 -74
- package/src/types/assets.ts +0 -5
- package/lib/logic/getBalance.d.ts +0 -5
- package/lib/logic/getBalance.d.ts.map +0 -1
- package/lib/logic/getBalance.js +0 -12
- package/lib/logic/getBalance.js.map +0 -1
- package/lib-es/logic/getBalance.d.ts +0 -5
- package/lib-es/logic/getBalance.d.ts.map +0 -1
- package/lib-es/logic/getBalance.js +0 -8
- package/lib-es/logic/getBalance.js.map +0 -1
- package/src/logic/getBalance.ts +0 -15
|
@@ -798,10 +798,15 @@ describe("getAccountShape", () => {
|
|
|
798
798
|
blockHeight: 316278241,
|
|
799
799
|
}));
|
|
800
800
|
|
|
801
|
-
const
|
|
801
|
+
const TOKEN_CONTRACT_ADDRESS = "0xd111::staked_coin::StakedAptos";
|
|
802
|
+
|
|
803
|
+
const mockGetBalances = jest
|
|
804
|
+
.fn()
|
|
805
|
+
.mockResolvedValue([{ contractAddress: TOKEN_CONTRACT_ADDRESS, amount: BigNumber(1234567) }]);
|
|
806
|
+
|
|
802
807
|
mockedAptosAPI.mockImplementation(() => ({
|
|
808
|
+
getBalances: mockGetBalances,
|
|
803
809
|
getAccountInfo: mockGetAccountInfo,
|
|
804
|
-
getBalance: mockGetBalance,
|
|
805
810
|
}));
|
|
806
811
|
|
|
807
812
|
const mockGetAccountSpy = jest.spyOn({ getAccount: mockGetAccountInfo }, "getAccount");
|
|
@@ -1312,9 +1317,14 @@ describe("getSubAccountShape", () => {
|
|
|
1312
1317
|
] as Operation[];
|
|
1313
1318
|
|
|
1314
1319
|
it("returns the correct information", async () => {
|
|
1315
|
-
const
|
|
1320
|
+
const TOKEN_CONTRACT_ADDRESS = "0xd111::staked_coin::StakedAptos";
|
|
1321
|
+
|
|
1322
|
+
const mockGetBalances = jest
|
|
1323
|
+
.fn()
|
|
1324
|
+
.mockResolvedValue([{ contractAddress: TOKEN_CONTRACT_ADDRESS, amount: BigNumber(1234567) }]);
|
|
1325
|
+
|
|
1316
1326
|
mockedAptosAPI.mockImplementation(() => ({
|
|
1317
|
-
|
|
1327
|
+
getBalances: mockGetBalances,
|
|
1318
1328
|
}));
|
|
1319
1329
|
|
|
1320
1330
|
const subAccount = await getSubAccountShape(currency, address, parentId, token, operations);
|
|
@@ -1421,9 +1431,14 @@ describe("getSubAccounts", () => {
|
|
|
1421
1431
|
] as Operation[];
|
|
1422
1432
|
|
|
1423
1433
|
it("returns the correct information", async () => {
|
|
1424
|
-
const
|
|
1434
|
+
const TOKEN_CONTRACT_ADDRESS = "0xd111::staked_coin::StakedAptos";
|
|
1435
|
+
|
|
1436
|
+
const mockGetBalances = jest
|
|
1437
|
+
.fn()
|
|
1438
|
+
.mockResolvedValue([{ contractAddress: TOKEN_CONTRACT_ADDRESS, amount: BigNumber(1234567) }]);
|
|
1439
|
+
|
|
1425
1440
|
mockedAptosAPI.mockImplementation(() => ({
|
|
1426
|
-
|
|
1441
|
+
getBalances: mockGetBalances,
|
|
1427
1442
|
}));
|
|
1428
1443
|
|
|
1429
1444
|
mockedDecodeTokenAccountId.mockReturnValue({
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { getBalances } from "../../logic/getBalances";
|
|
2
|
+
import { APTOS_ASSET_ID, TOKEN_TYPE } from "../../constants";
|
|
3
|
+
import type { AptosAPI } from "../../network";
|
|
4
|
+
|
|
5
|
+
describe("getBalance", () => {
|
|
6
|
+
it("should return balance with value 10", async () => {
|
|
7
|
+
const mockAptosClient = {
|
|
8
|
+
getBalances: jest.fn().mockResolvedValue([{ contractAddress: APTOS_ASSET_ID, amount: 10n }]),
|
|
9
|
+
} as Partial<AptosAPI> as AptosAPI;
|
|
10
|
+
|
|
11
|
+
const accountAddress = "0x4be47904b31063d60ac0dfde06e5dc203e647edbe853bae0e666ae5a763c3906";
|
|
12
|
+
|
|
13
|
+
const balances = await getBalances(mockAptosClient, accountAddress);
|
|
14
|
+
|
|
15
|
+
expect(balances).toStrictEqual([{ value: 10n, asset: { type: "native" } }]);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("should return empty array when no contract_address and no data", async () => {
|
|
19
|
+
const mockAptosClient = {
|
|
20
|
+
getBalances: jest.fn().mockResolvedValue([]),
|
|
21
|
+
} as Partial<AptosAPI> as AptosAPI;
|
|
22
|
+
|
|
23
|
+
const accountAddress = "0x4be47904b31063d60ac0dfde06e5dc203e647edbe853bae0e666ae5a763c3906";
|
|
24
|
+
|
|
25
|
+
const balances = await getBalances(mockAptosClient, accountAddress);
|
|
26
|
+
expect(balances).toStrictEqual([]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should return balance with 'native' contract_address (APTOS_ASSET_ID)", async () => {
|
|
30
|
+
const mockAptosClient = {
|
|
31
|
+
getBalances: jest.fn().mockResolvedValue([{ contractAddress: APTOS_ASSET_ID, amount: 10n }]),
|
|
32
|
+
} as Partial<AptosAPI> as AptosAPI;
|
|
33
|
+
|
|
34
|
+
const accountAddress = "0x4be47904b31063d60ac0dfde06e5dc203e647edbe853bae0e666ae5a763c3906";
|
|
35
|
+
|
|
36
|
+
const balances = await getBalances(mockAptosClient, accountAddress);
|
|
37
|
+
|
|
38
|
+
expect(balances).toStrictEqual([{ value: 10n, asset: { type: "native" } }]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should return token balance when contract_address is a coin token", async () => {
|
|
42
|
+
const TOKEN_ASSET_ID = "0x1::my_token::Token";
|
|
43
|
+
const mockAptosClient = {
|
|
44
|
+
getBalances: jest.fn().mockResolvedValue([{ contractAddress: TOKEN_ASSET_ID, amount: 25n }]),
|
|
45
|
+
} as Partial<AptosAPI> as AptosAPI;
|
|
46
|
+
|
|
47
|
+
const accountAddress = "0x4be47904b31063d60ac0dfde06e5dc203e647edbe853bae0e666ae5a763c3906";
|
|
48
|
+
|
|
49
|
+
const balances = await getBalances(mockAptosClient, accountAddress);
|
|
50
|
+
|
|
51
|
+
expect(balances).toStrictEqual([
|
|
52
|
+
{
|
|
53
|
+
value: 25n,
|
|
54
|
+
asset: { type: "token", contractAddress: TOKEN_ASSET_ID, standard: TOKEN_TYPE.COIN },
|
|
55
|
+
},
|
|
56
|
+
]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("should return token balance when contract_address is a fungible_asset token", async () => {
|
|
60
|
+
const TOKEN_ASSET_ID = "0x1";
|
|
61
|
+
const mockAptosClient = {
|
|
62
|
+
getBalances: jest.fn().mockResolvedValue([{ contractAddress: TOKEN_ASSET_ID, amount: 25n }]),
|
|
63
|
+
} as Partial<AptosAPI> as AptosAPI;
|
|
64
|
+
|
|
65
|
+
const accountAddress = "0x4be47904b31063d60ac0dfde06e5dc203e647edbe853bae0e666ae5a763c3906";
|
|
66
|
+
|
|
67
|
+
const balances = await getBalances(mockAptosClient, accountAddress);
|
|
68
|
+
|
|
69
|
+
expect(balances).toStrictEqual([
|
|
70
|
+
{
|
|
71
|
+
value: 25n,
|
|
72
|
+
asset: {
|
|
73
|
+
type: "token",
|
|
74
|
+
contractAddress: TOKEN_ASSET_ID,
|
|
75
|
+
standard: TOKEN_TYPE.FUNGIBLE_ASSET,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
]);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
@@ -14,11 +14,10 @@ import {
|
|
|
14
14
|
import network from "@ledgerhq/live-network";
|
|
15
15
|
import BigNumber from "bignumber.js";
|
|
16
16
|
import { AptosAPI } from "../../network";
|
|
17
|
-
import { AptosAsset
|
|
17
|
+
import { AptosAsset } from "../../types/assets";
|
|
18
18
|
import { Pagination, TransactionIntent } from "@ledgerhq/coin-framework/api/types";
|
|
19
|
-
import { TokenCurrency } from "@ledgerhq/types-cryptoassets";
|
|
20
19
|
import { APTOS_ASSET_ID } from "../../constants";
|
|
21
|
-
import { AptosTransaction } from "../../types";
|
|
20
|
+
import { AptosBalance, AptosTransaction } from "../../types";
|
|
22
21
|
|
|
23
22
|
jest.mock("@aptos-labs/ts-sdk");
|
|
24
23
|
jest.mock("@apollo/client");
|
|
@@ -96,75 +95,6 @@ describe("Aptos API", () => {
|
|
|
96
95
|
});
|
|
97
96
|
});
|
|
98
97
|
|
|
99
|
-
describe("getBalance", () => {
|
|
100
|
-
let token: TokenCurrency;
|
|
101
|
-
|
|
102
|
-
beforeEach(() => {
|
|
103
|
-
token = {
|
|
104
|
-
type: "TokenCurrency",
|
|
105
|
-
id: "aptos_token",
|
|
106
|
-
name: "Aptos Token",
|
|
107
|
-
ticker: "APT",
|
|
108
|
-
units: [{ name: "APT", code: "APT", magnitude: 6 }],
|
|
109
|
-
contractAddress: "APTOS_1_ADDRESS",
|
|
110
|
-
tokenType: "fungible_asset",
|
|
111
|
-
parentCurrency: {
|
|
112
|
-
type: "CryptoCurrency",
|
|
113
|
-
id: "aptos",
|
|
114
|
-
name: "Aptos",
|
|
115
|
-
ticker: "APT",
|
|
116
|
-
units: [{ name: "APT", code: "APT", magnitude: 6 }],
|
|
117
|
-
color: "#000000",
|
|
118
|
-
family: "aptos",
|
|
119
|
-
scheme: "aptos",
|
|
120
|
-
explorerViews: [],
|
|
121
|
-
managerAppName: "Aptos",
|
|
122
|
-
coinType: 637,
|
|
123
|
-
},
|
|
124
|
-
};
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it("get coin balance", async () => {
|
|
128
|
-
mockedAptos.mockImplementation(() => ({
|
|
129
|
-
view: jest.fn().mockReturnValue(["1234"]),
|
|
130
|
-
}));
|
|
131
|
-
|
|
132
|
-
token.tokenType = "coin";
|
|
133
|
-
const api = new AptosAPI("aptos");
|
|
134
|
-
const balance = await api.getBalance("address", token);
|
|
135
|
-
|
|
136
|
-
expect(balance).toEqual(new BigNumber(1234));
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it("get fungible assets balance", async () => {
|
|
140
|
-
mockedAptos.mockImplementation(() => ({
|
|
141
|
-
view: jest.fn().mockReturnValue(["12345"]),
|
|
142
|
-
}));
|
|
143
|
-
|
|
144
|
-
token.tokenType = "fungible_asset";
|
|
145
|
-
|
|
146
|
-
const api = new AptosAPI("aptos");
|
|
147
|
-
const balance = await api.getBalance("address", token);
|
|
148
|
-
|
|
149
|
-
expect(balance).toEqual(new BigNumber(12345));
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it("return 0 balace if could not retrieve proper balance of fungible assets", async () => {
|
|
153
|
-
mockedAptos.mockImplementation(() => ({
|
|
154
|
-
view: jest.fn().mockImplementation(() => {
|
|
155
|
-
throw new Error("error");
|
|
156
|
-
}),
|
|
157
|
-
}));
|
|
158
|
-
|
|
159
|
-
token.tokenType = "fungible_asset";
|
|
160
|
-
|
|
161
|
-
const api = new AptosAPI("aptos");
|
|
162
|
-
const balance = await api.getBalance("address", token);
|
|
163
|
-
|
|
164
|
-
expect(balance).toEqual(new BigNumber(0));
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
|
|
168
98
|
describe("getAccountInfo", () => {
|
|
169
99
|
it("calls getCoinBalance, fetchTransactions and getHeight", async () => {
|
|
170
100
|
mockedAptos.mockImplementation(() => ({
|
|
@@ -177,6 +107,12 @@ describe("Aptos API", () => {
|
|
|
177
107
|
block_height: "1",
|
|
178
108
|
block_hash: "83ca6d",
|
|
179
109
|
}),
|
|
110
|
+
getCurrentFungibleAssetBalances: jest.fn().mockResolvedValue([
|
|
111
|
+
{
|
|
112
|
+
asset_type: APTOS_ASSET_ID,
|
|
113
|
+
amount: new BigNumber(123),
|
|
114
|
+
},
|
|
115
|
+
]),
|
|
180
116
|
}));
|
|
181
117
|
|
|
182
118
|
mockedNetwork.mockResolvedValue(
|
|
@@ -307,6 +243,12 @@ describe("Aptos API", () => {
|
|
|
307
243
|
block_height: "1",
|
|
308
244
|
block_hash: "83ca6d",
|
|
309
245
|
}),
|
|
246
|
+
getCurrentFungibleAssetBalances: jest.fn().mockResolvedValue([
|
|
247
|
+
{
|
|
248
|
+
asset_type: APTOS_ASSET_ID,
|
|
249
|
+
amount: new BigNumber(123),
|
|
250
|
+
},
|
|
251
|
+
]),
|
|
310
252
|
}));
|
|
311
253
|
|
|
312
254
|
mockedNetwork.mockResolvedValue(
|
|
@@ -361,6 +303,12 @@ describe("Aptos API", () => {
|
|
|
361
303
|
block_height: "1",
|
|
362
304
|
block_hash: "83ca6d",
|
|
363
305
|
}),
|
|
306
|
+
getCurrentFungibleAssetBalances: jest.fn().mockResolvedValue([
|
|
307
|
+
{
|
|
308
|
+
asset_type: APTOS_ASSET_ID,
|
|
309
|
+
amount: new BigNumber(123),
|
|
310
|
+
},
|
|
311
|
+
]),
|
|
364
312
|
}));
|
|
365
313
|
|
|
366
314
|
mockedNetwork.mockResolvedValue(
|
|
@@ -398,8 +346,7 @@ describe("Aptos API", () => {
|
|
|
398
346
|
}));
|
|
399
347
|
|
|
400
348
|
const api = new AptosAPI("aptos");
|
|
401
|
-
const accountInfo = await api.getAccountInfo(
|
|
402
|
-
|
|
349
|
+
const accountInfo = await api.getAccountInfo(APTOS_ASSET_ID, "1");
|
|
403
350
|
expect(accountInfo.balance).toEqual(new BigNumber(123));
|
|
404
351
|
expect(accountInfo.transactions).toEqual([null]);
|
|
405
352
|
expect(accountInfo.blockHeight).toEqual(999);
|
|
@@ -612,19 +559,20 @@ describe("Aptos API", () => {
|
|
|
612
559
|
}));
|
|
613
560
|
|
|
614
561
|
const amount = BigInt(100);
|
|
615
|
-
const sender
|
|
562
|
+
const sender = {
|
|
616
563
|
xpub: "xpub",
|
|
617
564
|
freshAddress: "address1",
|
|
618
565
|
};
|
|
619
566
|
const recipient = "address2";
|
|
620
567
|
|
|
621
568
|
const api = new AptosAPI("aptos");
|
|
622
|
-
const transactionIntent: TransactionIntent<AptosAsset
|
|
569
|
+
const transactionIntent: TransactionIntent<AptosAsset> = {
|
|
623
570
|
asset: {
|
|
624
571
|
type: "native",
|
|
625
572
|
},
|
|
626
573
|
type: "send",
|
|
627
|
-
sender,
|
|
574
|
+
sender: sender.freshAddress,
|
|
575
|
+
senderPublicKey: sender.xpub,
|
|
628
576
|
amount,
|
|
629
577
|
recipient,
|
|
630
578
|
};
|
|
@@ -676,21 +624,22 @@ describe("Aptos API", () => {
|
|
|
676
624
|
}));
|
|
677
625
|
|
|
678
626
|
const amount = BigInt(100);
|
|
679
|
-
const sender
|
|
627
|
+
const sender = {
|
|
680
628
|
xpub: "xpub",
|
|
681
629
|
freshAddress: "address1",
|
|
682
630
|
};
|
|
683
631
|
const recipient = "address2";
|
|
684
632
|
|
|
685
633
|
const api = new AptosAPI("aptos");
|
|
686
|
-
const transactionIntent: TransactionIntent<AptosAsset
|
|
634
|
+
const transactionIntent: TransactionIntent<AptosAsset> = {
|
|
687
635
|
asset: {
|
|
688
636
|
type: "token",
|
|
689
637
|
standard: "coin",
|
|
690
638
|
contractAddress: "0x111",
|
|
691
639
|
},
|
|
692
640
|
type: "send",
|
|
693
|
-
sender,
|
|
641
|
+
sender: sender.freshAddress,
|
|
642
|
+
senderPublicKey: sender.xpub,
|
|
694
643
|
amount,
|
|
695
644
|
recipient,
|
|
696
645
|
};
|
|
@@ -741,21 +690,22 @@ describe("Aptos API", () => {
|
|
|
741
690
|
}));
|
|
742
691
|
|
|
743
692
|
const amount = BigInt(100);
|
|
744
|
-
const sender
|
|
693
|
+
const sender = {
|
|
745
694
|
xpub: "xpub",
|
|
746
695
|
freshAddress: "address1",
|
|
747
696
|
};
|
|
748
697
|
const recipient = "address2";
|
|
749
698
|
|
|
750
699
|
const api = new AptosAPI("aptos");
|
|
751
|
-
const transactionIntent: TransactionIntent<AptosAsset
|
|
700
|
+
const transactionIntent: TransactionIntent<AptosAsset> = {
|
|
752
701
|
asset: {
|
|
753
702
|
type: "token",
|
|
754
703
|
standard: "fungible_asset",
|
|
755
704
|
contractAddress: "0x111",
|
|
756
705
|
},
|
|
757
706
|
type: "send",
|
|
758
|
-
sender,
|
|
707
|
+
sender: sender.freshAddress,
|
|
708
|
+
senderPublicKey: sender.xpub,
|
|
759
709
|
amount,
|
|
760
710
|
recipient,
|
|
761
711
|
};
|
|
@@ -782,6 +732,10 @@ describe("Aptos API", () => {
|
|
|
782
732
|
|
|
783
733
|
describe("getBalances", () => {
|
|
784
734
|
it("returns an array of AptosBalances objects", async () => {
|
|
735
|
+
const expectedAptosBalace: AptosBalance = {
|
|
736
|
+
contractAddress: APTOS_ASSET_ID,
|
|
737
|
+
amount: BigNumber(200),
|
|
738
|
+
};
|
|
785
739
|
const assets = [{ asset_type: APTOS_ASSET_ID, amount: 200 }];
|
|
786
740
|
const mockGetCurrentFungibleAssetBalances = jest.fn().mockResolvedValue(assets);
|
|
787
741
|
mockedAptos.mockImplementation(() => ({
|
|
@@ -791,21 +745,61 @@ describe("Aptos API", () => {
|
|
|
791
745
|
const address = "0x42";
|
|
792
746
|
|
|
793
747
|
const api = new AptosAPI("aptos");
|
|
794
|
-
const balances = await api.getBalances(address);
|
|
748
|
+
const balances = await api.getBalances(address, APTOS_ASSET_ID);
|
|
795
749
|
|
|
796
750
|
expect(mockGetCurrentFungibleAssetBalances).toHaveBeenCalledWith({
|
|
797
751
|
options: {
|
|
798
|
-
offset: 0,
|
|
799
|
-
limit: 1000,
|
|
800
752
|
where: {
|
|
801
753
|
asset_type: { _eq: APTOS_ASSET_ID },
|
|
802
754
|
owner_address: { _eq: address },
|
|
803
755
|
},
|
|
804
756
|
},
|
|
805
757
|
});
|
|
806
|
-
expect(balances).
|
|
807
|
-
|
|
808
|
-
|
|
758
|
+
expect(balances).toEqual([expectedAptosBalace]);
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
it("returns an array of AptosBalances when just address ispassed", async () => {
|
|
762
|
+
const expectedAptosBalace: AptosBalance = {
|
|
763
|
+
contractAddress: APTOS_ASSET_ID,
|
|
764
|
+
amount: BigNumber(200),
|
|
765
|
+
};
|
|
766
|
+
const assets = [{ asset_type: APTOS_ASSET_ID, amount: 200n }];
|
|
767
|
+
const mockGetCurrentFungibleAssetBalances = jest.fn().mockResolvedValue(assets);
|
|
768
|
+
mockedAptos.mockImplementation(() => ({
|
|
769
|
+
getCurrentFungibleAssetBalances: mockGetCurrentFungibleAssetBalances,
|
|
770
|
+
}));
|
|
771
|
+
const address = "0x42";
|
|
772
|
+
const api = new AptosAPI("aptos");
|
|
773
|
+
|
|
774
|
+
const balance = await api.getBalances(address);
|
|
775
|
+
|
|
776
|
+
expect(mockGetCurrentFungibleAssetBalances).toHaveBeenCalledWith({
|
|
777
|
+
options: {
|
|
778
|
+
where: {
|
|
779
|
+
owner_address: { _eq: address },
|
|
780
|
+
},
|
|
781
|
+
},
|
|
782
|
+
});
|
|
783
|
+
expect(balance).toEqual([expectedAptosBalace]);
|
|
784
|
+
});
|
|
785
|
+
|
|
786
|
+
it("return 0 balace if could not retrieve proper balance of fungible assets", async () => {
|
|
787
|
+
const expectedBalance = new BigNumber(0);
|
|
788
|
+
mockedAptos.mockImplementation(() => ({
|
|
789
|
+
getCurrentFungibleAssetBalances: jest.fn().mockImplementation(() => {
|
|
790
|
+
throw new Error("error");
|
|
791
|
+
}),
|
|
792
|
+
}));
|
|
793
|
+
|
|
794
|
+
const api = new AptosAPI("aptos");
|
|
795
|
+
const balance = await api.getBalances("address", "0x42");
|
|
796
|
+
|
|
797
|
+
expect(balance).toEqual([
|
|
798
|
+
{
|
|
799
|
+
contractAddress: "",
|
|
800
|
+
amount: expectedBalance,
|
|
801
|
+
},
|
|
802
|
+
]);
|
|
809
803
|
});
|
|
810
804
|
});
|
|
811
805
|
|
package/src/api/index.ts
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AlpacaApi } from "@ledgerhq/coin-framework/api/index";
|
|
2
2
|
import type { AptosConfig as AptosConfigApi } from "../config";
|
|
3
3
|
import type { Balance, Pagination, TransactionIntent } from "@ledgerhq/coin-framework/api/types";
|
|
4
4
|
import coinConfig from "../config";
|
|
5
|
-
import type { AptosAsset
|
|
5
|
+
import type { AptosAsset } from "../types/assets";
|
|
6
6
|
import { AptosAPI } from "../network";
|
|
7
7
|
import { combine } from "../logic/combine";
|
|
8
8
|
import { craftTransaction } from "../logic/craftTransaction";
|
|
9
|
-
import {
|
|
9
|
+
import { getBalances } from "../logic/getBalances";
|
|
10
10
|
|
|
11
|
-
export function createApi(
|
|
12
|
-
config: AptosConfigApi,
|
|
13
|
-
): Api<AptosAsset, AptosExtra, AptosSender, AptosFeeParameters> {
|
|
11
|
+
export function createApi(config: AptosConfigApi): AlpacaApi<AptosAsset> {
|
|
14
12
|
coinConfig.setCoinConfig(() => ({ ...config, status: { type: "active" } }));
|
|
15
13
|
|
|
16
14
|
const client = new AptosAPI(config.aptosSettings);
|
|
@@ -20,9 +18,9 @@ export function createApi(
|
|
|
20
18
|
combine: (tx, signature, pubkey): string => combine(tx, signature, pubkey),
|
|
21
19
|
craftTransaction: (transactionIntent, _customFees): Promise<string> =>
|
|
22
20
|
craftTransaction(client, transactionIntent),
|
|
23
|
-
estimateFees: (transactionIntent: TransactionIntent<AptosAsset
|
|
21
|
+
estimateFees: (transactionIntent: TransactionIntent<AptosAsset>) =>
|
|
24
22
|
client.estimateFees(transactionIntent),
|
|
25
|
-
getBalance: (address): Promise<Balance<AptosAsset>[]> =>
|
|
23
|
+
getBalance: (address): Promise<Balance<AptosAsset>[]> => getBalances(client, address),
|
|
26
24
|
lastBlock: () => client.getLastBlock(),
|
|
27
25
|
listOperations: (address: string, pagination: Pagination) =>
|
|
28
26
|
client.listOperations(address, pagination),
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
encodeTokenAccountId,
|
|
14
14
|
} from "@ledgerhq/coin-framework/account/index";
|
|
15
15
|
import { AccountShapeInfo } from "@ledgerhq/coin-framework/bridge/jsHelpers";
|
|
16
|
+
import BigNumber from "bignumber.js";
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* List of properties of a sub account that can be updated when 2 "identical" accounts are found
|
|
@@ -70,7 +71,7 @@ export const mergeSubAccounts = (
|
|
|
70
71
|
mergeOps(
|
|
71
72
|
duplicatedAccount[name as keyof TokenAccount] as Operation[],
|
|
72
73
|
newSubAccount[name as keyof TokenAccount] as Operation[],
|
|
73
|
-
)
|
|
74
|
+
) ?? [];
|
|
74
75
|
}
|
|
75
76
|
}
|
|
76
77
|
|
|
@@ -100,7 +101,8 @@ export const getSubAccountShape = async (
|
|
|
100
101
|
): Promise<TokenAccount> => {
|
|
101
102
|
const aptosClient = new AptosAPI(currency.id);
|
|
102
103
|
const tokenAccountId = encodeTokenAccountId(parentId, token);
|
|
103
|
-
const
|
|
104
|
+
const balances = await aptosClient.getBalances(address, token.contractAddress);
|
|
105
|
+
const balance = balances.length > 0 ? balances[0].amount : BigNumber(0);
|
|
104
106
|
const firstOperation = operations
|
|
105
107
|
.sort((a, b) => b.date.getTime() - a.date.getTime())
|
|
106
108
|
.at(operations.length - 1);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TransactionIntent } from "@ledgerhq/coin-framework/lib/api/types";
|
|
2
|
-
import type { AptosAsset
|
|
2
|
+
import type { AptosAsset } from "../types/assets";
|
|
3
3
|
import type { Account, TokenAccount } from "@ledgerhq/types-live";
|
|
4
4
|
import type { AptosAPI } from "../network";
|
|
5
5
|
import buildTransaction, { isTokenType } from "./buildTransaction";
|
|
@@ -10,7 +10,7 @@ import type { AptosBalance } from "../types";
|
|
|
10
10
|
|
|
11
11
|
export async function craftTransaction(
|
|
12
12
|
aptosClient: AptosAPI,
|
|
13
|
-
transactionIntent: TransactionIntent<AptosAsset
|
|
13
|
+
transactionIntent: TransactionIntent<AptosAsset>,
|
|
14
14
|
): Promise<string> {
|
|
15
15
|
const newTx = createTransaction();
|
|
16
16
|
newTx.amount = BigNumber(transactionIntent.amount.toString());
|
|
@@ -19,8 +19,8 @@ export async function craftTransaction(
|
|
|
19
19
|
newTx.useAllAmount = transactionIntent.amount === BigInt(0);
|
|
20
20
|
|
|
21
21
|
const account = {
|
|
22
|
-
freshAddress: transactionIntent.sender
|
|
23
|
-
xpub: transactionIntent.
|
|
22
|
+
freshAddress: transactionIntent.sender,
|
|
23
|
+
xpub: transactionIntent.senderPublicKey,
|
|
24
24
|
subAccounts: new Array<TokenAccount>(),
|
|
25
25
|
} as Account;
|
|
26
26
|
|
|
@@ -29,7 +29,7 @@ export async function craftTransaction(
|
|
|
29
29
|
let balance: AptosBalance | undefined;
|
|
30
30
|
|
|
31
31
|
if (newTx.useAllAmount === true) {
|
|
32
|
-
const balances = await aptosClient.getBalances(transactionIntent.sender
|
|
32
|
+
const balances = await aptosClient.getBalances(transactionIntent.sender);
|
|
33
33
|
balance = balances?.find(
|
|
34
34
|
b => b.contractAddress.toLowerCase() === contractAddress?.toLowerCase(),
|
|
35
35
|
);
|
|
@@ -54,9 +54,7 @@ export async function craftTransaction(
|
|
|
54
54
|
return aptosTx.bcsToHex().toString();
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
function getContractAddress(
|
|
58
|
-
txIntent: TransactionIntent<AptosAsset, AptosExtra, AptosSender>,
|
|
59
|
-
): string {
|
|
57
|
+
function getContractAddress(txIntent: TransactionIntent<AptosAsset>): string {
|
|
60
58
|
if (txIntent.asset.type === "token" && isTokenType(txIntent.asset.standard)) {
|
|
61
59
|
return txIntent.asset.contractAddress;
|
|
62
60
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Balance } from "@ledgerhq/coin-framework/lib/api/types";
|
|
2
|
+
import type { AptosAsset } from "../types/assets";
|
|
3
|
+
import type { AptosAPI } from "../network";
|
|
4
|
+
import { APTOS_ASSET_ID, TOKEN_TYPE } from "../constants";
|
|
5
|
+
|
|
6
|
+
export async function getBalances(
|
|
7
|
+
aptosClient: AptosAPI,
|
|
8
|
+
address: string,
|
|
9
|
+
contract_address?: string,
|
|
10
|
+
): Promise<Balance<AptosAsset>[]> {
|
|
11
|
+
const balances = await aptosClient.getBalances(address, contract_address);
|
|
12
|
+
|
|
13
|
+
return balances.map(balance => {
|
|
14
|
+
const isNative = balance.contractAddress === APTOS_ASSET_ID;
|
|
15
|
+
const standard =
|
|
16
|
+
balance.contractAddress.split("::").length === 3
|
|
17
|
+
? TOKEN_TYPE.COIN
|
|
18
|
+
: TOKEN_TYPE.FUNGIBLE_ASSET;
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
value: BigInt(balance.amount.toString()),
|
|
22
|
+
asset: isNative
|
|
23
|
+
? { type: "native" }
|
|
24
|
+
: { type: "token", contractAddress: balance.contractAddress, standard: standard },
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
}
|