Skip to content

Commit d6b666d

Browse files
committed
PlaformVM high level utility methods.
1 parent a16452c commit d6b666d

File tree

8 files changed

+318
-16
lines changed

8 files changed

+318
-16
lines changed

examples/platformvm/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Tests for the Avalanche [PlatformVM RPC](https://docs.avax.network/build/avalanc
44

55
* [addDelegatorTx.ts](./addDelegatorTx.ts)
66
* [addValidatorTx.ts](./addValidatorTx.ts)
7+
* [buildAddDelegatorTx.ts](./buildAddDelegatorTx.ts)
8+
* [buildAddValidatorTx.ts](./buildAddValidatorTx.ts)
9+
* [buildCreateSubnetTx.ts](./buildCreateSubnetTx.ts)
10+
* [buildExportTx.ts](./buildExportTx.ts)
11+
* [buildImportTx.ts](./buildImportTx.ts)
712
* [createSubnetTx.ts](./createSubnetTx.ts)
813
* [exportTx-avax-xchain.ts](./exportTx-avax-xchain.ts)
914
* [importTx-avax-xchain.ts](./importTx-avax-xchain.ts)

examples/platformvm/addDelegatorTx.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
ParseableOutput
2222
} from "../../src/apis/platformvm"
2323
import { Output } from "../../src/common";
24-
import { Defaults, UnixNow } from "../../src/utils"
24+
import { Defaults, NodeIDStringToBuffer, UnixNow } from "../../src/utils"
2525

2626
const ip: string = "localhost"
2727
const port: number = 9650
@@ -43,7 +43,7 @@ const fee: BN = pchain.getDefaultTxFee()
4343
const threshold: number = 1
4444
const locktime: BN = new BN(0)
4545
const memo: Buffer = bintools.stringToBuffer("Manually add a delegator to the primary subnet")
46-
const nodeID: string = "DueWyGi3B9jtKfa9mPoecd4YSDJ1ftF69"
46+
const nodeID: string = "NodeID-DueWyGi3B9jtKfa9mPoecd4YSDJ1ftF69"
4747
const startTime: BN = UnixNow().add(new BN(60 * 1))
4848
const endTime: BN = startTime.add(new BN(2630000))
4949

@@ -83,17 +83,17 @@ const main = async (): Promise<any> => {
8383
})
8484

8585
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
86+
networkID,
87+
bintools.cb58Decode(pChainBlockchainID),
88+
outputs,
89+
inputs,
90+
memo,
91+
NodeIDStringToBuffer(nodeID),
92+
startTime,
93+
endTime,
94+
stakeAmount.minDelegatorStake,
95+
stakeOuts,
96+
rewardOwners
9797
)
9898

9999
const unsignedTx: UnsignedTx = new UnsignedTx(addDelegatorTx)

examples/platformvm/addValidatorTx.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
ParseableOutput
2222
} from "../../src/apis/platformvm"
2323
import { Output } from "../../src/common";
24-
import { Defaults, UnixNow } from "../../src/utils"
24+
import { Defaults, NodeIDStringToBuffer, UnixNow } from "../../src/utils"
2525

2626
const ip: string = "localhost"
2727
const port: number = 9650
@@ -43,7 +43,7 @@ const fee: BN = pchain.getDefaultTxFee()
4343
const threshold: number = 1
4444
const locktime: BN = new BN(0)
4545
const memo: Buffer = bintools.stringToBuffer("Manually add a validator to the primary subnet")
46-
const nodeID: string = "DueWyGi3B9jtKfa9mPoecd4YSDJ1ftF69"
46+
const nodeID: string = "NodeID-DueWyGi3B9jtKfa9mPoecd4YSDJ1ftF69"
4747
const startTime: BN = UnixNow().add(new BN(60 * 1))
4848
const endTime: BN = startTime.add(new BN(26300000))
4949
const delegationFee: number = 10
@@ -89,7 +89,7 @@ const main = async (): Promise<any> => {
8989
outputs,
9090
inputs,
9191
memo,
92-
bintools.cb58Decode(nodeID),
92+
NodeIDStringToBuffer(nodeID),
9393
startTime,
9494
endTime,
9595
stakeAmount.minValidatorStake,
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src";
7+
import {
8+
PlatformVMAPI,
9+
KeyChain,
10+
UTXOSet,
11+
UnsignedTx,
12+
Tx,
13+
} from "../../src/apis/platformvm"
14+
import { UnixNow } from "../../src/utils"
15+
16+
const ip: string = "localhost"
17+
const port: number = 9650
18+
const protocol: string = "http"
19+
const networkID: number = 12345
20+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
21+
const pchain: PlatformVMAPI = avalanche.PChain()
22+
const bintools: BinTools = BinTools.getInstance()
23+
const pKeychain: KeyChain = pchain.keyChain()
24+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
25+
pKeychain.importKey(privKey)
26+
const pAddressStrings: string[] = pchain.keyChain().getAddressStrings()
27+
const threshold: number = 1
28+
const locktime: BN = new BN(0)
29+
const memo: Buffer = bintools.stringToBuffer("PlatformVM utility method buildAddDelegatorTx to add a delegator to the primary subnet")
30+
const asOf: BN = UnixNow()
31+
const nodeID: string = "NodeID-DueWyGi3B9jtKfa9mPoecd4YSDJ1ftF69"
32+
const startTime: BN = UnixNow().add(new BN(60 * 1))
33+
const endTime: BN = startTime.add(new BN(2630000))
34+
35+
const main = async (): Promise<any> => {
36+
const stakeAmount: any = await pchain.getMinStake()
37+
const platformVMUTXOResponse: any = await pchain.getUTXOs(pAddressStrings)
38+
const utxoSet: UTXOSet = platformVMUTXOResponse.utxos
39+
40+
const unsignedTx: UnsignedTx = await pchain.buildAddDelegatorTx(
41+
utxoSet,
42+
pAddressStrings,
43+
pAddressStrings,
44+
pAddressStrings,
45+
nodeID,
46+
startTime,
47+
endTime,
48+
stakeAmount.minDelegatorStake,
49+
pAddressStrings,
50+
locktime,
51+
threshold,
52+
memo,
53+
asOf
54+
)
55+
56+
const tx: Tx = unsignedTx.sign(pKeychain)
57+
const id: string = await pchain.issueTx(tx)
58+
console.log(id)
59+
}
60+
61+
main()
62+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src";
7+
import {
8+
PlatformVMAPI,
9+
KeyChain,
10+
UTXOSet,
11+
UnsignedTx,
12+
Tx,
13+
} from "../../src/apis/platformvm"
14+
import { UnixNow } from "../../src/utils"
15+
16+
const ip: string = "localhost"
17+
const port: number = 9650
18+
const protocol: string = "http"
19+
const networkID: number = 12345
20+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
21+
const pchain: PlatformVMAPI = avalanche.PChain()
22+
const bintools: BinTools = BinTools.getInstance()
23+
const pKeychain: KeyChain = pchain.keyChain()
24+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
25+
pKeychain.importKey(privKey)
26+
const pAddressStrings: string[] = pchain.keyChain().getAddressStrings()
27+
const threshold: number = 1
28+
const locktime: BN = new BN(0)
29+
const memo: Buffer = bintools.stringToBuffer("PlatformVM utility method buildAddValidatorTx to add a validator to the primary subnet")
30+
const asOf: BN = UnixNow()
31+
const nodeID: string = "NodeID-DueWyGi3B9jtKfa9mPoecd4YSDJ1ftF69"
32+
const startTime: BN = UnixNow().add(new BN(60 * 1))
33+
const endTime: BN = startTime.add(new BN(26300000))
34+
const delegationFee: number = 10
35+
36+
const main = async (): Promise<any> => {
37+
const stakeAmount: any = await pchain.getMinStake()
38+
const platformVMUTXOResponse: any = await pchain.getUTXOs(pAddressStrings)
39+
const utxoSet: UTXOSet = platformVMUTXOResponse.utxos
40+
41+
const unsignedTx: UnsignedTx = await pchain.buildAddValidatorTx(
42+
utxoSet,
43+
pAddressStrings,
44+
pAddressStrings,
45+
pAddressStrings,
46+
nodeID,
47+
startTime,
48+
endTime,
49+
stakeAmount.minValidatorStake,
50+
pAddressStrings,
51+
delegationFee,
52+
locktime,
53+
threshold,
54+
memo,
55+
asOf
56+
)
57+
58+
const tx: Tx = unsignedTx.sign(pKeychain)
59+
const id: string = await pchain.issueTx(tx)
60+
console.log(id)
61+
}
62+
63+
main()
64+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src";
7+
import {
8+
PlatformVMAPI,
9+
KeyChain,
10+
UTXOSet,
11+
UnsignedTx,
12+
Tx,
13+
} from "../../src/apis/platformvm"
14+
import { UnixNow } from "../../src/utils";
15+
16+
const ip: string = "localhost"
17+
const port: number = 9650
18+
const protocol: string = "http"
19+
const networkID: number = 12345
20+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
21+
const pchain: PlatformVMAPI = avalanche.PChain()
22+
const bintools: BinTools = BinTools.getInstance()
23+
const pKeychain: KeyChain = pchain.keyChain()
24+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
25+
pKeychain.importKey(privKey)
26+
const pAddressStrings: string[] = pchain.keyChain().getAddressStrings()
27+
const threshold: number = 1
28+
const memo: Buffer = bintools.stringToBuffer("PlatformVM utility method buildCreateSubnetTx to create a new subnet")
29+
const asOf: BN = UnixNow()
30+
31+
const main = async (): Promise<any> => {
32+
const platformVMUTXOResponse: any = await pchain.getUTXOs(pAddressStrings)
33+
const utxoSet: UTXOSet = platformVMUTXOResponse.utxos
34+
35+
const unsignedTx: UnsignedTx = await pchain.buildCreateSubnetTx(
36+
utxoSet,
37+
pAddressStrings,
38+
pAddressStrings,
39+
pAddressStrings,
40+
threshold,
41+
memo,
42+
asOf
43+
)
44+
45+
const tx: Tx = unsignedTx.sign(pKeychain)
46+
const id: string = await pchain.issueTx(tx)
47+
console.log(id)
48+
}
49+
50+
main()
51+

examples/platformvm/buildExportTx.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src";
7+
import {
8+
AVMAPI,
9+
KeyChain as AVMKeyChain
10+
} from "../../src/apis/avm";
11+
import {
12+
PlatformVMAPI,
13+
KeyChain,
14+
UTXOSet,
15+
UnsignedTx,
16+
Tx,
17+
} from "../../src/apis/platformvm"
18+
import { Defaults, UnixNow } from "../../src/utils"
19+
20+
const ip: string = "localhost"
21+
const port: number = 9650
22+
const protocol: string = "http"
23+
const networkID: number = 12345
24+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
25+
const xchain: AVMAPI = avalanche.XChain()
26+
const pchain: PlatformVMAPI = avalanche.PChain()
27+
const bintools: BinTools = BinTools.getInstance()
28+
const xKeychain: AVMKeyChain = xchain.keyChain()
29+
const pKeychain: KeyChain = pchain.keyChain()
30+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
31+
xKeychain.importKey(privKey)
32+
pKeychain.importKey(privKey)
33+
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings()
34+
const pAddressStrings: string[] = pchain.keyChain().getAddressStrings()
35+
const xChainBlockchainID: string = Defaults.network['12345'].X.blockchainID
36+
const fee: BN = pchain.getDefaultTxFee()
37+
const threshold: number = 1
38+
const locktime: BN = new BN(0)
39+
const memo: Buffer = bintools.stringToBuffer("PlatformVM utility method buildExportTx to export AVAX from the P-Chain to the X-Chain")
40+
const asOf: BN = UnixNow()
41+
42+
const main = async (): Promise<any> => {
43+
const getBalanceResponse: any = await pchain.getBalance(pAddressStrings[0])
44+
const unlocked: BN = new BN(getBalanceResponse.unlocked)
45+
const platformVMUTXOResponse: any = await pchain.getUTXOs(pAddressStrings)
46+
const utxoSet: UTXOSet = platformVMUTXOResponse.utxos
47+
const unsignedTx: UnsignedTx = await pchain.buildExportTx(
48+
utxoSet,
49+
unlocked.sub(fee),
50+
xChainBlockchainID,
51+
xAddressStrings,
52+
pAddressStrings,
53+
pAddressStrings,
54+
memo,
55+
asOf,
56+
locktime,
57+
threshold
58+
)
59+
const tx: Tx = unsignedTx.sign(pKeychain)
60+
const id: string = await pchain.issueTx(tx)
61+
console.log(id)
62+
}
63+
64+
main()
65+

examples/platformvm/buildImportTx.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src";
7+
import {
8+
PlatformVMAPI,
9+
KeyChain,
10+
UTXOSet,
11+
UnsignedTx,
12+
Tx,
13+
} from "../../src/apis/platformvm"
14+
import { Defaults, UnixNow } from "../../src/utils"
15+
16+
const ip: string = "localhost"
17+
const port: number = 9650
18+
const protocol: string = "http"
19+
const networkID: number = 12345
20+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
21+
const pchain: PlatformVMAPI = avalanche.PChain()
22+
const bintools: BinTools = BinTools.getInstance()
23+
const pKeychain: KeyChain = pchain.keyChain()
24+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
25+
pKeychain.importKey(privKey)
26+
const pAddressStrings: string[] = pchain.keyChain().getAddressStrings()
27+
const xChainBlockchainID: string = Defaults.network['12345'].X.blockchainID
28+
const pChainBlockchainID: string = Defaults.network['12345'].P.blockchainID
29+
const threshold: number = 1
30+
const locktime: BN = new BN(0)
31+
const memo: Buffer = bintools.stringToBuffer("PlatformVM utility method buildImportTx to import AVAX to the P-Chain from the X-Chain")
32+
const asOf: BN = UnixNow()
33+
34+
const main = async (): Promise<any> => {
35+
const platformVMUTXOResponse: any = await pchain.getUTXOs(pAddressStrings, pChainBlockchainID)
36+
const utxoSet: UTXOSet = platformVMUTXOResponse.utxos
37+
const unsignedTx: UnsignedTx = await pchain.buildImportTx(
38+
utxoSet,
39+
pAddressStrings,
40+
xChainBlockchainID,
41+
pAddressStrings,
42+
pAddressStrings,
43+
pAddressStrings,
44+
memo,
45+
asOf,
46+
locktime,
47+
threshold
48+
)
49+
const tx: Tx = unsignedTx.sign(pKeychain)
50+
const id: string = await pchain.issueTx(tx)
51+
console.log(id)
52+
}
53+
54+
main()
55+

0 commit comments

Comments
 (0)