-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwithdraw.ts
More file actions
65 lines (54 loc) · 1.71 KB
/
withdraw.ts
File metadata and controls
65 lines (54 loc) · 1.71 KB
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
import { clusterApiUrl, Connection, Keypair, PublicKey } from "@solana/web3.js";
import BN from "bn.js";
import fs from "fs";
import os from "os";
import { PRESALE_PROGRAM_ID } from "../src";
import Presale from "../src/presale";
const connection = new Connection(clusterApiUrl("devnet"));
async function withdraw(
connection: Connection,
presaleAddress: PublicKey,
amount: BN,
user: Keypair
) {
const presaleInstance = await Presale.create(
connection,
presaleAddress,
PRESALE_PROGRAM_ID
);
const escrows = await presaleInstance.getPresaleEscrowByOwner(user.publicKey);
const withdrawTxs = await Promise.all(
escrows.map(async (escrow) => {
const escrowAccount = escrow.getEscrowAccount();
return presaleInstance.withdraw({
amount,
owner: escrowAccount.owner,
registryIndex: new BN(escrowAccount.registryIndex),
});
})
);
await Promise.all(
withdrawTxs.map(async (withdrawTx) => {
withdrawTx.sign(user);
const txSig = await connection.sendRawTransaction(withdrawTx.serialize());
console.log("Withdraw transaction sent:", txSig);
await connection.confirmTransaction(
{
signature: txSig,
lastValidBlockHeight: withdrawTx.lastValidBlockHeight,
blockhash: withdrawTx.recentBlockhash,
},
"finalized"
);
})
);
}
const keypairFilepath = `${os.homedir()}/.config/solana/id.json`;
const keypair = Keypair.fromSecretKey(
new Uint8Array(JSON.parse(fs.readFileSync(keypairFilepath, "utf-8")))
);
const presaleAddress = new PublicKey(
"59Av19ft9HjpitcN2VoEXvMiA8nrTh7WvNHVtZRhdaoi"
);
const amount = new BN(100000);
withdraw(connection, presaleAddress, amount, keypair);