UNPKG

@flarenetwork/flare-stake-tool

Version:
362 lines 16 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.exportCP = exportCP; exports.importCP = importCP; exports.exportPC = exportPC; exports.importPC = importPC; exports.addValidator = addValidator; exports.addDelegator = addDelegator; exports.internalTransfer = internalTransfer; const ethers_1 = require("ethers"); const settings = __importStar(require("./settings")); const flarejs_1 = require("@flarenetwork/flarejs"); const utils_1 = require("./utils"); const chain = __importStar(require("./flare/chain")); const flare_1 = require("./flare"); const bn_js_1 = require("bn.js"); const context_1 = require("./flare/context"); const FLR = 1e9; // one FLR in nanoFLR function getChainIdFromContext(sourceChain, context) { return sourceChain === "C" ? context.cBlockchainID : sourceChain === "P" ? context.pBlockchainID : context.xBlockchainID; } async function exportCP(ctx, params) { const provider = new ethers_1.JsonRpcProvider(settings.URL[ctx.config.hrp] + "/ext/bc/C/rpc"); const evmapi = new flarejs_1.evm.EVMApi(settings.URL[ctx.config.hrp]); const context = await flarejs_1.Context.getContextFromURI(settings.URL[ctx.config.hrp]); if (!ctx.cAddressHex) { throw new Error("cAddressHex is undefined or null"); } if (!ctx.pAddressBech32) { throw new Error("pAddressBech32 is undefined or null"); } if (!ctx.privkHex) { throw new Error("privkHex is undefined or null"); } if (!params.amount) { throw new Error("amount is required"); } const txCount = await provider.getTransactionCount(ctx.cAddressHex); const baseFee = await evmapi.getBaseFee(); const fee = !params.fee || BigInt(params.fee) === 0n || BigInt(params.fee) < baseFee ? baseFee : BigInt(params.fee); const exportTx = flarejs_1.evm.newExportTxFromBaseFee(context, fee / BigInt(FLR), BigInt(params.amount), context.pBlockchainID, flarejs_1.utils.hexToBuffer(ctx.cAddressHex), [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], BigInt(txCount)); await (0, flarejs_1.addTxSignatures)({ unsignedTx: exportTx, privateKeys: [flarejs_1.utils.hexToBuffer(ctx.privkHex)], }); return { txid: (await evmapi.issueSignedTx(exportTx.getSignedTx())).txID }; } async function importCP(ctx, params) { if (!ctx.pAddressBech32) { throw new Error("pAddressBech32 is undefined or null"); } if (!ctx.cAddressBech32) { throw new Error("cAddressBech32 is undefined or null"); } if (!ctx.privkHex) { throw new Error("privkHex is undefined or null"); } const pvmapi = new flarejs_1.pvm.PVMApi(settings.URL[ctx.config.hrp]); const feeState = await pvmapi.getFeeState(); const context = await flarejs_1.Context.getContextFromURI(settings.URL[ctx.config.hrp]); const isEtnaForkActive = await (0, context_1.isEtnaActive)(ctx.config.hrp); const { utxos } = await pvmapi.getUTXOs({ sourceChain: "C", addresses: [ctx.pAddressBech32], }); let importTx; if (isEtnaForkActive) { importTx = flarejs_1.pvm.e.newImportTx({ feeState, fromAddressesBytes: [flarejs_1.utils.bech32ToBytes(ctx.cAddressBech32)], sourceChainId: getChainIdFromContext("C", context), toAddressesBytes: [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], utxos, }, context); } else { importTx = flarejs_1.pvm.newImportTx(context, getChainIdFromContext("C", context), utxos, [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], [flarejs_1.utils.bech32ToBytes(ctx.cAddressBech32)]); } await (0, flarejs_1.addTxSignatures)({ unsignedTx: importTx, privateKeys: [flarejs_1.utils.hexToBuffer(ctx.privkHex)], }); return { txid: (await pvmapi.issueSignedTx(importTx.getSignedTx())).txID }; } async function exportPC(ctx, params) { if (!ctx.pAddressBech32) { throw new Error("pAddressBech32 is undefined or null"); } if (!ctx.privkHex) { throw new Error("privkHex is undefined or null"); } if (!params.amount) { throw new Error("amount is required"); } const pvmapi = new flarejs_1.pvm.PVMApi(settings.URL[ctx.config.hrp]); const context = await flarejs_1.Context.getContextFromURI(settings.URL[ctx.config.hrp]); const feeState = await pvmapi.getFeeState(); const isEtnaForkActive = await (0, context_1.isEtnaActive)(ctx.config.hrp); const { utxos } = await pvmapi.getUTXOs({ addresses: [ctx.pAddressBech32], }); let exportTx; if (isEtnaForkActive) { exportTx = flarejs_1.pvm.e.newExportTx({ destinationChainId: getChainIdFromContext("C", context), feeState, fromAddressesBytes: [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], outputs: [ flarejs_1.TransferableOutput.fromNative(context.avaxAssetID, BigInt(params.amount), [ flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32), ]), ], utxos, }, context); } else { exportTx = flarejs_1.pvm.newExportTx(context, getChainIdFromContext("C", context), [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], utxos, [ flarejs_1.TransferableOutput.fromNative(context.avaxAssetID, BigInt(params.amount), [ flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32), ]), ]); } await (0, flarejs_1.addTxSignatures)({ unsignedTx: exportTx, privateKeys: [flarejs_1.utils.hexToBuffer(ctx.privkHex)], }); return { txid: (await pvmapi.issueSignedTx(exportTx.getSignedTx())).txID }; } async function importPC(ctx, params) { if (!ctx.pAddressBech32) { throw new Error("pAddressBech32 is undefined or null"); } if (!ctx.cAddressBech32) { throw new Error("cAddressBech32 is undefined or null"); } if (!ctx.privkHex) { throw new Error("privkHex is undefined or null"); } if (!ctx.cAddressHex) { throw new Error("cAddressHex is undefined or null"); } const evmapi = new flarejs_1.evm.EVMApi(settings.URL[ctx.config.hrp]); const context = await flarejs_1.Context.getContextFromURI(settings.URL[ctx.config.hrp]); const { utxos } = await evmapi.getUTXOs({ sourceChain: "P", addresses: [ctx.cAddressBech32], }); const baseFee = await evmapi.getBaseFee(); const fee = !params.fee || BigInt(params.fee) === 0n || BigInt(params.fee) < baseFee ? baseFee : BigInt(params.fee); const tx = flarejs_1.evm.newImportTxFromBaseFee(context, flarejs_1.utils.hexToBuffer(ctx.cAddressHex), [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], utxos, getChainIdFromContext("P", context), fee / BigInt(FLR)); await (0, flarejs_1.addTxSignatures)({ unsignedTx: tx, privateKeys: [flarejs_1.utils.hexToBuffer(ctx.privkHex)], }); return { txid: (await evmapi.issueSignedTx(tx.getSignedTx())).txID }; } async function addValidator(ctx, params) { if (!ctx.pAddressBech32) { throw new Error("pAddressBech32 is undefined or null"); } if (!ctx.privkHex) { throw new Error("privkHex is undefined or null"); } if (!params.amount) { throw new Error("amount is required"); } if (!params.nodeId) { throw new Error("nodeId is required"); } if (!params.popBlsPublicKey) { throw new Error("popBlsPublicKey is required"); } if (!params.popBlsSignature) { throw new Error("popBlsSignature is required"); } if (!params.endTime) { throw new Error("endTime is required"); } if (!ctx.publicKey) { throw new Error("publicKey is undefined or null"); } if (!ctx.network) { throw new Error("network is undefined or null"); } const pvmapi = new flarejs_1.pvm.PVMApi(settings.URL[ctx.config.hrp]); const context = await flarejs_1.Context.getContextFromURI(settings.URL[ctx.config.hrp]); const feeState = await pvmapi.getFeeState(); const isEtnaForkActive = await (0, context_1.isEtnaActive)(ctx.config.hrp); const { utxos } = await pvmapi.getUTXOs({ addresses: [ctx.pAddressBech32] }); const start = BigInt((0, utils_1.adjustStartTime)(params.startTime)); const end = BigInt(params.endTime); const nodeId = params.nodeId; const publicKey = flarejs_1.utils.hexToBuffer(params.popBlsPublicKey); const signature = flarejs_1.utils.hexToBuffer(params.popBlsSignature); const pk = Buffer.concat(ctx.publicKey).toString("hex"); const account = (0, flare_1._getAccount)(ctx.network, pk); const stakes = await chain.getPStakes(account.network); await (0, flare_1._checkNumberOfStakes)(account, params.nodeId, new bn_js_1.BN(start.toString()), new bn_js_1.BN(params.endTime), stakes); let tx; if (isEtnaForkActive) { tx = flarejs_1.pvm.e.newAddPermissionlessValidatorTx({ end, delegatorRewardsOwner: [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], feeState, fromAddressesBytes: [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], nodeId, publicKey, rewardAddresses: [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], shares: Number(params.delegationFee) * 1e4, // default fee is 10% signature, start, subnetId: flarejs_1.networkIDs.PrimaryNetworkID.toString(), utxos, weight: BigInt(params.amount), }, context); } else { tx = flarejs_1.pvm.newAddPermissionlessValidatorTx(context, utxos, [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], nodeId, flarejs_1.networkIDs.PrimaryNetworkID.toString(), start, end, BigInt(params.amount), [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], Number(params.delegationFee) * 1e4, // default fee is 10% undefined, 1, 0n, publicKey, signature); } await (0, flarejs_1.addTxSignatures)({ unsignedTx: tx, privateKeys: [flarejs_1.utils.hexToBuffer(ctx.privkHex)], }); return { txid: (await pvmapi.issueSignedTx(tx.getSignedTx())).txID }; } async function addDelegator(ctx, params) { if (!ctx.pAddressBech32) { throw new Error("pAddressBech32 is undefined or null"); } if (!ctx.privkHex) { throw new Error("privkHex is undefined or null"); } if (!params.amount) { throw new Error("amount is required"); } if (!params.nodeId) { throw new Error("nodeId is required"); } if (!params.endTime) { throw new Error("endTime is required"); } if (!ctx.publicKey) { throw new Error("publicKey is undefined or null"); } if (!ctx.network) { throw new Error("network is undefined or null"); } const pvmapi = new flarejs_1.pvm.PVMApi(settings.URL[ctx.config.hrp]); const context = await flarejs_1.Context.getContextFromURI(settings.URL[ctx.config.hrp]); const feeState = await pvmapi.getFeeState(); const isEtnaForkActive = await (0, context_1.isEtnaActive)(ctx.config.hrp); const { utxos } = await pvmapi.getUTXOs({ addresses: [ctx.pAddressBech32] }); const start = BigInt((0, utils_1.adjustStartTime)(params.startTime)); const end = BigInt(params.endTime); const nodeId = params.nodeId; const pk = Buffer.concat(ctx.publicKey).toString("hex"); const account = (0, flare_1._getAccount)(ctx.network, pk); const stakes = await chain.getPStakes(account.network); await (0, flare_1._checkNumberOfStakes)(account, params.nodeId, new bn_js_1.BN(start.toString()), new bn_js_1.BN(params.endTime), stakes); await (0, flare_1._checkNodeId)(account, params.nodeId, stakes); let tx; if (isEtnaForkActive) { tx = flarejs_1.pvm.e.newAddPermissionlessDelegatorTx({ end, feeState, fromAddressesBytes: [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], nodeId, rewardAddresses: [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], start, subnetId: flarejs_1.networkIDs.PrimaryNetworkID.toString(), utxos, weight: BigInt(params.amount), }, context); } else { tx = flarejs_1.pvm.newAddPermissionlessDelegatorTx(context, utxos, [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)], nodeId, flarejs_1.networkIDs.PrimaryNetworkID.toString(), start, end, BigInt(params.amount), [flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32)]); } await (0, flarejs_1.addTxSignatures)({ unsignedTx: tx, privateKeys: [flarejs_1.utils.hexToBuffer(ctx.privkHex)], }); return { txid: (await pvmapi.issueSignedTx(tx.getSignedTx())).txID }; } async function internalTransfer(ctx, params) { if (!ctx.pAddressBech32) { throw new Error("pAddressBech32 is undefined or null"); } if (!params.transferAddress) { throw new Error("transferAddress is required"); } if (!params.amount) { throw new Error("amount is required"); } if (!ctx.privkHex) { throw new Error("privkHex is undefined or null"); } const pvmapi = new flarejs_1.pvm.PVMApi(settings.URL[ctx.config.hrp]); const context = await flarejs_1.Context.getContextFromURI(settings.URL[ctx.config.hrp]); const feeState = await pvmapi.getFeeState(); const isEtnaForkActive = await (0, context_1.isEtnaActive)(ctx.config.hrp); const { utxos } = await pvmapi.getUTXOs({ addresses: [ctx.pAddressBech32] }); const senderPAddressBytes = flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32); const recipientPAddressBytes = flarejs_1.utils.bech32ToBytes(params.transferAddress); let tx; if (isEtnaForkActive) { tx = flarejs_1.pvm.e.newBaseTx({ feeState, fromAddressesBytes: [senderPAddressBytes], outputs: [flarejs_1.TransferableOutput.fromNative(context.avaxAssetID, BigInt(params.amount), [recipientPAddressBytes])], utxos, }, context); } else { tx = flarejs_1.pvm.newBaseTx(context, [senderPAddressBytes], utxos, [ flarejs_1.TransferableOutput.fromNative(context.avaxAssetID, BigInt(params.amount), [recipientPAddressBytes]), ]); } await (0, flarejs_1.addTxSignatures)({ unsignedTx: tx, privateKeys: [flarejs_1.utils.hexToBuffer(ctx.privkHex)], }); return { txid: (await pvmapi.issueSignedTx(tx.getSignedTx())).txID }; } //# sourceMappingURL=transaction.js.map