suidouble 0.0.15 → 0.0.16
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 +1 -0
- package/lib/SuiCliCommands.js +32 -6
- package/lib/SuiInBrowser.js +1 -1
- package/lib/SuiLocalTestValidator.js +41 -2
- package/lib/SuiMaster.js +10 -2
- package/package.json +1 -1
- package/test/sui_in_browser.test.js +65 -0
- package/test/sui_master_onlocal.test.js +2 -2
package/README.md
CHANGED
|
@@ -17,6 +17,7 @@ Set of provider, package and object classes for javascript representation of Sui
|
|
|
17
17
|
- [Connecting web3 dapps to Sui](#sui-move-connect-in-browser)
|
|
18
18
|
- [Todo](#todo)
|
|
19
19
|
|
|
20
|
+
Also take a look at sample Vue dapp application [source code](https://github.com/suidouble/suidouble-sample-app) or [check it online](https://suidouble-sample-app.herokuapp.com/).
|
|
20
21
|
|
|
21
22
|
### installation
|
|
22
23
|
|
package/lib/SuiCliCommands.js
CHANGED
|
@@ -20,14 +20,40 @@ class SuiCliCommands {
|
|
|
20
20
|
throw new Error('can not spawn a proccess in this env');
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
return await new Promise((res,rej)=>{
|
|
24
|
+
let success = true;
|
|
25
|
+
let e = null;
|
|
26
|
+
const proc = doSpawn(command, [], {
|
|
27
|
+
env: {
|
|
28
|
+
...process.env,
|
|
29
|
+
...envVars,
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
proc.on('error', function(err) {
|
|
33
|
+
success = false;
|
|
34
|
+
e = err;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
setTimeout(()=>{
|
|
38
|
+
if (success) {
|
|
39
|
+
res(proc);
|
|
40
|
+
} else {
|
|
41
|
+
rej(e);
|
|
42
|
+
}
|
|
43
|
+
}, 100);
|
|
28
44
|
});
|
|
29
45
|
|
|
30
|
-
|
|
46
|
+
// const proc = doSpawn(command, [], {
|
|
47
|
+
// env: {
|
|
48
|
+
// ...process.env,
|
|
49
|
+
// ...envVars,
|
|
50
|
+
// }
|
|
51
|
+
// });
|
|
52
|
+
// proc.on('error', function(err) {
|
|
53
|
+
// console.log('Oh noez, teh errurz: ' + err);
|
|
54
|
+
// });
|
|
55
|
+
|
|
56
|
+
// return proc;
|
|
31
57
|
}
|
|
32
58
|
|
|
33
59
|
static async exec(command) {
|
package/lib/SuiInBrowser.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// const { spawn } = require('child_process');
|
|
2
2
|
const SuiCliCommands = require('./SuiCliCommands.js');
|
|
3
3
|
const SuiCommonMethods = require('./SuiCommonMethods.js');
|
|
4
|
+
const { JsonRpcProvider, localnetConnection, devnetConnection } = require('@mysten/sui.js');
|
|
4
5
|
|
|
5
6
|
class SuiLocalTestValidator extends SuiCommonMethods {
|
|
6
7
|
constructor(params = {}) {
|
|
@@ -8,6 +9,28 @@ class SuiLocalTestValidator extends SuiCommonMethods {
|
|
|
8
9
|
|
|
9
10
|
this._child = null;
|
|
10
11
|
this._active = false;
|
|
12
|
+
|
|
13
|
+
this._testFallbackEnabled = false;
|
|
14
|
+
if (params.testFallbackEnabled) {
|
|
15
|
+
// option for unit tests to fallback to sui:dev network in case
|
|
16
|
+
// there is no local validator installed
|
|
17
|
+
this._testFallbackEnabled = true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
this._providerName = 'sui:localnet';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get providerName() {
|
|
24
|
+
return this._providerName;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get provider() {
|
|
28
|
+
if (this._providerName === 'sui:localnet') {
|
|
29
|
+
return new JsonRpcProvider(localnetConnection);
|
|
30
|
+
} else if (this._providerName === 'sui:devnet') {
|
|
31
|
+
// if testFallbackEnabled == true and we can't start local node
|
|
32
|
+
return new JsonRpcProvider(devnetConnection);
|
|
33
|
+
}
|
|
11
34
|
}
|
|
12
35
|
|
|
13
36
|
get active() {
|
|
@@ -30,13 +53,28 @@ class SuiLocalTestValidator extends SuiCommonMethods {
|
|
|
30
53
|
}
|
|
31
54
|
|
|
32
55
|
async launch() {
|
|
33
|
-
if (this.
|
|
56
|
+
if (this._active) {
|
|
34
57
|
return this;
|
|
35
58
|
}
|
|
36
59
|
|
|
37
60
|
this.log('launching sui-test-validator ...');
|
|
38
61
|
|
|
39
|
-
|
|
62
|
+
try {
|
|
63
|
+
this._child = await SuiCliCommands.spawn('sui-test-validator', { RUST_LOG: 'consensus=off' });
|
|
64
|
+
} catch (e) {
|
|
65
|
+
if (this._testFallbackEnabled) {
|
|
66
|
+
// can't start local node. Let's switch to sui:dev
|
|
67
|
+
this.log('can not start local node. Fallback to sui:dev...');
|
|
68
|
+
|
|
69
|
+
this._child = null;
|
|
70
|
+
this._active = true;
|
|
71
|
+
this._providerName = 'sui:devnet';
|
|
72
|
+
|
|
73
|
+
return this;
|
|
74
|
+
} else {
|
|
75
|
+
throw e;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
40
78
|
|
|
41
79
|
this.__readyLaunchedPromiseResolver = null;
|
|
42
80
|
this.__readyLaunchedPromise = new Promise((res)=>{
|
|
@@ -67,6 +105,7 @@ class SuiLocalTestValidator extends SuiCommonMethods {
|
|
|
67
105
|
});
|
|
68
106
|
|
|
69
107
|
process.on('exit', ()=>{
|
|
108
|
+
console.log('this._testFallbackEnabled', this._testFallbackEnabled);
|
|
70
109
|
if (this._child) {
|
|
71
110
|
this._child.kill();
|
|
72
111
|
}
|
package/lib/SuiMaster.js
CHANGED
|
@@ -34,8 +34,16 @@ class SuiMaster extends SuiCommonMethods {
|
|
|
34
34
|
this._providerName = null;
|
|
35
35
|
if (params.provider) {
|
|
36
36
|
if (params.provider == 'local' || (params.provider.constructor && params.provider.constructor.name && params.provider.constructor.name == 'SuiLocalTestValidator')) {
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
if (params.provider == 'local') {
|
|
38
|
+
this._provider = new sui.JsonRpcProvider(sui.localnetConnection);
|
|
39
|
+
this._providerName = 'sui:localnet';
|
|
40
|
+
} else {
|
|
41
|
+
// SuiLocalTestValidator
|
|
42
|
+
this._providerName = params.provider.providerName;
|
|
43
|
+
this._provider = params.provider.provider;
|
|
44
|
+
}
|
|
45
|
+
// this._provider = new sui.JsonRpcProvider(sui.localnetConnection);
|
|
46
|
+
// this._providerName = 'sui:localnet';
|
|
39
47
|
} else if (params.provider == 'test' || params.provider == 'testnet') {
|
|
40
48
|
this._provider = new sui.JsonRpcProvider(sui.testnetConnection);
|
|
41
49
|
this._providerName = 'sui:testnet';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "suidouble",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"description": "Set of provider, package and object classes for javascript representation of Sui Move smart contracts. Use same code for publishing, upgrading, integration testing, interaction with smart contracts and integration in browser web3 dapps",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
// just a basic test of InBrowser classes. No real interaction, just structure and events
|
|
4
|
+
|
|
5
|
+
const t = require('tap');
|
|
6
|
+
const { test } = t;
|
|
7
|
+
|
|
8
|
+
const { SuiInBrowser } = require('..');
|
|
9
|
+
|
|
10
|
+
test('as single instance', async t => {
|
|
11
|
+
// probably you'd want a single instance of SuiInBrowser class on your dapp,
|
|
12
|
+
// so initialize it via class' static method:
|
|
13
|
+
const suiInBrowser = SuiInBrowser.getSingleton();
|
|
14
|
+
const suiInBrowserCopy = SuiInBrowser.getSingleton();
|
|
15
|
+
|
|
16
|
+
t.equal(suiInBrowser, suiInBrowserCopy);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('initialization', async t => {
|
|
20
|
+
const suiInBrowser = new SuiInBrowser({});
|
|
21
|
+
|
|
22
|
+
t.ok(suiInBrowser);
|
|
23
|
+
|
|
24
|
+
const gotAdapters = [];
|
|
25
|
+
// it should emit 'adapter' events, even though they are not installed (remember, we are in node.js now)
|
|
26
|
+
// adapter has propery of .isInstalled
|
|
27
|
+
suiInBrowser.addEventListener('adapter', (e)=>{
|
|
28
|
+
gotAdapters.push(e.detail);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// emit is not instant, but on *nextTick*
|
|
32
|
+
await new Promise((res)=>setTimeout(res, 100));
|
|
33
|
+
|
|
34
|
+
t.ok(gotAdapters.length > 0);
|
|
35
|
+
|
|
36
|
+
const suiWalletAdapter = gotAdapters.find((adapter)=>(adapter.name == 'Sui Wallet')); // there're few, but lets test one
|
|
37
|
+
|
|
38
|
+
t.ok(suiWalletAdapter);
|
|
39
|
+
|
|
40
|
+
t.equal(suiWalletAdapter.name, 'Sui Wallet');
|
|
41
|
+
t.ok(!suiWalletAdapter.isInstalled);
|
|
42
|
+
t.ok(suiWalletAdapter.icon);
|
|
43
|
+
t.ok(suiWalletAdapter.icon.indexOf('data:image/') != -1); // icon is data-url
|
|
44
|
+
|
|
45
|
+
t.ok(suiWalletAdapter.getDownloadURL()); // url to install extension
|
|
46
|
+
t.ok(suiWalletAdapter.getDownloadURL().indexOf('https://') != -1);
|
|
47
|
+
|
|
48
|
+
// we can get instance of suiMaster out of suiInBrowser even if we are not connected to wallet
|
|
49
|
+
// it will work without signer, but you can read data from chain
|
|
50
|
+
const suiMaster = await suiInBrowser.getSuiMaster();
|
|
51
|
+
|
|
52
|
+
t.ok(suiMaster);
|
|
53
|
+
t.ok(!suiMaster.connectedAddress); // nothing
|
|
54
|
+
t.ok(suiMaster.connectedChain); // but there's chain
|
|
55
|
+
|
|
56
|
+
// by default, SuiInBrowser gets you devnet connection (it's overloaded by Wallet Extension current chain)
|
|
57
|
+
t.equal(suiMaster.connectedChain, 'sui:devnet');
|
|
58
|
+
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('initialization via mainnet', async t => {
|
|
62
|
+
const suiInBrowser = new SuiInBrowser({defaultChain: 'sui:mainnet'});
|
|
63
|
+
const suiMaster = await suiInBrowser.getSuiMaster();
|
|
64
|
+
t.equal(suiMaster.connectedChain, 'sui:mainnet');
|
|
65
|
+
});
|
|
@@ -16,7 +16,7 @@ let contractAddressV2 = null;
|
|
|
16
16
|
let chatShopObjectId = null;
|
|
17
17
|
|
|
18
18
|
test('spawn local test node', async t => {
|
|
19
|
-
suiLocalTestValidator = await SuiLocalTestValidator.launch();
|
|
19
|
+
suiLocalTestValidator = await SuiLocalTestValidator.launch({ testFallbackEnabled: true });
|
|
20
20
|
t.ok(suiLocalTestValidator.active);
|
|
21
21
|
|
|
22
22
|
// SuiLocalTestValidator runs as signle instance. So you can't start it twice with static method
|
|
@@ -25,7 +25,7 @@ test('spawn local test node', async t => {
|
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
test('init suiMaster and connect it to local test validator', async t => {
|
|
28
|
-
suiMaster = new SuiMaster({provider: suiLocalTestValidator, as: 'somebody'});
|
|
28
|
+
suiMaster = new SuiMaster({provider: suiLocalTestValidator, as: 'somebody', debug: false});
|
|
29
29
|
await suiMaster.initialize();
|
|
30
30
|
|
|
31
31
|
t.ok(suiMaster.address); // there should be some address
|