1- import { acala , karura , mandala } from 'viem/chains' ;
2- import { createWalletClient , getContractAddress , http , publicActions } from 'viem' ;
3- import { expect } from 'chai' ;
4- import { mnemonicToAccount } from 'viem/accounts' ;
1+ import { assert , expect } from 'chai' ;
2+ import { getContractAddress } from 'viem' ;
53
6- import { acalaForkConfig } from './utils' ;
7- import EchoJson from '../artifacts/contracts/Echo.sol/Echo.json' ;
8-
9- const TEST_MNEMONIC = 'fox sight canyon orphan hotel grow hedgehog build bless august weather swarm' ;
10- const account = mnemonicToAccount ( TEST_MNEMONIC ) ;
11-
12- const targetChain = process . env . CHAIN ?? 'acalaFork' ;
13- const chainConfig = ( {
14- acalaFork : acalaForkConfig ,
15- mandala,
16- karura,
17- acala,
18- } ) [ targetChain ] ;
19-
20- if ( ! chainConfig ) {
21- throw new Error ( 'Invalid CHAIN env variable. Must be one { local, mandala, karura, acala }' ) ;
22- }
23-
24- console . log ( `creating client for ${ chainConfig . name } ` ) ;
25- const client = createWalletClient ( {
26- account,
27- chain : chainConfig ,
28- transport : http ( ) ,
29- } ) . extend ( publicActions ) ;
4+ import { ECHO_JSON as EchoJson } from './consts' ;
5+ import { client } from './utils' ;
306
317describe ( 'Echo contract' , function ( ) {
32- it ( 'can deploy, read, and write contract' , async ( ) => {
33- /* ----------------- deploy ----------------- */
8+ let contractAddr : `0x${string } `;
9+
10+ it ( 'deploy contract' , async ( ) => {
3411 const deployHash = await client . deployContract ( {
3512 abi : EchoJson . abi ,
36- args : [ ] ,
3713 bytecode : EchoJson . bytecode as `0x${string } `,
3814 } ) ;
3915
4016 await client . waitForTransactionReceipt ( { hash : deployHash } ) ;
4117 const tx = await client . getTransaction ( { hash : deployHash } ) ;
4218
43- const contractAddr = getContractAddress ( {
19+ contractAddr = getContractAddress ( {
4420 from : tx . from ,
4521 nonce : BigInt ( tx . nonce ) ,
4622 } ) ;
23+ } ) ;
4724
25+ it ( 'read and write contract' , async ( ) => {
4826 /* ----------------- read ----------------- */
4927 let echoValue = await client . readContract ( {
5028 address : contractAddr ,
@@ -70,4 +48,64 @@ describe('Echo contract', function () {
7048 } ) ;
7149 expect ( echoValue ) . to . equal ( 'Hello World!' ) ;
7250 } ) ;
51+
52+ it ( 'subscription' , async ( ) => {
53+ const msg = 'some mysterious msg' ;
54+ const msgHash = '0x9287dad622cb0d1809a6b37d936ec5ca8dd09645219887d4793fd63669f08715' ;
55+ let notified0 = false ;
56+ let notified1 = false ;
57+
58+ // general subscription
59+ const unwatch0 = client . watchContractEvent ( {
60+ address : contractAddr ,
61+ abi : EchoJson . abi ,
62+ eventName : 'NewEcho' ,
63+ onLogs : logs => {
64+ expect ( logs . length ) . to . equal ( 1 ) ;
65+ expect ( logs [ 0 ] . args . message ) . to . equal ( msgHash ) ;
66+ notified0 = true ;
67+ } ,
68+ } ) ;
69+
70+ // subscription with args
71+ const unwatch1 = client . watchContractEvent ( {
72+ address : contractAddr ,
73+ abi : EchoJson . abi ,
74+ eventName : 'NewEcho' ,
75+ args : { message : msg } ,
76+ onLogs : logs => {
77+ expect ( logs . length ) . to . equal ( 1 ) ;
78+ expect ( logs [ 0 ] . args . message ) . to . equal ( msgHash ) ;
79+ notified1 = true ;
80+ } ,
81+ } ) ;
82+
83+ // subscription that should not trigger
84+ const unwatch2 = client . watchContractEvent ( {
85+ address : contractAddr ,
86+ abi : EchoJson . abi ,
87+ eventName : 'NewEcho' ,
88+ args : { message : 'rand msg' } ,
89+ onLogs : _logs => {
90+ expect . fail ( 'should not trigger' ) ;
91+ } ,
92+ } ) ;
93+
94+ const { request } = await client . simulateContract ( {
95+ address : contractAddr ,
96+ abi : EchoJson . abi ,
97+ functionName : 'scream' ,
98+ args : [ msg ] ,
99+ } ) ;
100+ const callHash = await client . writeContract ( request ) ;
101+ await client . waitForTransactionReceipt ( { hash : callHash } ) ;
102+
103+ await new Promise ( resolve => setTimeout ( resolve , 5000 ) ) ; // wait for the notification
104+ expect ( notified0 ) . to . be . true ;
105+ expect ( notified1 ) . to . be . true ;
106+
107+ unwatch0 ( ) ;
108+ unwatch1 ( ) ;
109+ unwatch2 ( ) ;
110+ } ) ;
73111} ) ;
0 commit comments