1
1
// Usage: npx ts-node app/deploy/devnet/tests/14_delegate.ts
2
2
3
3
import { AnchorProvider , Wallet } from "@coral-xyz/anchor" ;
4
- import { Connection } from "@solana/web3.js" ;
4
+ import { Connection , PublicKey , Keypair } from "@solana/web3.js" ;
5
5
import { StakeConnection } from "../../../StakeConnection" ;
6
6
import { WHTokenBalance } from "../../../whTokenBalance" ;
7
7
import {
@@ -14,45 +14,40 @@ function sleep(ms) {
14
14
return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
15
15
}
16
16
17
+ async function delegateStake (
18
+ userKeypair : Keypair ,
19
+ amount : string ,
20
+ delegateTo ?: PublicKey ,
21
+ ) {
22
+ const connection = new Connection ( RPC_NODE ) ;
23
+ const provider = new AnchorProvider (
24
+ connection ,
25
+ new Wallet ( userKeypair ) ,
26
+ { } ,
27
+ ) ;
28
+
29
+ const stakeConnection = await StakeConnection . createStakeConnection (
30
+ connection ,
31
+ provider . wallet as Wallet ,
32
+ ) ;
33
+
34
+ await sleep ( 2000 ) ;
35
+ await stakeConnection . delegate ( delegateTo , WHTokenBalance . fromString ( amount ) ) ;
36
+
37
+ console . log ( `Delegation successful for user: ${ provider . wallet . publicKey . toBase58 ( ) } ` ) ;
38
+ if ( delegateTo ) {
39
+ console . log ( `Delegated to: ${ delegateTo . toBase58 ( ) } ` ) ;
40
+ }
41
+ }
42
+
17
43
async function main ( ) {
18
44
try {
19
- const connection = new Connection ( RPC_NODE ) ;
20
- const provider = new AnchorProvider (
21
- connection ,
22
- new Wallet ( USER_AUTHORITY_KEYPAIR ) ,
23
- { } ,
24
- ) ;
25
- const stakeConnection = await StakeConnection . createStakeConnection (
26
- connection ,
27
- provider . wallet as Wallet ,
28
- ) ;
29
-
30
- await stakeConnection . delegate (
31
- undefined ,
32
- WHTokenBalance . fromString ( "10000000" ) ,
33
- ) ;
34
- await sleep ( 10000 ) ;
35
-
36
- const user2Provider = new AnchorProvider (
37
- connection ,
38
- new Wallet ( USER2_AUTHORITY_KEYPAIR ) ,
39
- { } ,
40
- ) ;
41
- const user2StakeConnection = await StakeConnection . createStakeConnection (
42
- connection ,
43
- user2Provider . wallet as Wallet ,
44
- ) ;
45
-
46
- await user2StakeConnection . delegate (
47
- undefined ,
48
- WHTokenBalance . fromString ( "10000000" ) ,
49
- ) ;
50
- await sleep ( 10000 ) ;
51
-
52
- await stakeConnection . delegate (
53
- user2StakeConnection . userPublicKey ( ) ,
54
- WHTokenBalance . fromString ( "10000000" ) ,
55
- ) ;
45
+ // First user delegates to himself
46
+ await delegateStake ( USER_AUTHORITY_KEYPAIR , "10000000" ) ;
47
+ // Second user delegates to himself
48
+ await delegateStake ( USER2_AUTHORITY_KEYPAIR , "10000000" ) ;
49
+ // First user delegates to second user
50
+ await delegateStake ( USER_AUTHORITY_KEYPAIR , "10000000" , USER2_AUTHORITY_KEYPAIR . publicKey ) ;
56
51
} catch ( err ) {
57
52
console . error ( "Error:" , err ) ;
58
53
}
0 commit comments