forked from paul-schaaf/solana-escrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.ts
116 lines (104 loc) · 3.1 KB
/
setup.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import {
Connection,
LAMPORTS_PER_SOL,
PublicKey,
Signer,
} from "@solana/web3.js";
import { Token, TOKEN_PROGRAM_ID } from "@solana/spl-token";
import {
getKeypair,
getPublicKey,
getTokenBalance,
writePublicKey,
} from "./utils";
const createMint = (
connection: Connection,
{ publicKey, secretKey }: Signer
) => {
return Token.createMint(
connection,
{
publicKey,
secretKey,
},
publicKey,
null,
0,
TOKEN_PROGRAM_ID
);
};
const setupMint = async (
name: string,
connection: Connection,
alicePublicKey: PublicKey,
bobPublicKey: PublicKey,
clientKeypair: Signer
): Promise<[Token, PublicKey, PublicKey]> => {
console.log(`Creating Mint ${name}...`);
const mint = await createMint(connection, clientKeypair);
writePublicKey(mint.publicKey, `mint_${name.toLowerCase()}`);
console.log(`Creating Alice TokenAccount for ${name}...`);
const aliceTokenAccount = await mint.createAccount(alicePublicKey);
writePublicKey(aliceTokenAccount, `alice_${name.toLowerCase()}`);
console.log(`Creating Bob TokenAccount for ${name}...`);
const bobTokenAccount = await mint.createAccount(bobPublicKey);
writePublicKey(bobTokenAccount, `bob_${name.toLowerCase()}`);
return [mint, aliceTokenAccount, bobTokenAccount];
};
const setup = async () => {
const alicePublicKey = getPublicKey("alice");
const bobPublicKey = getPublicKey("bob");
const clientKeypair = getKeypair("id");
const connection = new Connection("http://localhost:8899", "confirmed");
console.log("Requesting SOL for Alice...");
// some networks like the local network provide an airdrop function (mainnet of course does not)
await connection.requestAirdrop(alicePublicKey, LAMPORTS_PER_SOL * 10);
console.log("Requesting SOL for Bob...");
await connection.requestAirdrop(bobPublicKey, LAMPORTS_PER_SOL * 10);
console.log("Requesting SOL for Client...");
await connection.requestAirdrop(
clientKeypair.publicKey,
LAMPORTS_PER_SOL * 10
);
const [mintX, aliceTokenAccountForX, bobTokenAccountForX] = await setupMint(
"X",
connection,
alicePublicKey,
bobPublicKey,
clientKeypair
);
console.log("Sending 50X to Alice's X TokenAccount...");
await mintX.mintTo(aliceTokenAccountForX, clientKeypair.publicKey, [], 50);
const [mintY, aliceTokenAccountForY, bobTokenAccountForY] = await setupMint(
"Y",
connection,
alicePublicKey,
bobPublicKey,
clientKeypair
);
console.log("Sending 50Y to Bob's Y TokenAccount...");
await mintY.mintTo(bobTokenAccountForY, clientKeypair.publicKey, [], 50);
console.log("✨Setup complete✨\n");
console.table([
{
"Alice Token Account X": await getTokenBalance(
aliceTokenAccountForX,
connection
),
"Alice Token Account Y": await getTokenBalance(
aliceTokenAccountForY,
connection
),
"Bob Token Account X": await getTokenBalance(
bobTokenAccountForX,
connection
),
"Bob Token Account Y": await getTokenBalance(
bobTokenAccountForY,
connection
),
},
]);
console.log("");
};
setup();