@injectivelabs/sdk-ts 0.0.66 → 0.0.67
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 +193 -106
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -6,9 +6,32 @@
|
|
|
6
6
|
|
|
7
7
|
_Accessing decentralized finance through TypeScript (for Web and Node environment)_
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
`@injectivelabs/sdk-ts` is a TypeScript SDK for writing applications on top of the Injective chain in both a Node.js and a browser environment.
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<a href="#"><strong>Documentation</strong></a>
|
|
13
|
+
·
|
|
14
|
+
<a href="https://github.com/InjectiveLabs/sdk-ts-examples">Examples</a>
|
|
15
|
+
·
|
|
16
|
+
<a href="#">API Reference</a>
|
|
17
|
+
·
|
|
18
|
+
<a href="https://www.npmjs.com/package/@injectivelabs/sdk-ts">NPM Package</a>
|
|
19
|
+
·
|
|
20
|
+
<a href="https://github.com/injectivelabs/sdk-ts">GitHub</a>
|
|
21
|
+
</p>
|
|
22
|
+
|
|
23
|
+
### ✨ Features
|
|
24
|
+
|
|
25
|
+
- **Written in TypeScript**, with type definitions,
|
|
26
|
+
- Works in Node.js and in the browser,
|
|
27
|
+
- Exposes on-chain data and the exchange-api data,
|
|
28
|
+
- Parses responses into native JavaScript types
|
|
29
|
+
- much more ...
|
|
30
|
+
|
|
31
|
+
We highly suggest using the `@injectivelabs/sdk-ts` with TypeScript, or JavaScript in a code editor that has support for type declarations, so you can take advantage of the helpful type hints that are included with the package.
|
|
10
32
|
|
|
11
|
-
|
|
33
|
+
|
|
34
|
+
### 📚 Installation
|
|
12
35
|
|
|
13
36
|
```bash
|
|
14
37
|
yarn add @injectivelabs/sdk-ts
|
|
@@ -18,48 +41,35 @@ yarn add @injectivelabs/sdk-ts
|
|
|
18
41
|
|
|
19
42
|
## 📖 Documentation
|
|
20
43
|
|
|
21
|
-
There are
|
|
22
|
-
|
|
23
|
-
We are going to go through all of the components of the package and explain them in depth.
|
|
44
|
+
There are two pieces of the `sdk-ts` - **querying a data source** and **making transactions**.
|
|
24
45
|
|
|
25
|
-
###
|
|
46
|
+
### Querying a data source
|
|
26
47
|
|
|
27
|
-
|
|
48
|
+
There are 2 data sources that can be accessed through the `sdk-ts`:
|
|
28
49
|
|
|
29
|
-
-
|
|
50
|
+
- The Injective chain itself through a sentry node,
|
|
51
|
+
- The Exchange API indexer (indexer of events from the Injective chain to a MongoDB),
|
|
30
52
|
|
|
31
|
-
|
|
53
|
+
For each of the data sources there are two ways that they can be queried:
|
|
32
54
|
|
|
33
|
-
|
|
55
|
+
- using the gRPC protocol,
|
|
56
|
+
- using REST
|
|
34
57
|
|
|
35
|
-
|
|
36
|
-
- Chain -> Consuming data directly from the chain,
|
|
37
|
-
- Exchange -> Consuming data directly from the exchange (indexer) API,
|
|
58
|
+
We also have a GraphQL consumer of the Peggy subgraph on Ethereum (used only for tracking deposits and withdrawals on Ethereum).
|
|
38
59
|
|
|
39
|
-
|
|
60
|
+
For the 2 main data sources, there are abstraction classes that developers can use to access specific modules of the Injective Chain **or** specific modules within the Exchange API. The responses of these requests are always mapped into normal JavaScript objects (regardless of the data source type) and served to the end user.
|
|
40
61
|
|
|
41
|
-
###
|
|
62
|
+
### Making Transactions
|
|
42
63
|
|
|
43
|
-
|
|
64
|
+
To interact with Injective, the user has to issue a transaction. Each transaction that is broadcasted to Injective can hold multiple messages that can change the state of the Injective chain. Within the `core` folder, developers can find a convenient way to make these messages with methods that can convert these messages in a way that they can be used by developers in different scenarios (broadcasting using the normal cosmos way or converted to EIP712 to be signed using Ethereum wallets).
|
|
44
65
|
|
|
45
|
-
Every message
|
|
66
|
+
Every message extends the `MsgBase` interface, which has couple of mapping functionalities:
|
|
46
67
|
- `toData` -> Converts the Message to a simple Object representation,
|
|
47
|
-
- `toProto` -> Returns a proto representation of the
|
|
48
|
-
- `toDirectSign` -> Converts the Message to a proto representation
|
|
49
|
-
- `toWeb3` -> Converts the Message to a web3 representation + type (ready
|
|
50
|
-
|
|
51
|
-
### Utils
|
|
52
|
-
|
|
53
|
-
This package contains some utility functions and constants.
|
|
68
|
+
- `toProto` -> Returns a proto representation of the Message,
|
|
69
|
+
- `toDirectSign` -> Converts the Message to a proto representation (ready to be used in the normal Cosmos way of handling transactions),
|
|
70
|
+
- `toWeb3` -> Converts the Message to a web3 representation + type (ready to be used in the Ethereum way of handling transactions using EIP712 typed data),
|
|
54
71
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
This package contains some utility functions and constants for client-side usage (a local - node environment)
|
|
58
|
-
|
|
59
|
-
- `Network` class -> Can be used to get a pre-defined set of endpoints (sentry, api, etc) for easier usage,
|
|
60
|
-
- `PrivateKey` class -> Can be used to sign transactions, etc
|
|
61
|
-
- `InjectiveTx` class -> Can be used to prepare a transaction for signing/broadcasting on the client side,
|
|
62
|
-
- `TxService` class -> Can be used for simulating or broadcasting a transaction on the client side
|
|
72
|
+
There are also some utility classes and functions that are exposed from the package. There is also a `local` folder that exposes some utility classes that can be used to make developers life easier in a Node environment.
|
|
63
73
|
|
|
64
74
|
---
|
|
65
75
|
|
|
@@ -69,17 +79,48 @@ Let's go through couple of use-cases of the `sdk-ts` so developers can have a re
|
|
|
69
79
|
|
|
70
80
|
### Consuming data
|
|
71
81
|
|
|
72
|
-
- Fetching user's balance from the chain
|
|
82
|
+
- Fetching user's inj balance from the chain
|
|
73
83
|
|
|
74
84
|
```ts
|
|
75
|
-
|
|
76
|
-
import {
|
|
85
|
+
import { getNetworkInfo, Network } from "@injectivelabs/networks";
|
|
86
|
+
import { ExchangeGrpcClient } from "@injectivelabs/sdk-ts/dist/client/exchange/ExchangeGrpcClient";
|
|
87
|
+
|
|
88
|
+
(async () => {
|
|
89
|
+
const network = getNetworkInfo(Network.TestnetK8s);
|
|
90
|
+
const injectiveAddress = "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku";
|
|
91
|
+
const chainClient = new ExchangeGrpcClient(
|
|
92
|
+
network.sentryGrpcApi
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const balances = await chainClient.bank.fetchBalances(
|
|
96
|
+
injectiveAddress
|
|
97
|
+
);
|
|
98
|
+
const injBalance = balances.find(balace => balance.denom === 'inj')
|
|
99
|
+
|
|
100
|
+
console.log(injBalance);
|
|
101
|
+
})();
|
|
77
102
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
- Fetching user's subaccount balance from the exchange api
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { getNetworkInfo, Network } from "@injectivelabs/networks";
|
|
109
|
+
import { ChainGrpcClient } from "@injectivelabs/sdk-ts/dist/client/chain/ChainGrpcClient";
|
|
110
|
+
|
|
111
|
+
(async () => {
|
|
112
|
+
const network = getNetworkInfo(Network.TestnetK8s);
|
|
113
|
+
const subaccountId = "0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000000";
|
|
114
|
+
const exchangeClient = new ChainGrpcClient(
|
|
115
|
+
network.exchangeApi
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
const subaccountBalancesList = await exchangeClient.account.fetchSubaccountBalancesList(
|
|
119
|
+
subaccountId
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
console.log(subaccountBalancesList);
|
|
123
|
+
})();
|
|
83
124
|
```
|
|
84
125
|
|
|
85
126
|
```ts
|
|
@@ -115,91 +156,137 @@ console.log(await derivativesApi.markets())
|
|
|
115
156
|
|
|
116
157
|
### Broadcasting Transactions
|
|
117
158
|
|
|
118
|
-
-
|
|
159
|
+
- Bidding on an auction
|
|
119
160
|
|
|
120
161
|
```ts
|
|
121
|
-
import {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
162
|
+
import { getNetworkInfo, Network } from "@injectivelabs/networks";
|
|
163
|
+
import {
|
|
164
|
+
AuctionCore,
|
|
165
|
+
ChainClient,
|
|
166
|
+
PrivateKey,
|
|
167
|
+
BaseAccount,
|
|
168
|
+
TxInjective,
|
|
169
|
+
TxService,
|
|
170
|
+
} from "@injectivelabs/sdk-ts";
|
|
171
|
+
import { BigNumberInBase } from "@injectivelabs/utils";
|
|
172
|
+
|
|
173
|
+
/** MsgBid Example */
|
|
174
|
+
(async () => {
|
|
175
|
+
const network = getNetworkInfo(Network.TestnetK8s);
|
|
176
|
+
const privateKey = PrivateKey.fromPrivateKey(
|
|
177
|
+
"f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3"
|
|
178
|
+
);
|
|
179
|
+
const injectiveAddress = privateKey.toBech32();
|
|
180
|
+
console.log(injectiveAddress, privateKey.toHex());
|
|
181
|
+
|
|
182
|
+
/** Account Details **/
|
|
183
|
+
const accountDetails = await new ChainClient.AuthRestApi(
|
|
184
|
+
network.sentryHttpApi
|
|
185
|
+
).account(injectiveAddress);
|
|
186
|
+
const baseAccount = BaseAccount.fromRestApi(accountDetails);
|
|
187
|
+
|
|
188
|
+
/** Prepare the Message */
|
|
189
|
+
const auctionModuleState = await new ChainClient.AuctionApi(
|
|
190
|
+
network.sentryGrpcApi
|
|
191
|
+
).moduleState();
|
|
192
|
+
const latestRound = auctionModuleState.getState()?.getAuctionRound();
|
|
193
|
+
const round = latestRound || 1;
|
|
194
|
+
const bid = 1; /** 100 INJ */
|
|
195
|
+
const amount = {
|
|
196
|
+
amount: new BigNumberInBase(bid).toWei().toFixed(),
|
|
197
|
+
denom: "inj",
|
|
198
|
+
};
|
|
199
|
+
const msg = new AuctionCore.MsgBid({
|
|
200
|
+
round,
|
|
201
|
+
amount,
|
|
202
|
+
injectiveAddress,
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
/** Prepare the Transaction **/
|
|
206
|
+
const txInjective = new TxInjective({
|
|
207
|
+
baseAccount,
|
|
156
208
|
msgs: [msg],
|
|
157
209
|
chainId: network.chainId,
|
|
158
|
-
address: injectiveAddress
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
210
|
+
address: injectiveAddress,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
/** Sign transaction */
|
|
214
|
+
const signature = await privateKey.sign(txInjective.signBytes);
|
|
215
|
+
const signedTxInjective = txInjective.withSignature(signature);
|
|
216
|
+
|
|
217
|
+
/** Calculate hash of the transaction */
|
|
218
|
+
console.log(`Transaction Hash: ${signedTxInjective.getTxHash()}`);
|
|
219
|
+
|
|
220
|
+
const txService = new TxService({
|
|
221
|
+
txInjective: signedTxInjective,
|
|
222
|
+
endpoint: network.sentryGrpcApi,
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
/** Simulate transaction */
|
|
226
|
+
const simulationResponse = await txService.simulate();
|
|
227
|
+
console.log(
|
|
228
|
+
`Transaction simulation response: ${JSON.stringify(
|
|
229
|
+
simulationResponse.gasInfo
|
|
230
|
+
)}`
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
/** Broadcast transaction */
|
|
234
|
+
const txResponse = await txService.broadcast();
|
|
235
|
+
console.log(
|
|
236
|
+
`Broadcasted transaction hash: ${JSON.stringify(txResponse.txhash)}`
|
|
237
|
+
);
|
|
238
|
+
})();
|
|
176
239
|
```
|
|
177
240
|
|
|
178
241
|
### Streaming Data
|
|
179
242
|
|
|
180
|
-
|
|
243
|
+
- Streaming users subaccount balances from the exchange API
|
|
181
244
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
245
|
+
```ts
|
|
246
|
+
import { getNetworkInfo, Network } from "@injectivelabs/networks";
|
|
247
|
+
import { protoObjectToJson } from "@injectivelabs/sdk-ts";
|
|
248
|
+
import { ExchangeGrpcStreamClient } from "@injectivelabs/sdk-ts/dist/client/exchange/ExchangeGrpcStreamClient";
|
|
249
|
+
|
|
250
|
+
(async () => {
|
|
251
|
+
const network = getNetworkInfo(Network.TestnetK8s);
|
|
252
|
+
|
|
253
|
+
const subaccountId =
|
|
254
|
+
"0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000000";
|
|
255
|
+
|
|
256
|
+
const exchangeClient = new ExchangeGrpcStreamClient(
|
|
257
|
+
network.exchangeApi
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
await exchangeClient.account.streamSubaccountBalance({
|
|
261
|
+
subaccountId,
|
|
262
|
+
callback: (subaccountBalance) => {
|
|
263
|
+
console.log(protoObjectToJson(subaccountBalance));
|
|
264
|
+
},
|
|
265
|
+
onEndCallback: (status) => {
|
|
266
|
+
console.log("Stream has ended with status: " + status);
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
})();
|
|
190
270
|
```
|
|
191
271
|
|
|
192
|
-
Don't forget to do `yarn` and install dependencies before executing any example.
|
|
193
|
-
|
|
194
272
|
---
|
|
195
273
|
|
|
196
274
|
## ⛑ Support
|
|
197
275
|
|
|
198
276
|
Reach out to us at one of the following places!
|
|
199
277
|
|
|
200
|
-
- Website at <a href="https://
|
|
278
|
+
- Website at <a href="https://injective.com" target="_blank">`injective.com`</a>
|
|
201
279
|
- Twitter at <a href="https://twitter.com/InjectiveLabs" target="_blank">`@InjectiveLabs`</a>
|
|
280
|
+
- Discord at <a href="https://discord.com/invite/NK4qdbv" target="_blank">`Discord`</a>
|
|
281
|
+
- Telegram at <a href="https://t.me/joininjective" target="_blank">`Telegram`</a>
|
|
202
282
|
|
|
203
283
|
---
|
|
204
284
|
|
|
205
285
|
## 🔓 License
|
|
286
|
+
|
|
287
|
+
This software is licensed under the MIT license. See [LICENSE](./LICENSE) for full disclosure.
|
|
288
|
+
|
|
289
|
+
<p> </p>
|
|
290
|
+
<div align="center">
|
|
291
|
+
<sub><em>Powering the future of decentralized finance.</em></sub>
|
|
292
|
+
</div>
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@injectivelabs/sdk-ts",
|
|
3
3
|
"description": "SDK in TypeScript for building Injective applications in a Node environment.",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.67",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Bojan Angjelkoski",
|
|
@@ -58,5 +58,5 @@
|
|
|
58
58
|
"snakecase-keys": "^5.4.1",
|
|
59
59
|
"tiny-secp256k1": "^2.2.1"
|
|
60
60
|
},
|
|
61
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "fe1050fcc442e8f8893e318cec0487ee5228c5f5"
|
|
62
62
|
}
|