@@ -11,11 +11,14 @@ import {
11
11
SerializeArgs ,
12
12
SYSVAR_INSTRUCTIONS_PUBKEY ,
13
13
World ,
14
+ SessionProgram ,
15
+ FindSessionTokenPda ,
14
16
} from "../index" ;
15
17
import BN from "bn.js" ;
16
18
import type web3 from "@solana/web3.js" ;
17
19
import {
18
20
type Connection ,
21
+ Keypair ,
19
22
type PublicKey ,
20
23
Transaction ,
21
24
type TransactionInstruction ,
@@ -47,6 +50,49 @@ export async function InitializeRegistry({
47
50
} ;
48
51
}
49
52
53
+ export async function CreateSession ( {
54
+ sessionSigner,
55
+ authority,
56
+ targetProgram,
57
+ topUp,
58
+ validity,
59
+ } : {
60
+ sessionSigner ?: Keypair ;
61
+ authority : PublicKey ;
62
+ targetProgram : PublicKey ;
63
+ topUp ?: boolean ;
64
+ validity ?: BN ;
65
+ } ) : Promise < {
66
+ instruction : TransactionInstruction ;
67
+ transaction : Transaction ;
68
+ sessionToken : PublicKey ;
69
+ sessionSigner : Keypair ;
70
+ } > {
71
+ sessionSigner = sessionSigner ?? Keypair . generate ( ) ;
72
+ const sessionToken = FindSessionTokenPda ( {
73
+ targetProgram,
74
+ sessionSigner : sessionSigner . publicKey ,
75
+ authority,
76
+ } ) ;
77
+ topUp = topUp ?? false ;
78
+ let instruction = await SessionProgram . methods
79
+ . createSession ( topUp , validity ?? null )
80
+ . accounts ( {
81
+ sessionSigner : sessionSigner . publicKey ,
82
+ authority,
83
+ targetProgram,
84
+ sessionToken,
85
+ } )
86
+ . instruction ( ) ;
87
+ const transaction = new Transaction ( ) . add ( instruction ) ;
88
+ return {
89
+ instruction,
90
+ transaction,
91
+ sessionToken,
92
+ sessionSigner,
93
+ } ;
94
+ }
95
+
50
96
/**
51
97
* Create the transaction to Initialize a new world
52
98
* @param payer
@@ -438,13 +484,15 @@ export async function ApplySystem({
438
484
world,
439
485
extraAccounts,
440
486
args,
487
+ sessionToken,
441
488
} : {
442
489
authority : PublicKey ;
443
490
systemId : PublicKey ;
444
491
entities : ApplySystemEntity [ ] ;
445
492
world : PublicKey ;
446
493
extraAccounts ?: web3 . AccountMeta [ ] ;
447
494
args ?: object ;
495
+ sessionToken ?: PublicKey ;
448
496
} ) : Promise < { instruction : TransactionInstruction ; transaction : Transaction } > {
449
497
const instruction = await createApplySystemInstruction ( {
450
498
authority,
@@ -453,6 +501,7 @@ export async function ApplySystem({
453
501
world,
454
502
extraAccounts,
455
503
args,
504
+ sessionToken,
456
505
} ) ;
457
506
const transaction = new Transaction ( ) . add ( instruction ) ;
458
507
return {
0 commit comments