UNPKG

@flarenetwork/flare-stake-tool

Version:
278 lines 12.8 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 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 context = await flarejs_1.Context.getContextFromURI(settings.URL[ctx.config.hrp]); const { utxos } = await pvmapi.getUTXOs({ sourceChain: 'C', addresses: [ctx.pAddressBech32] }); const 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 { utxos } = await pvmapi.getUTXOs({ addresses: [ctx.pAddressBech32] }); const 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 { 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 blsPublicKey = flarejs_1.utils.hexToBuffer(params.popBlsPublicKey); const blsSignature = flarejs_1.utils.hexToBuffer(params.popBlsSignature); const pk = Buffer.concat(ctx.publicKey).toString('hex'); const account = (0, flare_1._getAccount)(ctx.network, pk); let 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); const 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, blsPublicKey, blsSignature); 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 { 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); let stakes = await chain.getPStakes(account.network); await (0, flare_1._checkNumberOfStakes)(account, params.nodeId, new bn_js_1.BN(utils_1.adjustStartTime.toString()), new bn_js_1.BN(params.endTime), stakes); await (0, flare_1._checkNodeId)(account, params.nodeId, stakes); const 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 { utxos } = await pvmapi.getUTXOs({ addresses: [ctx.pAddressBech32] }); const pChainAddressBytes = flarejs_1.utils.bech32ToBytes(ctx.pAddressBech32); const pChainTransferAddressBytes = flarejs_1.utils.bech32ToBytes(params.transferAddress); const tx = flarejs_1.pvm.newBaseTx(context, [pChainAddressBytes], utxos, [ flarejs_1.TransferableOutput.fromNative(context.avaxAssetID, BigInt(params.amount), [ pChainTransferAddressBytes, ]), ]); 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