mod_test.ts
1 import { expect } from "@std/expect"; 2 import { createClient, http, publicActions, walletActions } from "viem"; 3 import { hardhat } from "viem/chains"; 4 import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; 5 6 import { random256BigInt } from "@massmarket/utils"; 7 import { 8 abi, 9 checkPermissions, 10 mintShop, 11 permissions, 12 publishInviteVerifier, 13 redeemInviteSecret, 14 setTokenURI, 15 } from "./mod.ts"; 16 17 const client = createClient({ 18 chain: hardhat, 19 transport: http(), 20 }).extend(walletActions).extend(publicActions); 21 22 Deno.test({ 23 name: "blockChain Client", 24 sanitizeResources: false, 25 sanitizeOps: false, 26 async fn(t) { 27 const shopId = random256BigInt(); 28 const [account] = await client.requestAddresses(); 29 30 await t.step("mintShop", async () => { 31 const transactionHash = await mintShop(client, account, [ 32 shopId, 33 account, 34 ]); 35 // this is still causing a leak 36 // https://github.com/wevm/viem/issues/2903 37 const receipt = await client.waitForTransactionReceipt({ 38 hash: transactionHash, 39 }); 40 expect(receipt.status).toBe("success"); 41 }); 42 await t.step("setTokenURI", async () => { 43 const test_uri = "/testing/path"; 44 const transactionHash = await setTokenURI(client, account, [ 45 shopId, 46 test_uri, 47 ]); 48 const receipt = await client.waitForTransactionReceipt({ 49 hash: transactionHash, 50 }); 51 expect(receipt.status).toBe("success"); 52 53 const uri = await client.readContract({ 54 address: abi.shopRegAddress, 55 abi: abi.shopRegAbi, 56 functionName: "tokenURI", 57 args: [shopId], 58 }); 59 expect(uri).toEqual(test_uri); 60 }); 61 }, 62 }); 63 64 Deno.test({ 65 name: "blockChain: registrationTokenRedeem", 66 sanitizeResources: false, 67 sanitizeOps: false, 68 async fn(t) { 69 const shopId = random256BigInt(); 70 // still contrived, we would use a separate keypair in reality and pass it via some sidechannel 71 // 72 // acc2 is the "long term wallet" of the new user 73 // if we knew that before hand, we could just call registerUser(acc2.address, Clerk) 74 // but we do this to exercise the invite flow 75 const client = createClient({ 76 chain: hardhat, 77 transport: http(), 78 }).extend(walletActions).extend(publicActions); 79 const [account1, account2] = await client.requestAddresses(); 80 81 await t.step("mintShop", async () => { 82 const transactionHash = await mintShop(client, account1, [ 83 shopId, 84 account1, 85 ]); 86 const receipt = await client.waitForTransactionReceipt({ 87 hash: transactionHash, 88 }); 89 expect(receipt.status).toBe("success"); 90 }); 91 92 const sk = generatePrivateKey(); 93 await t.step("publishInviteVerifier", async () => { 94 const token = privateKeyToAccount(sk); 95 96 const hash = await publishInviteVerifier(client, account1, [ 97 shopId, 98 token.address, 99 ]); 100 101 // wait for the transaction to be included in the blockchain 102 const receipt = await client.waitForTransactionReceipt({ 103 hash, 104 }); 105 expect(receipt.status).toBe("success"); 106 }); 107 108 await t.step("redeemInviteSecret", async () => { 109 const client2 = createClient({ 110 chain: hardhat, 111 transport: http(), 112 }).extend(walletActions).extend(publicActions); 113 114 const hash2 = await redeemInviteSecret(sk, client2, account2, shopId); 115 // wait for the transaction to be included in the blockchain 116 const transaction = await client2.waitForTransactionReceipt({ 117 hash: hash2, 118 }); 119 expect(transaction.status).toBe("success"); 120 121 const canUpdateRootHash = await checkPermissions(client2, [ 122 shopId, 123 account2, 124 permissions.updateRootHash, 125 ]); 126 expect(canUpdateRootHash).toBe(true); 127 }); 128 }, 129 });