forked from paul-schaaf/solana-escrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bob.ts
168 lines (152 loc) · 4.71 KB
/
bob.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
import {
Connection,
PublicKey,
Transaction,
TransactionInstruction,
} from "@solana/web3.js";
import BN = require("bn.js");
import {
EscrowLayout,
ESCROW_ACCOUNT_DATA_LAYOUT,
getKeypair,
getProgramId,
getPublicKey,
getTerms,
getTokenBalance,
logError,
} from "./utils";
const bob = async () => {
const bobKeypair = getKeypair("bob");
const bobXTokenAccountPubkey = getPublicKey("bob_x");
const bobYTokenAccountPubkey = getPublicKey("bob_y");
const escrowStateAccountPubkey = getPublicKey("escrow");
const escrowProgramId = getProgramId();
const terms = getTerms();
const connection = new Connection("http://localhost:8899", "confirmed");
const escrowAccount = await connection.getAccountInfo(
escrowStateAccountPubkey
);
if (escrowAccount === null) {
logError("Could not find escrow at given address!");
process.exit(1);
}
const encodedEscrowState = escrowAccount.data;
const decodedEscrowLayout = ESCROW_ACCOUNT_DATA_LAYOUT.decode(
encodedEscrowState
) as EscrowLayout;
const escrowState = {
escrowAccountPubkey: escrowStateAccountPubkey,
isInitialized: !!decodedEscrowLayout.isInitialized,
initializerAccountPubkey: new PublicKey(
decodedEscrowLayout.initializerPubkey
),
XTokenTempAccountPubkey: new PublicKey(
decodedEscrowLayout.initializerTempTokenAccountPubkey
),
initializerYTokenAccount: new PublicKey(
decodedEscrowLayout.initializerReceivingTokenAccountPubkey
),
expectedAmount: new BN(decodedEscrowLayout.expectedAmount, 10, "le"),
};
const PDA = await PublicKey.findProgramAddress(
[Buffer.from("escrow")],
escrowProgramId
);
const exchangeInstruction = new TransactionInstruction({
programId: escrowProgramId,
data: Buffer.from(
Uint8Array.of(1, ...new BN(terms.bobExpectedAmount).toArray("le", 8))
),
keys: [
{ pubkey: bobKeypair.publicKey, isSigner: true, isWritable: false },
{ pubkey: bobYTokenAccountPubkey, isSigner: false, isWritable: true },
{ pubkey: bobXTokenAccountPubkey, isSigner: false, isWritable: true },
{
pubkey: escrowState.XTokenTempAccountPubkey,
isSigner: false,
isWritable: true,
},
{
pubkey: escrowState.initializerAccountPubkey,
isSigner: false,
isWritable: true,
},
{
pubkey: escrowState.initializerYTokenAccount,
isSigner: false,
isWritable: true,
},
{ pubkey: escrowStateAccountPubkey, isSigner: false, isWritable: true },
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: PDA[0], isSigner: false, isWritable: false },
],
});
const aliceYTokenAccountPubkey = getPublicKey("alice_y");
const [aliceYbalance, bobXbalance] = await Promise.all([
getTokenBalance(aliceYTokenAccountPubkey, connection),
getTokenBalance(bobXTokenAccountPubkey, connection),
]);
console.log("Sending Bob's transaction...");
await connection.sendTransaction(
new Transaction().add(exchangeInstruction),
[bobKeypair],
{ skipPreflight: false, preflightCommitment: "confirmed" }
);
// sleep to allow time to update
await new Promise((resolve) => setTimeout(resolve, 1000));
if ((await connection.getAccountInfo(escrowStateAccountPubkey)) !== null) {
logError("Escrow account has not been closed");
process.exit(1);
}
if (
(await connection.getAccountInfo(escrowState.XTokenTempAccountPubkey)) !==
null
) {
logError("Temporary X token account has not been closed");
process.exit(1);
}
const newAliceYbalance = await getTokenBalance(
aliceYTokenAccountPubkey,
connection
);
if (newAliceYbalance !== aliceYbalance + terms.aliceExpectedAmount) {
logError(
`Alice's Y balance should be ${
aliceYbalance + terms.aliceExpectedAmount
} but is ${newAliceYbalance}`
);
process.exit(1);
}
const newBobXbalance = await getTokenBalance(
bobXTokenAccountPubkey,
connection
);
if (newBobXbalance !== bobXbalance + terms.bobExpectedAmount) {
logError(
`Bob's X balance should be ${
bobXbalance + terms.bobExpectedAmount
} but is ${newBobXbalance}`
);
process.exit(1);
}
console.log(
"✨Trade successfully executed. All temporary accounts closed✨\n"
);
console.table([
{
"Alice Token Account X": await getTokenBalance(
getPublicKey("alice_x"),
connection
),
"Alice Token Account Y": newAliceYbalance,
"Bob Token Account X": newBobXbalance,
"Bob Token Account Y": await getTokenBalance(
bobYTokenAccountPubkey,
connection
),
},
]);
console.log("");
};
bob();