Skip to content

Commit fc58218

Browse files
authored
Merge pull request #19 from windingtree/feat/node-contract
Smart contract interaction
2 parents e6f3429 + aa4306c commit fc58218

File tree

21 files changed

+928
-690
lines changed

21 files changed

+928
-690
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
# LOCAL_NODE=true
12
EXAMPLE_ENTITY_SIGNER_MNEMONIC=
3+
# EXAMPLE_ENTITY_SIGNER_PK=
24
EXAMPLE_ENTITY_ID=

examples/client/src/App.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useState, useEffect, useRef } from 'react';
2+
import { hardhat, polygonZkEvmTestnet } from 'viem/chains';
23
import { Client, ClientOptions, createClient, storage } from '../../../src/index.js'; // @windingtree/sdk
3-
import { RequestQuery, OfferOptions, chainConfig, serverAddress } from '../../shared/index.js';
4+
import { RequestQuery, OfferOptions, contractsConfig, serverAddress } from '../../shared/index.js';
45
import { OfferData } from '../../../src/shared/types.js';
56
import { useWallet } from './providers/WalletProvider/WalletProviderContext.js';
67
import { AccountWidget } from './providers/WalletProvider/AccountWidget.js';
@@ -11,6 +12,9 @@ import { MakeDeal } from './components/MakeDeal.js';
1112
import { Offers } from './components/Offers.js';
1213
import { Deals, DealsRegistryRecord } from './components/Deals.js';
1314

15+
/** Target chain config */
16+
const chain = import.meta.env.LOCAL_NODE === 'true' ? hardhat : polygonZkEvmTestnet;
17+
1418
/** Default request expiration time */
1519
const defaultExpire = '30s';
1620

@@ -38,7 +42,8 @@ export const App = () => {
3842
setError(undefined);
3943

4044
const options: ClientOptions = {
41-
chain: chainConfig,
45+
chain,
46+
contracts: contractsConfig,
4247
serverAddress,
4348
storageInitializer: storage.localStorage.createInitializer({
4449
session: false, // session or local storage

examples/client/src/components/Deals.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState, useCallback, useEffect } from 'react';
22
import { DateTime } from 'luxon';
3-
import { Address } from 'viem';
3+
import { Address, Hash } from 'viem';
44
import { Client, DealRecord, DealStatus } from '../../../../src/index.js'; // @windingtree/sdk
55
import { RequestQuery, OfferOptions } from '../../../shared/index.js';
66
import { centerEllipsis, formatBalance, parseWalletError } from '../utils.js';
@@ -57,7 +57,7 @@ export const TransferForm = ({ deal, client, onClose }: TransferFormProps) => {
5757
throw new Error('Ethereum client not ready');
5858
}
5959

60-
await client.deals.transfer(deal.offer.payload.id, to as Address, walletClient, setTx);
60+
await client.deals.transfer(deal.offer, to as Address, walletClient, setTx);
6161
setLoading(false);
6262
setSuccess(true);
6363
} catch (err) {
@@ -145,7 +145,7 @@ export const Cancel = ({ deal, client, onClose }: CancelProps) => {
145145
throw new Error('Ethereum client not ready');
146146
}
147147

148-
await client.deals.cancel(deal.offer.payload.id, walletClient, setTx);
148+
await client.deals.cancel(deal.offer, walletClient, setTx);
149149
setLoading(false);
150150
setSuccess(true);
151151
} catch (err) {

examples/client/src/components/MakeDeal.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface MakeDealProps {
1616
* Making of deal form
1717
*/
1818
export const MakeDeal = ({ offer, client }: MakeDealProps) => {
19-
const { account, publicClient, walletClient } = useWallet();
19+
const { account, walletClient } = useWallet();
2020
const [tx, setTx] = useState<string | undefined>();
2121
const [error, setError] = useState<string | undefined>();
2222
const [loading, setLoading] = useState<boolean>(false);
@@ -40,7 +40,7 @@ export const MakeDeal = ({ offer, client }: MakeDealProps) => {
4040
throw new Error('Client not ready');
4141
}
4242

43-
if (!publicClient || !walletClient) {
43+
if (!walletClient) {
4444
throw new Error('Ethereum client not ready');
4545
}
4646

@@ -57,7 +57,7 @@ export const MakeDeal = ({ offer, client }: MakeDealProps) => {
5757
setLoading(false);
5858
}
5959
},
60-
[client, offer, publicClient, walletClient],
60+
[client, offer, walletClient],
6161
);
6262

6363
if (!offer || !client) {

examples/node/index.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import 'dotenv/config';
22
import { EventHandler } from '@libp2p/interfaces/events';
33
import { DateTime } from 'luxon';
4-
import { Hash, Hex } from 'viem';
4+
import { Hash, Hex, zeroAddress } from 'viem';
5+
import { hardhat, polygonZkEvmTestnet } from 'viem/chains';
56
import { randomSalt } from '@windingtree/contracts';
67
import {
78
RequestQuery,
89
OfferOptions,
9-
chainConfig,
10+
contractsConfig,
1011
stableCoins,
1112
serverAddress,
1213
} from '../shared/index.js';
1314
import { createNode, Node, NodeOptions, Queue, createJobHandler } from '../../src/index.js';
1415
import { OfferData } from '../../src/shared/types.js';
16+
import { DealStatus } from '../../src/shared/contracts.js';
1517
import { noncePeriod } from '../../src/constants.js';
1618
import { memoryStorage } from '../../src/storage/index.js';
1719
import { nowSec, parseSeconds } from '../../src/utils/time.js';
@@ -20,6 +22,11 @@ import { createLogger } from '../../src/utils/logger.js';
2022

2123
const logger = createLogger('NodeMain');
2224

25+
/**
26+
* Chain config
27+
*/
28+
const chain = process.env.LOCAL_NODE === 'true' ? hardhat : polygonZkEvmTestnet;
29+
2330
/**
2431
* The supplier signer credentials
2532
*/
@@ -52,21 +59,35 @@ process.once('unhandledRejection', (error) => {
5259
*/
5360
interface DealHandlerOptions {
5461
node: Node<RequestQuery, OfferOptions>;
62+
[key: string]: unknown;
5563
}
5664

5765
/**
5866
* This handler looking up for a deal
5967
*/
6068
const dealHandler = createJobHandler<OfferData<RequestQuery, OfferOptions>, DealHandlerOptions>(
61-
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
62-
async ({ name, id, data: offer }, options) => {
69+
async ({ name, id, data: offer }, { node }) => {
6370
logger.trace(`Job "${name}" #${id} Checking for a deal. Offer #${offer.id}`);
64-
// const { node } = options;
6571

66-
// Makes request to the smart contract, checks for a deal
67-
// If the deal is found - check for double booking in the availability system
68-
// If double booking detected - rejects (and refunds) the deal
69-
// If not detected - claims the deal
72+
if (node) {
73+
// Check for a deal
74+
const [, , , buyer, , , status] = await node.deals.get(offer);
75+
76+
// Deal must be exists and not cancelled
77+
if (buyer !== zeroAddress && status === DealStatus.Created) {
78+
// check for double booking in the availability system
79+
// If double booking detected - rejects (and refunds) the deal
80+
81+
// If not detected - claims the deal
82+
await node.deals.claim(offer, undefined, (txHash: string, txSubj?: string) => {
83+
logger.trace(`Offer #${offer.payload.id} ${txSubj ?? 'claim'} tx hash: ${txHash}`);
84+
});
85+
86+
return true; // Returning true means that the job must be stopped
87+
}
88+
}
89+
90+
return; // Job continuing
7091
},
7192
);
7293

@@ -154,7 +175,8 @@ const main = async (): Promise<void> => {
154175

155176
const options: NodeOptions = {
156177
topics: ['hello'],
157-
chain: chainConfig,
178+
chain,
179+
contracts: contractsConfig,
158180
serverAddress,
159181
noncePeriod: Number(parseSeconds(noncePeriod)),
160182
supplierId,

examples/shared/index.ts

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Address } from 'viem';
2-
import { GenericQuery, GenericOfferOptions } from '../../src/shared/types.js';
3-
import { ProtocolChain } from '../../src/utils/contracts.js';
2+
import { GenericQuery, GenericOfferOptions, Contracts } from '../../src/shared/types.js';
43

54
export interface RequestQuery extends GenericQuery {
65
greeting: string;
@@ -21,58 +20,50 @@ export interface LocalEnv {
2120
const env =
2221
typeof window === 'undefined' ? process.env : (import.meta as unknown as { env: LocalEnv }).env;
2322

24-
export const chainConfig: ProtocolChain =
23+
export const contractsConfig: Contracts =
2524
env.LOCAL_NODE === 'hardhat' || env.VITE_LOCAL_NODE === 'hardhat'
2625
? {
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-
},
26+
config: {
27+
name: 'Config',
28+
version: '1',
29+
address: '0x3Aa5ebB10DC797CAC828524e59A333d0A371443c',
30+
},
31+
entities: {
32+
name: 'EntitiesRegistry',
33+
version: '1',
34+
address: '0x59b670e9fA9D0A427751Af201D676719a970857b',
35+
},
36+
market: {
37+
name: 'Market',
38+
version: '1',
39+
address: '0x09635F643e140090A9A8Dcd712eD6285858ceBef',
40+
},
41+
token: {
42+
name: 'LifToken',
43+
version: '1',
44+
address: '0x959922bE3CAee4b8Cd9a407cc3ac1C251C2007B1',
5045
},
5146
}
5247
: {
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-
},
48+
config: {
49+
name: 'Config',
50+
version: '1',
51+
address: '0x098b1d12cAfE7315C77b6d308A62ce02806260Ee',
52+
},
53+
entities: {
54+
name: 'EntitiesRegistry',
55+
version: '1',
56+
address: '0x4bB51528C83844b509E1152EEb05260eE1bf60e6',
57+
},
58+
market: {
59+
name: 'Market',
60+
version: '1',
61+
address: '0xDd5B6ffB3585E109ECddec5293e31cdc1e9DeD57',
62+
},
63+
token: {
64+
name: 'LifToken',
65+
version: '1',
66+
address: '0x4d60F4483BaA654CdAF1c5734D9E6B16735efCF8',
7667
},
7768
};
7869

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,27 @@
7575
"@types/node": "^18.15.11",
7676
"@types/mocha": "^10.0.1",
7777
"@types/chai": "^4.3.5",
78-
"@types/debug": "^4.1.7",
78+
"@types/debug": "^4.1.8",
7979
"@types/chai-as-promised": "^7.1.5",
8080
"@types/luxon": "^3.3.0",
8181
"mocha": "^10.2.0",
8282
"chai": "^4.3.7",
8383
"chai-as-promised": "^7.1.1",
8484
"ts-node": "^10.9.1",
85-
"typescript": "^5.0.4",
85+
"typescript": "^5.1.3",
8686
"semantic-release": "^21.0.2",
8787
"semantic-release-cli": "^5.4.4",
8888
"@semantic-release/changelog": "^6.0.3",
89-
"eslint": "^8.40.0",
90-
"@typescript-eslint/eslint-plugin": "^5.59.5",
91-
"@typescript-eslint/parser": "^5.59.5",
89+
"eslint": "^8.41.0",
90+
"@typescript-eslint/eslint-plugin": "^5.59.8",
91+
"@typescript-eslint/parser": "^5.59.8",
9292
"prettier": "^2.8.8",
9393
"husky": "^8.0.3",
9494
"git-cz": "^4.9.0",
95-
"@commitlint/config-conventional": "^17.6.3",
96-
"@commitlint/cli": "^17.6.3",
95+
"@commitlint/config-conventional": "^17.6.5",
96+
"@commitlint/cli": "^17.6.5",
9797
"lint-staged": "^13.2.2",
98-
"c8": "^7.13.0",
98+
"c8": "^7.14.0",
9999
"typedoc": "^0.24.7",
100100
"dotenv": "^16.0.3"
101101
},
@@ -106,7 +106,7 @@
106106
"@libp2p/mplex": "^7.1.3",
107107
"@libp2p/websockets": "^5.0.8",
108108
"ethers": "^6.4.0",
109-
"viem": "^0.3.41",
109+
"viem": "^0.3.49",
110110
"luxon": "^3.3.0",
111111
"h3-js": "^4.1.0",
112112
"debug": "^4.3.4",

0 commit comments

Comments
 (0)