UNPKG

@ledgerhq/coin-near

Version:
75 lines 3.21 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildTransaction = void 0; const logs_1 = require("@ledgerhq/logs"); const nearAPI = __importStar(require("near-api-js")); const api_1 = require("./api"); const logic_1 = require("./logic"); const buildTransaction = async (a, t, publicKey) => { const { nonce, block_hash } = await (0, api_1.getAccessKey)({ address: a.freshAddress, publicKey, }); const parsedNearAmount = t.amount.toFixed(); const actions = []; switch (t.mode) { case "stake": actions.push(nearAPI.transactions.functionCall("deposit_and_stake", {}, (0, logic_1.getStakingGas)().toFixed(), parsedNearAmount)); break; case "unstake": if (t.useAllAmount) { actions.push(nearAPI.transactions.functionCall("unstake_all", {}, (0, logic_1.getStakingGas)().toFixed(), "0")); } else { actions.push(nearAPI.transactions.functionCall("unstake", { amount: parsedNearAmount }, (0, logic_1.getStakingGas)().toFixed(), "0")); } break; case "withdraw": if (t.useAllAmount) { actions.push(nearAPI.transactions.functionCall("withdraw_all", {}, (0, logic_1.getStakingGas)(t).toNumber(), "0")); } else { actions.push(nearAPI.transactions.functionCall("withdraw", { amount: parsedNearAmount }, (0, logic_1.getStakingGas)().toFixed(), "0")); } break; default: actions.push(nearAPI.transactions.transfer(parsedNearAmount)); } try { const transaction = nearAPI.transactions.createTransaction(a.freshAddress, nearAPI.utils.PublicKey.fromString(publicKey), t.recipient, nonce + 1, actions, nearAPI.utils.serialize.base_decode(block_hash)); return transaction; } catch (e) { (0, logs_1.log)("Near", "Error building transaction", { error: e, transaction: t, account: a, }); throw e; } }; exports.buildTransaction = buildTransaction; //# sourceMappingURL=buildTransaction.js.map