-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialization-frontrunning.ts
111 lines (84 loc) · 3.13 KB
/
initialization-frontrunning.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
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { InitializationFrontrunning } from "../target/types/initialization_frontrunning";
const bpfProgram = new anchor.web3.PublicKey("BPFLoaderUpgradeab1e11111111111111111111111")
describe("initialization-frontrunning", () => {
const provider = anchor.AnchorProvider.env()
anchor.setProvider(provider);
const program = anchor.workspace.InitializationFrontrunning as Program<InitializationFrontrunning>;
const signer = anchor.web3.Keypair.generate()
before("Prepare", async () => {
await airdrop(provider.connection, signer.publicKey);
});
// x x x x x x x x x x x x x x x x x x x x x
// | | | | | | | | | | | | | | | | | | | | |
// ADD YOUR CODE BELOW
// | | | | | | | | | | | | | | | | | | | | |
// v v v v v v v v v v v v v v v v v v v v v
it.skip("Initialize Insecure", async () => {
const globalConfig = get_config_addresses(program.programId);
await program.methods.initializeInsecure(15).accounts({
signer: signer.publicKey,
globalConfig: globalConfig,
systemProgram: anchor.web3.SystemProgram.programId
}).signers([signer]).rpc({ commitment: "confirmed" });
});
it("Initialize Secure - wrong Authority", async () => {
const globalConfig = get_config_addresses(program.programId);
const programData = get_program_data_addresses(program.programId);
console.log("This Test case is meant to fail !!");
await program.methods.initializeSecure(15).accounts({
signer: signer.publicKey,
globalConfig: globalConfig,
programData: programData,
systemProgram: anchor.web3.SystemProgram.programId
}).signers([signer]).rpc({ commitment: "confirmed" });
});
it("Initialize Secure - correct Authority", async () => {
const globalConfig = get_config_addresses(program.programId);
const programData = get_program_data_addresses(program.programId);
await program.methods.initializeSecure(15).accounts({
signer: provider.wallet.publicKey,
globalConfig: globalConfig,
programData: programData,
systemProgram: anchor.web3.SystemProgram.programId
}).signers([]).rpc({ commitment: "confirmed" });
});
// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
// | | | | | | | | | | | | | | | | | | | | |
// ADD YOUR CODE ABOVE
// | | | | | | | | | | | | | | | | | | | | |
// x x x x x x x x x x x x x x x x x x x x x
});
async function airdrop(
connection: any,
address: any,
amount = 500_000_000_000
) {
await connection.confirmTransaction(
await connection.requestAirdrop(address, amount),
'confirmed'
);
}
function get_config_addresses(
program_id: anchor.web3.PublicKey,
): anchor.web3.PublicKey {
const [config, _] = anchor.web3.PublicKey.findProgramAddressSync(
[
Buffer.from('config'),
],
program_id
);
return config;
}
function get_program_data_addresses(
program_id: anchor.web3.PublicKey,
): anchor.web3.PublicKey {
const [program_data, _] = anchor.web3.PublicKey.findProgramAddressSync(
[
program_id.toBuffer(),
],
bpfProgram
);
return program_data;
}