Skip to content

Commit a115d88

Browse files
authored
[message-buffer 16/x] - integration repo scripts (#818)
* feat(message-buffer/scripts): add init-buffer script for integration repo * feat(message-buffer/scripts): fix init-buffer scrtips
1 parent f088854 commit a115d88

File tree

4 files changed

+202
-2
lines changed

4 files changed

+202
-2
lines changed

pythnet/message_buffer/Anchor.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ wallet = "~/.config/solana/id.json"
1414

1515
[scripts]
1616
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
17+
init-buffer = "anchor build && yarn run ts-node ./scripts/setup_message_buffer.ts"

pythnet/message_buffer/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"scripts": {
33
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
4-
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
4+
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check",
5+
"init-buffer": "yarn ts-node scripts/initialize-buffer.ts"
56
},
67
"dependencies": {
78
"@coral-xyz/anchor": "^0.27.0",
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
import * as anchor from "@coral-xyz/anchor";
2+
import { Program, Idl } from "@coral-xyz/anchor";
3+
import { MessageBuffer } from "../target/types/message_buffer";
4+
import messageBuffer from "../target/idl/message_buffer.json";
5+
import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet";
6+
import { assert } from "chai";
7+
8+
/**
9+
* Script to initialize the message buffer program and whitelist admin
10+
* using the integration repo setup
11+
*/
12+
13+
const payer = anchor.web3.Keypair.fromSecretKey(
14+
// Keypair at keys/funding.json
15+
Uint8Array.from([
16+
235, 245, 49, 124, 125, 91, 162, 107, 245, 83, 158, 7, 86, 181, 31, 252,
17+
215, 200, 125, 25, 126, 55, 37, 240, 205, 171, 71, 196, 2, 11, 137, 229,
18+
131, 30, 46, 220, 89, 75, 108, 173, 185, 146, 114, 253, 109, 67, 214, 133,
19+
117, 79, 154, 107, 133, 193, 249, 251, 40, 171, 42, 191, 192, 60, 188, 78,
20+
])
21+
);
22+
const endpoint = "http://pythnet:8899";
23+
console.log(`connecting to ${endpoint}`);
24+
const commitment = "finalized";
25+
const provider = new anchor.AnchorProvider(
26+
new anchor.web3.Connection(endpoint),
27+
new NodeWallet(payer),
28+
{
29+
commitment,
30+
preflightCommitment: commitment,
31+
skipPreflight: true,
32+
}
33+
);
34+
35+
anchor.setProvider(provider);
36+
const messageBufferPid = new anchor.web3.PublicKey(
37+
"BZZFM8qzdWvv4ysy8dxpAzjs9WJ6iy9y1ph2YNqWYzrm"
38+
);
39+
40+
const messageBufferProgram = new Program(
41+
messageBuffer as Idl,
42+
messageBufferPid,
43+
provider
44+
) as unknown as Program<MessageBuffer>;
45+
46+
const whitelistAdmin = payer;
47+
48+
const MESSAGE = Buffer.from("message");
49+
const [whitelistPubkey, whitelistBump] =
50+
anchor.web3.PublicKey.findProgramAddressSync(
51+
[MESSAGE, Buffer.from("whitelist")],
52+
messageBufferProgram.programId
53+
);
54+
55+
const pythOraclePid = new anchor.web3.PublicKey(
56+
"7th6GdMuo4u1zNLzFAyMY6psunHNsGjPjo8hXvcTgKei"
57+
);
58+
59+
const [pythOracleCpiAuth] = anchor.web3.PublicKey.findProgramAddressSync(
60+
[Buffer.from("upd_price_write"), messageBufferProgram.programId.toBuffer()],
61+
pythOraclePid
62+
);
63+
64+
const pythPriceAccountPk = new anchor.web3.PublicKey(
65+
"tvNV74CEkyEhmzJYiXGgcTMLCSX8JDPVi3er5ZSTJn2"
66+
);
67+
68+
const [messageBufferPda, messageBufferBump] =
69+
anchor.web3.PublicKey.findProgramAddressSync(
70+
[pythOracleCpiAuth.toBuffer(), MESSAGE, pythPriceAccountPk.toBuffer()],
71+
messageBufferProgram.programId
72+
);
73+
74+
async function main() {
75+
console.log("Initializing message buffer...");
76+
77+
console.group();
78+
console.log("Requesting airdrop");
79+
80+
let airdropSig = await provider.connection.requestAirdrop(
81+
payer.publicKey,
82+
1 * anchor.web3.LAMPORTS_PER_SOL
83+
);
84+
await provider.connection.confirmTransaction({
85+
signature: airdropSig,
86+
...(await provider.connection.getLatestBlockhash()),
87+
});
88+
89+
const whitelistAdminBalance = await provider.connection.getBalance(
90+
whitelistAdmin.publicKey
91+
);
92+
console.log(`whitelistAdminBalance: ${whitelistAdminBalance}`);
93+
console.log("Airdrop complete");
94+
console.groupEnd();
95+
96+
let whitelist = await messageBufferProgram.account.whitelist.fetchNullable(
97+
whitelistPubkey
98+
);
99+
100+
if (whitelist === null) {
101+
console.group("No whitelist detected. Initializing message buffer");
102+
const initializeTxnSig = await messageBufferProgram.methods
103+
.initialize()
104+
.accounts({
105+
admin: whitelistAdmin.publicKey,
106+
})
107+
.rpc();
108+
109+
console.log(`initializeTxnSig: ${initializeTxnSig}`);
110+
111+
console.log("fetching & checking whitelist");
112+
whitelist = await messageBufferProgram.account.whitelist.fetch(
113+
whitelistPubkey
114+
);
115+
116+
assert.strictEqual(whitelist.bump, whitelistBump);
117+
assert.isTrue(whitelist.admin.equals(whitelistAdmin.publicKey));
118+
console.groupEnd();
119+
} else {
120+
console.log("Whitelist already initialized");
121+
}
122+
123+
if (whitelist.allowedPrograms.length === 0) {
124+
console.group("Setting Allowed Programs");
125+
const allowedProgramAuthorities = [pythOracleCpiAuth];
126+
let setAllowedProgramSig = await messageBufferProgram.methods
127+
.setAllowedPrograms(allowedProgramAuthorities)
128+
.accounts({
129+
admin: whitelistAdmin.publicKey,
130+
})
131+
.signers([whitelistAdmin])
132+
.rpc();
133+
console.log(`setAllowedProgramSig: ${setAllowedProgramSig}`);
134+
console.log("fetching & checking whitelist after add");
135+
whitelist = await messageBufferProgram.account.whitelist.fetch(
136+
whitelistPubkey
137+
);
138+
console.info(`whitelist after add: ${JSON.stringify(whitelist)}`);
139+
const whitelistAllowedPrograms = whitelist.allowedPrograms.map((pk) =>
140+
pk.toString()
141+
);
142+
assert.deepEqual(
143+
whitelistAllowedPrograms,
144+
allowedProgramAuthorities.map((p) => p.toString())
145+
);
146+
console.groupEnd();
147+
} else {
148+
console.log("Allowed Programs already set");
149+
}
150+
151+
let messageBufferData = await getMessageBuffer(
152+
provider.connection,
153+
messageBufferPda
154+
);
155+
if (messageBufferData === null) {
156+
console.group("Creating Message Buffer");
157+
const msgBufferPdaMetas = [
158+
{
159+
pubkey: messageBufferPda,
160+
isSigner: false,
161+
isWritable: true,
162+
},
163+
];
164+
165+
await messageBufferProgram.methods
166+
.createBuffer(pythOracleCpiAuth, pythPriceAccountPk, 1024 * 8)
167+
.accounts({
168+
whitelist: whitelistPubkey,
169+
admin: whitelistAdmin.publicKey,
170+
systemProgram: anchor.web3.SystemProgram.programId,
171+
})
172+
.signers([whitelistAdmin])
173+
.remainingAccounts(msgBufferPdaMetas)
174+
.rpc({ skipPreflight: true });
175+
176+
console.log("fetching messageBuffer");
177+
const messageBufferData = await getMessageBuffer(
178+
provider.connection,
179+
messageBufferPda
180+
);
181+
console.log(`messageBufferData: ${messageBufferData.toString("utf-8")}`);
182+
console.groupEnd();
183+
} else {
184+
console.log("Message Buffer already created");
185+
console.log(`messageBufferData: ${messageBufferData.toString("utf-8")}`);
186+
}
187+
}
188+
189+
async function getMessageBuffer(
190+
connection: anchor.web3.Connection,
191+
accountKey: anchor.web3.PublicKey
192+
): Promise<Buffer | null> {
193+
let accountInfo = await connection.getAccountInfo(accountKey);
194+
return accountInfo ? accountInfo.data : null;
195+
}
196+
197+
void main();

pythnet/message_buffer/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"lib": ["es2015"],
66
"module": "commonjs",
77
"target": "es6",
8-
"esModuleInterop": true
8+
"esModuleInterop": true,
9+
"resolveJsonModule": true
910
}
1011
}

0 commit comments

Comments
 (0)