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