@ocap/client 1.25.3 → 1.25.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -80
- package/dist/report.html +1 -1
- package/docs/_sidebar.md +22 -0
- package/docs/api-reference-client-methods.ja.md +229 -0
- package/docs/api-reference-client-methods.md +229 -0
- package/docs/api-reference-client-methods.zh-TW.md +229 -0
- package/docs/api-reference-client-methods.zh.md +229 -0
- package/docs/api-reference-data-types.ja.md +482 -0
- package/docs/api-reference-data-types.md +482 -0
- package/docs/api-reference-data-types.zh-TW.md +482 -0
- package/docs/api-reference-data-types.zh.md +482 -0
- package/docs/api-reference-low-level-api.ja.md +228 -0
- package/docs/api-reference-low-level-api.md +228 -0
- package/docs/api-reference-low-level-api.zh-TW.md +228 -0
- package/docs/api-reference-low-level-api.zh.md +228 -0
- package/docs/api-reference-query-mutation-methods.ja.md +814 -0
- package/docs/api-reference-query-mutation-methods.md +814 -0
- package/docs/api-reference-query-mutation-methods.zh-TW.md +814 -0
- package/docs/api-reference-query-mutation-methods.zh.md +814 -0
- package/docs/api-reference-transaction-helpers.ja.md +649 -0
- package/docs/api-reference-transaction-helpers.md +649 -0
- package/docs/api-reference-transaction-helpers.zh-TW.md +649 -0
- package/docs/api-reference-transaction-helpers.zh.md +649 -0
- package/docs/api-reference.ja.md +23 -0
- package/docs/api-reference.md +23 -0
- package/docs/api-reference.zh-TW.md +23 -0
- package/docs/api-reference.zh.md +23 -0
- package/docs/core-concepts-client-architecture.ja.md +102 -0
- package/docs/core-concepts-client-architecture.md +102 -0
- package/docs/core-concepts-client-architecture.zh-TW.md +102 -0
- package/docs/core-concepts-client-architecture.zh.md +102 -0
- package/docs/core-concepts-event-subscriptions.ja.md +123 -0
- package/docs/core-concepts-event-subscriptions.md +123 -0
- package/docs/core-concepts-event-subscriptions.zh-TW.md +123 -0
- package/docs/core-concepts-event-subscriptions.zh.md +123 -0
- package/docs/core-concepts-gas-payment.ja.md +111 -0
- package/docs/core-concepts-gas-payment.md +111 -0
- package/docs/core-concepts-gas-payment.zh-TW.md +111 -0
- package/docs/core-concepts-gas-payment.zh.md +111 -0
- package/docs/core-concepts-transaction-lifecycle.ja.md +183 -0
- package/docs/core-concepts-transaction-lifecycle.md +183 -0
- package/docs/core-concepts-transaction-lifecycle.zh-TW.md +183 -0
- package/docs/core-concepts-transaction-lifecycle.zh.md +183 -0
- package/docs/core-concepts.ja.md +22 -0
- package/docs/core-concepts.md +22 -0
- package/docs/core-concepts.zh-TW.md +22 -0
- package/docs/core-concepts.zh.md +22 -0
- package/docs/getting-started-basic-usage.ja.md +87 -0
- package/docs/getting-started-basic-usage.md +87 -0
- package/docs/getting-started-basic-usage.zh-TW.md +87 -0
- package/docs/getting-started-basic-usage.zh.md +87 -0
- package/docs/getting-started-installation.ja.md +60 -0
- package/docs/getting-started-installation.md +60 -0
- package/docs/getting-started-installation.zh-TW.md +60 -0
- package/docs/getting-started-installation.zh.md +60 -0
- package/docs/getting-started.ja.md +16 -0
- package/docs/getting-started.md +16 -0
- package/docs/getting-started.zh-TW.md +16 -0
- package/docs/getting-started.zh.md +17 -0
- package/docs/how-to-guides-delegate-permissions.ja.md +167 -0
- package/docs/how-to-guides-delegate-permissions.md +167 -0
- package/docs/how-to-guides-delegate-permissions.zh-TW.md +167 -0
- package/docs/how-to-guides-delegate-permissions.zh.md +166 -0
- package/docs/how-to-guides-manage-accounts.ja.md +73 -0
- package/docs/how-to-guides-manage-accounts.md +73 -0
- package/docs/how-to-guides-manage-accounts.zh-TW.md +73 -0
- package/docs/how-to-guides-manage-accounts.zh.md +73 -0
- package/docs/how-to-guides-manage-assets.ja.md +255 -0
- package/docs/how-to-guides-manage-assets.md +255 -0
- package/docs/how-to-guides-manage-assets.zh-TW.md +255 -0
- package/docs/how-to-guides-manage-assets.zh.md +255 -0
- package/docs/how-to-guides-manage-tokens.ja.md +179 -0
- package/docs/how-to-guides-manage-tokens.md +179 -0
- package/docs/how-to-guides-manage-tokens.zh-TW.md +179 -0
- package/docs/how-to-guides-manage-tokens.zh.md +179 -0
- package/docs/how-to-guides-stake-tokens-and-assets.ja.md +205 -0
- package/docs/how-to-guides-stake-tokens-and-assets.md +205 -0
- package/docs/how-to-guides-stake-tokens-and-assets.zh-TW.md +205 -0
- package/docs/how-to-guides-stake-tokens-and-assets.zh.md +205 -0
- package/docs/how-to-guides-transfer-tokens-and-nfts.ja.md +179 -0
- package/docs/how-to-guides-transfer-tokens-and-nfts.md +179 -0
- package/docs/how-to-guides-transfer-tokens-and-nfts.zh-TW.md +179 -0
- package/docs/how-to-guides-transfer-tokens-and-nfts.zh.md +179 -0
- package/docs/how-to-guides.ja.md +27 -0
- package/docs/how-to-guides.md +27 -0
- package/docs/how-to-guides.zh-TW.md +27 -0
- package/docs/how-to-guides.zh.md +27 -0
- package/docs/overview.ja.md +70 -0
- package/docs/overview.md +70 -0
- package/docs/overview.zh-TW.md +70 -0
- package/docs/overview.zh.md +70 -0
- package/package.json +14 -14
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# Helper Methods
|
|
2
|
+
|
|
3
|
+
The `GraphQLClient` class is the primary interface for interacting with an OCAP-powered blockchain. It provides a comprehensive set of methods for querying the chain state, sending transactions, and subscribing to real-time events. It is designed to work seamlessly in both Node.js and browser environments.
|
|
4
|
+
|
|
5
|
+
```javascript Client Initialization icon=logos:javascript
|
|
6
|
+
const GraphQLClient = require('@ocap/client');
|
|
7
|
+
|
|
8
|
+
// Connect to the Beta chain
|
|
9
|
+
const client = new GraphQLClient('https://beta.abtnetwork.io/api');
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
const res = await client.getChainInfo();
|
|
13
|
+
console.log('Connected to chain:', res.info.network);
|
|
14
|
+
})();
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
This section provides a detailed reference for the core helper methods of the `GraphQLClient` class.
|
|
18
|
+
|
|
19
|
+
## Constructor
|
|
20
|
+
|
|
21
|
+
### new GraphQLClient(endpoint, autoInit)
|
|
22
|
+
|
|
23
|
+
Creates a new instance of the `GraphQLClient`.
|
|
24
|
+
|
|
25
|
+
**Parameters**
|
|
26
|
+
|
|
27
|
+
<x-field-group>
|
|
28
|
+
<x-field data-name="endpoint" data-type="string" data-required="true">
|
|
29
|
+
<x-field-desc markdown>The absolute URL of the GraphQL endpoint for the blockchain node (e.g., `https://beta.abtnetwork.io/api`).</x-field-desc>
|
|
30
|
+
</x-field>
|
|
31
|
+
<x-field data-name="autoInit" data-type="boolean" data-default="true" data-required="false">
|
|
32
|
+
<x-field-desc markdown>If `true`, the client will automatically fetch and cache essential chain information (the "context") upon initialization. This is recommended for most use cases.</x-field-desc>
|
|
33
|
+
</x-field>
|
|
34
|
+
</x-field-group>
|
|
35
|
+
|
|
36
|
+
**Example**
|
|
37
|
+
|
|
38
|
+
```javascript Creating a Client Instance icon=logos:javascript
|
|
39
|
+
const client = new GraphQLClient('https://beta.abtnetwork.io/api', true);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Core Methods
|
|
45
|
+
|
|
46
|
+
These methods provide fundamental functionality for interacting with the client and the chain.
|
|
47
|
+
|
|
48
|
+
### getContext()
|
|
49
|
+
|
|
50
|
+
Fetches and caches essential chain information, such as the chain ID, native token details, and transaction fee configurations. This method is called automatically if `autoInit` is enabled in the constructor. Subsequent calls will return the cached context.
|
|
51
|
+
|
|
52
|
+
**Returns**
|
|
53
|
+
|
|
54
|
+
<x-field data-name="Promise<object>" data-type="object" data-desc="A promise that resolves to the context object containing chain metadata.">
|
|
55
|
+
<x-field data-name="chainId" data-type="string" data-desc="The unique identifier of the blockchain network."></x-field>
|
|
56
|
+
<x-field data-name="consensus" data-type="string" data-desc="The consensus engine version."></x-field>
|
|
57
|
+
<x-field data-name="token" data-type="object" data-desc="Information about the native token.">
|
|
58
|
+
<x-field data-name="address" data-type="string" data-desc="The address of the native token contract."></x-field>
|
|
59
|
+
<x-field data-name="decimal" data-type="number" data-desc="The number of decimal places for the native token."></x-field>
|
|
60
|
+
<x-field data-name="symbol" data-type="string" data-desc="The symbol of the native token (e.g., TBA)."></x-field>
|
|
61
|
+
</x-field>
|
|
62
|
+
<x-field data-name="txConfig" data-type="object" data-desc="Transaction fee and gas configuration."></x-field>
|
|
63
|
+
</x-field>
|
|
64
|
+
|
|
65
|
+
**Example**
|
|
66
|
+
|
|
67
|
+
```javascript Fetching Chain Context icon=logos:javascript
|
|
68
|
+
async function logChainToken() {
|
|
69
|
+
const context = await client.getContext();
|
|
70
|
+
console.log(`Native Token Symbol: ${context.token.symbol}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
logChainToken();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### setGasPayer(wallet)
|
|
77
|
+
|
|
78
|
+
Configures a wallet to act as a "gas payer." When set, this wallet will sponsor the transaction fees for transactions sent through this client instance, enabling gasless experiences for users. For more details, see the [Gas Payment](./core-concepts-gas-payment.md) concept guide.
|
|
79
|
+
|
|
80
|
+
**Parameters**
|
|
81
|
+
|
|
82
|
+
<x-field data-name="wallet" data-type="WalletObject" data-required="true">
|
|
83
|
+
<x-field-desc markdown>A wallet object with `address`, `publicKey`, and `secretKey` properties, equipped with a `sign` method.</x-field-desc>
|
|
84
|
+
</x-field>
|
|
85
|
+
|
|
86
|
+
### decodeTx(input)
|
|
87
|
+
|
|
88
|
+
Deserializes a transaction from various formats into a human-readable JavaScript object.
|
|
89
|
+
|
|
90
|
+
**Parameters**
|
|
91
|
+
|
|
92
|
+
<x-field data-name="input" data-type="Buffer | string" data-required="true">
|
|
93
|
+
<x-field-desc markdown>The transaction data to decode. Can be a `Buffer` or a string in `hex`, `base58`, or `base64` format.</x-field-desc>
|
|
94
|
+
</x-field>
|
|
95
|
+
|
|
96
|
+
**Returns**
|
|
97
|
+
|
|
98
|
+
<x-field data-name="object" data-type="object" data-desc="The decoded transaction object."></x-field>
|
|
99
|
+
|
|
100
|
+
### getType(name)
|
|
101
|
+
|
|
102
|
+
Retrieves the Protobuf message class for a given type name. This is useful for advanced scenarios where you need to manually construct or inspect Protobuf messages.
|
|
103
|
+
|
|
104
|
+
**Parameters**
|
|
105
|
+
|
|
106
|
+
<x-field data-name="name" data-type="string" data-required="true" data-desc="The name of the Protobuf message type (e.g., 'Transaction', 'TransferTx')."></x-field>
|
|
107
|
+
|
|
108
|
+
**Returns**
|
|
109
|
+
|
|
110
|
+
<x-field data-name="class | null" data-type="object" data-desc="The message class constructor, or null if not found."></x-field>
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## Event Subscription
|
|
115
|
+
|
|
116
|
+
The client supports real-time event subscriptions over WebSockets, allowing your application to react instantly to on-chain events.
|
|
117
|
+
|
|
118
|
+
### subscribe(topic, callback)
|
|
119
|
+
|
|
120
|
+
Establishes a WebSocket connection and subscribes to a specific event topic.
|
|
121
|
+
|
|
122
|
+
**Parameters**
|
|
123
|
+
|
|
124
|
+
<x-field-group>
|
|
125
|
+
<x-field data-name="topic" data-type="string" data-required="true" data-desc="The event topic to subscribe to (e.g., 'newBlock', 'tx:transfer')."></x-field>
|
|
126
|
+
<x-field data-name="callback" data-type="function" data-required="true" data-desc="A function to execute when an event is received. It receives the event payload as its only argument."></x-field>
|
|
127
|
+
</x-field-group>
|
|
128
|
+
|
|
129
|
+
### unsubscribe(topic, callback)
|
|
130
|
+
|
|
131
|
+
Removes a previously registered callback for a specific topic.
|
|
132
|
+
|
|
133
|
+
**Parameters**
|
|
134
|
+
|
|
135
|
+
<x-field-group>
|
|
136
|
+
<x-field data-name="topic" data-type="string" data-required="true" data-desc="The event topic to unsubscribe from."></x-field>
|
|
137
|
+
<x-field data-name="callback" data-type="function" data-required="true" data-desc="The specific callback function to remove."></x-field>
|
|
138
|
+
</x-field-group>
|
|
139
|
+
|
|
140
|
+
**Example**
|
|
141
|
+
|
|
142
|
+
```javascript Subscribing to New Blocks icon=logos:javascript
|
|
143
|
+
const handleNewBlock = (block) => {
|
|
144
|
+
console.log(`New block received! Height: ${block.height}`);
|
|
145
|
+
|
|
146
|
+
// Unsubscribe after receiving one block
|
|
147
|
+
client.unsubscribe('newBlock', handleNewBlock);
|
|
148
|
+
console.log('Unsubscribed from newBlock events.');
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
client.subscribe('newBlock', handleNewBlock);
|
|
152
|
+
console.log('Subscribed to newBlock events...');
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## Token Utility Methods
|
|
158
|
+
|
|
159
|
+
These helpers simplify conversions between the human-readable token amount and the on-chain base unit representation.
|
|
160
|
+
|
|
161
|
+
### fromUnitToToken(value)
|
|
162
|
+
|
|
163
|
+
Converts a value from the chain's base unit (a large integer string) to a standard decimal string, based on the native token's decimal places.
|
|
164
|
+
|
|
165
|
+
**Parameters**
|
|
166
|
+
|
|
167
|
+
<x-field data-name="value" data-type="string" data-required="true" data-desc="The amount in the chain's base unit."></x-field>
|
|
168
|
+
|
|
169
|
+
**Returns**
|
|
170
|
+
|
|
171
|
+
<x-field data-name="string" data-type="string" data-desc="The amount in the standard token unit."></x-field>
|
|
172
|
+
|
|
173
|
+
### fromTokenToUnit(amount)
|
|
174
|
+
|
|
175
|
+
Converts a standard decimal amount into the chain's base unit representation (a BN.js instance).
|
|
176
|
+
|
|
177
|
+
**Parameters**
|
|
178
|
+
|
|
179
|
+
<x-field data-name="amount" data-type="number | string" data-required="true" data-desc="The token amount in its standard decimal form."></x-field>
|
|
180
|
+
|
|
181
|
+
**Returns**
|
|
182
|
+
|
|
183
|
+
<x-field data-name="BN" data-type="object" data-desc="A BN.js instance representing the value in the chain's base unit."></x-field>
|
|
184
|
+
|
|
185
|
+
**Example**
|
|
186
|
+
|
|
187
|
+
```javascript Token Amount Conversion icon=logos:javascript
|
|
188
|
+
async function convertToken() {
|
|
189
|
+
// Convert 100 TBA to its base unit
|
|
190
|
+
const unitAmount = await client.fromTokenToUnit(100);
|
|
191
|
+
console.log(`100 TBA is ${unitAmount.toString()} in base units.`);
|
|
192
|
+
|
|
193
|
+
// Convert it back
|
|
194
|
+
const tokenAmount = await client.fromUnitToToken(unitAmount.toString());
|
|
195
|
+
console.log(`${unitAmount.toString()} base units is ${tokenAmount} TBA.`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
convertToken();
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## Method Discovery
|
|
204
|
+
|
|
205
|
+
The client dynamically generates methods for every transaction type supported by the connected OCAP node. These discovery methods allow you to programmatically list all available transaction-related functions.
|
|
206
|
+
|
|
207
|
+
### getTxSendMethods()
|
|
208
|
+
|
|
209
|
+
Returns an array of all available `send...Tx` method names. These methods handle the full lifecycle of signing and sending a transaction.
|
|
210
|
+
|
|
211
|
+
### getTxEncodeMethods()
|
|
212
|
+
|
|
213
|
+
Returns an array of all available `encode...Tx` method names. These methods prepare and serialize a transaction into a buffer, but do not sign it.
|
|
214
|
+
|
|
215
|
+
### getTxSignMethods()
|
|
216
|
+
|
|
217
|
+
Returns an array of all available `sign...Tx` method names. These methods encode and then sign a transaction, returning the signed transaction object.
|
|
218
|
+
|
|
219
|
+
### getTxMultiSignMethods()
|
|
220
|
+
|
|
221
|
+
Returns an array of all available `multiSign...Tx` method names, used for multi-signature workflows.
|
|
222
|
+
|
|
223
|
+
**Example**
|
|
224
|
+
|
|
225
|
+
```javascript Listing Available Transaction Methods icon=logos:javascript
|
|
226
|
+
const sendMethods = client.getTxSendMethods();
|
|
227
|
+
console.log('Available send methods:', sendMethods);
|
|
228
|
+
// Example output: [ 'sendPokeTx', 'sendTransferTx', ... ]
|
|
229
|
+
```
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# 輔助方法
|
|
2
|
+
|
|
3
|
+
`GraphQLClient` 類別是與 OCAP 驅動的區塊鏈互動的主要介面。它提供了一套全面的方法,用於查詢鏈狀態、發送交易以及訂閱即時事件。它被設計為在 Node.js 和瀏覽器環境中都能無縫運作。
|
|
4
|
+
|
|
5
|
+
```javascript Client Initialization icon=logos:javascript
|
|
6
|
+
const GraphQLClient = require('@ocap/client');
|
|
7
|
+
|
|
8
|
+
// Connect to the Beta chain
|
|
9
|
+
const client = new GraphQLClient('https://beta.abtnetwork.io/api');
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
const res = await client.getChainInfo();
|
|
13
|
+
console.log('Connected to chain:', res.info.network);
|
|
14
|
+
})();
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
本節為 `GraphQLClient` 類別的核心輔助方法提供了詳細的參考。
|
|
18
|
+
|
|
19
|
+
## 建構函式
|
|
20
|
+
|
|
21
|
+
### new GraphQLClient(endpoint, autoInit)
|
|
22
|
+
|
|
23
|
+
建立一個新的 `GraphQLClient` 實例。
|
|
24
|
+
|
|
25
|
+
**參數**
|
|
26
|
+
|
|
27
|
+
<x-field-group>
|
|
28
|
+
<x-field data-name="endpoint" data-type="string" data-required="true">
|
|
29
|
+
<x-field-desc markdown>區塊鏈節點的 GraphQL 端點的絕對 URL(例如:`https://beta.abtnetwork.io/api`)。</x-field-desc>
|
|
30
|
+
</x-field>
|
|
31
|
+
<x-field data-name="autoInit" data-type="boolean" data-default="true" data-required="false">
|
|
32
|
+
<x-field-desc markdown>若為 `true`,客戶端將在初始化時自動獲取並快取必要的鏈資訊(「上下文」)。建議在大多數使用場景下使用。</x-field-desc>
|
|
33
|
+
</x-field>
|
|
34
|
+
</x-field-group>
|
|
35
|
+
|
|
36
|
+
**範例**
|
|
37
|
+
|
|
38
|
+
```javascript Creating a Client Instance icon=logos:javascript
|
|
39
|
+
const client = new GraphQLClient('https://beta.abtnetwork.io/api', true);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## 核心方法
|
|
45
|
+
|
|
46
|
+
這些方法提供了與客戶端和鏈互動的基本功能。
|
|
47
|
+
|
|
48
|
+
### getContext()
|
|
49
|
+
|
|
50
|
+
獲取並快取必要的鏈資訊,例如鏈 ID、原生代幣詳細資訊和交易費用配置。如果在建構函式中啟用了 `autoInit`,則會自動呼叫此方法。後續的呼叫將返回快取的上下文。
|
|
51
|
+
|
|
52
|
+
**傳回值**
|
|
53
|
+
|
|
54
|
+
<x-field data-name="Promise<object>" data-type="object" data-desc="一個解析為包含鏈元資料的上下文物件的 Promise。">
|
|
55
|
+
<x-field data-name="chainId" data-type="string" data-desc="區塊鏈網路的唯一識別碼。"></x-field>
|
|
56
|
+
<x-field data-name="consensus" data-type="string" data-desc="共識引擎版本。"></x-field>
|
|
57
|
+
<x-field data-name="token" data-type="object" data-desc="有關原生代幣的資訊。">
|
|
58
|
+
<x-field data-name="address" data-type="string" data-desc="原生代幣合約的地址。"></x-field>
|
|
59
|
+
<x-field data-name="decimal" data-type="number" data-desc="原生代幣的小數位數。"></x-field>
|
|
60
|
+
<x-field data-name="symbol" data-type="string" data-desc="原生代幣的符號(例如:TBA)。"></x-field>
|
|
61
|
+
</x-field>
|
|
62
|
+
<x-field data-name="txConfig" data-type="object" data-desc="交易費用和 gas 配置。"></x-field>
|
|
63
|
+
</x-field>
|
|
64
|
+
|
|
65
|
+
**範例**
|
|
66
|
+
|
|
67
|
+
```javascript Fetching Chain Context icon=logos:javascript
|
|
68
|
+
async function logChainToken() {
|
|
69
|
+
const context = await client.getContext();
|
|
70
|
+
console.log(`Native Token Symbol: ${context.token.symbol}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
logChainToken();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### setGasPayer(wallet)
|
|
77
|
+
|
|
78
|
+
設定一個錢包作為「gas 支付者」。設定後,此錢包將為透過此客戶端實例發送的交易支付交易費用,為使用者提供無 gas 體驗。更多詳細資訊,請參閱 [Gas 支付](./core-concepts-gas-payment.md) 概念指南。
|
|
79
|
+
|
|
80
|
+
**參數**
|
|
81
|
+
|
|
82
|
+
<x-field data-name="wallet" data-type="WalletObject" data-required="true">
|
|
83
|
+
<x-field-desc markdown>一個錢包物件,具有 `address`、`publicKey` 和 `secretKey` 屬性,並配備一個 `sign` 方法。</x-field-desc>
|
|
84
|
+
</x-field>
|
|
85
|
+
|
|
86
|
+
### decodeTx(input)
|
|
87
|
+
|
|
88
|
+
將交易從各種格式反序列化為人類可讀的 JavaScript 物件。
|
|
89
|
+
|
|
90
|
+
**參數**
|
|
91
|
+
|
|
92
|
+
<x-field data-name="input" data-type="Buffer | string" data-required="true">
|
|
93
|
+
<x-field-desc markdown>要解碼的交易資料。可以是 `Buffer` 或 `hex`、`base58` 或 `base64` 格式的字串。</x-field-desc>
|
|
94
|
+
</x-field>
|
|
95
|
+
|
|
96
|
+
**傳回值**
|
|
97
|
+
|
|
98
|
+
<x-field data-name="object" data-type="object" data-desc="已解碼的交易物件。"></x-field>
|
|
99
|
+
|
|
100
|
+
### getType(name)
|
|
101
|
+
|
|
102
|
+
根據給定的類型名稱檢索 Protobuf 訊息類別。這對於需要手動建構或檢查 Protobuf 訊息的進階場景非常有用。
|
|
103
|
+
|
|
104
|
+
**參數**
|
|
105
|
+
|
|
106
|
+
<x-field data-name="name" data-type="string" data-required="true" data-desc="Protobuf 訊息類型的名稱(例如:'Transaction', 'TransferTx')。"></x-field>
|
|
107
|
+
|
|
108
|
+
**傳回值**
|
|
109
|
+
|
|
110
|
+
<x-field data-name="class | null" data-type="object" data-desc="訊息類別的建構函式,如果找不到則為 null。"></x-field>
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## 事件訂閱
|
|
115
|
+
|
|
116
|
+
客戶端支援透過 WebSocket 進行即時事件訂閱,讓您的應用程式能夠即時回應鏈上事件。
|
|
117
|
+
|
|
118
|
+
### subscribe(topic, callback)
|
|
119
|
+
|
|
120
|
+
建立一個 WebSocket 連接並訂閱特定的事件主題。
|
|
121
|
+
|
|
122
|
+
**參數**
|
|
123
|
+
|
|
124
|
+
<x-field-group>
|
|
125
|
+
<x-field data-name="topic" data-type="string" data-required="true" data-desc="要訂閱的事件主題(例如:'newBlock', 'tx:transfer')。"></x-field>
|
|
126
|
+
<x-field data-name="callback" data-type="function" data-required="true" data-desc="接收到事件時要執行的函式。它會接收事件的有效負載作為其唯一參數。"></x-field>
|
|
127
|
+
</x-field-group>
|
|
128
|
+
|
|
129
|
+
### unsubscribe(topic, callback)
|
|
130
|
+
|
|
131
|
+
移除先前為特定主題註冊的回呼函式。
|
|
132
|
+
|
|
133
|
+
**參數**
|
|
134
|
+
|
|
135
|
+
<x-field-group>
|
|
136
|
+
<x-field data-name="topic" data-type="string" data-required="true" data-desc="要取消訂閱的事件主題。"></x-field>
|
|
137
|
+
<x-field data-name="callback" data-type="function" data-required="true" data-desc="要移除的特定回呼函式。"></x-field>
|
|
138
|
+
</x-field-group>
|
|
139
|
+
|
|
140
|
+
**範例**
|
|
141
|
+
|
|
142
|
+
```javascript Subscribing to New Blocks icon=logos:javascript
|
|
143
|
+
const handleNewBlock = (block) => {
|
|
144
|
+
console.log(`New block received! Height: ${block.height}`);
|
|
145
|
+
|
|
146
|
+
// Unsubscribe after receiving one block
|
|
147
|
+
client.unsubscribe('newBlock', handleNewBlock);
|
|
148
|
+
console.log('Unsubscribed from newBlock events.');
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
client.subscribe('newBlock', handleNewBlock);
|
|
152
|
+
console.log('Subscribed to newBlock events...');
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## 代幣工具方法
|
|
158
|
+
|
|
159
|
+
這些輔助方法簡化了人類可讀的代幣數量與鏈上基本單位表示之間的轉換。
|
|
160
|
+
|
|
161
|
+
### fromUnitToToken(value)
|
|
162
|
+
|
|
163
|
+
根據原生代幣的小數位數,將一個值從鏈的基本單位(一個大整數字串)轉換為標準的十進位字串。
|
|
164
|
+
|
|
165
|
+
**參數**
|
|
166
|
+
|
|
167
|
+
<x-field data-name="value" data-type="string" data-required="true" data-desc="以鏈的基本單位表示的數量。"></x-field>
|
|
168
|
+
|
|
169
|
+
**傳回值**
|
|
170
|
+
|
|
171
|
+
<x-field data-name="string" data-type="string" data-desc="以標準代幣單位表示的數量。"></x-field>
|
|
172
|
+
|
|
173
|
+
### fromTokenToUnit(amount)
|
|
174
|
+
|
|
175
|
+
將一個標準的十進位數量轉換為鏈的基本單位表示(一個 BN.js 實例)。
|
|
176
|
+
|
|
177
|
+
**參數**
|
|
178
|
+
|
|
179
|
+
<x-field data-name="amount" data-type="number | string" data-required="true" data-desc="以其標準十進位形式表示的代幣數量。"></x-field>
|
|
180
|
+
|
|
181
|
+
**傳回值**
|
|
182
|
+
|
|
183
|
+
<x-field data-name="BN" data-type="object" data-desc="一個代表鏈基本單位中數值的 BN.js 實例。"></x-field>
|
|
184
|
+
|
|
185
|
+
**範例**
|
|
186
|
+
|
|
187
|
+
```javascript Token Amount Conversion icon=logos:javascript
|
|
188
|
+
async function convertToken() {
|
|
189
|
+
// Convert 100 TBA to its base unit
|
|
190
|
+
const unitAmount = await client.fromTokenToUnit(100);
|
|
191
|
+
console.log(`100 TBA is ${unitAmount.toString()} in base units.`);
|
|
192
|
+
|
|
193
|
+
// Convert it back
|
|
194
|
+
const tokenAmount = await client.fromUnitToToken(unitAmount.toString());
|
|
195
|
+
console.log(`${unitAmount.toString()} base units is ${tokenAmount} TBA.`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
convertToken();
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## 方法探索
|
|
204
|
+
|
|
205
|
+
客戶端會為所連接的 OCAP 節點支援的每種交易類型動態產生方法。這些探索方法讓您能夠以程式化的方式列出所有可用的交易相關函式。
|
|
206
|
+
|
|
207
|
+
### getTxSendMethods()
|
|
208
|
+
|
|
209
|
+
傳回所有可用的 `send...Tx` 方法名稱陣列。這些方法處理簽署和發送交易的完整生命週期。
|
|
210
|
+
|
|
211
|
+
### getTxEncodeMethods()
|
|
212
|
+
|
|
213
|
+
傳回所有可用的 `encode...Tx` 方法名稱陣列。這些方法準備並將交易序列化為緩衝區,但不會簽署它。
|
|
214
|
+
|
|
215
|
+
### getTxSignMethods()
|
|
216
|
+
|
|
217
|
+
傳回所有可用的 `sign...Tx` 方法名稱陣列。這些方法會編碼然後簽署交易,並傳回已簽署的交易物件。
|
|
218
|
+
|
|
219
|
+
### getTxMultiSignMethods()
|
|
220
|
+
|
|
221
|
+
傳回所有可用的 `multiSign...Tx` 方法名稱陣列,用於多重簽名工作流程。
|
|
222
|
+
|
|
223
|
+
**範例**
|
|
224
|
+
|
|
225
|
+
```javascript Listing Available Transaction Methods icon=logos:javascript
|
|
226
|
+
const sendMethods = client.getTxSendMethods();
|
|
227
|
+
console.log('Available send methods:', sendMethods);
|
|
228
|
+
// 範例輸出:[ 'sendPokeTx', 'sendTransferTx', ... ]
|
|
229
|
+
```
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# 辅助方法
|
|
2
|
+
|
|
3
|
+
`GraphQLClient` 类是与由 OCAP 驱动的区块链进行交互的主要接口。它提供了一套全面的方法,用于查询链状态、发送交易以及订阅实时事件。它被设计为可以在 Node.js 和浏览器环境中无缝工作。
|
|
4
|
+
|
|
5
|
+
```javascript Client Initialization icon=logos:javascript
|
|
6
|
+
const GraphQLClient = require('@ocap/client');
|
|
7
|
+
|
|
8
|
+
// 连接到 Beta 链
|
|
9
|
+
const client = new GraphQLClient('https://beta.abtnetwork.io/api');
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
const res = await client.getChainInfo();
|
|
13
|
+
console.log('已连接到链:', res.info.network);
|
|
14
|
+
})();
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
本节提供了 `GraphQLClient` 类的核心辅助方法的详细参考。
|
|
18
|
+
|
|
19
|
+
## 构造函数
|
|
20
|
+
|
|
21
|
+
### new GraphQLClient(endpoint, autoInit)
|
|
22
|
+
|
|
23
|
+
创建一个新的 `GraphQLClient` 实例。
|
|
24
|
+
|
|
25
|
+
**参数**
|
|
26
|
+
|
|
27
|
+
<x-field-group>
|
|
28
|
+
<x-field data-name="endpoint" data-type="string" data-required="true">
|
|
29
|
+
<x-field-desc markdown>区块链节点的 GraphQL 端点的绝对 URL(例如,`https://beta.abtnetwork.io/api`)。</x-field-desc>
|
|
30
|
+
</x-field>
|
|
31
|
+
<x-field data-name="autoInit" data-type="boolean" data-default="true" data-required="false">
|
|
32
|
+
<x-field-desc markdown>如果为 `true`,客户端将在初始化时自动获取并缓存关键的链信息(即“上下文”)。对于大多数用例,推荐使用此设置。</x-field-desc>
|
|
33
|
+
</x-field>
|
|
34
|
+
</x-field-group>
|
|
35
|
+
|
|
36
|
+
**示例**
|
|
37
|
+
|
|
38
|
+
```javascript Creating a Client Instance icon=logos:javascript
|
|
39
|
+
const client = new GraphQLClient('https://beta.abtnetwork.io/api', true);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## 核心方法
|
|
45
|
+
|
|
46
|
+
这些方法提供了与客户端和链进行交互的基础功能。
|
|
47
|
+
|
|
48
|
+
### getContext()
|
|
49
|
+
|
|
50
|
+
获取并缓存关键的链信息,例如链 ID、原生代币详情和交易费配置。如果在构造函数中启用了 `autoInit`,此方法会自动调用。后续调用将返回缓存的上下文。
|
|
51
|
+
|
|
52
|
+
**返回**
|
|
53
|
+
|
|
54
|
+
<x-field data-name="Promise<object>" data-type="object" data-desc="一个解析为包含链元数据的上下文对象的 Promise。">
|
|
55
|
+
<x-field data-name="chainId" data-type="string" data-desc="区块链网络的唯一标识符。"></x-field>
|
|
56
|
+
<x-field data-name="consensus" data-type="string" data-desc="共识引擎版本。"></x-field>
|
|
57
|
+
<x-field data-name="token" data-type="object" data-desc="关于原生代币的信息。">
|
|
58
|
+
<x-field data-name="address" data-type="string" data-desc="原生代币合约的地址。"></x-field>
|
|
59
|
+
<x-field data-name="decimal" data-type="number" data-desc="原生代币的小数位数。"></x-field>
|
|
60
|
+
<x-field data-name="symbol" data-type="string" data-desc="原生代币的符号(例如,TBA)。"></x-field>
|
|
61
|
+
</x-field>
|
|
62
|
+
<x-field data-name="txConfig" data-type="object" data-desc="交易费和 gas 配置。"></x-field>
|
|
63
|
+
</x-field>
|
|
64
|
+
|
|
65
|
+
**示例**
|
|
66
|
+
|
|
67
|
+
```javascript Fetching Chain Context icon=logos:javascript
|
|
68
|
+
async function logChainToken() {
|
|
69
|
+
const context = await client.getContext();
|
|
70
|
+
console.log(`原生代币符号: ${context.token.symbol}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
logChainToken();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### setGasPayer(wallet)
|
|
77
|
+
|
|
78
|
+
配置一个钱包作为“gas 支付方”。设置后,该钱包将为通过此客户端实例发送的交易赞助交易费,从而为用户实现无 gas 体验。更多详情,请参阅 [Gas 支付](./core-concepts-gas-payment.md) 概念指南。
|
|
79
|
+
|
|
80
|
+
**参数**
|
|
81
|
+
|
|
82
|
+
<x-field data-name="wallet" data-type="WalletObject" data-required="true">
|
|
83
|
+
<x-field-desc markdown>一个钱包对象,包含 `address`、`publicKey` 和 `secretKey` 属性,并配备了一个 `sign` 方法。</x-field-desc>
|
|
84
|
+
</x-field>
|
|
85
|
+
|
|
86
|
+
### decodeTx(input)
|
|
87
|
+
|
|
88
|
+
将各种格式的交易反序列化为人类可读的 JavaScript 对象。
|
|
89
|
+
|
|
90
|
+
**参数**
|
|
91
|
+
|
|
92
|
+
<x-field data-name="input" data-type="Buffer | string" data-required="true">
|
|
93
|
+
<x-field-desc markdown>要解码的交易数据。可以是 `Buffer`,也可以是 `hex`、`base58` 或 `base64` 格式的字符串。</x-field-desc>
|
|
94
|
+
</x-field>
|
|
95
|
+
|
|
96
|
+
**返回**
|
|
97
|
+
|
|
98
|
+
<x-field data-name="object" data-type="object" data-desc="解码后的交易对象。"></x-field>
|
|
99
|
+
|
|
100
|
+
### getType(name)
|
|
101
|
+
|
|
102
|
+
根据给定的类型名称检索 Protobuf 消息类。这对于需要手动构建或检查 Protobuf 消息的高级场景非常有用。
|
|
103
|
+
|
|
104
|
+
**参数**
|
|
105
|
+
|
|
106
|
+
<x-field data-name="name" data-type="string" data-required="true" data-desc="Protobuf 消息类型的名称(例如,'Transaction'、'TransferTx')。"></x-field>
|
|
107
|
+
|
|
108
|
+
**返回**
|
|
109
|
+
|
|
110
|
+
<x-field data-name="class | null" data-type="object" data-desc="消息类的构造函数,如果未找到则为 null。"></x-field>
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## 事件订阅
|
|
115
|
+
|
|
116
|
+
客户端支持通过 WebSocket 进行实时事件订阅,使您的应用程序能够即时响应链上事件。
|
|
117
|
+
|
|
118
|
+
### subscribe(topic, callback)
|
|
119
|
+
|
|
120
|
+
建立一个 WebSocket 连接并订阅一个特定的事件主题。
|
|
121
|
+
|
|
122
|
+
**参数**
|
|
123
|
+
|
|
124
|
+
<x-field-group>
|
|
125
|
+
<x-field data-name="topic" data-type="string" data-required="true" data-desc="要订阅的事件主题(例如,'newBlock'、'tx:transfer')。"></x-field>
|
|
126
|
+
<x-field data-name="callback" data-type="function" data-required="true" data-desc="当接收到事件时执行的函数。它接收事件负载作为其唯一参数。"></x-field>
|
|
127
|
+
</x-field-group>
|
|
128
|
+
|
|
129
|
+
### unsubscribe(topic, callback)
|
|
130
|
+
|
|
131
|
+
移除先前为特定主题注册的回调。
|
|
132
|
+
|
|
133
|
+
**参数**
|
|
134
|
+
|
|
135
|
+
<x-field-group>
|
|
136
|
+
<x-field data-name="topic" data-type="string" data-required="true" data-desc="要取消订阅的事件主题。"></x-field>
|
|
137
|
+
<x-field data-name="callback" data-type="function" data-required="true" data-desc="要移除的特定回调函数。"></x-field>
|
|
138
|
+
</x-field-group>
|
|
139
|
+
|
|
140
|
+
**示例**
|
|
141
|
+
|
|
142
|
+
```javascript Subscribing to New Blocks icon=logos:javascript
|
|
143
|
+
const handleNewBlock = (block) => {
|
|
144
|
+
console.log(`接收到新区块!高度: ${block.height}`);
|
|
145
|
+
|
|
146
|
+
// 接收到一个区块后取消订阅
|
|
147
|
+
client.unsubscribe('newBlock', handleNewBlock);
|
|
148
|
+
console.log('已取消订阅 newBlock 事件。');
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
client.subscribe('newBlock', handleNewBlock);
|
|
152
|
+
console.log('已订阅 newBlock 事件...');
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## 代币工具方法
|
|
158
|
+
|
|
159
|
+
这些辅助方法简化了人类可读的代币数量与链上基本单位表示之间的转换。
|
|
160
|
+
|
|
161
|
+
### fromUnitToToken(value)
|
|
162
|
+
|
|
163
|
+
根据原生代币的小数位数,将一个值从链的基本单位(一个大整数字符串)转换为标准的十进制字符串。
|
|
164
|
+
|
|
165
|
+
**参数**
|
|
166
|
+
|
|
167
|
+
<x-field data-name="value" data-type="string" data-required="true" data-desc="以链的基本单位表示的数量。"></x-field>
|
|
168
|
+
|
|
169
|
+
**返回**
|
|
170
|
+
|
|
171
|
+
<x-field data-name="string" data-type="string" data-desc="以标准代币单位表示的数量。"></x-field>
|
|
172
|
+
|
|
173
|
+
### fromTokenToUnit(amount)
|
|
174
|
+
|
|
175
|
+
将一个标准的十进制数量转换为链的基本单位表示(一个 BN.js 实例)。
|
|
176
|
+
|
|
177
|
+
**参数**
|
|
178
|
+
|
|
179
|
+
<x-field data-name="amount" data-type="number | string" data-required="true" data-desc="以标准十进制形式表示的代币数量。"></x-field>
|
|
180
|
+
|
|
181
|
+
**返回**
|
|
182
|
+
|
|
183
|
+
<x-field data-name="BN" data-type="object" data-desc="一个 BN.js 实例,表示以链的基本单位计的值。"></x-field>
|
|
184
|
+
|
|
185
|
+
**示例**
|
|
186
|
+
|
|
187
|
+
```javascript Token Amount Conversion icon=logos:javascript
|
|
188
|
+
async function convertToken() {
|
|
189
|
+
// 将 100 TBA 转换为其基本单位
|
|
190
|
+
const unitAmount = await client.fromTokenToUnit(100);
|
|
191
|
+
console.log(`100 TBA 等于 ${unitAmount.toString()} 基本单位。`);
|
|
192
|
+
|
|
193
|
+
// 将其转换回来
|
|
194
|
+
const tokenAmount = await client.fromUnitToToken(unitAmount.toString());
|
|
195
|
+
console.log(`${unitAmount.toString()} 基本单位等于 ${tokenAmount} TBA。`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
convertToken();
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## 方法发现
|
|
204
|
+
|
|
205
|
+
客户端会为所连接的 OCAP 节点支持的每种交易类型动态生成方法。这些发现方法允许您以编程方式列出所有可用的与交易相关的功能。
|
|
206
|
+
|
|
207
|
+
### getTxSendMethods()
|
|
208
|
+
|
|
209
|
+
返回所有可用的 `send...Tx` 方法名称的数组。这些方法处理签名和发送交易的完整生命周期。
|
|
210
|
+
|
|
211
|
+
### getTxEncodeMethods()
|
|
212
|
+
|
|
213
|
+
返回所有可用的 `encode...Tx` 方法名称的数组。这些方法将交易准备并序列化为缓冲区,但不进行签名。
|
|
214
|
+
|
|
215
|
+
### getTxSignMethods()
|
|
216
|
+
|
|
217
|
+
返回所有可用的 `sign...Tx` 方法名称的数组。这些方法对交易进行编码然后签名,并返回已签名的交易对象。
|
|
218
|
+
|
|
219
|
+
### getTxMultiSignMethods()
|
|
220
|
+
|
|
221
|
+
返回所有可用的 `multiSign...Tx` 方法名称的数组,用于多重签名工作流。
|
|
222
|
+
|
|
223
|
+
**示例**
|
|
224
|
+
|
|
225
|
+
```javascript Listing Available Transaction Methods icon=logos:javascript
|
|
226
|
+
const sendMethods = client.getTxSendMethods();
|
|
227
|
+
console.log('可用的发送方法:', sendMethods);
|
|
228
|
+
// 示例输出:[ 'sendPokeTx', 'sendTransferTx', ... ]
|
|
229
|
+
```
|