koilib 1.5.0 → 2.3.0
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 +133 -86
- package/dist/koinos.js +12087 -5016
- package/dist/koinos.min.js +1 -1
- package/lib/Contract.d.ts +135 -230
- package/lib/Contract.js +219 -154
- package/lib/Contract.js.map +1 -1
- package/lib/Provider.d.ts +43 -18
- package/lib/Provider.js +69 -19
- package/lib/Provider.js.map +1 -1
- package/lib/Serializer.d.ts +81 -0
- package/lib/Serializer.js +169 -0
- package/lib/Serializer.js.map +1 -0
- package/lib/Signer.d.ts +170 -18
- package/lib/Signer.js +215 -42
- package/lib/Signer.js.map +1 -1
- package/lib/index.d.ts +2 -10
- package/lib/index.js +5 -12
- package/lib/index.js.map +1 -1
- package/lib/index2.js +2 -10
- package/lib/index2.js.map +1 -1
- package/lib/interface.d.ts +232 -35
- package/lib/jsonDescriptors/krc20-proto.json +183 -0
- package/lib/jsonDescriptors/protocol-proto.json +246 -0
- package/lib/utils.d.ts +281 -0
- package/lib/utils.js +109 -1
- package/lib/utils.js.map +1 -1
- package/package.json +10 -3
- package/lib/Multihash.d.ts +0 -41
- package/lib/Multihash.js +0 -38
- package/lib/Multihash.js.map +0 -1
- package/lib/VariableBlob.d.ts +0 -35
- package/lib/VariableBlob.js +0 -227
- package/lib/VariableBlob.js.map +0 -1
- package/lib/Wallet.d.ts +0 -250
- package/lib/Wallet.js +0 -301
- package/lib/Wallet.js.map +0 -1
- package/lib/abi.d.ts +0 -177
- package/lib/abi.js +0 -160
- package/lib/abi.js.map +0 -1
- package/lib/serializer.d.ts +0 -41
- package/lib/serializer.js +0 -273
- package/lib/serializer.js.map +0 -1
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ Install the package from NPM
|
|
|
17
17
|
npm install koilib
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
You can also load it directly to the browser by downloading the bunble file located at `dist/koinos.min.js`, or
|
|
20
|
+
You can also load it directly to the browser by downloading the bunble file located at `dist/koinos.min.js`, or its non-minified version `dist/koinos.js` (useful for debugging).
|
|
21
21
|
|
|
22
22
|
## Usage
|
|
23
23
|
|
|
@@ -32,25 +32,22 @@ You can also load it directly to the browser by downloading the bunble file loca
|
|
|
32
32
|
<script src="koinos.min.js"></script>
|
|
33
33
|
<script>
|
|
34
34
|
(async () => {
|
|
35
|
+
const provider = new Provider(["http://api.koinos.io:8080"]);
|
|
35
36
|
const signer = Signer.fromSeed("my seed");
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
id: "
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
inputs: { type: "string" },
|
|
43
|
-
outputs: { type: "uint64" },
|
|
44
|
-
},
|
|
45
|
-
},
|
|
37
|
+
signer.provider = provider;
|
|
38
|
+
const koinContract = new Contract({
|
|
39
|
+
id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
|
|
40
|
+
abi: utils.Krc20Abi,
|
|
41
|
+
provider,
|
|
42
|
+
signer,
|
|
46
43
|
});
|
|
44
|
+
const koin = koinContract.functions;
|
|
47
45
|
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
args: wallet.getAddress(),
|
|
46
|
+
// Get balance
|
|
47
|
+
const { result } = await koin.balanceOf({
|
|
48
|
+
owner: signer.getAddress(),
|
|
52
49
|
});
|
|
53
|
-
console.log(
|
|
50
|
+
console.log(balance.result);
|
|
54
51
|
})();
|
|
55
52
|
</script>
|
|
56
53
|
</head>
|
|
@@ -63,13 +60,13 @@ You can also load it directly to the browser by downloading the bunble file loca
|
|
|
63
60
|
With Typescript import the library
|
|
64
61
|
|
|
65
62
|
```typescript
|
|
66
|
-
import {
|
|
63
|
+
import { Signer, Contract, Provider, Serializer, utils } from "koilib";
|
|
67
64
|
```
|
|
68
65
|
|
|
69
66
|
With Javascript import the library with require
|
|
70
67
|
|
|
71
68
|
```javascript
|
|
72
|
-
const {
|
|
69
|
+
const { Signer, Contract, Provider, Serializer, utils } = require("koilib");
|
|
73
70
|
```
|
|
74
71
|
|
|
75
72
|
There are 4 principal classes:
|
|
@@ -77,74 +74,50 @@ There are 4 principal classes:
|
|
|
77
74
|
- **Signer**: Class containing the private key. It is used to sign.
|
|
78
75
|
- **Provider**: Class to connect with the RPC node.
|
|
79
76
|
- **Contract**: Class defining the contract to interact with.
|
|
80
|
-
- **
|
|
77
|
+
- **Serializer**: Class with the protocol buffers definitions to
|
|
78
|
+
serialize/deserialize data types.
|
|
81
79
|
|
|
82
|
-
The following code shows how to
|
|
80
|
+
The following code shows how to sign a transaction, broadcast
|
|
83
81
|
a transaction, and read contracts.
|
|
84
82
|
|
|
85
83
|
```typescript
|
|
86
84
|
(async () => {
|
|
87
85
|
// define signer, provider, and contract
|
|
86
|
+
const provider = new Provider(["http://api.koinos.io:8080"]);
|
|
88
87
|
const signer = Signer.fromSeed("my seed");
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
id: "
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
inputs: {
|
|
96
|
-
type: [
|
|
97
|
-
{
|
|
98
|
-
name: "from",
|
|
99
|
-
type: "string",
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
name: "to",
|
|
103
|
-
type: "string",
|
|
104
|
-
},
|
|
105
|
-
{
|
|
106
|
-
name: "value",
|
|
107
|
-
type: "uint64",
|
|
108
|
-
},
|
|
109
|
-
],
|
|
110
|
-
},
|
|
111
|
-
},
|
|
112
|
-
balance_of: {
|
|
113
|
-
id: 0x15619248,
|
|
114
|
-
inputs: { type: "string" },
|
|
115
|
-
outputs: { type: "uint64" },
|
|
116
|
-
},
|
|
117
|
-
},
|
|
88
|
+
signer.provider = provider;
|
|
89
|
+
const koinContract = new Contract({
|
|
90
|
+
id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
|
|
91
|
+
abi: utils.Krc20Abi,
|
|
92
|
+
provider,
|
|
93
|
+
signer,
|
|
118
94
|
});
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
value: BigInt(1000),
|
|
130
|
-
},
|
|
95
|
+
const koin = koinContract.functions;
|
|
96
|
+
|
|
97
|
+
// optional: preformat input/output
|
|
98
|
+
koinContract.abi.methods.balanceOf.preformatInput = (owner) => ({ owner });
|
|
99
|
+
koinContract.abi.methods.balanceOf.preformatOutput = (res) =>
|
|
100
|
+
utils.formatUnits(res.value, 8);
|
|
101
|
+
koinContract.abi.methods.transfer.preformatInput = (input) => ({
|
|
102
|
+
from: signer.getAddress(),
|
|
103
|
+
to: input.to,
|
|
104
|
+
value: utils.parseUnits(input.value, 8),
|
|
131
105
|
});
|
|
132
106
|
|
|
133
|
-
//
|
|
134
|
-
const
|
|
135
|
-
|
|
107
|
+
// Transfer
|
|
108
|
+
const { transaction, transactionResponse } = await koin.transfer({
|
|
109
|
+
to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
|
|
110
|
+
value: "10.0001",
|
|
136
111
|
});
|
|
112
|
+
console.log(`Transaction id ${transaction.id} submitted`);
|
|
137
113
|
|
|
138
|
-
//
|
|
139
|
-
await
|
|
140
|
-
|
|
114
|
+
// wait to be mined
|
|
115
|
+
const blockId = await transactionResponse.wait();
|
|
116
|
+
console.log(`Transaction mined. Block id: ${blockId}`);
|
|
141
117
|
|
|
142
118
|
// read the balance
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
args: wallet.getAddress(),
|
|
146
|
-
});
|
|
147
|
-
console.log(Number(balance) / 1e8);
|
|
119
|
+
const { result } = await koin.balanceOf(signer.getAddress());
|
|
120
|
+
console.log(balance.result);
|
|
148
121
|
})();
|
|
149
122
|
```
|
|
150
123
|
|
|
@@ -152,26 +125,100 @@ It's also possible to upload contracts. First, follow the instructions in [koino
|
|
|
152
125
|
|
|
153
126
|
```typescript
|
|
154
127
|
(async () => {
|
|
155
|
-
// define signer, provider and
|
|
128
|
+
// define signer, provider and bytecode
|
|
129
|
+
const provider = new Provider(["http://api.koinos.io:8080"]);
|
|
156
130
|
const signer = Signer.fromSeed("my seed");
|
|
157
|
-
|
|
158
|
-
const wallet = new Wallet({ signer, provider });
|
|
159
|
-
// encode operation to upload the contract
|
|
131
|
+
signer.provider = provider;
|
|
160
132
|
const bytecode = fs.readFileSync("my_contract.wasm");
|
|
161
|
-
const op = wallet.encodeUploadContractOperation(bytecode);
|
|
162
|
-
// create a transaction
|
|
163
|
-
const tx = await wallet.newTransaction({
|
|
164
|
-
operations: [op],
|
|
165
|
-
});
|
|
166
|
-
// sign and send transaction
|
|
167
|
-
await wallet.signTransaction(tx);
|
|
168
|
-
await wallet.sendTransaction(tx);
|
|
169
133
|
|
|
170
|
-
|
|
171
|
-
|
|
134
|
+
// create contract and deploy
|
|
135
|
+
const contract = new Contract({ signer, provider, bytecode });
|
|
136
|
+
const { transactionResponse } = await contract.deploy();
|
|
137
|
+
// wait to be mined
|
|
138
|
+
const blockId = await transactionResponse.wait();
|
|
139
|
+
console.log(`Contract uploaded in block id ${blockId}`);
|
|
172
140
|
})();
|
|
173
141
|
```
|
|
174
142
|
|
|
143
|
+
### Create ABIs
|
|
144
|
+
|
|
145
|
+
ABIs are composed of 2 elements: methods and types.
|
|
146
|
+
|
|
147
|
+
- The methods define the names of the entries of the smart contract, the corresponding endpoints and the name of the types used.
|
|
148
|
+
- The types all the description to serialize and deserialize using proto buffers.
|
|
149
|
+
|
|
150
|
+
To generate the types is necessary to use the dependency protobufjs. The following example shows how to generate the protobuf descriptor from a .proto file.
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
const fs = require("fs");
|
|
154
|
+
const pbjs = require("protobufjs/cli/pbjs");
|
|
155
|
+
|
|
156
|
+
pbjs.main(["--target", "json", "./token.proto"], (err, output) => {
|
|
157
|
+
if (err) throw err;
|
|
158
|
+
fs.writeFileSync("./token-proto.json", output);
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Then this descriptor can be loaded to define the ABI:
|
|
163
|
+
|
|
164
|
+
```js
|
|
165
|
+
const tokenJson = require("./token-proto.json");
|
|
166
|
+
const abiToken = {
|
|
167
|
+
methods: {
|
|
168
|
+
balanceOf: {
|
|
169
|
+
entryPoint: 0x15619248,
|
|
170
|
+
inputs: "balance_of_arguments",
|
|
171
|
+
outputs: "balance_of_result",
|
|
172
|
+
readOnly: true,
|
|
173
|
+
defaultOutput: { value: "0" },
|
|
174
|
+
},
|
|
175
|
+
transfer: {
|
|
176
|
+
entryPoint: 0x62efa292,
|
|
177
|
+
inputs: "transfer_arguments",
|
|
178
|
+
outputs: "transfer_result",
|
|
179
|
+
},
|
|
180
|
+
mint: {
|
|
181
|
+
entryPoint: 0xc2f82bdc,
|
|
182
|
+
inputs: "mint_argumnets",
|
|
183
|
+
outputs: "mint_result",
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
types: tokenJson,
|
|
187
|
+
};
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Note that this example uses "defaultOutput" for the method
|
|
191
|
+
"balanceOf". This is used when the smart contract returns an
|
|
192
|
+
empty response (for instance when there are no balance records
|
|
193
|
+
for a specific address) and you require a default output in
|
|
194
|
+
such cases.
|
|
195
|
+
|
|
196
|
+
## FAQ
|
|
197
|
+
|
|
198
|
+
1. Can this library be used to create smart contracts?
|
|
199
|
+
|
|
200
|
+
No. You need to install [koinos-cdt](https://github.com/koinos/koinos-cdt) for
|
|
201
|
+
this purpose.
|
|
202
|
+
|
|
203
|
+
2. Can this library be used to deploy smart contracts?
|
|
204
|
+
|
|
205
|
+
Yes. If you already have the contract compiled as a .wasm file you can use
|
|
206
|
+
the Contract class to load the bytecode and deploy it.
|
|
207
|
+
|
|
208
|
+
3. Can this library be used to create the ABI for any smart contract?
|
|
209
|
+
|
|
210
|
+
For the ABI you need the .proto file and the library
|
|
211
|
+
[protobufjs](https://www.npmjs.com/package/protobufjs). Then follow the format
|
|
212
|
+
for the ABI as described in the previous section. It's important to note that
|
|
213
|
+
this ABI is not the same ABI used in [koinos-cli](https://docs.koinos.io/architecture/contract-abi.html).
|
|
214
|
+
In particular, descriptors use different format (koilib using json format, cli
|
|
215
|
+
using binary format).
|
|
216
|
+
|
|
217
|
+
4. Can this library be used to interact with smart contracts?
|
|
218
|
+
|
|
219
|
+
Yes. You can use it to call readOnly functions, or send transactions
|
|
220
|
+
to the contract by calling write functions.
|
|
221
|
+
|
|
175
222
|
## Documentation
|
|
176
223
|
|
|
177
224
|
The complete documentation can be found at https://joticajulian.github.io/koilib/
|