-
Notifications
You must be signed in to change notification settings - Fork 17
/
solana_utils.ts
40 lines (37 loc) · 979 Bytes
/
solana_utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import {
Connection,
Keypair,
PublicKey,
SystemProgram,
Transaction,
sendAndConfirmTransaction,
} from "@solana/web3.js";
export async function createAccount(
connection: Connection,
authority: Keypair,
size: number,
owner: PublicKey
): Promise<PublicKey> {
const lamports = await connection.getMinimumBalanceForRentExemption(size);
let address = Keypair.generate();
const transaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: authority.publicKey,
newAccountPubkey: address.publicKey,
lamports,
space: size,
programId: owner,
})
);
transaction.feePayer = authority.publicKey;
let hash = await connection.getRecentBlockhash();
transaction.recentBlockhash = hash.blockhash;
// Sign transaction, broadcast, and confirm
await sendAndConfirmTransaction(
connection,
transaction,
[authority, address],
{ commitment: "confirmed" }
);
return address.publicKey;
}