PHP WebShell
Текущая директория: /usr/lib/node_modules/bitgo/node_modules/avalanche/examples/platformvm
Просмотр файла: addDelegatorTx.ts
import { Avalanche, BinTools, BN, Buffer } from "avalanche/dist"
import {
PlatformVMAPI,
KeyChain,
SECPTransferOutput,
SECPTransferInput,
TransferableOutput,
TransferableInput,
UTXOSet,
UTXO,
AmountOutput,
UnsignedTx,
AddDelegatorTx,
Tx,
SECPOwnerOutput,
ParseableOutput
} from "avalanche/dist/apis/platformvm"
import { Output } from "avalanche/dist/common"
import {
PrivateKeyPrefix,
DefaultLocalGenesisPrivateKey,
Defaults,
NodeIDStringToBuffer,
UnixNow
} from "avalanche/dist/utils"
const ip: string = "localhost"
const port: number = 9650
const protocol: string = "http"
const networkID: number = 1337
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
const pchain: PlatformVMAPI = avalanche.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 delegator 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(2630000))
const main = async (): Promise<any> => {
const stakeAmount: any = await pchain.getMinStake()
const avaxAssetID: Buffer = await pchain.getAVAXAssetID()
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(
avaxAssetID,
secpTransferOutput
)
outputs.push(transferableOutput)
const stakeSECPTransferOutput: SECPTransferOutput = new SECPTransferOutput(
stakeAmount.minValidatorStake,
pAddresses,
locktime,
threshold
)
const stakeTransferableOutput: TransferableOutput = new TransferableOutput(
avaxAssetID,
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,
avaxAssetID,
secpTransferInput
)
inputs.push(input)
}
})
const addDelegatorTx: AddDelegatorTx = new AddDelegatorTx(
networkID,
bintools.cb58Decode(pChainBlockchainID),
outputs,
inputs,
memo,
NodeIDStringToBuffer(nodeID),
startTime,
endTime,
stakeAmount.minDelegatorStake,
stakeOuts,
rewardOwners
)
const unsignedTx: UnsignedTx = new UnsignedTx(addDelegatorTx)
const tx: Tx = unsignedTx.sign(pKeychain)
const txid: string = await pchain.issueTx(tx)
console.log(`Success! TXID: ${txid}`)
}
main()
Выполнить команду
Для локальной разработки. Не используйте в интернете!