@ledgerhq/coin-canton 0.5.1-nightly.2 → 0.5.1-nightly.4
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 +25 -0
- package/lib/bridge/prepareTransaction.d.ts.map +1 -1
- package/lib/bridge/prepareTransaction.js +2 -3
- package/lib/bridge/prepareTransaction.js.map +1 -1
- package/lib/common-logic/transaction/estimateFees.d.ts +1 -1
- package/lib/common-logic/transaction/estimateFees.d.ts.map +1 -1
- package/lib/common-logic/transaction/estimateFees.js +35 -3
- package/lib/common-logic/transaction/estimateFees.js.map +1 -1
- package/lib/common-logic/transaction/estimateFees.test.d.ts +2 -0
- package/lib/common-logic/transaction/estimateFees.test.d.ts.map +1 -0
- package/lib/common-logic/transaction/estimateFees.test.js +72 -0
- package/lib/common-logic/transaction/estimateFees.test.js.map +1 -0
- package/lib-es/bridge/prepareTransaction.d.ts.map +1 -1
- package/lib-es/bridge/prepareTransaction.js +2 -3
- package/lib-es/bridge/prepareTransaction.js.map +1 -1
- package/lib-es/common-logic/transaction/estimateFees.d.ts +1 -1
- package/lib-es/common-logic/transaction/estimateFees.d.ts.map +1 -1
- package/lib-es/common-logic/transaction/estimateFees.js +35 -3
- package/lib-es/common-logic/transaction/estimateFees.js.map +1 -1
- package/lib-es/common-logic/transaction/estimateFees.test.d.ts +2 -0
- package/lib-es/common-logic/transaction/estimateFees.test.d.ts.map +1 -0
- package/lib-es/common-logic/transaction/estimateFees.test.js +37 -0
- package/lib-es/common-logic/transaction/estimateFees.test.js.map +1 -0
- package/package.json +10 -10
- package/src/bridge/prepareTransaction.ts +2 -3
- package/src/common-logic/transaction/estimateFees.test.ts +50 -0
- package/src/common-logic/transaction/estimateFees.ts +48 -4
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
|
|
2
|
-
> @ledgerhq/coin-canton@0.5.1-nightly.
|
|
2
|
+
> @ledgerhq/coin-canton@0.5.1-nightly.3 build /home/runner/work/ledger-live/ledger-live/libs/coin-modules/coin-canton
|
|
3
3
|
> tsc && tsc -m esnext --moduleResolution bundler --outDir lib-es
|
|
4
4
|
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
# @ledgerhq/coin-canton
|
|
2
2
|
|
|
3
|
+
## 0.5.1-nightly.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`a37c06f`](https://github.com/LedgerHQ/ledger-live/commit/a37c06f97b061a5db0dad3632bb5c3e8f293677c)]:
|
|
8
|
+
- @ledgerhq/types-live@6.85.0-nightly.4
|
|
9
|
+
- @ledgerhq/coin-framework@6.4.1-nightly.4
|
|
10
|
+
|
|
11
|
+
## 0.5.1-nightly.3
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- [#11956](https://github.com/LedgerHQ/ledger-live/pull/11956) [`2abaa52`](https://github.com/LedgerHQ/ledger-live/commit/2abaa522471e8247d274c4b22cb4e297969ebe9a) Thanks [@hedi-edelbloute](https://github.com/hedi-edelbloute)! - Update coin-canton coverage command
|
|
16
|
+
|
|
17
|
+
- [#11923](https://github.com/LedgerHQ/ledger-live/pull/11923) [`49a8534`](https://github.com/LedgerHQ/ledger-live/commit/49a85340a9c4e5dd8dd07db87028fe48f307d1f6) Thanks [@hedi-edelbloute](https://github.com/hedi-edelbloute)! - Canton accurate fees
|
|
18
|
+
|
|
19
|
+
- Updated dependencies [[`ab0e1bc`](https://github.com/LedgerHQ/ledger-live/commit/ab0e1bcc97b66b750b6c29e618eb03ce6f25bb7b), [`c2d8d86`](https://github.com/LedgerHQ/ledger-live/commit/c2d8d8670f848989836c46ea08ae88c88086fdd6), [`f015ef3`](https://github.com/LedgerHQ/ledger-live/commit/f015ef32660905d00f55a45f451f38bc12aec9ba)]:
|
|
20
|
+
- @ledgerhq/cryptoassets@13.29.0-nightly.3
|
|
21
|
+
- @ledgerhq/errors@6.26.0-nightly.0
|
|
22
|
+
- @ledgerhq/live-env@2.17.0-nightly.2
|
|
23
|
+
- @ledgerhq/types-live@6.85.0-nightly.3
|
|
24
|
+
- @ledgerhq/coin-framework@6.4.1-nightly.3
|
|
25
|
+
- @ledgerhq/devices@8.5.2-nightly.1
|
|
26
|
+
- @ledgerhq/live-network@2.0.18-nightly.2
|
|
27
|
+
|
|
3
28
|
## 0.5.1-nightly.2
|
|
4
29
|
|
|
5
30
|
### Patch Changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prepareTransaction.d.ts","sourceRoot":"","sources":["../../src/bridge/prepareTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAKvC,eAAO,MAAM,kBAAkB,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC,oBAAoB,
|
|
1
|
+
{"version":3,"file":"prepareTransaction.d.ts","sourceRoot":"","sources":["../../src/bridge/prepareTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAKvC,eAAO,MAAM,kBAAkB,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC,oBAAoB,CAQ/E,CAAC"}
|
|
@@ -9,9 +9,8 @@ const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
|
9
9
|
const updateTransaction_1 = require("./updateTransaction");
|
|
10
10
|
const prepareTransaction = async (account, transaction) => {
|
|
11
11
|
let fee = transaction.fee;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
12
|
+
const amount = transaction.amount || (0, bignumber_js_1.default)(0);
|
|
13
|
+
fee = (0, bignumber_js_1.default)((await (0, common_logic_1.estimateFees)(account.currency, BigInt(amount.toString()))).toString());
|
|
15
14
|
return (0, updateTransaction_1.updateTransaction)(transaction, { fee });
|
|
16
15
|
};
|
|
17
16
|
exports.prepareTransaction = prepareTransaction;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prepareTransaction.js","sourceRoot":"","sources":["../../src/bridge/prepareTransaction.ts"],"names":[],"mappings":";;;;;;AAEA,kDAA+C;AAC/C,gEAAqC;AACrC,2DAAwD;AAEjD,MAAM,kBAAkB,GAAqD,KAAK,EACvF,OAAO,EACP,WAAW,EACX,EAAE;IACF,IAAI,GAAG,GAAG,WAAW,CAAC,GAAG,CAAC;IAC1B,
|
|
1
|
+
{"version":3,"file":"prepareTransaction.js","sourceRoot":"","sources":["../../src/bridge/prepareTransaction.ts"],"names":[],"mappings":";;;;;;AAEA,kDAA+C;AAC/C,gEAAqC;AACrC,2DAAwD;AAEjD,MAAM,kBAAkB,GAAqD,KAAK,EACvF,OAAO,EACP,WAAW,EACX,EAAE;IACF,IAAI,GAAG,GAAG,WAAW,CAAC,GAAG,CAAC;IAC1B,MAAM,MAAM,GAAG,WAAW,CAAC,MAAM,IAAI,IAAA,sBAAS,EAAC,CAAC,CAAC,CAAC;IAClD,GAAG,GAAG,IAAA,sBAAS,EAAC,CAAC,MAAM,IAAA,2BAAY,EAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC9F,OAAO,IAAA,qCAAiB,EAAC,WAAW,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;AACjD,CAAC,CAAC;AARW,QAAA,kBAAkB,sBAQ7B"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
|
|
2
|
-
export declare function estimateFees(currency: CryptoCurrency): Promise<bigint>;
|
|
2
|
+
export declare function estimateFees(currency: CryptoCurrency, amount: bigint): Promise<bigint>;
|
|
3
3
|
//# sourceMappingURL=estimateFees.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"estimateFees.d.ts","sourceRoot":"","sources":["../../../src/common-logic/transaction/estimateFees.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"estimateFees.d.ts","sourceRoot":"","sources":["../../../src/common-logic/transaction/estimateFees.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAoBnE,wBAAsB,YAAY,CAAC,QAAQ,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAU5F"}
|
|
@@ -5,8 +5,40 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.estimateFees = estimateFees;
|
|
7
7
|
const config_1 = __importDefault(require("../../config"));
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const magnitude = 10n ** 38n;
|
|
9
|
+
const cantonCoinTiers = [
|
|
10
|
+
// 100n because of rateDenominator for percentages
|
|
11
|
+
{ limit: 100n * 100n * magnitude, rateNumerator: 1n, rateDenominator: 100n }, // 1.0%
|
|
12
|
+
{ limit: 1000n * 100n * magnitude, rateNumerator: 1n, rateDenominator: 1000n }, // 0.1%
|
|
13
|
+
{ limit: 1000000n * 100n * magnitude, rateNumerator: 1n, rateDenominator: 10000n }, // 0.01%
|
|
14
|
+
{ limit: null, rateNumerator: 1n, rateDenominator: 100000n }, // 0.001%
|
|
15
|
+
];
|
|
16
|
+
const feeValue = (currency) => config_1.default.getCoinConfig(currency).fee;
|
|
17
|
+
async function estimateFees(currency, amount) {
|
|
18
|
+
const forcedValue = feeValue(currency);
|
|
19
|
+
if (forcedValue) {
|
|
20
|
+
return Promise.resolve(BigInt(forcedValue) * magnitude);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
return Promise.resolve(
|
|
24
|
+
// Add 1 CC as base fee
|
|
25
|
+
1n * magnitude + BigInt(calculateFeeWithTiers(amount, cantonCoinTiers)));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function calculateFeeWithTiers(value, tiers) {
|
|
29
|
+
let remaining = value;
|
|
30
|
+
let lastLimit = 0n;
|
|
31
|
+
let fee = 0n;
|
|
32
|
+
for (const tier of tiers) {
|
|
33
|
+
if (remaining <= 0n)
|
|
34
|
+
break;
|
|
35
|
+
const currentLimit = tier.limit ?? BigInt(2 ** 62); // highest bigint value
|
|
36
|
+
const tierCap = currentLimit - lastLimit;
|
|
37
|
+
const applicable = remaining < tierCap ? remaining : tierCap;
|
|
38
|
+
fee += (applicable * tier.rateNumerator) / tier.rateDenominator;
|
|
39
|
+
remaining -= applicable;
|
|
40
|
+
lastLimit = currentLimit;
|
|
41
|
+
}
|
|
42
|
+
return fee;
|
|
11
43
|
}
|
|
12
44
|
//# sourceMappingURL=estimateFees.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"estimateFees.js","sourceRoot":"","sources":["../../../src/common-logic/transaction/estimateFees.ts"],"names":[],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"estimateFees.js","sourceRoot":"","sources":["../../../src/common-logic/transaction/estimateFees.ts"],"names":[],"mappings":";;;;;AAqBA,oCAUC;AA/BD,0DAAsC;AAGtC,MAAM,SAAS,GAAW,GAAG,IAAI,GAAG,CAAC;AAQrC,MAAM,eAAe,GAAc;IACjC,kDAAkD;IAClD,EAAE,KAAK,EAAE,IAAI,GAAG,IAAI,GAAG,SAAS,EAAE,aAAa,EAAE,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,EAAE,OAAO;IACrF,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,GAAG,SAAS,EAAE,aAAa,EAAE,EAAE,EAAE,eAAe,EAAE,KAAK,EAAE,EAAE,OAAO;IACvF,EAAE,KAAK,EAAE,QAAU,GAAG,IAAI,GAAG,SAAS,EAAE,aAAa,EAAE,EAAE,EAAE,eAAe,EAAE,MAAO,EAAE,EAAE,QAAQ;IAC/F,EAAE,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,EAAE,EAAE,eAAe,EAAE,OAAQ,EAAE,EAAE,SAAS;CACzE,CAAC;AAEF,MAAM,QAAQ,GAAG,CAAC,QAAwB,EAAE,EAAE,CAAC,gBAAU,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC;AAE/E,KAAK,UAAU,YAAY,CAAC,QAAwB,EAAE,MAAc;IACzE,MAAM,WAAW,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACvC,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,SAAS,CAAC,CAAC;IAC1D,CAAC;SAAM,CAAC;QACN,OAAO,OAAO,CAAC,OAAO;QACpB,uBAAuB;QACvB,EAAE,GAAG,SAAS,GAAG,MAAM,CAAC,qBAAqB,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,CACxE,CAAC;IACJ,CAAC;AACH,CAAC;AAED,SAAS,qBAAqB,CAAC,KAAa,EAAE,KAAgB;IAC5D,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,IAAI,SAAS,GAAG,EAAE,CAAC;IACnB,IAAI,GAAG,GAAG,EAAE,CAAC;IAEb,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,SAAS,IAAI,EAAE;YAAE,MAAM;QAE3B,MAAM,YAAY,GAAG,IAAI,CAAC,KAAK,IAAI,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,uBAAuB;QAC3E,MAAM,OAAO,GAAG,YAAY,GAAG,SAAS,CAAC;QACzC,MAAM,UAAU,GAAG,SAAS,GAAG,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC;QAE7D,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,IAAI,CAAC,eAAe,CAAC;QAEhE,SAAS,IAAI,UAAU,CAAC;QACxB,SAAS,GAAG,YAAY,CAAC;IAC3B,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"estimateFees.test.d.ts","sourceRoot":"","sources":["../../../src/common-logic/transaction/estimateFees.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
const estimateFees_1 = require("./estimateFees");
|
|
37
|
+
const coinConfigModule = __importStar(require("../../config"));
|
|
38
|
+
const mockCurrency = {
|
|
39
|
+
id: "canton_network",
|
|
40
|
+
};
|
|
41
|
+
const magnitude = 10n ** 38n;
|
|
42
|
+
describe("estimateFees", () => {
|
|
43
|
+
const mockGetCoinConfig = jest.spyOn(coinConfigModule.default, "getCoinConfig");
|
|
44
|
+
afterEach(() => {
|
|
45
|
+
jest.clearAllMocks();
|
|
46
|
+
});
|
|
47
|
+
it("returns 2 CC fees for 100 CC", async () => {
|
|
48
|
+
mockGetCoinConfig.mockReturnValue({
|
|
49
|
+
fee: undefined,
|
|
50
|
+
});
|
|
51
|
+
const result = await (0, estimateFees_1.estimateFees)(mockCurrency, 100n * magnitude);
|
|
52
|
+
// 2 CC
|
|
53
|
+
expect(result).toEqual(2n * magnitude);
|
|
54
|
+
});
|
|
55
|
+
it("returns 11 CC fees for 1000 CC", async () => {
|
|
56
|
+
mockGetCoinConfig.mockReturnValue({
|
|
57
|
+
fee: undefined,
|
|
58
|
+
});
|
|
59
|
+
const result = await (0, estimateFees_1.estimateFees)(mockCurrency, 1000n * magnitude);
|
|
60
|
+
// 11 CC
|
|
61
|
+
expect(result).toEqual(11n * magnitude);
|
|
62
|
+
});
|
|
63
|
+
it("returns forced fees when setup in config", async () => {
|
|
64
|
+
mockGetCoinConfig.mockReturnValue({
|
|
65
|
+
fee: 3,
|
|
66
|
+
});
|
|
67
|
+
const result = await (0, estimateFees_1.estimateFees)(mockCurrency, 1000n * magnitude);
|
|
68
|
+
// 3 CC
|
|
69
|
+
expect(result).toEqual(3n * magnitude);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
//# sourceMappingURL=estimateFees.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"estimateFees.test.js","sourceRoot":"","sources":["../../../src/common-logic/transaction/estimateFees.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA8C;AAC9C,+DAAiD;AAGjD,MAAM,YAAY,GAAG;IACnB,EAAE,EAAE,gBAAgB;CACQ,CAAC;AAE/B,MAAM,SAAS,GAAW,GAAG,IAAI,GAAG,CAAC;AAErC,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,MAAM,iBAAiB,GAAG,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;IAEhF,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,8BAA8B,EAAE,KAAK,IAAI,EAAE;QAC5C,iBAAiB,CAAC,eAAe,CAAC;YAChC,GAAG,EAAE,SAAS;SACiC,CAAC,CAAC;QAEnD,MAAM,MAAM,GAAG,MAAM,IAAA,2BAAY,EAAC,YAAY,EAAE,IAAI,GAAG,SAAS,CAAC,CAAC;QAElE,OAAO;QACP,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,EAAE,GAAG,SAAS,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,gCAAgC,EAAE,KAAK,IAAI,EAAE;QAC9C,iBAAiB,CAAC,eAAe,CAAC;YAChC,GAAG,EAAE,SAAS;SACiC,CAAC,CAAC;QAEnD,MAAM,MAAM,GAAG,MAAM,IAAA,2BAAY,EAAC,YAAY,EAAE,KAAK,GAAG,SAAS,CAAC,CAAC;QAEnE,QAAQ;QACR,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,GAAG,SAAS,CAAC,CAAC;IAC1C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,0CAA0C,EAAE,KAAK,IAAI,EAAE;QACxD,iBAAiB,CAAC,eAAe,CAAC;YAChC,GAAG,EAAE,CAAC;SACyC,CAAC,CAAC;QAEnD,MAAM,MAAM,GAAG,MAAM,IAAA,2BAAY,EAAC,YAAY,EAAE,KAAK,GAAG,SAAS,CAAC,CAAC;QAEnE,OAAO;QACP,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,EAAE,GAAG,SAAS,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prepareTransaction.d.ts","sourceRoot":"","sources":["../../src/bridge/prepareTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAKvC,eAAO,MAAM,kBAAkB,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC,oBAAoB,
|
|
1
|
+
{"version":3,"file":"prepareTransaction.d.ts","sourceRoot":"","sources":["../../src/bridge/prepareTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAKvC,eAAO,MAAM,kBAAkB,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC,oBAAoB,CAQ/E,CAAC"}
|
|
@@ -3,9 +3,8 @@ import BigNumber from "bignumber.js";
|
|
|
3
3
|
import { updateTransaction } from "./updateTransaction";
|
|
4
4
|
export const prepareTransaction = async (account, transaction) => {
|
|
5
5
|
let fee = transaction.fee;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
6
|
+
const amount = transaction.amount || BigNumber(0);
|
|
7
|
+
fee = BigNumber((await estimateFees(account.currency, BigInt(amount.toString()))).toString());
|
|
9
8
|
return updateTransaction(transaction, { fee });
|
|
10
9
|
};
|
|
11
10
|
//# sourceMappingURL=prepareTransaction.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prepareTransaction.js","sourceRoot":"","sources":["../../src/bridge/prepareTransaction.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,SAAS,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,MAAM,CAAC,MAAM,kBAAkB,GAAqD,KAAK,EACvF,OAAO,EACP,WAAW,EACX,EAAE;IACF,IAAI,GAAG,GAAG,WAAW,CAAC,GAAG,CAAC;IAC1B,
|
|
1
|
+
{"version":3,"file":"prepareTransaction.js","sourceRoot":"","sources":["../../src/bridge/prepareTransaction.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,SAAS,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,MAAM,CAAC,MAAM,kBAAkB,GAAqD,KAAK,EACvF,OAAO,EACP,WAAW,EACX,EAAE;IACF,IAAI,GAAG,GAAG,WAAW,CAAC,GAAG,CAAC;IAC1B,MAAM,MAAM,GAAG,WAAW,CAAC,MAAM,IAAI,SAAS,CAAC,CAAC,CAAC,CAAC;IAClD,GAAG,GAAG,SAAS,CAAC,CAAC,MAAM,YAAY,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC9F,OAAO,iBAAiB,CAAC,WAAW,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;AACjD,CAAC,CAAC"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
|
|
2
|
-
export declare function estimateFees(currency: CryptoCurrency): Promise<bigint>;
|
|
2
|
+
export declare function estimateFees(currency: CryptoCurrency, amount: bigint): Promise<bigint>;
|
|
3
3
|
//# sourceMappingURL=estimateFees.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"estimateFees.d.ts","sourceRoot":"","sources":["../../../src/common-logic/transaction/estimateFees.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"estimateFees.d.ts","sourceRoot":"","sources":["../../../src/common-logic/transaction/estimateFees.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAoBnE,wBAAsB,YAAY,CAAC,QAAQ,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAU5F"}
|
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
import coinConfig from "../../config";
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
const magnitude = 10n ** 38n;
|
|
3
|
+
const cantonCoinTiers = [
|
|
4
|
+
// 100n because of rateDenominator for percentages
|
|
5
|
+
{ limit: 100n * 100n * magnitude, rateNumerator: 1n, rateDenominator: 100n }, // 1.0%
|
|
6
|
+
{ limit: 1000n * 100n * magnitude, rateNumerator: 1n, rateDenominator: 1000n }, // 0.1%
|
|
7
|
+
{ limit: 1000000n * 100n * magnitude, rateNumerator: 1n, rateDenominator: 10000n }, // 0.01%
|
|
8
|
+
{ limit: null, rateNumerator: 1n, rateDenominator: 100000n }, // 0.001%
|
|
9
|
+
];
|
|
10
|
+
const feeValue = (currency) => coinConfig.getCoinConfig(currency).fee;
|
|
11
|
+
export async function estimateFees(currency, amount) {
|
|
12
|
+
const forcedValue = feeValue(currency);
|
|
13
|
+
if (forcedValue) {
|
|
14
|
+
return Promise.resolve(BigInt(forcedValue) * magnitude);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
return Promise.resolve(
|
|
18
|
+
// Add 1 CC as base fee
|
|
19
|
+
1n * magnitude + BigInt(calculateFeeWithTiers(amount, cantonCoinTiers)));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function calculateFeeWithTiers(value, tiers) {
|
|
23
|
+
let remaining = value;
|
|
24
|
+
let lastLimit = 0n;
|
|
25
|
+
let fee = 0n;
|
|
26
|
+
for (const tier of tiers) {
|
|
27
|
+
if (remaining <= 0n)
|
|
28
|
+
break;
|
|
29
|
+
const currentLimit = tier.limit ?? BigInt(2 ** 62); // highest bigint value
|
|
30
|
+
const tierCap = currentLimit - lastLimit;
|
|
31
|
+
const applicable = remaining < tierCap ? remaining : tierCap;
|
|
32
|
+
fee += (applicable * tier.rateNumerator) / tier.rateDenominator;
|
|
33
|
+
remaining -= applicable;
|
|
34
|
+
lastLimit = currentLimit;
|
|
35
|
+
}
|
|
36
|
+
return fee;
|
|
5
37
|
}
|
|
6
38
|
//# sourceMappingURL=estimateFees.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"estimateFees.js","sourceRoot":"","sources":["../../../src/common-logic/transaction/estimateFees.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,cAAc,CAAC;AAGtC,MAAM,QAAQ,GAAG,CAAC,QAAwB,EAAE,EAAE,
|
|
1
|
+
{"version":3,"file":"estimateFees.js","sourceRoot":"","sources":["../../../src/common-logic/transaction/estimateFees.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,cAAc,CAAC;AAGtC,MAAM,SAAS,GAAW,GAAG,IAAI,GAAG,CAAC;AAQrC,MAAM,eAAe,GAAc;IACjC,kDAAkD;IAClD,EAAE,KAAK,EAAE,IAAI,GAAG,IAAI,GAAG,SAAS,EAAE,aAAa,EAAE,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,EAAE,OAAO;IACrF,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,GAAG,SAAS,EAAE,aAAa,EAAE,EAAE,EAAE,eAAe,EAAE,KAAK,EAAE,EAAE,OAAO;IACvF,EAAE,KAAK,EAAE,QAAU,GAAG,IAAI,GAAG,SAAS,EAAE,aAAa,EAAE,EAAE,EAAE,eAAe,EAAE,MAAO,EAAE,EAAE,QAAQ;IAC/F,EAAE,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,EAAE,EAAE,eAAe,EAAE,OAAQ,EAAE,EAAE,SAAS;CACzE,CAAC;AAEF,MAAM,QAAQ,GAAG,CAAC,QAAwB,EAAE,EAAE,CAAC,UAAU,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC;AAEtF,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,QAAwB,EAAE,MAAc;IACzE,MAAM,WAAW,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACvC,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,SAAS,CAAC,CAAC;IAC1D,CAAC;SAAM,CAAC;QACN,OAAO,OAAO,CAAC,OAAO;QACpB,uBAAuB;QACvB,EAAE,GAAG,SAAS,GAAG,MAAM,CAAC,qBAAqB,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,CACxE,CAAC;IACJ,CAAC;AACH,CAAC;AAED,SAAS,qBAAqB,CAAC,KAAa,EAAE,KAAgB;IAC5D,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,IAAI,SAAS,GAAG,EAAE,CAAC;IACnB,IAAI,GAAG,GAAG,EAAE,CAAC;IAEb,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,SAAS,IAAI,EAAE;YAAE,MAAM;QAE3B,MAAM,YAAY,GAAG,IAAI,CAAC,KAAK,IAAI,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,uBAAuB;QAC3E,MAAM,OAAO,GAAG,YAAY,GAAG,SAAS,CAAC;QACzC,MAAM,UAAU,GAAG,SAAS,GAAG,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC;QAE7D,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,IAAI,CAAC,eAAe,CAAC;QAEhE,SAAS,IAAI,UAAU,CAAC;QACxB,SAAS,GAAG,YAAY,CAAC;IAC3B,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"estimateFees.test.d.ts","sourceRoot":"","sources":["../../../src/common-logic/transaction/estimateFees.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { estimateFees } from "./estimateFees";
|
|
2
|
+
import * as coinConfigModule from "../../config";
|
|
3
|
+
const mockCurrency = {
|
|
4
|
+
id: "canton_network",
|
|
5
|
+
};
|
|
6
|
+
const magnitude = 10n ** 38n;
|
|
7
|
+
describe("estimateFees", () => {
|
|
8
|
+
const mockGetCoinConfig = jest.spyOn(coinConfigModule.default, "getCoinConfig");
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
jest.clearAllMocks();
|
|
11
|
+
});
|
|
12
|
+
it("returns 2 CC fees for 100 CC", async () => {
|
|
13
|
+
mockGetCoinConfig.mockReturnValue({
|
|
14
|
+
fee: undefined,
|
|
15
|
+
});
|
|
16
|
+
const result = await estimateFees(mockCurrency, 100n * magnitude);
|
|
17
|
+
// 2 CC
|
|
18
|
+
expect(result).toEqual(2n * magnitude);
|
|
19
|
+
});
|
|
20
|
+
it("returns 11 CC fees for 1000 CC", async () => {
|
|
21
|
+
mockGetCoinConfig.mockReturnValue({
|
|
22
|
+
fee: undefined,
|
|
23
|
+
});
|
|
24
|
+
const result = await estimateFees(mockCurrency, 1000n * magnitude);
|
|
25
|
+
// 11 CC
|
|
26
|
+
expect(result).toEqual(11n * magnitude);
|
|
27
|
+
});
|
|
28
|
+
it("returns forced fees when setup in config", async () => {
|
|
29
|
+
mockGetCoinConfig.mockReturnValue({
|
|
30
|
+
fee: 3,
|
|
31
|
+
});
|
|
32
|
+
const result = await estimateFees(mockCurrency, 1000n * magnitude);
|
|
33
|
+
// 3 CC
|
|
34
|
+
expect(result).toEqual(3n * magnitude);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
//# sourceMappingURL=estimateFees.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"estimateFees.test.js","sourceRoot":"","sources":["../../../src/common-logic/transaction/estimateFees.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,KAAK,gBAAgB,MAAM,cAAc,CAAC;AAGjD,MAAM,YAAY,GAAG;IACnB,EAAE,EAAE,gBAAgB;CACQ,CAAC;AAE/B,MAAM,SAAS,GAAW,GAAG,IAAI,GAAG,CAAC;AAErC,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,MAAM,iBAAiB,GAAG,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;IAEhF,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,8BAA8B,EAAE,KAAK,IAAI,EAAE;QAC5C,iBAAiB,CAAC,eAAe,CAAC;YAChC,GAAG,EAAE,SAAS;SACiC,CAAC,CAAC;QAEnD,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC,YAAY,EAAE,IAAI,GAAG,SAAS,CAAC,CAAC;QAElE,OAAO;QACP,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,EAAE,GAAG,SAAS,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,gCAAgC,EAAE,KAAK,IAAI,EAAE;QAC9C,iBAAiB,CAAC,eAAe,CAAC;YAChC,GAAG,EAAE,SAAS;SACiC,CAAC,CAAC;QAEnD,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC,YAAY,EAAE,KAAK,GAAG,SAAS,CAAC,CAAC;QAEnE,QAAQ;QACR,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,GAAG,SAAS,CAAC,CAAC;IAC1C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,0CAA0C,EAAE,KAAK,IAAI,EAAE;QACxD,iBAAiB,CAAC,eAAe,CAAC;YAChC,GAAG,EAAE,CAAC;SACyC,CAAC,CAAC;QAEnD,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC,YAAY,EAAE,KAAK,GAAG,SAAS,CAAC,CAAC;QAEnE,OAAO;QACP,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,EAAE,GAAG,SAAS,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ledgerhq/coin-canton",
|
|
3
|
-
"version": "0.5.1-nightly.
|
|
3
|
+
"version": "0.5.1-nightly.4",
|
|
4
4
|
"description": "Canton coin integration",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ledger",
|
|
@@ -101,13 +101,13 @@
|
|
|
101
101
|
"bignumber.js": "^9.1.2",
|
|
102
102
|
"invariant": "^2.2.4",
|
|
103
103
|
"rxjs": "^7.8.1",
|
|
104
|
-
"@ledgerhq/coin-framework": "^6.4.1-nightly.
|
|
105
|
-
"@ledgerhq/cryptoassets": "^13.
|
|
106
|
-
"@ledgerhq/devices": "8.5.2-nightly.
|
|
107
|
-
"@ledgerhq/errors": "^6.
|
|
108
|
-
"@ledgerhq/live-env": "^2.17.0-nightly.
|
|
109
|
-
"@ledgerhq/live-network": "^2.0.18-nightly.
|
|
110
|
-
"@ledgerhq/types-live": "^6.85.0-nightly.
|
|
104
|
+
"@ledgerhq/coin-framework": "^6.4.1-nightly.4",
|
|
105
|
+
"@ledgerhq/cryptoassets": "^13.29.0-nightly.3",
|
|
106
|
+
"@ledgerhq/devices": "8.5.2-nightly.1",
|
|
107
|
+
"@ledgerhq/errors": "^6.26.0-nightly.0",
|
|
108
|
+
"@ledgerhq/live-env": "^2.17.0-nightly.2",
|
|
109
|
+
"@ledgerhq/live-network": "^2.0.18-nightly.2",
|
|
110
|
+
"@ledgerhq/types-live": "^6.85.0-nightly.4"
|
|
111
111
|
},
|
|
112
112
|
"devDependencies": {
|
|
113
113
|
"@types/invariant": "^2.2.37",
|
|
@@ -117,13 +117,13 @@
|
|
|
117
117
|
"jest": "^29.7.0",
|
|
118
118
|
"ts-jest": "^29.1.1",
|
|
119
119
|
"typescript": "^5.4.5",
|
|
120
|
-
"@ledgerhq/types-cryptoassets": "^7.27.0-nightly.
|
|
120
|
+
"@ledgerhq/types-cryptoassets": "^7.27.0-nightly.1",
|
|
121
121
|
"@ledgerhq/disable-network-setup": "^0.0.0"
|
|
122
122
|
},
|
|
123
123
|
"scripts": {
|
|
124
124
|
"clean": "rimraf lib lib-es",
|
|
125
125
|
"build": "tsc && tsc -m esnext --moduleResolution bundler --outDir lib-es",
|
|
126
|
-
"coverage": "jest --coverage
|
|
126
|
+
"coverage": "jest --coverage",
|
|
127
127
|
"prewatch": "pnpm build",
|
|
128
128
|
"watch": "tsc --watch",
|
|
129
129
|
"doc": "documentation readme src/** --section=API --pe ts --re ts --re d.ts",
|
|
@@ -9,8 +9,7 @@ export const prepareTransaction: AccountBridge<Transaction>["prepareTransaction"
|
|
|
9
9
|
transaction,
|
|
10
10
|
) => {
|
|
11
11
|
let fee = transaction.fee;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
12
|
+
const amount = transaction.amount || BigNumber(0);
|
|
13
|
+
fee = BigNumber((await estimateFees(account.currency, BigInt(amount.toString()))).toString());
|
|
15
14
|
return updateTransaction(transaction, { fee });
|
|
16
15
|
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { estimateFees } from "./estimateFees";
|
|
2
|
+
import * as coinConfigModule from "../../config";
|
|
3
|
+
import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
|
|
4
|
+
|
|
5
|
+
const mockCurrency = {
|
|
6
|
+
id: "canton_network",
|
|
7
|
+
} as unknown as CryptoCurrency;
|
|
8
|
+
|
|
9
|
+
const magnitude: bigint = 10n ** 38n;
|
|
10
|
+
|
|
11
|
+
describe("estimateFees", () => {
|
|
12
|
+
const mockGetCoinConfig = jest.spyOn(coinConfigModule.default, "getCoinConfig");
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
jest.clearAllMocks();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("returns 2 CC fees for 100 CC", async () => {
|
|
19
|
+
mockGetCoinConfig.mockReturnValue({
|
|
20
|
+
fee: undefined,
|
|
21
|
+
} as unknown as coinConfigModule.CantonCoinConfig);
|
|
22
|
+
|
|
23
|
+
const result = await estimateFees(mockCurrency, 100n * magnitude);
|
|
24
|
+
|
|
25
|
+
// 2 CC
|
|
26
|
+
expect(result).toEqual(2n * magnitude);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("returns 11 CC fees for 1000 CC", async () => {
|
|
30
|
+
mockGetCoinConfig.mockReturnValue({
|
|
31
|
+
fee: undefined,
|
|
32
|
+
} as unknown as coinConfigModule.CantonCoinConfig);
|
|
33
|
+
|
|
34
|
+
const result = await estimateFees(mockCurrency, 1000n * magnitude);
|
|
35
|
+
|
|
36
|
+
// 11 CC
|
|
37
|
+
expect(result).toEqual(11n * magnitude);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("returns forced fees when setup in config", async () => {
|
|
41
|
+
mockGetCoinConfig.mockReturnValue({
|
|
42
|
+
fee: 3,
|
|
43
|
+
} as unknown as coinConfigModule.CantonCoinConfig);
|
|
44
|
+
|
|
45
|
+
const result = await estimateFees(mockCurrency, 1000n * magnitude);
|
|
46
|
+
|
|
47
|
+
// 3 CC
|
|
48
|
+
expect(result).toEqual(3n * magnitude);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -1,9 +1,53 @@
|
|
|
1
1
|
import coinConfig from "../../config";
|
|
2
2
|
import type { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
coinConfig.getCoinConfig(currency).fee ?? 2n * 10n ** 38n;
|
|
4
|
+
const magnitude: bigint = 10n ** 38n;
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
type FeeTier = {
|
|
7
|
+
limit: bigint | null; // `null` means no upper limit
|
|
8
|
+
rateNumerator: bigint; // e.g., 1 for 1%
|
|
9
|
+
rateDenominator: bigint; // e.g., 100 for 1%
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const cantonCoinTiers: FeeTier[] = [
|
|
13
|
+
// 100n because of rateDenominator for percentages
|
|
14
|
+
{ limit: 100n * 100n * magnitude, rateNumerator: 1n, rateDenominator: 100n }, // 1.0%
|
|
15
|
+
{ limit: 1000n * 100n * magnitude, rateNumerator: 1n, rateDenominator: 1000n }, // 0.1%
|
|
16
|
+
{ limit: 1_000_000n * 100n * magnitude, rateNumerator: 1n, rateDenominator: 10_000n }, // 0.01%
|
|
17
|
+
{ limit: null, rateNumerator: 1n, rateDenominator: 100_000n }, // 0.001%
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
const feeValue = (currency: CryptoCurrency) => coinConfig.getCoinConfig(currency).fee;
|
|
21
|
+
|
|
22
|
+
export async function estimateFees(currency: CryptoCurrency, amount: bigint): Promise<bigint> {
|
|
23
|
+
const forcedValue = feeValue(currency);
|
|
24
|
+
if (forcedValue) {
|
|
25
|
+
return Promise.resolve(BigInt(forcedValue) * magnitude);
|
|
26
|
+
} else {
|
|
27
|
+
return Promise.resolve(
|
|
28
|
+
// Add 1 CC as base fee
|
|
29
|
+
1n * magnitude + BigInt(calculateFeeWithTiers(amount, cantonCoinTiers)),
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function calculateFeeWithTiers(value: bigint, tiers: FeeTier[]): bigint {
|
|
35
|
+
let remaining = value;
|
|
36
|
+
let lastLimit = 0n;
|
|
37
|
+
let fee = 0n;
|
|
38
|
+
|
|
39
|
+
for (const tier of tiers) {
|
|
40
|
+
if (remaining <= 0n) break;
|
|
41
|
+
|
|
42
|
+
const currentLimit = tier.limit ?? BigInt(2 ** 62); // highest bigint value
|
|
43
|
+
const tierCap = currentLimit - lastLimit;
|
|
44
|
+
const applicable = remaining < tierCap ? remaining : tierCap;
|
|
45
|
+
|
|
46
|
+
fee += (applicable * tier.rateNumerator) / tier.rateDenominator;
|
|
47
|
+
|
|
48
|
+
remaining -= applicable;
|
|
49
|
+
lastLimit = currentLimit;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return fee;
|
|
9
53
|
}
|