suidouble 0.0.3
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/LICENSE +201 -0
- package/README.md +302 -0
- package/index.js +9 -0
- package/lib/SuiCliCommands.js +67 -0
- package/lib/SuiCommonMethods.js +44 -0
- package/lib/SuiEvent.js +35 -0
- package/lib/SuiInBrowser.js +284 -0
- package/lib/SuiInBrowserAdapter.js +181 -0
- package/lib/SuiLocalTestValidator.js +93 -0
- package/lib/SuiMaster.js +199 -0
- package/lib/SuiMemoryObjectStorage.js +69 -0
- package/lib/SuiObject.js +211 -0
- package/lib/SuiPackage.js +520 -0
- package/lib/SuiPackageModule.js +314 -0
- package/lib/SuiPaginatedResponse.js +76 -0
- package/lib/SuiPseudoRandomAddress.js +35 -0
- package/lib/SuiTestScenario.js +161 -0
- package/lib/data/icons.json +8 -0
- package/package.json +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# suidouble
|
|
2
|
+
|
|
3
|
+
Set of provider, package and object classes for javascript representation of Sui's smart contracts. Use same code for publishing, upgrading, integration testing, interaction with smart contracts and integration in browser dapps. Very alpha for now.
|
|
4
|
+
|
|
5
|
+
- [Installation](#installation)
|
|
6
|
+
- [Usage](#usage)
|
|
7
|
+
- [Connecting](#connecting)
|
|
8
|
+
- [Attaching a package](#attaching-a-package)
|
|
9
|
+
- [Interacting with smart contract](#interacting-with-smart-contract)
|
|
10
|
+
- [SuiObject](#suiobject)
|
|
11
|
+
- [Fetching Events](#fetching-events)
|
|
12
|
+
- [Executing smart contract method](#executing-smart-contract-method)
|
|
13
|
+
- [Fetching objects](#fetching-objects)
|
|
14
|
+
- [Publishing the package](#publishing-the-package)
|
|
15
|
+
- [Upgrading the package](#upgrading-the-package)
|
|
16
|
+
- [Writing Sue Move intergration tests](#sui-move-integration-testing)
|
|
17
|
+
- [Connecting web3 dapps to Sui](#sui-move-connect-in-browser)
|
|
18
|
+
- [Todo](#todo)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### installation
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
npm install suidouble --save
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### usage
|
|
28
|
+
|
|
29
|
+
#### connecting
|
|
30
|
+
|
|
31
|
+
Main class to interact with blockchain is SuiMaster:
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
const { SuiMaster } = require('suidouble');
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
You can initialize it directly, if you have keypair, secret phrase and can use it in code (so on node.js side - server side or CLI apps):
|
|
38
|
+
```javascript
|
|
39
|
+
const suiMaster = new SuiMaster({
|
|
40
|
+
keypair: Ed25519Keypair,
|
|
41
|
+
debug: true, // echo testing messages to console
|
|
42
|
+
provider: 'test', // 'test', 'dev', 'local', 'main' or instance of this lib's SuiLocalTestValidator, or instance of Sui's JsonRpcProvider
|
|
43
|
+
});
|
|
44
|
+
const suiMaster = new SuiMaster({
|
|
45
|
+
debug: false,
|
|
46
|
+
phrase: 'thrive mean two thrive mean two thrive mean two thrive mean two', // secret phrase to generate keypair
|
|
47
|
+
provider: 'dev',
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Also, there's option to generate pseudo-random phrases and wallets from strings, works like a charm for testing:
|
|
52
|
+
```javascript
|
|
53
|
+
const suiMasterAsAdmin = new SuiMaster({ as: 'admin', provider: 'dev', });
|
|
54
|
+
const suiMasterAsUser = new SuiMaster({ as: 'user', provider: 'dev', });
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
On browser side, you'd probably want to use Sui wallets extensions adapters to sign message and don't store any keypairs or secret phrases in your code. So there's SuiInBrowser class for this, which can setup suiMaster instance for you. See 'Sui Move Connect in browser' section or sample UI application's code for more details.
|
|
58
|
+
```javascript
|
|
59
|
+
const { SuiInBrowser } = require('suidouble');
|
|
60
|
+
const suiInBrowser = SuiInBrowser.getSingleton(); // you probably don't want to keep few connections, so there's singleton
|
|
61
|
+
/// ...
|
|
62
|
+
suiInBrowser.addEventListener('connected', async()=>{
|
|
63
|
+
const connectedSuiMaster = await suiInBrowser.getSuiMaster(); // can post transactions now
|
|
64
|
+
console.log('read-write on', suiInBrowser.getCurrentChain(), 'as', suiMaster.address);
|
|
65
|
+
});
|
|
66
|
+
suiInBrowser.connect(adapter);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Take a look at more detailed [web3 connect code](#sui-move-connect-in-browser)
|
|
70
|
+
|
|
71
|
+
#### attaching a package
|
|
72
|
+
|
|
73
|
+
By default, suiMaster doesn't know of any smart contracts. There're 3 ways to attach one for interaction.
|
|
74
|
+
|
|
75
|
+
You can do it directly if you know contract's address (id). This is the option for browser apps and testing existing package:
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
const contract = suiMaster.addPackage({
|
|
79
|
+
id: '0x20cded4f9df05e37b44e3be2ffa9004dec77786950719fad6083694fdca45bf2',
|
|
80
|
+
});
|
|
81
|
+
await contract.isOnChain();
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
On node.js side, if you have Move's project with package code, you can attach it with path. This is the option for TDD and package publishing.
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
const contract = suiMaster.addPackage({
|
|
88
|
+
path: '../path_to_move_project_root/',
|
|
89
|
+
});
|
|
90
|
+
await contract.isOnChain();
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Yes, it can find it's address on chain, by comparing Move's module names with package you own on chain. Works ok if you want to test upgrading or something. Also, you can attach the package only by modules names. This will work in browser too (note: you have to own this package, its UpgradeCap):
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
const contract = suiMaster.addPackage({
|
|
97
|
+
modules: ['chat', 'anothermodulename'],
|
|
98
|
+
});
|
|
99
|
+
await contract.isOnChain();
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
#### interacting with smart contract
|
|
103
|
+
|
|
104
|
+
##### SuiObject
|
|
105
|
+
|
|
106
|
+
Everyhing in Sui is an object. So is in suidouble. SuiObject's instance class follows:
|
|
107
|
+
|
|
108
|
+
```javascript
|
|
109
|
+
suiObject.id; // '0x10cded4f9df05e37b44e3be2ffa9004dec77786950719fad6083694fdca45bf2' or something
|
|
110
|
+
suiObject.address; // very same, '0x10cded4f9df05e37b44e3be2ffa9004dec77786950719fad6083694fdca45bf2'
|
|
111
|
+
suiObject.isShared; // boolean. Is object shared (see Sui docs)
|
|
112
|
+
suiObject.isImmutable; // boolean. Is object immutable (see Sui docs)
|
|
113
|
+
suiObject.isDeleted; // marked as removed from blockchain in result of Sui Move contract method call
|
|
114
|
+
suiObject.type; // full type name, with package-module prefix, '0x20cded4f9df05e37b44e3be2ffa9004dec77786950719fad6083694fdca45bf2::chat::ChatResponse'
|
|
115
|
+
suiObject.typeName; // type name with no prefixes, eg 'ChatResponse'
|
|
116
|
+
suiObject.fields; // {}, object. Fields stored on blockchain
|
|
117
|
+
suiObject.display; // display object stored on blockchain
|
|
118
|
+
suiObject.localProperties; // {} object. Any local properties you want to attach to object. No interaction with blockchain. May be helpful to store some temp data
|
|
119
|
+
suiObject.isOwnedBy('0x10cded4f9df05e37b44e3be2ffa9004dec77786950719fad6083694fdca45bf2'); // is object owned by somebody or some object
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
@todo: better SuiObject documentation
|
|
123
|
+
|
|
124
|
+
##### fetching events
|
|
125
|
+
|
|
126
|
+
```javascript
|
|
127
|
+
const events = await contract.fetchEvents('modulename', {eventTypeName: 'ChatResponseCreated', order: 'descending'});
|
|
128
|
+
// events is instance of SuiPaginatedResponse. Data is stored in .data, has method to fetch next page - .nextPage();
|
|
129
|
+
while (events.hasNextPage) {
|
|
130
|
+
for (const event of events.data) {
|
|
131
|
+
// event is instance of SuiEvent
|
|
132
|
+
console.log('event', event.parsedJson); // data on blockchain
|
|
133
|
+
console.log('timestamp', event.timestampMs); // time event emited
|
|
134
|
+
}
|
|
135
|
+
await events.nextPage();
|
|
136
|
+
}
|
|
137
|
+
// const events = await contract.fetchEvents('modulename', {order: 'descending'}); // or all module events
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
##### executing smart contract method
|
|
141
|
+
|
|
142
|
+
```javascript
|
|
143
|
+
// executing method with parameters of (chat_shop: &ChatShop, metadata: vector<u8>, text: vector<u8>)
|
|
144
|
+
const res = await contract.moveCall('chat', 'post', ['0x10cded4f9df05e37b44e3be2ffa9004dec77786950719fad6083694fdca45bf2', [3,24,55], 'anotherparam']);
|
|
145
|
+
// or await contract.modules.chat.moveCall('methodname', ['somedata', [3,24,55], 'anotherparam']);
|
|
146
|
+
console.log(res);
|
|
147
|
+
for (const object of res.created) {
|
|
148
|
+
console.log('created', object.address, 'with type of', object.typeName); // instances of SuiObject (@todo: write documentation for it)
|
|
149
|
+
}
|
|
150
|
+
for (const object of res.mutated) {
|
|
151
|
+
console.log('mutated', object.address, 'with type of', object.typeName);
|
|
152
|
+
}
|
|
153
|
+
for (const object of res.deleted) {
|
|
154
|
+
console.log('deleted', object.address, 'with type of', object.typeName, object.isDeleted);
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
##### fetching objects
|
|
159
|
+
|
|
160
|
+
There's instance of SuiMemoryObjectStorage attached to every SuiMaster instance. Every smart contract method call adds created and mutated objects to it. You can also attach any object with it's address (id).
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
contract.modules.modulename.pushObject('0x10cded4f9df05e37b44e3be2ffa9004dec77786950719fad6083694fdca45bf2');
|
|
164
|
+
await contract.modules.modulename.fetchObjects(); // fetch objects fields etc
|
|
165
|
+
const object = contract.modules.modulename.objectStorage.byAddress('0x10cded4f9df05e37b44e3be2ffa9004dec77786950719fad6083694fdca45bf2');
|
|
166
|
+
```
|
|
167
|
+
@todo: move pushing/fetching to SuiMemoryObjectStorage directly, as there's nothing package or module related?
|
|
168
|
+
@todo: invalidation? No need to re-fetch all objects each time
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
### publishing the package
|
|
172
|
+
|
|
173
|
+
Builds a package and publish it to blockchain. CLI thing, as it needs `execSync` to run `sui move build`. Tested on Ubuntu, works good. If you have some issues with other platforms - please feel free to let me know or post Pull Request.
|
|
174
|
+
|
|
175
|
+
```javascript
|
|
176
|
+
const { SuiMaster } = require('suidouble');
|
|
177
|
+
|
|
178
|
+
const provider = 'dev';
|
|
179
|
+
const suiMaster = new SuiMaster({ debug: true, as: 'admin', provider: provider, });
|
|
180
|
+
|
|
181
|
+
await suiMaster.requestSuiFromFaucet();
|
|
182
|
+
await suiMaster.getBalance();
|
|
183
|
+
|
|
184
|
+
const package = suiMaster.addPackage({
|
|
185
|
+
path: '../path_to_move_project_root/',
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
await package.publish();
|
|
189
|
+
console.log('published as', package.address);
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### upgrading the package
|
|
193
|
+
|
|
194
|
+
Same, it's for CLI as it re-builds the package.
|
|
195
|
+
|
|
196
|
+
```javascript
|
|
197
|
+
const { SuiMaster } = require('suidouble');
|
|
198
|
+
|
|
199
|
+
const provider = 'local';// or await SuiLocalTestValidator.launch({debug: true});
|
|
200
|
+
|
|
201
|
+
const suiMaster = new SuiMaster({ debug: true, as: 'admin', provider: provider, });
|
|
202
|
+
await suiMaster.requestSuiFromFaucet();
|
|
203
|
+
await suiMaster.getBalance();
|
|
204
|
+
|
|
205
|
+
const package = suiMaster.addPackage({
|
|
206
|
+
path: '../path_to_move_project_root/',
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
if (!(await package.isOnChain())) { // suidouble tries to find package with needed modules in UpgradeCaps owned by you
|
|
210
|
+
await package.publish();
|
|
211
|
+
} else {
|
|
212
|
+
await package.upgrade();
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Sui Move Integration Testing
|
|
217
|
+
|
|
218
|
+
CLI integration tests, it runs local testing node (has to be installed), build and deploy a Move package into it and run unit tests over.
|
|
219
|
+
suidouble try to mimic Sui Move's testing framework:
|
|
220
|
+
|
|
221
|
+
```javascript
|
|
222
|
+
const SuiTestScenario = require('./lib/SuiTestScenario.js');
|
|
223
|
+
|
|
224
|
+
const testScenario = new SuiTestScenario({
|
|
225
|
+
path: '../path_to_move_project_root/',
|
|
226
|
+
debug: true,
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
await testScenario.begin('admin');
|
|
230
|
+
await testScenario.init();
|
|
231
|
+
|
|
232
|
+
await testScenario.nextTx('admin', async()=>{
|
|
233
|
+
const chatShop = testScenario.takeShared('ChatShop');
|
|
234
|
+
await testScenario.moveCall('chat', 'post', [chatShop.address, 'posting a message', 'metadata']);
|
|
235
|
+
const chatTopMessage = testScenario.takeShared('ChatTopMessage');
|
|
236
|
+
|
|
237
|
+
assert(chatTopMessage != null);
|
|
238
|
+
assert(chatTopMessage.id != null);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
await testScenario.nextTx('somebody', async()=>{
|
|
242
|
+
const chatTopMessage = testScenario.takeShared('ChatTopMessage');
|
|
243
|
+
await testScenario.moveCall('chat', 'reply', [chatTopMessage.address, 'posting a response', 'metadata']);
|
|
244
|
+
const chatResponse = testScenario.takeFromSender('ChatResponse');
|
|
245
|
+
|
|
246
|
+
assert(chatResponse != null);
|
|
247
|
+
assert(chatResponse.id != null);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
await testScenario.end();
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Sui Move Connect in browser
|
|
254
|
+
|
|
255
|
+
```javascript
|
|
256
|
+
const { SuiInBrowser } = require('suidouble');
|
|
257
|
+
|
|
258
|
+
const suiInBrowser = SuiInBrowser.getSingleton();
|
|
259
|
+
const suiMaster = await suiInBrowser.getSuiMaster(); // not yet connected, works in read-only mode (no signing-posting txs).
|
|
260
|
+
console.log('read-only on', suiInBrowser.getCurrentChain());
|
|
261
|
+
|
|
262
|
+
suiInBrowser.addEventListener('adapter', (adapter)=>{
|
|
263
|
+
console.log(adapter.name);
|
|
264
|
+
console.log(adapter.icon);
|
|
265
|
+
console.log(adapter.getDownloadURL());
|
|
266
|
+
|
|
267
|
+
if (adapter.name == 'Sui Wallet') {
|
|
268
|
+
suiInBrowser.connect(adapter);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
suiInBrowser.addEventListener('connected', async()=>{
|
|
273
|
+
const connectedSuiMaster = await suiInBrowser.getSuiMaster(); // can post transactions now
|
|
274
|
+
console.log('read-write on', suiInBrowser.getCurrentChain(), 'as', suiMaster.address);
|
|
275
|
+
|
|
276
|
+
const contract = connectedSuiMaster.addPackage({
|
|
277
|
+
id: '0x20cded4f9df05e37b44e3be2ffa9004dec77786950719fad6083694fdca45bf2',
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
await contract.isOnChain();
|
|
281
|
+
|
|
282
|
+
const events = await contract.fetchEvents('chat', {eventTypeName: 'ChatResponseCreated', order: 'descending'});
|
|
283
|
+
for (const event of events.data) {
|
|
284
|
+
// instances of SuiEvent (@todo: write documentation for it)
|
|
285
|
+
console.log('event', event.parsedJson);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const res = await contract.moveCall('chat', 'post', ['somedata', [3,24,55], 'anotherparam']);
|
|
289
|
+
console.log(res);
|
|
290
|
+
for (const object of res.created) {
|
|
291
|
+
console.log('created', object.address, 'with type of', object.typeName); // instances of SuiObject (@todo: write documentation for it)
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### Todo
|
|
298
|
+
|
|
299
|
+
- subscribe to events
|
|
300
|
+
- suiobject invalidation/fetching optimization
|
|
301
|
+
- better documentation
|
|
302
|
+
- unit tests
|
package/index.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
let doExecSync = null;
|
|
2
|
+
let doSpawn = null;
|
|
3
|
+
let doFs = null;
|
|
4
|
+
let doPath = null;
|
|
5
|
+
try {
|
|
6
|
+
let { execSync, spawn } = require('child_process');
|
|
7
|
+
doExecSync = execSync;
|
|
8
|
+
doSpawn = spawn;
|
|
9
|
+
|
|
10
|
+
const fs = require('fs');
|
|
11
|
+
const path = require('path');
|
|
12
|
+
|
|
13
|
+
doFs = fs;
|
|
14
|
+
doPath = path;
|
|
15
|
+
} catch (e) {}
|
|
16
|
+
|
|
17
|
+
class SuiCliCommands {
|
|
18
|
+
static async spawn(command, envVars = {}) {
|
|
19
|
+
if (!doSpawn) {
|
|
20
|
+
throw new Error('can not spawn a proccess in this env');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const process = doSpawn(command, [], {
|
|
24
|
+
env: {
|
|
25
|
+
...process.env,
|
|
26
|
+
...envVars,
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return process;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static async exec(command) {
|
|
34
|
+
if (!doExecSync) {
|
|
35
|
+
throw new Error('can not exec a proccess in this env');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return doExecSync(
|
|
39
|
+
command,
|
|
40
|
+
{ encoding: 'utf-8' },
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static async getModulesNamesFromPackagePath(path) {
|
|
45
|
+
if (!doPath || !doFs) {
|
|
46
|
+
throw new Error('can not access path in this env');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
const buildPathContent = await doFs.promises.readdir(path.join(this._path, 'build'));
|
|
51
|
+
|
|
52
|
+
// @todo: there may be some junk folders and we'd have to get project name from Move.toml ?
|
|
53
|
+
const buildPath = buildPathContent[0];
|
|
54
|
+
|
|
55
|
+
const dirents = await doFs.promises.readdir(doPath.join(this._path, 'build', buildPath, 'bytecode_modules'), { withFileTypes: true });
|
|
56
|
+
const names = dirents
|
|
57
|
+
.filter(dirent => dirent.isFile())
|
|
58
|
+
.map(dirent => dirent.name.split('.mv').join(''));
|
|
59
|
+
|
|
60
|
+
return names;
|
|
61
|
+
} catch (e) {
|
|
62
|
+
throw new Error('can not get modules names from local package path');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
module.exports = SuiCliCommands;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
|
|
2
|
+
class CustomEvent extends Event {
|
|
3
|
+
#detail;
|
|
4
|
+
|
|
5
|
+
constructor(type, options) {
|
|
6
|
+
super(type, options);
|
|
7
|
+
this.#detail = options?.detail ?? null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
get detail() {
|
|
11
|
+
return this.#detail;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
class SuiCommonMethods extends EventTarget {
|
|
16
|
+
constructor(params = {}) {
|
|
17
|
+
super();
|
|
18
|
+
|
|
19
|
+
this._debug = !!params.debug;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
log(...args) {
|
|
23
|
+
if (!this._debug) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let prefix = (this._suiMaster ? (''+this._suiMaster.instanceN+' |') : (this.instanceN ? ''+this.instanceN+' |' : '') );
|
|
28
|
+
// prefix += this.constructor.name+' | ';
|
|
29
|
+
|
|
30
|
+
args.unshift(this.constructor.name+' |');
|
|
31
|
+
args.unshift(prefix);
|
|
32
|
+
console.info.apply(null, args);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
emit(eventType, data) {
|
|
36
|
+
try {
|
|
37
|
+
this.dispatchEvent(new CustomEvent(eventType, { detail: data }));
|
|
38
|
+
} catch (e) {
|
|
39
|
+
console.error(e);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
module.exports = SuiCommonMethods;
|
package/lib/SuiEvent.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const SuiCommonMethods = require('./SuiCommonMethods.js');
|
|
2
|
+
|
|
3
|
+
class SuiEvent extends SuiCommonMethods {
|
|
4
|
+
constructor(params = {}) {
|
|
5
|
+
super(params);
|
|
6
|
+
|
|
7
|
+
this._suiMaster = params.suiMaster;
|
|
8
|
+
if (!this._suiMaster) {
|
|
9
|
+
throw new Error('suiMaster is requried for suiPackage');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
this._data = params.data || {};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get data() {
|
|
16
|
+
return this._data;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get parsedJson() {
|
|
20
|
+
if (this._data.parsedJson) {
|
|
21
|
+
return this._data.parsedJson;
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get timestampMs() {
|
|
27
|
+
if (this._data.timestampMs) {
|
|
28
|
+
return parseInt(this._data.timestampMs, 10);
|
|
29
|
+
} else {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
module.exports = SuiEvent;
|