Skip to content

Commit d218d40

Browse files
authored
update viem example to include public network config (#915)
1 parent 23c82ac commit d218d40

File tree

6 files changed

+53
-23
lines changed

6 files changed

+53
-23
lines changed

examples/viem/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,10 @@ yarn build
5151
```
5252
yarn test
5353
```
54+
55+
- run tests on public networks
56+
```
57+
yarn test:mandala
58+
yarn test:karura
59+
yarn test:acala
60+
```

examples/viem/hardhat.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { HardhatUserConfig } from 'hardhat/config';
22

33
const config: HardhatUserConfig = {
44
solidity: '0.8.9',
5+
mocha: {
6+
timeout: 987654321,
7+
},
58
};
69

710
export default config;

examples/viem/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
"main": "src/index.ts",
55
"scripts": {
66
"build": "hardhat compile",
7-
"test": "hardhat test"
7+
"test": "hardhat test",
8+
"test:mandala": "CHAIN=mandala hardhat test",
9+
"test:karura": "CHAIN=karura hardhat test",
10+
"test:acala": "CHAIN=acala hardhat test"
811
},
912
"devDependencies": {
1013
"@types/node": "^20.10.5",
1114
"hardhat": "^2.19.4",
1215
"typescript": "^5.3.3",
13-
"viem": "^1.21.0"
16+
"viem": "^1.21.4"
1417
}
1518
}

examples/viem/test/echo.test.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
import { expect } from 'chai';
22
import { createWalletClient, http, getContractAddress, publicActions } from 'viem'
3+
import { mandala, karura, acala } from 'viem/chains'
34
import { mnemonicToAccount } from 'viem/accounts'
45

56
import EchoJson from '../artifacts/contracts/Echo.sol/Echo.json';
7+
import { localChainConfig } from './utils';
68

79
const TEST_MNEMONIC = 'fox sight canyon orphan hotel grow hedgehog build bless august weather swarm';
8-
const account = mnemonicToAccount(TEST_MNEMONIC)
10+
const account = mnemonicToAccount(TEST_MNEMONIC);
11+
12+
const targetChain = process.env.CHAIN ?? 'local';
13+
const chainConfig = ({
14+
local: localChainConfig,
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}`)
925
const client = createWalletClient({
1026
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')
27+
chain: chainConfig,
28+
transport: http()
2629
}).extend(publicActions)
2730

2831
describe('Echo contract', function () {

examples/viem/test/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const localChainConfig = {
2+
name: 'local',
3+
id: 595,
4+
nativeCurrency: {
5+
name: 'acala',
6+
symbol: 'ACA',
7+
decimals: 18,
8+
},
9+
rpcUrls: {
10+
default: { http: ['http://localhost:8545'] },
11+
public: { http: ['http://localhost:8545'] },
12+
},
13+
network: 'local',
14+
};

yarn.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)