|
| 1 | +import { |
| 2 | + HatsModulesClient, |
| 3 | + solidityToTypescriptType, |
| 4 | +} from "@hatsprotocol/modules-sdk"; |
| 5 | +import { createPublicClient, createWalletClient, http } from "viem"; |
| 6 | +import { privateKeyToAccount } from "viem/accounts"; |
| 7 | +import { base, baseSepolia } from "viem/chains"; |
| 8 | +import { createAnvil } from "@viem/anvil"; |
| 9 | +import { bundleModules } from "../bundler"; |
| 10 | +import type { |
| 11 | + PublicClient, |
| 12 | + WalletClient, |
| 13 | + PrivateKeyAccount, |
| 14 | + Address, |
| 15 | +} from "viem"; |
| 16 | +import type { Anvil } from "@viem/anvil"; |
| 17 | +import type { Module, Registry } from "@hatsprotocol/modules-sdk"; |
| 18 | +import "dotenv/config"; |
| 19 | + |
| 20 | +describe("Base deployments", () => { |
| 21 | + let publicClient: PublicClient; |
| 22 | + let walletClient: WalletClient; |
| 23 | + let hatsModulesClient: HatsModulesClient; |
| 24 | + let anvil: Anvil; |
| 25 | + let deployerAccount: PrivateKeyAccount; |
| 26 | + let instances: Address[] = []; |
| 27 | + |
| 28 | + beforeAll(async () => { |
| 29 | + anvil = createAnvil({ |
| 30 | + forkUrl: process.env.BASE_SEPOLIA_RPC, |
| 31 | + startTimeout: 20000, |
| 32 | + }); |
| 33 | + await anvil.start(); |
| 34 | + |
| 35 | + deployerAccount = privateKeyToAccount( |
| 36 | + "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", |
| 37 | + ); |
| 38 | + |
| 39 | + // init Viem clients |
| 40 | + publicClient = createPublicClient({ |
| 41 | + chain: baseSepolia, |
| 42 | + transport: http("http://127.0.0.1:8545"), |
| 43 | + }) as PublicClient; |
| 44 | + walletClient = createWalletClient({ |
| 45 | + chain: baseSepolia, |
| 46 | + transport: http("http://127.0.0.1:8545"), |
| 47 | + }); |
| 48 | + |
| 49 | + const registryModules: Registry = bundleModules() as unknown as Registry; |
| 50 | + |
| 51 | + hatsModulesClient = new HatsModulesClient({ |
| 52 | + publicClient, |
| 53 | + walletClient, |
| 54 | + }); |
| 55 | + |
| 56 | + await hatsModulesClient.prepare(registryModules); |
| 57 | + }, 30000); |
| 58 | + |
| 59 | + afterAll(async () => { |
| 60 | + await anvil.stop(); |
| 61 | + }, 30000); |
| 62 | + |
| 63 | + test("Test create all modules", async () => { |
| 64 | + const modules = hatsModulesClient.getModules(); |
| 65 | + |
| 66 | + // create new module instance for each module which is deployed on goerli |
| 67 | + for (const [id, module] of Object.entries(modules)) { |
| 68 | + console.log(`Testing module: ${module.name}`); |
| 69 | + if (module.name === "JokeRace Eligibility") { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + // the unlock module has dependencies on other external contracts |
| 74 | + if ( |
| 75 | + module.implementationAddress === |
| 76 | + "0x4c7803041851f7a17Fc6b5Ff5c911FC748160637" |
| 77 | + ) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + // check if module is deployed on base sepolia. If not, then skip |
| 82 | + let isOnBaseSepolia = false; |
| 83 | + for (let i = 0; i < module.deployments.length; i++) { |
| 84 | + if (module.deployments[i].chainId === "84532") { |
| 85 | + isOnBaseSepolia = true; |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + if (!isOnBaseSepolia) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + const hatId = module.creationArgs.useHatId |
| 94 | + ? BigInt( |
| 95 | + "0x0000000100000000000000000000000000000000000000000000000000000000", |
| 96 | + ) |
| 97 | + : BigInt("0"); |
| 98 | + const immutableArgs: unknown[] = []; |
| 99 | + const mutableArgs: unknown[] = []; |
| 100 | + |
| 101 | + // prepare immutable args |
| 102 | + for (let i = 0; i < module.creationArgs.immutable.length; i++) { |
| 103 | + let arg: unknown; |
| 104 | + const exampleArg = module.creationArgs.immutable[i].example; |
| 105 | + const tsType = solidityToTypescriptType( |
| 106 | + module.creationArgs.immutable[i].type, |
| 107 | + ); |
| 108 | + if (tsType === "bigint") { |
| 109 | + arg = BigInt(exampleArg as string); |
| 110 | + } else if (tsType === "bigint[]") { |
| 111 | + arg = (exampleArg as Array<string>).map((val) => BigInt(val)); |
| 112 | + } else { |
| 113 | + arg = exampleArg; |
| 114 | + } |
| 115 | + |
| 116 | + immutableArgs.push(arg); |
| 117 | + } |
| 118 | + |
| 119 | + // prepare mutable args |
| 120 | + for (let i = 0; i < module.creationArgs.mutable.length; i++) { |
| 121 | + let arg: unknown; |
| 122 | + const exampleArg = module.creationArgs.mutable[i].example; |
| 123 | + const tsType = solidityToTypescriptType( |
| 124 | + module.creationArgs.mutable[i].type, |
| 125 | + ); |
| 126 | + if (tsType === "bigint") { |
| 127 | + arg = BigInt(exampleArg as string); |
| 128 | + } else if (tsType === "bigint[]") { |
| 129 | + arg = (exampleArg as Array<string>).map((val) => BigInt(val)); |
| 130 | + } else { |
| 131 | + arg = exampleArg; |
| 132 | + } |
| 133 | + |
| 134 | + mutableArgs.push(arg); |
| 135 | + } |
| 136 | + |
| 137 | + // create new module instance |
| 138 | + const res = await hatsModulesClient.createNewInstance({ |
| 139 | + account: deployerAccount, |
| 140 | + moduleId: id, |
| 141 | + hatId: hatId, |
| 142 | + immutableArgs: immutableArgs, |
| 143 | + mutableArgs: mutableArgs, |
| 144 | + }); |
| 145 | + |
| 146 | + instances.push(res.newInstance); |
| 147 | + |
| 148 | + // check correct hat Id in the new instance |
| 149 | + const hatIdResult = await publicClient.readContract({ |
| 150 | + address: res.newInstance as Address, |
| 151 | + abi: module.abi, |
| 152 | + functionName: "hatId", |
| 153 | + args: [], |
| 154 | + }); |
| 155 | + expect(hatIdResult).toBe(hatId); |
| 156 | + } |
| 157 | + }, 30000); |
| 158 | + |
| 159 | + test("Test module parameters", async () => { |
| 160 | + for (let i = 0; i < instances.length; i++) { |
| 161 | + let instance = instances[i]; |
| 162 | + |
| 163 | + const module = await hatsModulesClient.getModuleByInstance(instance); |
| 164 | + const res = await hatsModulesClient.getInstanceParameters(instance); |
| 165 | + |
| 166 | + if (res === undefined || res.length !== module?.parameters.length) { |
| 167 | + throw new Error( |
| 168 | + `Error: could not read all parameters from the instance of module ${module?.name}`, |
| 169 | + ); |
| 170 | + } |
| 171 | + } |
| 172 | + }, 30000); |
| 173 | +}); |
0 commit comments