@initia/initia.js 0.0.49 → 0.0.51

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 CHANGED
@@ -1,2 +1,222 @@
1
1
  # initia.js
2
2
  JavaScript SDK for Initia, written in TypeScript
3
+
4
+ ## Main Features
5
+
6
+ * Improve user-friendly Typescript definitions with Initia core data structures integration
7
+ * Core Layer: Key management, BCS serialization, Support Initia.proto etc
8
+ * Client Layer: API request generation, LCD provider etc
9
+
10
+ ## Prerequisites
11
+
12
+ Initia.js library requires the installation of the following packages in order to function properly.
13
+
14
+ * node.js v14+
15
+ * npm
16
+
17
+ ## Installation
18
+
19
+ Before installation, check the latest version of [npm](https://www.npmjs.com/package/@initia/initia.js): 
20
+
21
+ ```bash
22
+ npm install @initia/initia.js
23
+ ```
24
+
25
+ ## Usage;
26
+
27
+ The usage section of this document provides detailed explanations and code examples of the most commonly used classes of the Initia.js library, which can be utilized both in a Node.js environment and within a browser.
28
+
29
+ ### LCD client 
30
+
31
+ **LCD**(Light Client Daemon) class facilitates interaction with the Initia blockchain.
32
+
33
+ ```typescript
34
+ import { LCDClient } from '@initia/initia.js'
35
+
36
+ const lcd = new LCDClient('https://stone-rest.initia.tech/', {
37
+ chainId: "stone-7",
38
+ gasPrices: "0.005uinit", // default gas prices
39
+ gasAdjustment: "2.0", // default gas adjustment for fee estimation
40
+ });
41
+ ```
42
+
43
+
44
+ > **`gasPrices`** and **`gasAdjustment`**are optional, but essential for the fee estimation
45
+
46
+
47
+ ### Key
48
+
49
+ An abstract key interface that enables transaction signing and provides Bech32 address and public key derivation from a public key. 
50
+
51
+ ```typescript
52
+ import { MnemonicKey } from "@initia/initia.js";
53
+
54
+ const key = new MnemonicKey({
55
+ mnemonic: "bird upset ... evil cigar", // (optional) if null, generate a new Mnemonic key
56
+ account: 0, // (optional) BIP44 account number. default = 0
57
+ index: 0, // (optional) BIP44 index number. defualt = 0
58
+ coinType: 118, // (optional) BIP44 coinType. default = 118
59
+ });
60
+ ```
61
+
62
+ ### BCS
63
+
64
+ **BCS**(Binary Canonical Serialization) is the binary encoding for Move resources and other non-module values published on-chain.  
65
+
66
+ ```typescript
67
+ import { BCS } from "@initia/initia.js";
68
+
69
+ const bcs = BCS.getInstance();
70
+
71
+ // serialize, serialize value to BCS and encode it to base64
72
+ const serializedU64 = bcs.serialize("u64" /*type*/, 1234 /*value*/);
73
+
74
+ // deserialize
75
+ const deserializedU64 = bcs.deserialize(
76
+ "u64", //type
77
+ serializedU64 // base64 encoded and BCS serialize value
78
+ );
79
+
80
+ // vector
81
+ const serializedVector = bcs.serialize("vector<u64>", [123, 456, 678]);
82
+
83
+ // option
84
+ const serializedSome = bcs.serialize("option<u64>", 123); // some
85
+ const serializedNone = bcs.serialize("option<u64>", null); // none
86
+ ```
87
+
88
+ **Support types for BCS**
89
+ > \`u8\`, \`u16\`, \`u32\`, \`u64\`, \`u128\`, \`u256\`, \`bool\`, \`vector\`, \`address\`, \`string\`, \`option\`
90
+
91
+
92
+ ### Msg&#x20;
93
+
94
+ Msgs are object whose end-goal is to trigger state-transitions. They are wrapped in transactions, which may contain one or more of them.
95
+
96
+ * `MsgSend()`&#x20;
97
+
98
+ Send coins to others.
99
+
100
+ ```typescript
101
+ import { MsgSend } from "@initia/initia.js";
102
+
103
+ const msg = new MsgSend(
104
+ "init1kdwzpz3wzvpdj90gtga4fw5zm9tk4cyrgnjauu", // sender address
105
+ "init18sj3x80fdjc6gzfvwl7lf8sxcvuvqjpvcmp6np", // recipient address
106
+ "1000uinit", // send amount
107
+ );
108
+ ```
109
+
110
+ * `MsgDelegate()`
111
+
112
+ Delegate governance coin to validators (staking).
113
+
114
+ ```typescript
115
+ import { MsgDelegate } from "@initia/initia.js";
116
+
117
+ const msg = new MsgDelegate(
118
+ "init1kdwzpz3wzvpdj90gtga4fw5zm9tk4cyrgnjauu", // delegator address
119
+ "init18sj3x80fdjc6gzfvwl7lf8sxcvuvqjpvcmp6np", // validator's operator addres
120
+ "100000uinit", // delegate amount
121
+ )
122
+ ```
123
+
124
+ * `MsgUndelegate()`
125
+
126
+ Undelegate governance coin from validators (unstaking).
127
+
128
+ ```typescript
129
+ import { MsgUndelegate } from "@initia/initia.js";
130
+
131
+ const msg = new MsgUndelegate(
132
+ "init1kdwzpz3wzvpdj90gtga4fw5zm9tk4cyrgnjauu", // delegator address
133
+ "init18sj3x80fdjc6gzfvwl7lf8sxcvuvqjpvcmp6np", // validator's operator addres
134
+ "100000uinit", // undelegate amount
135
+ )
136
+ ```
137
+
138
+ * `MsgExecute()`
139
+
140
+ Execute move contract function.
141
+
142
+ ```typescript
143
+ import { MsgExecute } from "@initia/initia.js";
144
+
145
+ const msg = new MsgExecute(
146
+ "init1kdwzpz3wzvpdj90gtga4fw5zm9tk4cyrgnjauu", // sender address
147
+ "init18sj3x80fdjc6gzfvwl7lf8sxcvuvqjpvcmp6np", // owner of the module
148
+ "dex", // name of the module
149
+ "swap_script", // function name
150
+ ["0x1::native_uinit::Coin", "0x2::module_name::AnotherCoin"], // type arguments
151
+ [bcs.serialize("u64", 10000), bcs.serialize("optoin<u64>", null)], // arguments
152
+ );
153
+ ```
154
+
155
+ ### Tx broadcasting&#x20;
156
+
157
+ * `createAndSignTx()`
158
+
159
+ Create a wallet and sign transaction. &#x20;
160
+
161
+ ```typescript
162
+ import { Wallet, LCDClient, MnemonicKey } from "@initia/initia.js";
163
+
164
+ const key = new MnemonicKey({
165
+ mnemonic:
166
+ 'moral wise tape glance grit gentle movie doll omit you pet soon enter year funny gauge digital supply cereal city ring egg repair coyote',
167
+ });
168
+
169
+ const lcd = new LCDClient('https://stone-rest.initia.tech/', {
170
+ chainId: 'stone-7',
171
+ gasPrices: '0.15uinit',
172
+ gasAdjustment: '2.0',
173
+ });
174
+
175
+ const wallet = new Wallet(lcd, key);
176
+
177
+ const sendMsg = new MsgSend(
178
+ "init14l3c2vxrdvu6y0sqykppey930s4kufsvt97aeu", // sender address
179
+ "init18sj3x80fdjc6gzfvwl7lf8sxcvuvqjpvcmp6np", // recipient address
180
+ "1000uinit", // send amount
181
+ );
182
+
183
+ const signedTx = await wallet.createAndSignTx({
184
+ msgs: [sendMsg],
185
+ memo: "sample memo",
186
+ });
187
+ ```
188
+
189
+ When sending coins with `MsgSend`, sender address should be same with wallet address.
190
+
191
+ * `broadcast()`
192
+
193
+ `broadcast()` is the action that sends your transaction to the blockchain code.
194
+
195
+ ```typescript
196
+ const broadcastResult = await lcd.tx.broadcast(signedTx);
197
+ ```
198
+
199
+ ### Queries&#x20;
200
+
201
+ * `balance()`
202
+
203
+ Query the balance of the account.
204
+
205
+ ```typescript
206
+ const balances = await lcd.bank.balance("init14l3c2vxrdvu6y0sqykppey930s4kufsvt97aeu");
207
+ ```
208
+
209
+ * `viewfunction()`
210
+
211
+ Obtain the return values of a Move function that has a `public entry`.
212
+
213
+ ```typescript
214
+ const res = await lcd.move
215
+ .viewFunction(
216
+ '0x1', // owner of the module
217
+ 'dex', // name of the module
218
+ 'get_swap_simulation', // function name
219
+ ['0x1::native_uinit::Coin', '0x1::native_uusdc::Coin'], // type arguments
220
+ [bcs.serialize('u64', 10000)] // arguments
221
+ )
222
+ ```