Skip to content

Commit 69d6009

Browse files
committed
added viem subscription tests
1 parent 78133f0 commit 69d6009

File tree

5 files changed

+167
-119
lines changed

5 files changed

+167
-119
lines changed

e2e-tests/e2e-viem/contracts/Echo.sol

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ contract Echo{
44
string public echo;
55
uint echoCount;
66

7-
event NewEcho(string message, uint count);
7+
event NewEcho(
8+
string indexed message,
9+
uint256 count
10+
);
811

912
constructor() {
1013
echo = "Deployed successfully!";

e2e-tests/e2e-viem/docker-compose.yml

Lines changed: 0 additions & 86 deletions
This file was deleted.

e2e-tests/e2e-viem/test/consts.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
export const ECHO_JSON = {
2+
_format: 'hh-sol-artifact-1',
3+
contractName: 'Echo',
4+
sourceName: 'contracts/Echo.sol',
5+
abi: [
6+
{
7+
inputs: [],
8+
stateMutability: 'nonpayable',
9+
type: 'constructor',
10+
},
11+
{
12+
anonymous: false,
13+
inputs: [
14+
{
15+
indexed: true,
16+
internalType: 'string',
17+
name: 'message',
18+
type: 'string',
19+
},
20+
{
21+
indexed: false,
22+
internalType: 'uint256',
23+
name: 'count',
24+
type: 'uint256',
25+
},
26+
],
27+
name: 'NewEcho',
28+
type: 'event',
29+
},
30+
{
31+
inputs: [],
32+
name: 'echo',
33+
outputs: [
34+
{
35+
internalType: 'string',
36+
name: '',
37+
type: 'string',
38+
},
39+
],
40+
stateMutability: 'view',
41+
type: 'function',
42+
},
43+
{
44+
inputs: [
45+
{
46+
internalType: 'string',
47+
name: 'message',
48+
type: 'string',
49+
},
50+
],
51+
name: 'scream',
52+
outputs: [
53+
{
54+
internalType: 'string',
55+
name: '',
56+
type: 'string',
57+
},
58+
],
59+
stateMutability: 'nonpayable',
60+
type: 'function',
61+
},
62+
],
63+
bytecode: '0x608060405234801561001057600080fd5b506040518060400160405280601681526020017f4465706c6f796564207375636365737366756c6c7921000000000000000000008152506000908051906020019061005c929190610062565b50610166565b82805461006e90610134565b90600052602060002090601f01602090048101928261009057600085556100d7565b82601f106100a957805160ff19168380011785556100d7565b828001600101855582156100d7579182015b828111156100d65782518255916020019190600101906100bb565b5b5090506100e491906100e8565b5090565b5b808211156101015760008160009055506001016100e9565b5090565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061014c57607f821691505b602082108114156101605761015f610105565b5b50919050565b610633806101756000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806329f4acb01461003b578063a773179714610059575b600080fd5b610043610089565b60405161005091906102dc565b60405180910390f35b610073600480360381019061006e9190610447565b610117565b60405161008091906102dc565b60405180910390f35b60008054610096906104bf565b80601f01602080910402602001604051908101604052809291908181526020018280546100c2906104bf565b801561010f5780601f106100e45761010080835404028352916020019161010f565b820191906000526020600020905b8154815290600101906020018083116100f257829003601f168201915b505050505081565b6060816000908051906020019061012f9291906101a0565b506001806000828254610142919061052a565b925050819055508160405161015791906105bc565b60405180910390207f8b4855b98ea931f3580ec353f7cf98d66f7424b654cfc00e50d87d6b53d9e4ee60015460405161019091906105e2565b60405180910390a2819050919050565b8280546101ac906104bf565b90600052602060002090601f0160209004810192826101ce5760008555610215565b82601f106101e757805160ff1916838001178555610215565b82800160010185558215610215579182015b828111156102145782518255916020019190600101906101f9565b5b5090506102229190610226565b5090565b5b8082111561023f576000816000905550600101610227565b5090565b600081519050919050565b600082825260208201905092915050565b60005b8381101561027d578082015181840152602081019050610262565b8381111561028c576000848401525b50505050565b6000601f19601f8301169050919050565b60006102ae82610243565b6102b8818561024e565b93506102c881856020860161025f565b6102d181610292565b840191505092915050565b600060208201905081810360008301526102f681846102a3565b905092915050565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b61035482610292565b810181811067ffffffffffffffff821117156103735761037261031c565b5b80604052505050565b60006103866102fe565b9050610392828261034b565b919050565b600067ffffffffffffffff8211156103b2576103b161031c565b5b6103bb82610292565b9050602081019050919050565b82818337600083830152505050565b60006103ea6103e584610397565b61037c565b90508281526020810184848401111561040657610405610317565b5b6104118482856103c8565b509392505050565b600082601f83011261042e5761042d610312565b5b813561043e8482602086016103d7565b91505092915050565b60006020828403121561045d5761045c610308565b5b600082013567ffffffffffffffff81111561047b5761047a61030d565b5b61048784828501610419565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806104d757607f821691505b602082108114156104eb576104ea610490565b5b50919050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610535826104f1565b9150610540836104f1565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03821115610575576105746104fb565b5b828201905092915050565b600081905092915050565b600061059682610243565b6105a08185610580565b93506105b081856020860161025f565b80840191505092915050565b60006105c8828461058b565b915081905092915050565b6105dc816104f1565b82525050565b60006020820190506105f760008301846105d3565b9291505056fea2646970667358221220fb4c282160abfe97dd28d56ae49c82129f613a915136a56d953f458dcec4c08a64736f6c63430008090033',
64+
deployedBytecode: '0x608060405234801561001057600080fd5b50600436106100365760003560e01c806329f4acb01461003b578063a773179714610059575b600080fd5b610043610089565b60405161005091906102dc565b60405180910390f35b610073600480360381019061006e9190610447565b610117565b60405161008091906102dc565b60405180910390f35b60008054610096906104bf565b80601f01602080910402602001604051908101604052809291908181526020018280546100c2906104bf565b801561010f5780601f106100e45761010080835404028352916020019161010f565b820191906000526020600020905b8154815290600101906020018083116100f257829003601f168201915b505050505081565b6060816000908051906020019061012f9291906101a0565b506001806000828254610142919061052a565b925050819055508160405161015791906105bc565b60405180910390207f8b4855b98ea931f3580ec353f7cf98d66f7424b654cfc00e50d87d6b53d9e4ee60015460405161019091906105e2565b60405180910390a2819050919050565b8280546101ac906104bf565b90600052602060002090601f0160209004810192826101ce5760008555610215565b82601f106101e757805160ff1916838001178555610215565b82800160010185558215610215579182015b828111156102145782518255916020019190600101906101f9565b5b5090506102229190610226565b5090565b5b8082111561023f576000816000905550600101610227565b5090565b600081519050919050565b600082825260208201905092915050565b60005b8381101561027d578082015181840152602081019050610262565b8381111561028c576000848401525b50505050565b6000601f19601f8301169050919050565b60006102ae82610243565b6102b8818561024e565b93506102c881856020860161025f565b6102d181610292565b840191505092915050565b600060208201905081810360008301526102f681846102a3565b905092915050565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b61035482610292565b810181811067ffffffffffffffff821117156103735761037261031c565b5b80604052505050565b60006103866102fe565b9050610392828261034b565b919050565b600067ffffffffffffffff8211156103b2576103b161031c565b5b6103bb82610292565b9050602081019050919050565b82818337600083830152505050565b60006103ea6103e584610397565b61037c565b90508281526020810184848401111561040657610405610317565b5b6104118482856103c8565b509392505050565b600082601f83011261042e5761042d610312565b5b813561043e8482602086016103d7565b91505092915050565b60006020828403121561045d5761045c610308565b5b600082013567ffffffffffffffff81111561047b5761047a61030d565b5b61048784828501610419565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806104d757607f821691505b602082108114156104eb576104ea610490565b5b50919050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610535826104f1565b9150610540836104f1565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03821115610575576105746104fb565b5b828201905092915050565b600081905092915050565b600061059682610243565b6105a08185610580565b93506105b081856020860161025f565b80840191505092915050565b60006105c8828461058b565b915081905092915050565b6105dc816104f1565b82525050565b60006020820190506105f760008301846105d3565b9291505056fea2646970667358221220fb4c282160abfe97dd28d56ae49c82129f613a915136a56d953f458dcec4c08a64736f6c63430008090033',
65+
linkReferences: {},
66+
deployedLinkReferences: {},
67+
} as const;

e2e-tests/e2e-viem/test/echo.test.ts

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,28 @@
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

317
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 () => {
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
});

e2e-tests/e2e-viem/test/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { acala, karura, mandala } from 'viem/chains';
2+
import { createWalletClient, http, publicActions } from 'viem';
3+
import { mnemonicToAccount } from 'viem/accounts';
4+
15
export const acalaForkConfig = {
26
name: 'acala fork',
37
id: 787,
@@ -12,3 +16,25 @@ export const acalaForkConfig = {
1216
},
1317
network: 'local',
1418
};
19+
20+
const TEST_MNEMONIC = 'fox sight canyon orphan hotel grow hedgehog build bless august weather swarm';
21+
const account = mnemonicToAccount(TEST_MNEMONIC);
22+
23+
const targetChain = process.env.CHAIN ?? 'acalaFork';
24+
const chainConfig = ({
25+
acalaFork: acalaForkConfig,
26+
mandala,
27+
karura,
28+
acala,
29+
})[targetChain];
30+
31+
if (!chainConfig) {
32+
throw new Error('Invalid CHAIN env variable. Must be one { local, mandala, karura, acala }');
33+
}
34+
35+
console.log(`creating client for ${chainConfig.name}`);
36+
export const client = createWalletClient({
37+
account,
38+
chain: chainConfig,
39+
transport: http(),
40+
}).extend(publicActions);

0 commit comments

Comments
 (0)