Skip to content

Commit b101231

Browse files
add ts test for create-and-update
1 parent 40b0b59 commit b101231

2 files changed

Lines changed: 24 additions & 49 deletions

File tree

create-and-update/programs/create-and-update/src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#![allow(deprecated)]
33

44
use anchor_lang::{prelude::*, AnchorDeserialize, AnchorSerialize};
5-
use borsh::BorshSerialize;
65
use light_sdk::{
76
account::LightAccount,
87
address::v2::derive_address,
@@ -88,20 +87,13 @@ pub mod create_and_update {
8887
.get_tree_pubkey(&light_cpi_accounts)
8988
.map_err(|_| ErrorCode::AccountNotEnoughKeys)?;
9089

91-
msg!(
92-
"new_account_address_tree_pubkey: {:?}",
93-
new_account_address_tree_pubkey
94-
);
9590
// Create new compressed account
9691
let (new_address, new_address_seed) = derive_address(
9792
&[SECOND_SEED, ctx.accounts.signer.key().as_ref()],
9893
new_account_address_tree_pubkey,
9994
&crate::ID,
10095
);
10196

102-
msg!("new_address: {:?}", new_address);
103-
msg!("new_address_seed: {:?}", new_address_seed);
104-
10597
let mut new_data_account = LightAccount::<DataAccount>::new_init(
10698
&crate::ID,
10799
Some(new_address),

create-and-update/tests/create_and_update.ts

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,11 @@ describe("create-and-update anchor", () => {
3737

3838
it("creates and updates compressed accounts atomically", async () => {
3939
const signer = new web3.Keypair();
40-
const rpc = createRpc(
41-
"http://127.0.0.1:8899",
42-
"http://127.0.0.1:8784",
43-
"http://127.0.0.1:3001",
44-
{
45-
commitment: "confirmed",
46-
}
47-
);
40+
const rpc = createRpc();
4841

4942
await rpc.requestAirdrop(signer.publicKey, web3.LAMPORTS_PER_SOL);
5043
await sleep(2000);
5144

52-
// get tree infos
5345
const stateTreeInfos = await rpc.getStateTreeInfos();
5446
const stateTreeInfo = selectStateTreeInfo(stateTreeInfos);
5547

@@ -91,7 +83,6 @@ describe("create-and-update anchor", () => {
9183
);
9284
assert.strictEqual(decoded.message, "Initial message");
9385

94-
// Derive second address
9586
const secondSeed = new TextEncoder().encode("second");
9687
const secondAddressSeed = deriveAddressSeedV2([
9788
secondSeed,
@@ -102,8 +93,6 @@ describe("create-and-update anchor", () => {
10293
addressTreeInfo.tree,
10394
program.programId
10495
);
105-
console.log("secondAddress", Array.from(secondAddress.toBytes()));
106-
console.log("secondAddressSeed", Array.from(secondAddressSeed));
10796

10897
await createAndUpdateAccounts(
10998
rpc,
@@ -112,7 +101,6 @@ describe("create-and-update anchor", () => {
112101
firstAccount,
113102
secondAddress,
114103
addressTreeInfo,
115-
"Initial message",
116104
"Hello from second account",
117105
"Updated first message"
118106
);
@@ -164,9 +152,6 @@ describe("create-and-update anchor", () => {
164152
});
165153

166154
const remainingAccounts = packedAccounts.toAccountMetas().remainingAccounts;
167-
for (const account of remainingAccounts) {
168-
console.log("remainingAccount", account.pubkey.toBase58());
169-
}
170155
const tx = await program.methods
171156
.createCompressedAccount(
172157
proof,
@@ -185,8 +170,7 @@ describe("create-and-update anchor", () => {
185170
const recentBlockhash = (await rpc.getRecentBlockhash()).blockhash;
186171

187172
const signedTx = buildAndSignTx(tx.instructions, signer, recentBlockhash);
188-
const sig = await sendAndConfirmTx(rpc, signedTx, { skipPreflight: true });
189-
console.log("createCompressedAccount sig", sig);
173+
const sig = await sendAndConfirmTx(rpc, signedTx);
190174
return sig;
191175
}
192176

@@ -197,7 +181,6 @@ describe("create-and-update anchor", () => {
197181
existingAccount: CompressedAccountWithMerkleContext,
198182
newAddress: anchor.web3.PublicKey,
199183
addressTreeInfo: TreeInfo,
200-
existingMessage: string,
201184
newAccountMessage: string,
202185
updatedMessage: string
203186
) {
@@ -223,35 +206,38 @@ describe("create-and-update anchor", () => {
223206
]
224207
);
225208

226-
console.log("existing hash", existingAccount.hash.toArray());
209+
const coder = new anchor.BorshCoder(program.idl);
210+
const currentAccountData = coder.types.decode(
211+
"dataAccount",
212+
existingAccount.data.data
213+
);
227214

228215
const config = SystemAccountMetaConfig.new(program.programId);
229216
const packedAccounts = PackedAccounts.newWithSystemAccountsV2(config);
230217

231-
const existingQueueIndex = packedAccounts.insertOrGet(
232-
existingAccount.treeInfo.queue
233-
);
234-
const existingMerkleTreeIndex = packedAccounts.insertOrGet(
235-
existingAccount.treeInfo.tree
236-
);
237-
const outputStateTreeIndex = packedAccounts.insertOrGet(
238-
existingAccount.treeInfo.queue
239-
);
240-
241218
const existingAccountMeta = {
242219
treeInfo: {
243-
merkleTreePubkeyIndex: existingMerkleTreeIndex,
244-
queuePubkeyIndex: existingQueueIndex,
245-
leafIndex: existingAccount.leafIndex,
246-
proveByIndex: false,
247220
rootIndex: proofRpcResult.rootIndices[0],
221+
// Note: set this to true for local testing.
222+
proveByIndex: true,
223+
merkleTreePubkeyIndex: packedAccounts.insertOrGet(
224+
existingAccount.treeInfo.tree
225+
),
226+
queuePubkeyIndex: packedAccounts.insertOrGet(
227+
existingAccount.treeInfo.queue
228+
),
229+
leafIndex: existingAccount.leafIndex,
248230
},
249231
address: existingAccount.address,
250-
outputStateTreeIndex,
232+
outputStateTreeIndex: packedAccounts.insertOrGet(
233+
existingAccount.treeInfo.queue
234+
),
251235
};
252236

237+
// for new account's address
253238
const addressQueueIndex = packedAccounts.insertOrGet(addressTreeInfo.queue);
254239
const addressTreeIndex = packedAccounts.insertOrGet(addressTreeInfo.tree);
240+
255241
const packedAddressTreeInfo: PackedAddressTreeInfo = {
256242
rootIndex: proofRpcResult.rootIndices[1],
257243
addressMerkleTreePubkeyIndex: addressTreeIndex,
@@ -266,15 +252,13 @@ describe("create-and-update anchor", () => {
266252
});
267253

268254
const remainingAccounts = packedAccounts.toAccountMetas().remainingAccounts;
269-
for (const account of remainingAccounts) {
270-
console.log("remainingAccount", account.pubkey.toBase58());
271-
}
255+
272256
const tx = await program.methods
273257
.createAndUpdate(
274258
proof,
275259
{
276260
accountMeta: existingAccountMeta,
277-
message: existingMessage,
261+
message: currentAccountData.message,
278262
updateMessage: updatedMessage,
279263
},
280264
{
@@ -292,8 +276,7 @@ describe("create-and-update anchor", () => {
292276

293277
const recentBlockhash = (await rpc.getRecentBlockhash()).blockhash;
294278
const signedTx = buildAndSignTx(tx.instructions, signer, recentBlockhash);
295-
const sig = await sendAndConfirmTx(rpc, signedTx, { skipPreflight: false });
296-
console.log("createAndUpdate sig", sig);
279+
const sig = await sendAndConfirmTx(rpc, signedTx);
297280
return sig;
298281
}
299282
});

0 commit comments

Comments
 (0)