Skip to content

Commit 8c4b585

Browse files
committed
refactor: 💡 Examples uses latest Node and Client updates
1 parent 1c0ac1a commit 8c4b585

File tree

5 files changed

+92
-47
lines changed

5 files changed

+92
-47
lines changed

‎examples/client/src/App.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState, useEffect, useRef, useCallback } from 'react';
2-
import { Hash } from 'viem';
2+
import { Hash, stringify } from 'viem';
33
import {
44
Client,
55
ClientOptions,
@@ -10,7 +10,6 @@ import {
1010
} from '../../../src/index.js'; // @windingtree/sdk
1111
import { RequestQuery, OfferOptions, chainConfig, serverAddress } from '../../shared/index.js';
1212
import { OfferData } from '../../../src/shared/types.js';
13-
import { stringify } from '../../../src/utils/hash.js';
1413
import { centerEllipsis, formatBalance, parseWalletError } from './utils.js';
1514
import { useWallet } from './providers/WalletProvider/WalletProviderContext.js';
1615
import { ConnectButton } from './providers/WalletProvider/ConnectButton.js';

‎examples/client/src/providers/WalletProvider/index.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {
66
custom,
77
createPublicClient,
88
createWalletClient,
9+
http,
910
} from 'viem';
10-
import { polygonZkEvmTestnet } from 'viem/chains';
11+
import { polygonZkEvmTestnet, hardhat } from 'viem/chains';
1112
import { WalletContext } from './WalletProviderContext';
1213
import { formatBalance } from '../../utils';
1314

@@ -18,7 +19,7 @@ export const WalletProvider = ({ children }: PropsWithChildren) => {
1819
const [account, setAccount] = useState<Address | undefined>();
1920
const [loading, setLoading] = useState<boolean>(false);
2021
const [isConnected, setIsConnected] = useState<boolean>(false);
21-
const [balance, setBalance] = useState<string>('0.00');
22+
const [balance, setBalance] = useState<string>('0.0000');
2223
const [error, setError] = useState<string | undefined>();
2324

2425
const getChainId = async (publicClient: PublicClient): Promise<bigint> =>
@@ -38,12 +39,12 @@ export const WalletProvider = ({ children }: PropsWithChildren) => {
3839

3940
const handleChainChanged = async (): Promise<void> => {
4041
const publicClient = createPublicClient({
41-
chain: polygonZkEvmTestnet,
42-
transport: custom(window.ethereum),
42+
chain: import.meta.env.VITE_LOCAL_NODE === 'hardhat' ? hardhat : polygonZkEvmTestnet,
43+
transport: http(),
4344
});
4445
setPublicClient(publicClient);
4546
const walletClient = createWalletClient({
46-
chain: polygonZkEvmTestnet,
47+
chain: import.meta.env.VITE_LOCAL_NODE === 'hardhat' ? hardhat : polygonZkEvmTestnet,
4748
transport: custom(window.ethereum),
4849
});
4950
setWalletClient(walletClient);

‎examples/client/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const centerEllipsis = (text: string, width = 4, prefix = 2): string =>
1212

1313
// Formats balance value
1414
export const formatBalance = (balance: bigint, decimals: number): string => {
15-
const ether = formatUnits(balance, decimals);
15+
const ether = formatUnits(balance, 18);
1616
const decimalPart = ether.split('.')[1] || '';
1717
const paddedDecimalPart = decimalPart.padEnd(decimals, '0');
1818
return `${ether.split('.')[0]}.${paddedDecimalPart.slice(0, decimals)}`;

‎examples/node/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import 'dotenv/config';
22
import { EventHandler } from '@libp2p/interfaces/events';
33
import { DateTime } from 'luxon';
4-
import { Hash } from 'viem';
4+
import { Hash, Hex } from 'viem';
5+
import { randomSalt } from '@windingtree/contracts';
56
import {
67
RequestQuery,
78
OfferOptions,
@@ -14,22 +15,19 @@ import { OfferData } from '../../src/shared/types.js';
1415
import { noncePeriod } from '../../src/constants.js';
1516
import { memoryStorage } from '../../src/storage/index.js';
1617
import { nowSec, parseSeconds } from '../../src/utils/time.js';
17-
import { randomSalt } from '../../src/utils/uid.js';
1818
import { RequestEvent } from '../../src/node/requestManager.js';
1919
import { createLogger } from '../../src/utils/logger.js';
2020

2121
const logger = createLogger('NodeMain');
2222

2323
/**
24-
* These are randomly generated wallets, just for demonstration.
25-
* In production, you have to provide (and handle) them in a secure way
24+
* The supplier signer credentials
2625
*/
2726
const signerMnemonic = process.env.EXAMPLE_ENTITY_SIGNER_MNEMONIC;
27+
const signerPk = process.env.EXAMPLE_ENTITY_SIGNER_PK as Hex;
2828

29-
if (!signerMnemonic) {
30-
throw new Error(
31-
'Entity signer mnemonic must be provided with EXAMPLE_ENTITY_SIGNER_MNEMONIC env',
32-
);
29+
if (!signerMnemonic && !signerPk) {
30+
throw new Error('Either signerMnemonic or signerPk must be provided with env');
3331
}
3432

3533
/**
@@ -156,6 +154,7 @@ const main = async (): Promise<void> => {
156154
noncePeriod: Number(parseSeconds(noncePeriod)),
157155
supplierId,
158156
signerSeedPhrase: signerMnemonic,
157+
signerPk: signerPk,
159158
};
160159
const node = createNode<RequestQuery, OfferOptions>(options);
161160

‎examples/shared/index.ts

Lines changed: 77 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,84 @@ export interface OfferOptions extends GenericOfferOptions {
1212
buonasera: boolean;
1313
}
1414

15-
export const chainConfig: ProtocolChain = {
16-
chainId: 1442,
17-
contracts: {
18-
config: {
19-
name: 'Config',
20-
version: '1',
21-
address: '0x098b1d12cAfE7315C77b6d308A62ce02806260Ee',
22-
},
23-
entities: {
24-
name: 'EntitiesRegistry',
25-
version: '1',
26-
address: '0x4bB51528C83844b509E1152EEb05260eE1bf60e6',
27-
},
28-
market: {
29-
name: 'Market',
30-
version: '1',
31-
address: '0xDd5B6ffB3585E109ECddec5293e31cdc1e9DeD57',
32-
},
33-
token: {
34-
name: 'LifToken',
35-
version: '1',
36-
address: '0x4d60F4483BaA654CdAF1c5734D9E6B16735efCF8',
37-
},
38-
},
39-
};
15+
export interface LocalEnv {
16+
LOCAL_NODE?: string;
17+
VITE_LOCAL_NODE?: string;
18+
}
19+
20+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
21+
const env =
22+
typeof window === 'undefined' ? process.env : (import.meta as unknown as { env: LocalEnv }).env;
23+
24+
export const chainConfig: ProtocolChain =
25+
env.LOCAL_NODE === 'hardhat' || env.VITE_LOCAL_NODE === 'hardhat'
26+
? {
27+
chainId: 31337, // Local Hardhat node
28+
chainName: 'hardhat',
29+
contracts: {
30+
config: {
31+
name: 'Config',
32+
version: '1',
33+
address: '0x3Aa5ebB10DC797CAC828524e59A333d0A371443c',
34+
},
35+
entities: {
36+
name: 'EntitiesRegistry',
37+
version: '1',
38+
address: '0x59b670e9fA9D0A427751Af201D676719a970857b',
39+
},
40+
market: {
41+
name: 'Market',
42+
version: '1',
43+
address: '0x09635F643e140090A9A8Dcd712eD6285858ceBef',
44+
},
45+
token: {
46+
name: 'LifToken',
47+
version: '1',
48+
address: '0x959922bE3CAee4b8Cd9a407cc3ac1C251C2007B1',
49+
},
50+
},
51+
}
52+
: {
53+
chainId: 1442,
54+
chainName: 'zkSyncTestnet',
55+
contracts: {
56+
config: {
57+
name: 'Config',
58+
version: '1',
59+
address: '0x098b1d12cAfE7315C77b6d308A62ce02806260Ee',
60+
},
61+
entities: {
62+
name: 'EntitiesRegistry',
63+
version: '1',
64+
address: '0x4bB51528C83844b509E1152EEb05260eE1bf60e6',
65+
},
66+
market: {
67+
name: 'Market',
68+
version: '1',
69+
address: '0xDd5B6ffB3585E109ECddec5293e31cdc1e9DeD57',
70+
},
71+
token: {
72+
name: 'LifToken',
73+
version: '1',
74+
address: '0x4d60F4483BaA654CdAF1c5734D9E6B16735efCF8',
75+
},
76+
},
77+
};
4078

41-
export const stableCoins: Record<string, Address> = {
42-
stable6: '0x8CB96383609C56af1Fe44DB7591F94AEE2fa43b2',
43-
stable6permit: '0x4556d5C1486d799f67FA96c84F1d0552486CAAF4',
44-
stable18: '0x4EcB659060Da61D795D777bb21BAe3599b301C66',
45-
stable18permit: '0xF54784206A53EF19fd3024D8cdc7A6251A4A0d67',
46-
};
79+
export const stableCoins: Record<string, Address> =
80+
env.LOCAL_NODE === 'hardhat' || env.VITE_LOCAL_NODE === 'hardhat'
81+
? {
82+
stable6: '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0',
83+
stable6permit: '0x5FC8d32690cc91D4c39d9d3abcBD16989F875707',
84+
stable18: '0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6',
85+
stable18permit: '0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e',
86+
}
87+
: {
88+
stable6: '0x8CB96383609C56af1Fe44DB7591F94AEE2fa43b2',
89+
stable6permit: '0x4556d5C1486d799f67FA96c84F1d0552486CAAF4',
90+
stable18: '0x4EcB659060Da61D795D777bb21BAe3599b301C66',
91+
stable18permit: '0xF54784206A53EF19fd3024D8cdc7A6251A4A0d67',
92+
};
4793

4894
export const serverAddress =
4995
'/ip4/127.0.0.1/tcp/33333/ws/p2p/QmcXbDrzUU5ERqRaronWmAJXwe6c7AEkS7qdcsjgEuWPCf';

0 commit comments

Comments
 (0)