|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { createWalletClient, http, getContractAddress, publicActions } from 'viem' |
| 3 | +import { mnemonicToAccount } from 'viem/accounts' |
| 4 | + |
| 5 | +import EchoJson from '../artifacts/contracts/Echo.sol/Echo.json'; |
| 6 | + |
| 7 | +const TEST_MNEMONIC = 'fox sight canyon orphan hotel grow hedgehog build bless august weather swarm'; |
| 8 | +const account = mnemonicToAccount(TEST_MNEMONIC) |
| 9 | +const client = createWalletClient({ |
| 10 | + account, |
| 11 | + chain: { // TODO: support public mandala after PR is merged |
| 12 | + name: 'local', |
| 13 | + id: 595, |
| 14 | + nativeCurrency: { |
| 15 | + name: 'acala', |
| 16 | + symbol: 'ACA', |
| 17 | + decimals: 18, |
| 18 | + }, |
| 19 | + rpcUrls: { |
| 20 | + default: { http: ['http://localhost:8545'] }, |
| 21 | + public: { http: ['http://localhost:8545'] }, |
| 22 | + }, |
| 23 | + network: 'local', |
| 24 | + }, |
| 25 | + transport: http('http://localhost:8545') |
| 26 | +}).extend(publicActions) |
| 27 | + |
| 28 | +describe('Echo contract', function () { |
| 29 | + it("can deploy, read, and write contract", async () => { |
| 30 | + /* ----------------- deploy ----------------- */ |
| 31 | + const deployHash = await client.deployContract({ |
| 32 | + abi: EchoJson.abi, |
| 33 | + args: [], |
| 34 | + bytecode: EchoJson.bytecode as `0x${string}`, |
| 35 | + }) |
| 36 | + |
| 37 | + await client.waitForTransactionReceipt({ hash: deployHash }) |
| 38 | + const tx = await client.getTransaction({ hash: deployHash }) |
| 39 | + |
| 40 | + const contractAddr = getContractAddress({ |
| 41 | + from: tx.from, |
| 42 | + nonce: BigInt(tx.nonce), |
| 43 | + }) |
| 44 | + |
| 45 | + /* ----------------- read ----------------- */ |
| 46 | + let echoValue = await client.readContract({ |
| 47 | + address: contractAddr, |
| 48 | + abi: EchoJson.abi, |
| 49 | + functionName: 'echo', |
| 50 | + }) |
| 51 | + expect(echoValue).to.equal('Deployed successfully!'); |
| 52 | + |
| 53 | + /* ----------------- write ----------------- */ |
| 54 | + const { request } = await client.simulateContract({ |
| 55 | + address: contractAddr, |
| 56 | + abi: EchoJson.abi, |
| 57 | + functionName: 'scream', |
| 58 | + args: ['Hello World!'], |
| 59 | + }) |
| 60 | + const callHash = await client.writeContract(request) |
| 61 | + await client.waitForTransactionReceipt({ hash: callHash }) |
| 62 | + |
| 63 | + echoValue = await client.readContract({ |
| 64 | + address: contractAddr, |
| 65 | + abi: EchoJson.abi, |
| 66 | + functionName: 'echo', |
| 67 | + }) |
| 68 | + expect(echoValue).to.equal('Hello World!'); |
| 69 | + }) |
| 70 | +}); |
0 commit comments