Skip to content

Commit 4ee3fcb

Browse files
committed
update contract
1 parent 55b7532 commit 4ee3fcb

File tree

3 files changed

+57
-30
lines changed

3 files changed

+57
-30
lines changed

src/client/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import WebSocket from 'isomorphic-ws'
44
import {createPatch} from 'rfc6902'
55
import {AddOperation, RemoveOperation, ReplaceOperation} from 'rfc6902/diff'
66
import {createClient, Client as SubscriptionClient} from '../../graphql-ws/src'
7+
import type {ExecutionResult} from 'graphql'
78
import {
89
Network,
910
Environment,
@@ -65,10 +66,10 @@ export default class HgraphClient implements Client {
6566
this.subscriptions = []
6667
}
6768

68-
async query(
69+
async query<T>(
6970
flexibleRequestBody: FlexibleRequestBody,
7071
abortSignal?: AbortSignal
71-
) {
72+
): Promise<ExecutionResult<T>> {
7273
const body = formatRequestBody(flexibleRequestBody)
7374
const response = await fetch(this.endpoint, {
7475
method: 'POST',
@@ -80,7 +81,7 @@ export default class HgraphClient implements Client {
8081
if (!response.ok)
8182
throw new Error(`${response.status} - ${response.statusText}`)
8283

83-
return parse(await response.text())
84+
return parse(await response.text()) as ExecutionResult<T>
8485
}
8586

8687
removeSubscription(observable: ObservableSubscription) {

src/contract/README.md

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,86 @@
11
# HgraphContract
22

3-
HgraphContract is designed to facilitate interaction with smart contracts on the Hedera network.
3+
HgraphContract is designed to facilitate interaction with smart contracts on the
4+
Hedera network.
45

56
## Usage
67

78
### Importing the Library
89

9-
To use the `HgraphContract` class, you need to import it along with the required types from the library:
10+
To use the `HgraphContract` class, you need to import it along with the required
11+
types from the library:
1012

1113
```typescript
1214
import {
13-
default as HgraphClient,
14-
Network,
15-
Environment,
16-
HgraphContract,
17-
ContractQueryEventsParams,
18-
} from '@hgraph.io/sdk';
15+
default as HgraphClient,
16+
Network,
17+
Environment,
18+
HgraphContract,
19+
ContractQueryEventsParams,
20+
} from '@hgraph.io/sdk'
1921
```
2022

2123
### Creating a HgraphClient instance
24+
2225
```typescript
2326
const options = {
2427
network: Network.HederaTestnet,
2528
environment: Environment.Development,
26-
};
29+
}
2730

28-
const hgClient = new HgraphClient(options);
31+
const hgClient = new HgraphClient(options)
2932
```
3033

3134
### Creating a HgraphContract Instance
32-
To create a contract instance, you need to provide the hedera contractId, contract ABI, and a hgraph client instance.
33-
The hgraph client instance is responsible for communicating with the Hgraph GraphQL platform.
35+
36+
To create a contract instance, you need to provide the hedera contractId,
37+
contract ABI, and a hgraph client instance. The hgraph client instance is
38+
responsible for communicating with the Hgraph GraphQL platform.
3439

3540
```typescript
3641
const contractOptions: ContractOptions = {
3742
contractId: '0.0.1234',
38-
abi: [/* ABI array */],
43+
abi: [
44+
/* ABI array */
45+
],
3946
client: hgClient,
40-
};
47+
}
4148

42-
const hgraphContract = new HgraphContract(contractOptions);
49+
const hgraphContract = new HgraphContract(contractOptions)
4350
```
4451

4552
### Querying Events
4653

47-
You can query events from the contract using the `queryEvents` method. This method allows you to specify parameters such as limit, offset, and order.
54+
You can query events from the contract using the `queryEvents` method. This
55+
method allows you to specify parameters such as limit, offset, and order.
4856

4957
```typescript
5058
const queryParams: ContractQueryEventsParams = {
5159
limit: 10,
5260
offset: 0,
5361
order: 'desc',
54-
};
62+
}
63+
64+
const events = await hgraphContract.queryEvents(queryParams)
65+
console.log(events)
66+
```
67+
68+
To strongly type the returned logs you can directly use the schema definitions
69+
that ship with the SDK:
5570

56-
const events = await hgraphContract.queryEvents(queryParams);
57-
console.log(events);
58-
```
71+
```typescript
72+
import {ContractEventLogs} from '@hgraph.io/sdk'
73+
import type {Contract_Log} from '@hgraph.io/sdk'
74+
75+
const {data} = await hgClient.query<{
76+
logs: Pick<Contract_Log, 'data' | 'topic0' | 'topic1' | 'topic2' | 'topic3'>[]
77+
}>({
78+
query: ContractEventLogs,
79+
variables: {contract_id: 1234, limit: 10, offset: 0, order: 'desc'},
80+
})
81+
82+
data.logs.forEach((log) => {
83+
// log has type Pick<Contract_Log, 'data' | 'topic0' | 'topic1' | 'topic2' | 'topic3'>
84+
console.log(log)
85+
})
86+
```

src/contract/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import HgraphClient, {
66
ContractOptions,
77
ContractQueryEventsParams,
88
} from '../types'
9+
import type {Contract_Log} from '../types'
910

1011
export {LogDescription, InterfaceAbi, Contract as EthersContract} from 'ethers'
1112

@@ -30,13 +31,10 @@ export class HgraphContract implements Contract {
3031
const {limit = 10, offset = 0, order = 'desc'} = params
3132

3233
const {data: eventLogs, errors} = await this.client.query<{
33-
logs: {
34-
data: string | null
35-
topic0: string | null
36-
topic1: string | null
37-
topic2: string | null
38-
topic3: string | null
39-
}[]
34+
logs: Pick<
35+
Contract_Log,
36+
'data' | 'topic0' | 'topic1' | 'topic2' | 'topic3'
37+
>[]
4038
}>({
4139
query: ContractEventLogs,
4240
variables: {

0 commit comments

Comments
 (0)