@ledgerhq/coin-aptos
Version:
Ledger Aptos Coin integration
122 lines • 5.63 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 expect_1 = __importDefault(require("expect"));
const devices_1 = require("@ledgerhq/devices");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const currencies_1 = require("@ledgerhq/cryptoassets/currencies");
const specs_1 = require("@ledgerhq/coin-framework/bot/specs");
const speculos_deviceActions_1 = require("./speculos-deviceActions");
const MIN_SAFE = new bignumber_js_1.default(0.0001);
const maxAccount = 6;
const aptosSpecs = {
name: "Aptos",
currency: (0, currencies_1.getCryptoCurrencyById)("aptos"),
appQuery: {
model: devices_1.DeviceModelId.nanoSP,
appName: "Aptos",
},
genericDeviceAction: speculos_deviceActions_1.acceptTransaction,
testTimeout: 2 * 60 * 1000,
minViableAmount: MIN_SAFE,
transactionCheck: ({ maxSpendable }) => {
(0, invariant_1.default)(maxSpendable.gt(MIN_SAFE), "balance is too low");
},
mutations: [
{
name: "Send ~50%",
feature: "send",
maxRun: 1,
testDestination: specs_1.genericTestDestination,
transaction: ({ account, siblings, bridge, maxSpendable }) => {
(0, invariant_1.default)(maxSpendable.gt(MIN_SAFE), "balance is too low");
const sibling = (0, specs_1.pickSiblings)(siblings, maxAccount);
const recipient = sibling.freshAddress;
const amount = maxSpendable.div(2).integerValue();
const transaction = bridge.createTransaction(account);
const updates = [
{
recipient,
},
{ amount },
];
return {
transaction,
updates,
};
},
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: "Transfer Max",
feature: "sendMax",
maxRun: 1,
transaction: ({ account, siblings, bridge }) => {
const updates = [
{
recipient: (0, specs_1.pickSiblings)(siblings, maxAccount).freshAddress,
},
{
useAllAmount: true,
},
];
return {
transaction: bridge.createTransaction(account),
updates,
};
},
testDestination: specs_1.genericTestDestination,
test: ({ account }) => {
(0, specs_1.botTest)("account spendable balance is zero", () => (0, expect_1.default)(account.spendableBalance.toString()).toBe("0"));
},
},
{
name: "Send ~50% of token amount",
feature: "tokens",
maxRun: 1,
deviceAction: speculos_deviceActions_1.acceptTokenTransaction,
transaction: ({ account, bridge, siblings, maxSpendable }) => {
(0, invariant_1.default)(maxSpendable.gt(MIN_SAFE), "Balance is too low");
const senderTokenAcc = findTokenSubAccountWithBalance(account);
(0, invariant_1.default)(senderTokenAcc, "Sender token account with available balance not found");
const sibling = (0, specs_1.pickSiblings)(siblings, maxAccount);
const recipientTokenAcc = findTokenSubAccountWithBalance(sibling);
(0, invariant_1.default)(recipientTokenAcc, "Receiver token account with available balance not found");
const amount = senderTokenAcc.spendableBalance.div(2).integerValue();
const recipient = sibling.freshAddress;
const transaction = bridge.createTransaction(account);
const subAccountId = senderTokenAcc.id;
return {
transaction,
updates: [{ subAccountId }, { recipient }, { amount }],
};
},
test: input => {
expectTokenAccountCorrectBalanceChange(input);
},
},
],
};
function findTokenSubAccountWithBalance(account) {
return account.subAccounts?.find(acc => acc.type === "TokenAccount" && acc.balance.gt(0));
}
function expectTokenAccountCorrectBalanceChange({ account, accountBeforeTransaction, status, transaction, }) {
const tokenAccId = transaction.subAccountId;
if (!tokenAccId)
throw new Error("Wrong subAccountId");
const tokenAccAfterTx = account.subAccounts?.find(acc => acc.id === tokenAccId);
const tokenAccBeforeTx = accountBeforeTransaction.subAccounts?.find(acc => acc.id === tokenAccId);
if (!tokenAccAfterTx || !tokenAccBeforeTx) {
throw new Error("Token sub accounts not found!");
}
(0, specs_1.botTest)("Token balance decreased with operation", () => (0, expect_1.default)(tokenAccAfterTx.balance.toString()).toBe(tokenAccBeforeTx.balance.minus(status.amount).toString()));
}
exports.default = {
aptosSpecs,
};
//# sourceMappingURL=bot-specs.js.map