|
1 | 1 | # HgraphContract |
2 | 2 |
|
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. |
4 | 5 |
|
5 | 6 | ## Usage |
6 | 7 |
|
7 | 8 | ### Importing the Library |
8 | 9 |
|
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: |
10 | 12 |
|
11 | 13 | ```typescript |
12 | 14 | 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' |
19 | 21 | ``` |
20 | 22 |
|
21 | 23 | ### Creating a HgraphClient instance |
| 24 | + |
22 | 25 | ```typescript |
23 | 26 | const options = { |
24 | 27 | network: Network.HederaTestnet, |
25 | 28 | environment: Environment.Development, |
26 | | -}; |
| 29 | +} |
27 | 30 |
|
28 | | -const hgClient = new HgraphClient(options); |
| 31 | +const hgClient = new HgraphClient(options) |
29 | 32 | ``` |
30 | 33 |
|
31 | 34 | ### 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. |
34 | 39 |
|
35 | 40 | ```typescript |
36 | 41 | const contractOptions: ContractOptions = { |
37 | 42 | contractId: '0.0.1234', |
38 | | - abi: [/* ABI array */], |
| 43 | + abi: [ |
| 44 | + /* ABI array */ |
| 45 | + ], |
39 | 46 | client: hgClient, |
40 | | -}; |
| 47 | +} |
41 | 48 |
|
42 | | -const hgraphContract = new HgraphContract(contractOptions); |
| 49 | +const hgraphContract = new HgraphContract(contractOptions) |
43 | 50 | ``` |
44 | 51 |
|
45 | 52 | ### Querying Events |
46 | 53 |
|
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. |
48 | 56 |
|
49 | 57 | ```typescript |
50 | 58 | const queryParams: ContractQueryEventsParams = { |
51 | 59 | limit: 10, |
52 | 60 | offset: 0, |
53 | 61 | 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: |
55 | 70 |
|
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 | +``` |
0 commit comments