@ledgerhq/coin-near
Version:
139 lines • 7.31 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("@ledgerhq/coin-module-framework/currencies/index");
const index_2 = require("@ledgerhq/cryptoassets/index");
const devices_1 = require("@ledgerhq/devices");
const specs_1 = require("@ledgerhq/ledger-wallet-framework/bot/specs");
const bignumber_js_1 = require("bignumber.js");
const expect_1 = __importDefault(require("expect"));
const invariant_1 = __importDefault(require("invariant"));
const speculos_deviceActions_1 = require("./speculos-deviceActions");
const currency = (0, index_2.getCryptoCurrencyById)("near");
const minimalAmount = (0, index_1.parseCurrencyUnit)(currency.units[0], "0.00001");
const stakingFee = (0, index_1.parseCurrencyUnit)(currency.units[0], "0.002");
const maxAccount = 8;
const validator = "ledgerbyfigment.poolv1.near";
const near = {
name: "NEAR",
currency,
appQuery: {
model: devices_1.DeviceModelId.nanoS,
appName: "NEAR",
},
genericDeviceAction: speculos_deviceActions_1.acceptTransaction,
testTimeout: 2 * 60 * 1000,
mutations: [
{
name: "Move 50% to another account",
feature: "send",
maxRun: 1,
transaction: ({ account, siblings, bridge, maxSpendable }) => {
(0, invariant_1.default)(maxSpendable.gt(minimalAmount), "balance is too low");
const sibling = (0, specs_1.pickSiblings)(siblings, maxAccount);
const recipient = sibling.freshAddress;
const amount = maxSpendable.div(2).integerValue();
return {
transaction: bridge.createTransaction(account),
updates: [{ recipient }, { amount }],
};
},
test: ({ accountBeforeTransaction, operation, account }) => {
(0, specs_1.botTest)("account spendable balance decreased with operation", () => (0, expect_1.default)(account.spendableBalance).toEqual(accountBeforeTransaction.spendableBalance.minus(operation.value)));
},
},
{
name: "Send max to another account",
feature: "sendMax",
maxRun: 1,
transaction: ({ account, siblings, bridge, maxSpendable }) => {
(0, invariant_1.default)(maxSpendable.gt(minimalAmount), "balance is too low");
const sibling = (0, specs_1.pickSiblings)(siblings, maxAccount);
const recipient = sibling.freshAddress;
return {
transaction: bridge.createTransaction(account),
updates: [{ recipient }, { useAllAmount: true }],
};
},
test: ({ account }) => {
(0, specs_1.botTest)("account spendable balance is zero", () => (0, expect_1.default)(account.spendableBalance.toString()).toBe("0"));
},
},
{
name: "Stake",
feature: "staking",
maxRun: 1,
transaction: ({ account, bridge, maxSpendable }) => {
(0, invariant_1.default)(maxSpendable.gt(minimalAmount.plus(stakingFee)), "balance is too low");
const amount = minimalAmount.times(10).times(Math.random()).integerValue();
return {
transaction: bridge.createTransaction(account),
updates: [{ mode: "stake", recipient: validator }, { amount }],
};
},
test: ({ accountBeforeTransaction, operation, account }) => {
const { nearResources } = account;
const { nearResources: beforeTransactionNearResources } = accountBeforeTransaction;
// Sometimes, 1 yoctoNEAR gets deducted from the staked amount, so we assert against a range.
(0, specs_1.botTest)("account staked balance increased with operation", () => {
(0, expect_1.default)(nearResources.stakedBalance.gte(beforeTransactionNearResources.stakedBalance.plus(operation.value.minus("1")))).toBe(true);
(0, expect_1.default)(nearResources.stakedBalance.lte(beforeTransactionNearResources.stakedBalance.plus(operation.value))).toBe(true);
});
},
},
{
name: "Unstake",
feature: "staking",
maxRun: 1,
transaction: ({ account, bridge, maxSpendable }) => {
(0, invariant_1.default)(maxSpendable.gt(stakingFee), "balance is too low for fees");
const { nearResources } = account;
const staked = nearResources?.stakedBalance || new bignumber_js_1.BigNumber(0);
(0, invariant_1.default)(staked.gt(minimalAmount), "staked balance is too low for unstaking");
const halfStaked = staked.div(2);
const amount = halfStaked.gt(minimalAmount)
? halfStaked.integerValue()
: staked.integerValue();
return {
transaction: bridge.createTransaction(account),
updates: [{ mode: "unstake", recipient: validator }, { amount }],
};
},
test: ({ accountBeforeTransaction, account }) => {
const { nearResources } = account;
const { nearResources: beforeTransactionNearResources } = accountBeforeTransaction;
(0, specs_1.botTest)("account pending balance increased", () => (0, expect_1.default)(nearResources.pendingBalance.gt(beforeTransactionNearResources.pendingBalance)).toBe(true));
},
},
{
name: "Withdraw",
feature: "staking",
maxRun: 1,
transaction: ({ account, bridge, maxSpendable }) => {
(0, invariant_1.default)(maxSpendable.gt(stakingFee), "balance is too low for fees");
const { nearResources } = account;
const available = nearResources?.availableBalance || new bignumber_js_1.BigNumber(0);
(0, invariant_1.default)(available.gt(minimalAmount), "available balance is too low for withdrawing");
const halfAvailable = available.div(2);
const amount = halfAvailable.gt(minimalAmount)
? halfAvailable.integerValue()
: available.integerValue();
return {
transaction: bridge.createTransaction(account),
updates: [{ mode: "withdraw", recipient: validator }, { amount }],
};
},
test: ({ accountBeforeTransaction, account }) => {
const { nearResources } = account;
const { nearResources: beforeTransactionNearResources } = accountBeforeTransaction;
(0, specs_1.botTest)("account withdrawable balance decreased", () => (0, expect_1.default)(nearResources.availableBalance.lt(beforeTransactionNearResources.availableBalance)).toBe(true));
},
},
],
};
exports.default = {
near,
};
//# sourceMappingURL=specs.js.map