@ledgerhq/coin-aptos 2.2.0-nightly.3 → 2.2.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.
Files changed (97) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +25 -21
  3. package/lib/__tests__/api/craftTransaction.unit.test.js +31 -16
  4. package/lib/__tests__/api/craftTransaction.unit.test.js.map +1 -1
  5. package/lib/__tests__/api/getBalance.unit.test.js +28 -68
  6. package/lib/__tests__/api/getBalance.unit.test.js.map +1 -1
  7. package/lib/__tests__/api/index.integ.test.js +11 -30
  8. package/lib/__tests__/api/index.integ.test.js.map +1 -1
  9. package/lib/__tests__/api/index.test.js.map +1 -1
  10. package/lib/__tests__/bridge/synchronisation.test.js +6 -15
  11. package/lib/__tests__/bridge/synchronisation.test.js.map +1 -1
  12. package/lib/__tests__/network/client.test.js +66 -69
  13. package/lib/__tests__/network/client.test.js.map +1 -1
  14. package/lib/api/index.d.ts +3 -3
  15. package/lib/api/index.d.ts.map +1 -1
  16. package/lib/api/index.js +2 -2
  17. package/lib/api/index.js.map +1 -1
  18. package/lib/bridge/synchronisation.d.ts.map +1 -1
  19. package/lib/bridge/synchronisation.js +2 -7
  20. package/lib/bridge/synchronisation.js.map +1 -1
  21. package/lib/logic/craftTransaction.d.ts +2 -2
  22. package/lib/logic/craftTransaction.d.ts.map +1 -1
  23. package/lib/logic/craftTransaction.js +3 -3
  24. package/lib/logic/craftTransaction.js.map +1 -1
  25. package/lib/logic/getBalance.d.ts +5 -0
  26. package/lib/logic/getBalance.d.ts.map +1 -0
  27. package/lib/logic/getBalance.js +12 -0
  28. package/lib/logic/getBalance.js.map +1 -0
  29. package/lib/network/client.d.ts +7 -3
  30. package/lib/network/client.d.ts.map +1 -1
  31. package/lib/network/client.js +62 -28
  32. package/lib/network/client.js.map +1 -1
  33. package/lib/types/assets.d.ts +4 -0
  34. package/lib/types/assets.d.ts.map +1 -1
  35. package/lib-es/__tests__/api/craftTransaction.unit.test.js +31 -16
  36. package/lib-es/__tests__/api/craftTransaction.unit.test.js.map +1 -1
  37. package/lib-es/__tests__/api/getBalance.unit.test.js +26 -69
  38. package/lib-es/__tests__/api/getBalance.unit.test.js.map +1 -1
  39. package/lib-es/__tests__/api/index.integ.test.js +11 -30
  40. package/lib-es/__tests__/api/index.integ.test.js.map +1 -1
  41. package/lib-es/__tests__/api/index.test.js.map +1 -1
  42. package/lib-es/__tests__/bridge/synchronisation.test.js +6 -15
  43. package/lib-es/__tests__/bridge/synchronisation.test.js.map +1 -1
  44. package/lib-es/__tests__/network/client.test.js +66 -69
  45. package/lib-es/__tests__/network/client.test.js.map +1 -1
  46. package/lib-es/api/index.d.ts +3 -3
  47. package/lib-es/api/index.d.ts.map +1 -1
  48. package/lib-es/api/index.js +2 -2
  49. package/lib-es/api/index.js.map +1 -1
  50. package/lib-es/bridge/synchronisation.d.ts.map +1 -1
  51. package/lib-es/bridge/synchronisation.js +2 -4
  52. package/lib-es/bridge/synchronisation.js.map +1 -1
  53. package/lib-es/logic/craftTransaction.d.ts +2 -2
  54. package/lib-es/logic/craftTransaction.d.ts.map +1 -1
  55. package/lib-es/logic/craftTransaction.js +3 -3
  56. package/lib-es/logic/craftTransaction.js.map +1 -1
  57. package/lib-es/logic/getBalance.d.ts +5 -0
  58. package/lib-es/logic/getBalance.d.ts.map +1 -0
  59. package/lib-es/logic/getBalance.js +8 -0
  60. package/lib-es/logic/getBalance.js.map +1 -0
  61. package/lib-es/network/client.d.ts +7 -3
  62. package/lib-es/network/client.d.ts.map +1 -1
  63. package/lib-es/network/client.js +62 -28
  64. package/lib-es/network/client.js.map +1 -1
  65. package/lib-es/types/assets.d.ts +4 -0
  66. package/lib-es/types/assets.d.ts.map +1 -1
  67. package/package.json +8 -8
  68. package/src/__tests__/api/craftTransaction.unit.test.ts +38 -23
  69. package/src/__tests__/api/getBalance.unit.test.ts +28 -82
  70. package/src/__tests__/api/index.integ.test.ts +15 -36
  71. package/src/__tests__/api/index.test.ts +6 -4
  72. package/src/__tests__/bridge/synchronisation.test.ts +6 -21
  73. package/src/__tests__/network/client.test.ts +89 -83
  74. package/src/api/index.ts +8 -6
  75. package/src/bridge/synchronisation.ts +2 -4
  76. package/src/logic/craftTransaction.ts +8 -6
  77. package/src/logic/getBalance.ts +15 -0
  78. package/src/network/client.ts +74 -31
  79. package/src/types/assets.ts +5 -0
  80. package/lib/__tests__/logic/getBalances.test.d.ts +0 -2
  81. package/lib/__tests__/logic/getBalances.test.d.ts.map +0 -1
  82. package/lib/__tests__/logic/getBalances.test.js +0 -63
  83. package/lib/__tests__/logic/getBalances.test.js.map +0 -1
  84. package/lib/logic/getBalances.d.ts +0 -5
  85. package/lib/logic/getBalances.d.ts.map +0 -1
  86. package/lib/logic/getBalances.js +0 -21
  87. package/lib/logic/getBalances.js.map +0 -1
  88. package/lib-es/__tests__/logic/getBalances.test.d.ts +0 -2
  89. package/lib-es/__tests__/logic/getBalances.test.d.ts.map +0 -1
  90. package/lib-es/__tests__/logic/getBalances.test.js +0 -61
  91. package/lib-es/__tests__/logic/getBalances.test.js.map +0 -1
  92. package/lib-es/logic/getBalances.d.ts +0 -5
  93. package/lib-es/logic/getBalances.d.ts.map +0 -1
  94. package/lib-es/logic/getBalances.js +0 -17
  95. package/lib-es/logic/getBalances.js.map +0 -1
  96. package/src/__tests__/logic/getBalances.test.ts +0 -80
  97. package/src/logic/getBalances.ts +0 -27
@@ -14,10 +14,11 @@ 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 } from "../../types/assets";
17
+ import { AptosAsset, AptosExtra, AptosSender } from "../../types/assets";
18
18
  import { Pagination, TransactionIntent } from "@ledgerhq/coin-framework/api/types";
19
+ import { TokenCurrency } from "@ledgerhq/types-cryptoassets";
19
20
  import { APTOS_ASSET_ID } from "../../constants";
20
- import { AptosBalance, AptosTransaction } from "../../types";
21
+ import { AptosTransaction } from "../../types";
21
22
 
22
23
  jest.mock("@aptos-labs/ts-sdk");
23
24
  jest.mock("@apollo/client");
@@ -95,6 +96,75 @@ describe("Aptos API", () => {
95
96
  });
96
97
  });
97
98
 
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
+
98
168
  describe("getAccountInfo", () => {
99
169
  it("calls getCoinBalance, fetchTransactions and getHeight", async () => {
100
170
  mockedAptos.mockImplementation(() => ({
@@ -107,12 +177,6 @@ describe("Aptos API", () => {
107
177
  block_height: "1",
108
178
  block_hash: "83ca6d",
109
179
  }),
110
- getCurrentFungibleAssetBalances: jest.fn().mockResolvedValue([
111
- {
112
- asset_type: APTOS_ASSET_ID,
113
- amount: new BigNumber(123),
114
- },
115
- ]),
116
180
  }));
117
181
 
118
182
  mockedNetwork.mockResolvedValue(
@@ -243,12 +307,6 @@ describe("Aptos API", () => {
243
307
  block_height: "1",
244
308
  block_hash: "83ca6d",
245
309
  }),
246
- getCurrentFungibleAssetBalances: jest.fn().mockResolvedValue([
247
- {
248
- asset_type: APTOS_ASSET_ID,
249
- amount: new BigNumber(123),
250
- },
251
- ]),
252
310
  }));
253
311
 
254
312
  mockedNetwork.mockResolvedValue(
@@ -303,12 +361,6 @@ describe("Aptos API", () => {
303
361
  block_height: "1",
304
362
  block_hash: "83ca6d",
305
363
  }),
306
- getCurrentFungibleAssetBalances: jest.fn().mockResolvedValue([
307
- {
308
- asset_type: APTOS_ASSET_ID,
309
- amount: new BigNumber(123),
310
- },
311
- ]),
312
364
  }));
313
365
 
314
366
  mockedNetwork.mockResolvedValue(
@@ -346,7 +398,8 @@ describe("Aptos API", () => {
346
398
  }));
347
399
 
348
400
  const api = new AptosAPI("aptos");
349
- const accountInfo = await api.getAccountInfo(APTOS_ASSET_ID, "1");
401
+ const accountInfo = await api.getAccountInfo("APTOS_1_ADDRESS", "1");
402
+
350
403
  expect(accountInfo.balance).toEqual(new BigNumber(123));
351
404
  expect(accountInfo.transactions).toEqual([null]);
352
405
  expect(accountInfo.blockHeight).toEqual(999);
@@ -559,20 +612,19 @@ describe("Aptos API", () => {
559
612
  }));
560
613
 
561
614
  const amount = BigInt(100);
562
- const sender = {
615
+ const sender: AptosSender = {
563
616
  xpub: "xpub",
564
617
  freshAddress: "address1",
565
618
  };
566
619
  const recipient = "address2";
567
620
 
568
621
  const api = new AptosAPI("aptos");
569
- const transactionIntent: TransactionIntent<AptosAsset> = {
622
+ const transactionIntent: TransactionIntent<AptosAsset, AptosExtra, AptosSender> = {
570
623
  asset: {
571
624
  type: "native",
572
625
  },
573
626
  type: "send",
574
- sender: sender.freshAddress,
575
- senderPublicKey: sender.xpub,
627
+ sender,
576
628
  amount,
577
629
  recipient,
578
630
  };
@@ -624,22 +676,21 @@ describe("Aptos API", () => {
624
676
  }));
625
677
 
626
678
  const amount = BigInt(100);
627
- const sender = {
679
+ const sender: AptosSender = {
628
680
  xpub: "xpub",
629
681
  freshAddress: "address1",
630
682
  };
631
683
  const recipient = "address2";
632
684
 
633
685
  const api = new AptosAPI("aptos");
634
- const transactionIntent: TransactionIntent<AptosAsset> = {
686
+ const transactionIntent: TransactionIntent<AptosAsset, AptosExtra, AptosSender> = {
635
687
  asset: {
636
688
  type: "token",
637
689
  standard: "coin",
638
690
  contractAddress: "0x111",
639
691
  },
640
692
  type: "send",
641
- sender: sender.freshAddress,
642
- senderPublicKey: sender.xpub,
693
+ sender,
643
694
  amount,
644
695
  recipient,
645
696
  };
@@ -690,22 +741,21 @@ describe("Aptos API", () => {
690
741
  }));
691
742
 
692
743
  const amount = BigInt(100);
693
- const sender = {
744
+ const sender: AptosSender = {
694
745
  xpub: "xpub",
695
746
  freshAddress: "address1",
696
747
  };
697
748
  const recipient = "address2";
698
749
 
699
750
  const api = new AptosAPI("aptos");
700
- const transactionIntent: TransactionIntent<AptosAsset> = {
751
+ const transactionIntent: TransactionIntent<AptosAsset, AptosExtra, AptosSender> = {
701
752
  asset: {
702
753
  type: "token",
703
754
  standard: "fungible_asset",
704
755
  contractAddress: "0x111",
705
756
  },
706
757
  type: "send",
707
- sender: sender.freshAddress,
708
- senderPublicKey: sender.xpub,
758
+ sender,
709
759
  amount,
710
760
  recipient,
711
761
  };
@@ -732,10 +782,6 @@ describe("Aptos API", () => {
732
782
 
733
783
  describe("getBalances", () => {
734
784
  it("returns an array of AptosBalances objects", async () => {
735
- const expectedAptosBalace: AptosBalance = {
736
- contractAddress: APTOS_ASSET_ID,
737
- amount: BigNumber(200),
738
- };
739
785
  const assets = [{ asset_type: APTOS_ASSET_ID, amount: 200 }];
740
786
  const mockGetCurrentFungibleAssetBalances = jest.fn().mockResolvedValue(assets);
741
787
  mockedAptos.mockImplementation(() => ({
@@ -745,61 +791,21 @@ describe("Aptos API", () => {
745
791
  const address = "0x42";
746
792
 
747
793
  const api = new AptosAPI("aptos");
748
- const balances = await api.getBalances(address, APTOS_ASSET_ID);
794
+ const balances = await api.getBalances(address);
749
795
 
750
796
  expect(mockGetCurrentFungibleAssetBalances).toHaveBeenCalledWith({
751
797
  options: {
798
+ offset: 0,
799
+ limit: 1000,
752
800
  where: {
753
801
  asset_type: { _eq: APTOS_ASSET_ID },
754
802
  owner_address: { _eq: address },
755
803
  },
756
804
  },
757
805
  });
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
- ]);
806
+ expect(balances).toHaveLength(1);
807
+ expect(balances[0].contractAddress).toBe(assets[0].asset_type);
808
+ expect(balances[0].amount).toStrictEqual(BigNumber(assets[0].amount));
803
809
  });
804
810
  });
805
811
 
package/src/api/index.ts CHANGED
@@ -1,14 +1,16 @@
1
- import type { AlpacaApi } from "@ledgerhq/coin-framework/api/index";
1
+ import type { Api } 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 } from "../types/assets";
5
+ import type { AptosAsset, AptosExtra, AptosFeeParameters, AptosSender } 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 { getBalances } from "../logic/getBalances";
9
+ import { getBalance } from "../logic/getBalance";
10
10
 
11
- export function createApi(config: AptosConfigApi): AlpacaApi<AptosAsset> {
11
+ export function createApi(
12
+ config: AptosConfigApi,
13
+ ): Api<AptosAsset, AptosExtra, AptosSender, AptosFeeParameters> {
12
14
  coinConfig.setCoinConfig(() => ({ ...config, status: { type: "active" } }));
13
15
 
14
16
  const client = new AptosAPI(config.aptosSettings);
@@ -18,9 +20,9 @@ export function createApi(config: AptosConfigApi): AlpacaApi<AptosAsset> {
18
20
  combine: (tx, signature, pubkey): string => combine(tx, signature, pubkey),
19
21
  craftTransaction: (transactionIntent, _customFees): Promise<string> =>
20
22
  craftTransaction(client, transactionIntent),
21
- estimateFees: (transactionIntent: TransactionIntent<AptosAsset>) =>
23
+ estimateFees: (transactionIntent: TransactionIntent<AptosAsset, AptosExtra, AptosSender>) =>
22
24
  client.estimateFees(transactionIntent),
23
- getBalance: (address): Promise<Balance<AptosAsset>[]> => getBalances(client, address),
25
+ getBalance: (address): Promise<Balance<AptosAsset>[]> => getBalance(client, address),
24
26
  lastBlock: () => client.getLastBlock(),
25
27
  listOperations: (address: string, pagination: Pagination) =>
26
28
  client.listOperations(address, pagination),
@@ -13,7 +13,6 @@ 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";
17
16
 
18
17
  /**
19
18
  * List of properties of a sub account that can be updated when 2 "identical" accounts are found
@@ -71,7 +70,7 @@ export const mergeSubAccounts = (
71
70
  mergeOps(
72
71
  duplicatedAccount[name as keyof TokenAccount] as Operation[],
73
72
  newSubAccount[name as keyof TokenAccount] as Operation[],
74
- ) ?? [];
73
+ ) || [];
75
74
  }
76
75
  }
77
76
 
@@ -101,8 +100,7 @@ export const getSubAccountShape = async (
101
100
  ): Promise<TokenAccount> => {
102
101
  const aptosClient = new AptosAPI(currency.id);
103
102
  const tokenAccountId = encodeTokenAccountId(parentId, token);
104
- const balances = await aptosClient.getBalances(address, token.contractAddress);
105
- const balance = balances.length > 0 ? balances[0].amount : BigNumber(0);
103
+ const balance = await aptosClient.getBalance(address, token);
106
104
  const firstOperation = operations
107
105
  .sort((a, b) => b.date.getTime() - a.date.getTime())
108
106
  .at(operations.length - 1);
@@ -1,5 +1,5 @@
1
1
  import type { TransactionIntent } from "@ledgerhq/coin-framework/lib/api/types";
2
- import type { AptosAsset } from "../types/assets";
2
+ import type { AptosAsset, AptosExtra, AptosSender } 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, AptosExtra, AptosSender>,
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.senderPublicKey,
22
+ freshAddress: transactionIntent.sender.freshAddress,
23
+ xpub: transactionIntent.sender.xpub,
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.freshAddress);
33
33
  balance = balances?.find(
34
34
  b => b.contractAddress.toLowerCase() === contractAddress?.toLowerCase(),
35
35
  );
@@ -54,7 +54,9 @@ export async function craftTransaction(
54
54
  return aptosTx.bcsToHex().toString();
55
55
  }
56
56
 
57
- function getContractAddress(txIntent: TransactionIntent<AptosAsset>): string {
57
+ function getContractAddress(
58
+ txIntent: TransactionIntent<AptosAsset, AptosExtra, AptosSender>,
59
+ ): string {
58
60
  if (txIntent.asset.type === "token" && isTokenType(txIntent.asset.standard)) {
59
61
  return txIntent.asset.contractAddress;
60
62
  }
@@ -0,0 +1,15 @@
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
+
5
+ export async function getBalance(
6
+ aptosClient: AptosAPI,
7
+ address: string,
8
+ ): Promise<Balance<AptosAsset>[]> {
9
+ const balance = await aptosClient.getBalances(address);
10
+
11
+ return balance.map(x => ({
12
+ value: BigInt(x.amount.toString()),
13
+ asset: { type: "native" },
14
+ }));
15
+ }
@@ -36,6 +36,7 @@ import type {
36
36
  GetAccountTransactionsDataQuery,
37
37
  GetAccountTransactionsDataGtQueryVariables,
38
38
  } from "./graphql/types";
39
+ import { TokenCurrency } from "@ledgerhq/types-cryptoassets";
39
40
  import {
40
41
  BlockInfo,
41
42
  FeeEstimation,
@@ -43,7 +44,7 @@ import {
43
44
  Pagination,
44
45
  TransactionIntent,
45
46
  } from "@ledgerhq/coin-framework/api/types";
46
- import { AptosAsset } from "../types/assets";
47
+ import { AptosAsset, AptosExtra, AptosFeeParameters, AptosSender } from "../types/assets";
47
48
  import { log } from "@ledgerhq/logs";
48
49
  import { transactionsToOperations } from "../logic/transactionsToOperations";
49
50
  import { isTestnet } from "../logic/isTestnet";
@@ -87,12 +88,13 @@ export class AptosAPI {
87
88
 
88
89
  async getAccountInfo(address: string, startAt?: string) {
89
90
  const [balance, transactions, blockHeight] = await Promise.all([
90
- this.getBalances(address, APTOS_ASSET_ID),
91
+ this.getCoinBalance(address, APTOS_ASSET_ID),
91
92
  this.fetchTransactions(address, startAt),
92
93
  this.getHeight(),
93
94
  ]);
95
+
94
96
  return {
95
- balance: balance[0].amount ?? BigInt(0),
97
+ balance,
96
98
  transactions,
97
99
  blockHeight,
98
100
  };
@@ -165,6 +167,14 @@ export class AptosAPI {
165
167
  return pendingTx.data.hash;
166
168
  }
167
169
 
170
+ async getBalance(address: string, token: TokenCurrency): Promise<BigNumber> {
171
+ if (token.tokenType === "coin") {
172
+ return await this.getCoinBalance(address, token.contractAddress);
173
+ }
174
+
175
+ return await this.getFABalance(address, token.contractAddress);
176
+ }
177
+
168
178
  async getLastBlock(): Promise<BlockInfo> {
169
179
  const { block_height } = await this.aptosClient.getLedgerInfo();
170
180
  const block = await this.aptosClient.getBlockByHeight({ blockHeight: Number(block_height) });
@@ -175,8 +185,48 @@ export class AptosAPI {
175
185
  };
176
186
  }
177
187
 
178
- async estimateFees(transactionIntent: TransactionIntent<AptosAsset>): Promise<FeeEstimation> {
179
- const publicKeyEd = new Ed25519PublicKey(transactionIntent?.senderPublicKey ?? "");
188
+ async getCoinBalance(address: string, contract_address: string): Promise<BigNumber> {
189
+ try {
190
+ const [balanceStr] = await this.aptosClient.view<[string]>({
191
+ payload: {
192
+ function: "0x1::coin::balance",
193
+ typeArguments: [contract_address],
194
+ functionArguments: [address],
195
+ },
196
+ });
197
+ const balance = parseInt(balanceStr, 10);
198
+ return new BigNumber(balance);
199
+ } catch (error) {
200
+ log("error", "getCoinBalance", {
201
+ error,
202
+ });
203
+ return new BigNumber(0);
204
+ }
205
+ }
206
+
207
+ async getFABalance(address: string, contract_address: string): Promise<BigNumber> {
208
+ try {
209
+ const [balanceStr] = await this.aptosClient.view<[string]>({
210
+ payload: {
211
+ function: "0x1::primary_fungible_store::balance",
212
+ typeArguments: ["0x1::object::ObjectCore"],
213
+ functionArguments: [address, contract_address],
214
+ },
215
+ });
216
+ const balance = parseInt(balanceStr, 10);
217
+ return new BigNumber(balance);
218
+ } catch (error) {
219
+ log("error", "getFABalance", {
220
+ error,
221
+ });
222
+ return new BigNumber(0);
223
+ }
224
+ }
225
+
226
+ async estimateFees(
227
+ transactionIntent: TransactionIntent<AptosAsset, AptosExtra, AptosSender>,
228
+ ): Promise<FeeEstimation<AptosFeeParameters>> {
229
+ const publicKeyEd = new Ed25519PublicKey(transactionIntent.sender.xpub);
180
230
 
181
231
  const txPayload: InputEntryFunctionData = {
182
232
  function: "0x1::aptos_account::transfer_coins",
@@ -206,7 +256,11 @@ export class AptosAPI {
206
256
  gasUnitPrice: DEFAULT_GAS_PRICE.toString(),
207
257
  };
208
258
 
209
- const tx = await this.generateTransaction(transactionIntent.sender, txPayload, txOptions);
259
+ const tx = await this.generateTransaction(
260
+ transactionIntent.sender.freshAddress,
261
+ txPayload,
262
+ txOptions,
263
+ );
210
264
 
211
265
  const simulation = await this.simulateTransaction(publicKeyEd, tx);
212
266
  const completedTx = simulation[0];
@@ -219,7 +273,6 @@ export class AptosAPI {
219
273
  return {
220
274
  value: BigInt(expectedGas.toString()),
221
275
  parameters: {
222
- storageLimit: BigInt(0),
223
276
  gasLimit: BigInt(gasLimit.toString()),
224
277
  gasPrice: BigInt(gasPrice.toString()),
225
278
  },
@@ -301,31 +354,21 @@ export class AptosAPI {
301
354
  };
302
355
  }
303
356
 
304
- async getBalances(address: string, contractAddress?: string): Promise<AptosBalance[]> {
305
- try {
306
- const whereCondition: any = {
307
- owner_address: { _eq: address },
308
- };
309
-
310
- if (contractAddress !== undefined && contractAddress !== "") {
311
- whereCondition.asset_type = { _eq: contractAddress };
312
- }
313
-
314
- const response = await this.aptosClient.getCurrentFungibleAssetBalances({
315
- options: {
316
- where: whereCondition,
357
+ async getBalances(address: string): Promise<AptosBalance[]> {
358
+ const response = await this.aptosClient.getCurrentFungibleAssetBalances({
359
+ options: {
360
+ offset: 0,
361
+ limit: 1000,
362
+ where: {
363
+ asset_type: { _eq: APTOS_ASSET_ID }, // to return all asset balances (native / token) we should remove this filter
364
+ owner_address: { _eq: address },
317
365
  },
318
- });
366
+ },
367
+ });
319
368
 
320
- return response.map(x => ({
321
- contractAddress: x.asset_type ?? "",
322
- amount: BigNumber(x.amount),
323
- }));
324
- } catch (error) {
325
- log("error", "getCoinBalance", {
326
- error,
327
- });
328
- return [{ amount: BigNumber(0), contractAddress: "" }];
329
- }
369
+ return response.map(x => ({
370
+ contractAddress: x.asset_type ?? "-",
371
+ amount: BigNumber(x.amount),
372
+ }));
330
373
  }
331
374
  }
@@ -9,6 +9,11 @@ export type AptosTokenInformation = {
9
9
 
10
10
  export type AptosExtra = Record<string, unknown>;
11
11
 
12
+ export type AptosSender = {
13
+ xpub: string;
14
+ freshAddress: string;
15
+ };
16
+
12
17
  export type AptosFeeParameters = {
13
18
  gasLimit: bigint;
14
19
  gasPrice: bigint;
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=getBalances.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getBalances.test.d.ts","sourceRoot":"","sources":["../../../src/__tests__/logic/getBalances.test.ts"],"names":[],"mappings":""}
@@ -1,63 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const getBalances_1 = require("../../logic/getBalances");
4
- const constants_1 = require("../../constants");
5
- describe("getBalance", () => {
6
- it("should return balance with value 10", async () => {
7
- const mockAptosClient = {
8
- getBalances: jest.fn().mockResolvedValue([{ contractAddress: constants_1.APTOS_ASSET_ID, amount: 10n }]),
9
- };
10
- const accountAddress = "0x4be47904b31063d60ac0dfde06e5dc203e647edbe853bae0e666ae5a763c3906";
11
- const balances = await (0, getBalances_1.getBalances)(mockAptosClient, accountAddress);
12
- expect(balances).toStrictEqual([{ value: 10n, asset: { type: "native" } }]);
13
- });
14
- it("should return empty array when no contract_address and no data", async () => {
15
- const mockAptosClient = {
16
- getBalances: jest.fn().mockResolvedValue([]),
17
- };
18
- const accountAddress = "0x4be47904b31063d60ac0dfde06e5dc203e647edbe853bae0e666ae5a763c3906";
19
- const balances = await (0, getBalances_1.getBalances)(mockAptosClient, accountAddress);
20
- expect(balances).toStrictEqual([]);
21
- });
22
- it("should return balance with 'native' contract_address (APTOS_ASSET_ID)", async () => {
23
- const mockAptosClient = {
24
- getBalances: jest.fn().mockResolvedValue([{ contractAddress: constants_1.APTOS_ASSET_ID, amount: 10n }]),
25
- };
26
- const accountAddress = "0x4be47904b31063d60ac0dfde06e5dc203e647edbe853bae0e666ae5a763c3906";
27
- const balances = await (0, getBalances_1.getBalances)(mockAptosClient, accountAddress);
28
- expect(balances).toStrictEqual([{ value: 10n, asset: { type: "native" } }]);
29
- });
30
- it("should return token balance when contract_address is a coin token", async () => {
31
- const TOKEN_ASSET_ID = "0x1::my_token::Token";
32
- const mockAptosClient = {
33
- getBalances: jest.fn().mockResolvedValue([{ contractAddress: TOKEN_ASSET_ID, amount: 25n }]),
34
- };
35
- const accountAddress = "0x4be47904b31063d60ac0dfde06e5dc203e647edbe853bae0e666ae5a763c3906";
36
- const balances = await (0, getBalances_1.getBalances)(mockAptosClient, accountAddress);
37
- expect(balances).toStrictEqual([
38
- {
39
- value: 25n,
40
- asset: { type: "token", contractAddress: TOKEN_ASSET_ID, standard: constants_1.TOKEN_TYPE.COIN },
41
- },
42
- ]);
43
- });
44
- it("should return token balance when contract_address is a fungible_asset token", async () => {
45
- const TOKEN_ASSET_ID = "0x1";
46
- const mockAptosClient = {
47
- getBalances: jest.fn().mockResolvedValue([{ contractAddress: TOKEN_ASSET_ID, amount: 25n }]),
48
- };
49
- const accountAddress = "0x4be47904b31063d60ac0dfde06e5dc203e647edbe853bae0e666ae5a763c3906";
50
- const balances = await (0, getBalances_1.getBalances)(mockAptosClient, accountAddress);
51
- expect(balances).toStrictEqual([
52
- {
53
- value: 25n,
54
- asset: {
55
- type: "token",
56
- contractAddress: TOKEN_ASSET_ID,
57
- standard: constants_1.TOKEN_TYPE.FUNGIBLE_ASSET,
58
- },
59
- },
60
- ]);
61
- });
62
- });
63
- //# sourceMappingURL=getBalances.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getBalances.test.js","sourceRoot":"","sources":["../../../src/__tests__/logic/getBalances.test.ts"],"names":[],"mappings":";;AAAA,yDAAsD;AACtD,+CAA6D;AAG7D,QAAQ,CAAC,YAAY,EAAE,GAAG,EAAE;IAC1B,EAAE,CAAC,qCAAqC,EAAE,KAAK,IAAI,EAAE;QACnD,MAAM,eAAe,GAAG;YACtB,WAAW,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,iBAAiB,CAAC,CAAC,EAAE,eAAe,EAAE,0BAAc,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;SAC5D,CAAC;QAEnC,MAAM,cAAc,GAAG,oEAAoE,CAAC;QAE5F,MAAM,QAAQ,GAAG,MAAM,IAAA,yBAAW,EAAC,eAAe,EA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{ AptosAPI } from "../network";
4
- export declare function getBalances(aptosClient: AptosAPI, address: string, contract_address?: string): Promise<Balance<AptosAsset>[]>;
5
- //# sourceMappingURL=getBalances.d.ts.map