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' ;
5
3
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' ;
30
6
31
7
describe ( '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 ( ) => {
34
11
const deployHash = await client . deployContract ( {
35
12
abi : EchoJson . abi ,
36
- args : [ ] ,
37
13
bytecode : EchoJson . bytecode as `0x${string } `,
38
14
} ) ;
39
15
40
16
await client . waitForTransactionReceipt ( { hash : deployHash } ) ;
41
17
const tx = await client . getTransaction ( { hash : deployHash } ) ;
42
18
43
- const contractAddr = getContractAddress ( {
19
+ contractAddr = getContractAddress ( {
44
20
from : tx . from ,
45
21
nonce : BigInt ( tx . nonce ) ,
46
22
} ) ;
23
+ } ) ;
47
24
25
+ it ( 'read and write contract' , async ( ) => {
48
26
/* ----------------- read ----------------- */
49
27
let echoValue = await client . readContract ( {
50
28
address : contractAddr ,
@@ -70,4 +48,64 @@ describe('Echo contract', function () {
70
48
} ) ;
71
49
expect ( echoValue ) . to . equal ( 'Hello World!' ) ;
72
50
} ) ;
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
+ } ) ;
73
111
} ) ;
0 commit comments