Created
September 9, 2020 13:59
-
-
Save cgcardona/d6831ad0fbdd0e807fe4c48e375c4e5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PlatformVM AddDelegatorTx | |
// More info: https://docs.avax.network/v1.0/en/references/platform-transaction-serialization/#what-unsigned-add-delegator-tx-contains | |
import { Avalanche, BinTools, BN } from "avalanche" | |
import { Buffer } from 'buffer/' | |
import { | |
AddDelegatorTx, | |
AmountOutput, | |
ParseableOutput, | |
PlatformVMAPI, | |
PlatformVMConstants, | |
PlatformVMKeyChain, | |
SecpInput, | |
SecpOwnerOutput, | |
SecpTransferOutput, | |
TransferableInput, | |
TransferableOutput, | |
Tx, | |
UnsignedTx, | |
UTXO, | |
UTXOSet, | |
} from "avalanche/dist/apis/platformvm" | |
import { UnixNow } from "avalanche/dist/utils" | |
const ip: string = "localhost" | |
const protocol: string = "http" | |
const networkID: number = 12345 | |
const port: number = 9650 | |
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID) | |
const bintools: BinTools = BinTools.getInstance() | |
const platformvm: PlatformVMAPI = avalanche.PChain() | |
const blockchainID: Buffer = bintools.cb58Decode(platformvm.getBlockchainID()) | |
const pKeychain: PlatformVMKeyChain = platformvm.keyChain() | |
const memo: Buffer = bintools.stringToBuffer("Avalanche.js") | |
pKeychain.importKey("PrivateKey-") | |
const pAddresses: Buffer[] = platformvm.keyChain().getAddresses() | |
const pAddressStrings: string[] = platformvm.keyChain().getAddressStrings() | |
console.log(pAddressStrings) | |
const locktime: BN = new BN(0) | |
const threshold: number = 1 | |
const ins: TransferableInput[] = [] | |
const outs: TransferableOutput[] = [] | |
const stakeOuts: TransferableOutput[] = [] | |
const main = async (): Promise<any> => { | |
let assetID: Buffer = await platformvm.getAVAXAssetID() | |
let result: any = await platformvm.getBalance(pAddressStrings[0]) | |
let balance: BN = new BN(result.balance) | |
let fee: BN = platformvm.getFee() | |
const stakeAmount: BN = PlatformVMConstants.MINSTAKE | |
let avaxAmount: BN = balance.sub(fee).sub(stakeAmount) | |
const startTime: BN = UnixNow().add(new BN(60)) | |
const endTime: BN = startTime.add(new BN(60 * 60 * 24 * 2)) | |
const nodeID: string = "5mb46qkSBj81k9g9e4VFjGGSbaaSLFRzD" | |
const utxoSet: UTXOSet = await platformvm.getUTXOs([pAddressStrings[0]]) | |
const utxo: UTXO = utxoSet.getAllUTXOs()[0] | |
const amountOutput: AmountOutput = utxo.getOutput() as AmountOutput | |
let amt: BN = amountOutput.getAmount().clone() | |
const txid: Buffer = utxo.getTxID() | |
const outputidx: Buffer = utxo.getOutputIdx() | |
const secpTransferOutput: SecpTransferOutput = new SecpTransferOutput(avaxAmount, pAddresses, locktime, threshold) | |
const transferableOutput: TransferableOutput = new TransferableOutput(assetID, secpTransferOutput) | |
outs.push(transferableOutput) | |
const stakeSECPTransferOutput: SecpTransferOutput = new SecpTransferOutput(stakeAmount, pAddresses, locktime, threshold) | |
const stakeTransferableOutput: TransferableOutput = new TransferableOutput(assetID, stakeSECPTransferOutput) | |
stakeOuts.push(stakeTransferableOutput) | |
const platformVMSecpInput: SecpInput = new SecpInput(amt) | |
platformVMSecpInput.addSignatureIdx(0, pAddresses[0]) | |
const transferableInput: TransferableInput = new TransferableInput(txid, outputidx, assetID, platformVMSecpInput) | |
ins.push(transferableInput) | |
const rewardOutputOwners: SecpOwnerOutput = new SecpOwnerOutput(pAddresses, locktime, threshold) | |
const rewardOwners: ParseableOutput = new ParseableOutput(rewardOutputOwners) | |
const addDelegatorTx: AddDelegatorTx = new AddDelegatorTx(networkID, blockchainID, outs, ins, memo, bintools.cb58Decode(nodeID), startTime, endTime, stakeAmount, stakeOuts, rewardOwners) | |
const unsignedTx: UnsignedTx = new UnsignedTx(addDelegatorTx) | |
const tx: Tx = unsignedTx.sign(pKeychain) | |
const id: string = await platformvm.issueTx(tx) | |
console.log(id) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment