Skip to content

Commit 2e47aa0

Browse files
committed
PlatformVM example scripts.
1 parent 0a93dc1 commit 2e47aa0

File tree

8 files changed

+670
-0
lines changed

8 files changed

+670
-0
lines changed

examples/avm/exportTx-avax-pchain.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src";
7+
import {
8+
AVMAPI,
9+
KeyChain as AVMKeyChain,
10+
SECPTransferOutput,
11+
SECPTransferInput,
12+
TransferableOutput,
13+
TransferableInput,
14+
UTXOSet,
15+
UTXO,
16+
AmountOutput,
17+
UnsignedTx,
18+
Tx,
19+
ExportTx
20+
} from "../../src/apis/avm"
21+
import { Defaults } from "../../src/utils"
22+
23+
const ip: string = "localhost"
24+
const port: number = 9650
25+
const protocol: string = "http"
26+
const networkID: number = 12345
27+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
28+
const xchain: AVMAPI = avalanche.XChain()
29+
const bintools: BinTools = BinTools.getInstance()
30+
const xKeychain: AVMKeyChain = xchain.keyChain()
31+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
32+
xKeychain.importKey(privKey)
33+
const xAddresses: Buffer[] = xchain.keyChain().getAddresses()
34+
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings()
35+
const blockchainid: string = Defaults.network['12345'].X.blockchainID
36+
const pChainBlockchainID: string = Defaults.network['12345'].P.blockchainID
37+
const exportedOuts: TransferableOutput[] = []
38+
const outputs: TransferableOutput[] = []
39+
const inputs: TransferableInput[] = []
40+
const fee: BN = xchain.getDefaultTxFee()
41+
const threshold: number = 1
42+
const locktime: BN = new BN(0)
43+
const memo: Buffer = bintools.stringToBuffer("Manually Export AVAX from X-Chain to P-Chain")
44+
45+
const main = async (): Promise<any> => {
46+
const avaxAssetID: Buffer = await xchain.getAVAXAssetID()
47+
const getBalanceResponse: any = await xchain.getBalance(xAddressStrings[0], bintools.cb58Encode(avaxAssetID))
48+
const balance: BN = new BN(getBalanceResponse.balance)
49+
const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput(balance.sub(fee), xAddresses, locktime, threshold)
50+
const transferableOutput: TransferableOutput = new TransferableOutput(avaxAssetID, secpTransferOutput)
51+
exportedOuts.push(transferableOutput)
52+
53+
const avmUTXOResponse: any = await xchain.getUTXOs(xAddressStrings)
54+
const utxoSet: UTXOSet = avmUTXOResponse.utxos
55+
const utxos: UTXO[] = utxoSet.getAllUTXOs()
56+
utxos.forEach((utxo: UTXO) => {
57+
const amountOutput: AmountOutput = utxo.getOutput() as AmountOutput
58+
const amt: BN = amountOutput.getAmount().clone()
59+
const txid: Buffer = utxo.getTxID()
60+
const outputidx: Buffer = utxo.getOutputIdx()
61+
62+
const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt)
63+
secpTransferInput.addSignatureIdx(0, xAddresses[0])
64+
65+
const input: TransferableInput = new TransferableInput(txid, outputidx, avaxAssetID, secpTransferInput)
66+
inputs.push(input)
67+
})
68+
69+
const exportTx: ExportTx = new ExportTx(
70+
networkID,
71+
bintools.cb58Decode(blockchainid),
72+
outputs,
73+
inputs,
74+
memo,
75+
bintools.cb58Decode(pChainBlockchainID),
76+
exportedOuts
77+
)
78+
const unsignedTx: UnsignedTx = new UnsignedTx(exportTx)
79+
const tx: Tx = unsignedTx.sign(xKeychain)
80+
const id: string = await xchain.issueTx(tx)
81+
console.log(id)
82+
}
83+
84+
main()
85+

examples/avm/importTx-avax-pchain.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src";
7+
import {
8+
AVMAPI,
9+
KeyChain as AVMKeyChain,
10+
SECPTransferOutput,
11+
SECPTransferInput,
12+
TransferableOutput,
13+
TransferableInput,
14+
UTXOSet,
15+
UTXO,
16+
AmountOutput,
17+
UnsignedTx,
18+
Tx,
19+
ImportTx
20+
} from "../../src/apis/avm"
21+
import { Defaults } from "../../src/utils"
22+
23+
const ip: string = "localhost"
24+
const port: number = 9650
25+
const protocol: string = "http"
26+
const networkID: number = 12345
27+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
28+
const xchain: AVMAPI = avalanche.XChain()
29+
const bintools: BinTools = BinTools.getInstance()
30+
const xKeychain: AVMKeyChain = xchain.keyChain()
31+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
32+
xKeychain.importKey(privKey)
33+
const xAddresses: Buffer[] = xchain.keyChain().getAddresses()
34+
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings()
35+
const blockchainid: string = Defaults.network['12345'].X.blockchainID
36+
const cChainBlockchainID: string = Defaults.network['12345'].P.blockchainID
37+
const importedInputs: TransferableInput[] = []
38+
const outputs: TransferableOutput[] = []
39+
const inputs: TransferableInput[] = []
40+
const fee: BN = xchain.getDefaultTxFee()
41+
const threshold: number = 1
42+
const locktime: BN = new BN(0)
43+
const memo: Buffer = bintools.stringToBuffer("Manually Import AVAX to the X-Chain from the P-Chain")
44+
// Uncomment for codecID 00 01
45+
// const codecID: number = 1
46+
47+
const main = async (): Promise<any> => {
48+
const avaxAssetID: Buffer = await xchain.getAVAXAssetID()
49+
const avmUTXOResponse: any = await xchain.getUTXOs(xAddressStrings, cChainBlockchainID)
50+
const utxoSet: UTXOSet = avmUTXOResponse.utxos
51+
const utxo: UTXO = utxoSet.getAllUTXOs()[0]
52+
const amountOutput: AmountOutput = utxo.getOutput() as AmountOutput
53+
const amt: BN = amountOutput.getAmount().clone()
54+
const txid: Buffer = utxo.getTxID()
55+
const outputidx: Buffer = utxo.getOutputIdx()
56+
57+
const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt)
58+
secpTransferInput.addSignatureIdx(0, xAddresses[0])
59+
// Uncomment for codecID 00 01
60+
// secpTransferInput.setCodecID(codecID)
61+
62+
const input: TransferableInput = new TransferableInput(txid, outputidx, avaxAssetID, secpTransferInput)
63+
importedInputs.push(input)
64+
65+
const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput(amt.sub(fee), xAddresses, locktime, threshold)
66+
// Uncomment for codecID 00 01
67+
// secpTransferOutput.setCodecID(codecID)
68+
const transferableOutput: TransferableOutput = new TransferableOutput(avaxAssetID, secpTransferOutput)
69+
outputs.push(transferableOutput)
70+
71+
const importTx: ImportTx = new ImportTx(
72+
networkID,
73+
bintools.cb58Decode(blockchainid),
74+
outputs,
75+
inputs,
76+
memo,
77+
bintools.cb58Decode(cChainBlockchainID),
78+
importedInputs
79+
)
80+
// Uncomment for codecID 00 01
81+
// importTx.setCodecID(codecID)
82+
83+
const unsignedTx: UnsignedTx = new UnsignedTx(importTx)
84+
const tx: Tx = unsignedTx.sign(xKeychain)
85+
const id: string = await xchain.issueTx(tx)
86+
console.log(id)
87+
}
88+
89+
main()
90+

examples/platformvm/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# PlatformVM
2+
3+
Tests for the Avalanche [PlatformVM RPC](https://docs.avax.network/build/avalanchego-apis/platform-chain-p-chain-api)
4+
5+
* [addDelegatorTx.ts](./addDelegatorTx.ts)
6+
* [addValidatorTx.ts](./addValidatorTx.ts)
7+
* [createSubnetTx.ts](./createSubnetTx.ts)
8+
* [exportTx-avax-xchain.ts](./exportTx-avax-xchain.ts)
9+
* [importTx-avax-xchain.ts](./importTx-avax-xchain.ts)

examples/platformvm/addDelegatorTx.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src";
7+
import {
8+
PlatformVMAPI,
9+
KeyChain,
10+
SECPTransferOutput,
11+
SECPTransferInput,
12+
TransferableOutput,
13+
TransferableInput,
14+
UTXOSet,
15+
UTXO,
16+
AmountOutput,
17+
UnsignedTx,
18+
AddDelegatorTx,
19+
Tx,
20+
SECPOwnerOutput,
21+
ParseableOutput
22+
} from "../../src/apis/platformvm"
23+
import { Output } from "../../src/common";
24+
import { Defaults, UnixNow } from "../../src/utils"
25+
26+
const ip: string = "localhost"
27+
const port: number = 9650
28+
const protocol: string = "http"
29+
const networkID: number = 12345
30+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
31+
const pchain: PlatformVMAPI = avalanche.PChain()
32+
const bintools: BinTools = BinTools.getInstance()
33+
const pKeychain: KeyChain = pchain.keyChain()
34+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
35+
pKeychain.importKey(privKey)
36+
const pAddresses: Buffer[] = pchain.keyChain().getAddresses()
37+
const pAddressStrings: string[] = pchain.keyChain().getAddressStrings()
38+
const pChainBlockchainID: string = Defaults.network['12345'].P.blockchainID
39+
const outputs: TransferableOutput[] = []
40+
const inputs: TransferableInput[] = []
41+
const stakeOuts: TransferableOutput[] = []
42+
const fee: BN = pchain.getDefaultTxFee()
43+
const threshold: number = 1
44+
const locktime: BN = new BN(0)
45+
const memo: Buffer = bintools.stringToBuffer("Manually add a delegator to the primary subnet")
46+
const nodeID: string = "DueWyGi3B9jtKfa9mPoecd4YSDJ1ftF69"
47+
const startTime: BN = UnixNow().add(new BN(60 * 1))
48+
const endTime: BN = startTime.add(new BN(2630000))
49+
50+
const main = async (): Promise<any> => {
51+
const stakeAmount: any = await pchain.getMinStake()
52+
const avaxAssetID: Buffer = await pchain.getAVAXAssetID()
53+
const getBalanceResponse: any = await pchain.getBalance(pAddressStrings[0])
54+
const unlocked: BN = new BN(getBalanceResponse.unlocked)
55+
const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput(unlocked.sub(fee).sub(stakeAmount.minValidatorStake), pAddresses, locktime, threshold)
56+
const transferableOutput: TransferableOutput = new TransferableOutput(avaxAssetID, secpTransferOutput)
57+
outputs.push(transferableOutput)
58+
59+
const stakeSECPTransferOutput: SECPTransferOutput = new SECPTransferOutput(stakeAmount.minValidatorStake, pAddresses, locktime, threshold)
60+
const stakeTransferableOutput: TransferableOutput = new TransferableOutput(avaxAssetID, stakeSECPTransferOutput)
61+
stakeOuts.push(stakeTransferableOutput)
62+
63+
const rewardOutputOwners: SECPOwnerOutput = new SECPOwnerOutput(pAddresses, locktime, threshold)
64+
const rewardOwners: ParseableOutput = new ParseableOutput(rewardOutputOwners)
65+
66+
const platformVMUTXOResponse: any = await pchain.getUTXOs(pAddressStrings)
67+
const utxoSet: UTXOSet = platformVMUTXOResponse.utxos
68+
const utxos: UTXO[] = utxoSet.getAllUTXOs()
69+
utxos.forEach((utxo: UTXO) => {
70+
const output: Output = utxo.getOutput()
71+
if(output.getOutputID() === 7) {
72+
const amountOutput: AmountOutput = utxo.getOutput() as AmountOutput
73+
const amt: BN = amountOutput.getAmount().clone()
74+
const txid: Buffer = utxo.getTxID()
75+
const outputidx: Buffer = utxo.getOutputIdx()
76+
77+
const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt)
78+
secpTransferInput.addSignatureIdx(0, pAddresses[0])
79+
80+
const input: TransferableInput = new TransferableInput(txid, outputidx, avaxAssetID, secpTransferInput)
81+
inputs.push(input)
82+
}
83+
})
84+
85+
const addDelegatorTx: AddDelegatorTx = new AddDelegatorTx(
86+
networkID,
87+
bintools.cb58Decode(pChainBlockchainID),
88+
outputs,
89+
inputs,
90+
memo,
91+
bintools.cb58Decode(nodeID),
92+
startTime,
93+
endTime,
94+
stakeAmount.minDelegatorStake,
95+
stakeOuts,
96+
rewardOwners
97+
)
98+
99+
const unsignedTx: UnsignedTx = new UnsignedTx(addDelegatorTx)
100+
const tx: Tx = unsignedTx.sign(pKeychain)
101+
const id: string = await pchain.issueTx(tx)
102+
console.log(id)
103+
}
104+
105+
main()
106+

0 commit comments

Comments
 (0)