@ledgerhq/coin-icon
Version:
Ledger Icon Coin integration
104 lines • 4.56 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const invariant_1 = __importDefault(require("invariant"));
const specs_1 = require("@ledgerhq/coin-framework/bot/specs");
const index_1 = require("@ledgerhq/coin-framework/serialization/index");
const devices_1 = require("@ledgerhq/devices");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const expect_1 = __importDefault(require("expect"));
const speculos_deviceActions_1 = require("./speculos-deviceActions");
const logic_1 = require("./logic");
const currencies_1 = require("@ledgerhq/cryptoassets/currencies");
const maxAccounts = 6;
const currency = (0, currencies_1.getCryptoCurrencyById)("icon");
// FIX ME ICON have a bug where the amounts from the API have imprecisions
const expectedApproximate = (value, expected, delta = (0, logic_1.convertICXtoLoop)(0.000005)) => {
if (value.minus(expected).abs().gt(delta)) {
(0, expect_1.default)(value.toString()).toEqual(value.toString());
}
};
const icon = {
name: "Icon",
currency,
appQuery: {
model: devices_1.DeviceModelId.nanoS,
appName: "Icon",
},
genericDeviceAction: speculos_deviceActions_1.acceptTransaction,
testTimeout: 2 * 60 * 1000,
transactionCheck: ({ maxSpendable }) => {
(0, invariant_1.default)(maxSpendable.gt(logic_1.EXISTENTIAL_DEPOSIT_RECOMMENDED_MARGIN.multipliedBy(2)), "balance is too low");
},
test: ({ operation, optimisticOperation }) => {
const opExpected = (0, index_1.toOperationRaw)({
...optimisticOperation,
});
delete opExpected.value;
delete opExpected.fee;
delete opExpected.date;
delete opExpected.blockHash;
delete opExpected.blockHeight;
delete opExpected.transactionSequenceNumber;
(0, specs_1.botTest)("optimistic operation matches", () => (0, expect_1.default)((0, index_1.toOperationRaw)(operation)).toMatchObject(opExpected));
},
mutations: [
{
name: "send 50%~",
feature: "send",
maxRun: 1,
transaction: ({ account, siblings, bridge }) => {
(0, invariant_1.default)(account.spendableBalance.gt(0), "balance is 0");
const sibling = (0, specs_1.pickSiblings)(siblings, maxAccounts);
let amount = account.spendableBalance.div(2).integerValue();
if (!sibling.used && amount.lt(logic_1.EXISTENTIAL_DEPOSIT)) {
(0, invariant_1.default)(account.spendableBalance.gt(logic_1.EXISTENTIAL_DEPOSIT), "send is too low to activate account");
amount = logic_1.EXISTENTIAL_DEPOSIT;
}
return {
transaction: bridge.createTransaction(account),
updates: [
{
recipient: sibling.freshAddress,
},
{
amount,
},
],
};
},
test: ({ accountBeforeTransaction, operation, account }) => {
(0, specs_1.botTest)("account spendable balance decreased with operation", () => expectedApproximate(account.spendableBalance, accountBeforeTransaction.spendableBalance.minus(operation.value)));
},
},
{
name: "send max",
feature: "sendMax",
maxRun: 1,
transaction: ({ account, siblings, bridge }) => {
(0, invariant_1.default)(account.spendableBalance.gt(0), "balance is 0");
const sibling = (0, specs_1.pickSiblings)(siblings, maxAccounts);
return {
transaction: bridge.createTransaction(account),
updates: [
{
recipient: sibling.freshAddress,
},
{
useAllAmount: true,
},
],
};
},
test: ({ account }) => {
(0, specs_1.botTest)("account spendable balance is zero", () => expectedApproximate(account.spendableBalance, new bignumber_js_1.default(0)));
},
},
],
};
exports.default = {
icon,
};
//# sourceMappingURL=specs.js.map