import { Lux, BinTools, BN, Buffer } from "lux/dist" import { PlatformVMAPI, KeyChain, SECPTransferOutput, SECPTransferInput, TransferableOutput, TransferableInput, UTXOSet, UTXO, AmountOutput, UnsignedTx, AddValidatorTx, Tx, SECPOwnerOutput, ParseableOutput } from "lux/dist/apis/platformvm" import { Output } from "lux/dist/common" import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey, Defaults, NodeIDStringToBuffer, UnixNow } from "lux/dist/utils" const ip: string = "localhost" const port: number = 9650 const protocol: string = "http" const networkID: number = 1337 const lux: Lux = new Lux(ip, port, protocol, networkID) const pchain: PlatformVMAPI = lux.PChain() const bintools: BinTools = BinTools.getInstance() const pKeychain: KeyChain = pchain.keyChain() const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` pKeychain.importKey(privKey) const pAddresses: Buffer[] = pchain.keyChain().getAddresses() const pAddressStrings: string[] = pchain.keyChain().getAddressStrings() const pChainBlockchainID: string = Defaults.network[networkID].P.blockchainID const outputs: TransferableOutput[] = [] const inputs: TransferableInput[] = [] const stakeOuts: TransferableOutput[] = [] const fee: BN = pchain.getDefaultTxFee() const threshold: number = 1 const locktime: BN = new BN(0) const memo: Buffer = Buffer.from( "Manually add a validator to the primary subnet" ) const nodeID: string = "NodeID-DueWyGi3B9jtKfa9mPoecd4YSDJ1ftF69" const startTime: BN = UnixNow().add(new BN(60 * 1)) const endTime: BN = startTime.add(new BN(26300000)) const delegationFee: number = 10 const main = async (): Promise => { const stakeAmount: any = await pchain.getMinStake() const luxAssetID: Buffer = await pchain.getLUXAssetID() const getBalanceResponse: any = await pchain.getBalance(pAddressStrings[0]) const unlocked: BN = new BN(getBalanceResponse.unlocked) const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput( unlocked.sub(fee).sub(stakeAmount.minValidatorStake), pAddresses, locktime, threshold ) const transferableOutput: TransferableOutput = new TransferableOutput( luxAssetID, secpTransferOutput ) outputs.push(transferableOutput) const stakeSECPTransferOutput: SECPTransferOutput = new SECPTransferOutput( stakeAmount.minValidatorStake, pAddresses, locktime, threshold ) const stakeTransferableOutput: TransferableOutput = new TransferableOutput( luxAssetID, stakeSECPTransferOutput ) stakeOuts.push(stakeTransferableOutput) const rewardOutputOwners: SECPOwnerOutput = new SECPOwnerOutput( pAddresses, locktime, threshold ) const rewardOwners: ParseableOutput = new ParseableOutput(rewardOutputOwners) const platformVMUTXOResponse: any = await pchain.getUTXOs(pAddressStrings) const utxoSet: UTXOSet = platformVMUTXOResponse.utxos const utxos: UTXO[] = utxoSet.getAllUTXOs() utxos.forEach((utxo: UTXO) => { const output: Output = utxo.getOutput() if (output.getOutputID() === 7) { const amountOutput: AmountOutput = utxo.getOutput() as AmountOutput const amt: BN = amountOutput.getAmount().clone() const txid: Buffer = utxo.getTxID() const outputidx: Buffer = utxo.getOutputIdx() const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt) secpTransferInput.addSignatureIdx(0, pAddresses[0]) const input: TransferableInput = new TransferableInput( txid, outputidx, luxAssetID, secpTransferInput ) inputs.push(input) } }) const addValidatorTx: AddValidatorTx = new AddValidatorTx( networkID, bintools.cb58Decode(pChainBlockchainID), outputs, inputs, memo, NodeIDStringToBuffer(nodeID), startTime, endTime, stakeAmount.minValidatorStake, stakeOuts, rewardOwners, delegationFee ) const unsignedTx: UnsignedTx = new UnsignedTx(addValidatorTx) const tx: Tx = unsignedTx.sign(pKeychain) const txid: string = await pchain.issueTx(tx) console.log(`Success! TXID: ${txid}`) } main()