diff --git a/CHANGELOG.md b/CHANGELOG.md index efc584371..dbe863c52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,6 @@ ## 0.6.0 (TBD) -* Added WASM consumable notes API + improved note models (#561). * Allow to set expiration delta for `TransactionRequest` (#553). * Added WASM consumable notes API + improved note models (#561). * [BREAKING] Refactored `OutputNoteRecord` to use states and transitions for updates (#551). diff --git a/crates/web-client/src/models/consumable_note_record.rs b/crates/web-client/src/models/consumable_note_record.rs index ee16c7bfb..76f8851b1 100644 --- a/crates/web-client/src/models/consumable_note_record.rs +++ b/crates/web-client/src/models/consumable_note_record.rs @@ -25,8 +25,10 @@ pub struct NoteConsumability { #[wasm_bindgen] impl NoteConsumability { - #[wasm_bindgen(constructor)] - pub fn new(account_id: AccountId, consumable_after_block: Option) -> NoteConsumability { + pub(crate) fn new( + account_id: AccountId, + consumable_after_block: Option, + ) -> NoteConsumability { NoteConsumability { account_id, consumable_after_block } } diff --git a/crates/web-client/test/notes.test.ts b/crates/web-client/test/notes.test.ts index 63eff70dc..12f55bd41 100644 --- a/crates/web-client/test/notes.test.ts +++ b/crates/web-client/test/notes.test.ts @@ -5,7 +5,9 @@ import { consumeTransaction, fetchAndCacheAccountAuth, mintTransaction, + sendTransaction, setupWalletAndFaucet, + syncState, } from "./webClientTestUtils"; const getInputNote = async (noteId: string) => { @@ -49,6 +51,7 @@ const getConsumableNotes = async (accountId?: string) => { const client = window.client; let records; if (_accountId) { + console.log({ _accountId }); const accountId = window.AccountId.from_hex(_accountId); records = await client.get_consumable_notes(accountId); } else { @@ -102,6 +105,7 @@ describe("get_consumable_notes", () => { expect(record.consumability).to.have.lengthOf(1); expect(record.consumability[0].accountId).to.equal(accountId1); expect(record.noteId).to.equal(noteId1); + expect(record.consumability[0].consumableAfterBlock).to.be.undefined; }); }); it("no filter by account", async () => { @@ -117,6 +121,39 @@ describe("get_consumable_notes", () => { accountId2, ]); expect(result).to.have.lengthOf(2); + const consumableRecord1 = result.find((r) => r.noteId === noteId1); + const consumableRecord2 = result.find((r) => r.noteId === noteId2); + + consumableRecord1!!.consumability.forEach((c) => { + expect(c.accountId).to.equal(accountId1); + }); + + consumableRecord2!!.consumability.forEach((c) => { + expect(c.accountId).to.equal(accountId2); + }); + }); + it.only("p2idr consume after block", async () => { + const { accountId: senderAccountId, faucetId } = + await setupWalletAndFaucet(); + const { accountId: targetAccountId } = await setupWalletAndFaucet(); + const recallHeight = 100; + await sendTransaction( + senderAccountId, + targetAccountId, + faucetId, + 100, + recallHeight + ); + + const consumableRecipient = await getConsumableNotes(targetAccountId); + const consumableSender = await getConsumableNotes(senderAccountId); + expect(consumableSender).to.have.lengthOf(1); + expect(consumableSender[0].consumability[0].consumableAfterBlock).to.equal( + recallHeight + ); + expect(consumableRecipient).to.have.lengthOf(1); + expect(consumableRecipient[0].consumability[0].consumableAfterBlock).to.be + .undefined; }); }); diff --git a/crates/web-client/test/webClientTestUtils.ts b/crates/web-client/test/webClientTestUtils.ts index 1426e6729..61dbfa3a8 100644 --- a/crates/web-client/test/webClientTestUtils.ts +++ b/crates/web-client/test/webClientTestUtils.ts @@ -54,6 +54,72 @@ export const mintTransaction = async ( ); }; +export const sendTransaction = async ( + senderAccountId: string, + targetAccountId: string, + faucetAccountId: string, + amount: number, + recallHeight?: number +) => { + return testingPage.evaluate( + async ( + _senderAccountId, + _targetAccountId, + _faucetAccountId, + _amount, + _recallHeight + ) => { + const client = window.client; + + const senderAccountId = window.AccountId.from_hex(_senderAccountId); + const targetAccountId = window.AccountId.from_hex(_targetAccountId); + const faucetAccountId = window.AccountId.from_hex(_faucetAccountId); + + await client.fetch_and_cache_account_auth_by_pub_key( + window.AccountId.from_hex(_faucetAccountId) + ); + let mint_transaction_result = await client.new_mint_transaction( + senderAccountId, + window.AccountId.from_hex(_faucetAccountId), + window.NoteType.private(), + BigInt(1000) + ); + let created_notes = mint_transaction_result.created_notes().notes(); + let created_note_ids = created_notes.map((note) => note.id().to_string()); + await new Promise((r) => setTimeout(r, 20000)); // TODO: Replace this with loop of sync -> check uncommitted transactions -> sleep + await client.sync_state(); + + await client.fetch_and_cache_account_auth_by_pub_key(senderAccountId); + await client.new_consume_transaction(senderAccountId, created_note_ids); + await new Promise((r) => setTimeout(r, 20000)); // TODO: Replace this with loop of sync -> check uncommitted transactions -> sleep + await client.sync_state(); + + await client.fetch_and_cache_account_auth_by_pub_key(senderAccountId); + let send_transaction_result = await client.new_send_transaction( + senderAccountId, + targetAccountId, + faucetAccountId, + window.NoteType.public(), + BigInt(_amount), + _recallHeight + ); + let send_created_notes = send_transaction_result.created_notes().notes(); + let send_created_note_ids = send_created_notes.map((note) => + note.id().to_string() + ); + await new Promise((r) => setTimeout(r, 20000)); // TODO: Replace this with loop of sync -> check uncommitted transactions -> sleep + await client.sync_state(); + + return send_created_note_ids; + }, + senderAccountId, + targetAccountId, + faucetAccountId, + amount, + recallHeight + ); +}; + interface ConsumeTransactionResult { transactionId: string; nonce: string | undefined; @@ -145,7 +211,10 @@ export const fetchAndCacheAccountAuth = async (accountId: string) => { export const syncState = async () => { return await testingPage.evaluate(async () => { const client = window.client; - await client.sync_state(); + const summary = await client.sync_state(); + return { + blockNum: summary.block_num(), + }; }); };