Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

suidouble

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

suidouble - npm Package Compare versions

Comparing version 0.0.17 to 0.0.18

6

lib/SuiCommonMethods.js

@@ -37,3 +37,7 @@

try {
this.dispatchEvent(new CustomEvent(eventType, { detail: data }));
if (data.isSuiEvent) {
this.dispatchEvent(data);
} else {
this.dispatchEvent(new CustomEvent(eventType, { detail: data }));
}
} catch (e) {

@@ -40,0 +44,0 @@ console.error(e);

const SuiCommonMethods = require('./SuiCommonMethods.js');
class SuiEvent extends SuiCommonMethods {
class SuiEvent extends Event {
constructor(params = {}) {
super(params);
const typeName = params.data ? (params.data.type.split('::').pop()) : null;
super(typeName, {});
this._debug = !!params.debug;
this._suiMaster = params.suiMaster;

@@ -13,4 +15,23 @@ if (!this._suiMaster) {

this._data = params.data || {};
this.detail = this; // quick backward support as this is the instance of CustomEvent
}
log(...args) {
if (!this._debug) {
return;
}
let prefix = (this._suiMaster ? (''+this._suiMaster.instanceN+' |') : (this.instanceN ? ''+this.instanceN+' |' : '') );
// prefix += this.constructor.name+' | ';
args.unshift(this.constructor.name+' |');
args.unshift(prefix);
console.info.apply(null, args);
}
get isSuiEvent() {
return true;
}
/**

@@ -17,0 +38,0 @@ * In module type name, without package and module prefix

1

lib/SuiLocalTestValidator.js

@@ -108,3 +108,2 @@ // const { spawn } = require('child_process');

process.on('exit', ()=>{
console.log('this._testFallbackEnabled', this._testFallbackEnabled);
if (this._child) {

@@ -111,0 +110,0 @@ this._child.kill();

@@ -6,2 +6,3 @@ const sui = require('@mysten/sui.js');

const SuiPaginatedResponse = require('./SuiPaginatedResponse.js');
const SuiEvent = require('./SuiEvent.js');
// fromB64, toB64

@@ -32,4 +33,41 @@

this._normalizedMoveModule = {};
this._unsubscribeFunction = null;
}
async subscribeEvents() {
this.log('subscribing to events of module', this._moduleName);
// we need very first package version's id here. So we are getting it from normalized data
const normalizedPackageAddress = await this.getNormalizedPackageAddress();
const onMessage = (rawEvent) => {
const suiEvent = new SuiEvent({
suiMaster: this._suiMaster,
debug: this._debug,
data: rawEvent,
});
const eventTypeName = suiEvent.typeName;
this.log('got event', eventTypeName);
this.emit(eventTypeName, suiEvent);
};
this._unsubscribeFunction = await this._suiMaster._provider.subscribeEvent({
filter: {"MoveModule": {"package": normalizedPackageAddress, "module": this._moduleName} },
onMessage: onMessage,
});
}
async unsubscribeEvents() {
if (this._unsubscribeFunction) {
await this._unsubscribeFunction();
this._unsubscribeFunction = null;
return true;
}
return false;
}
get objectStorage() {

@@ -186,4 +224,11 @@ return this._suiMaster.objectStorage;

const eventData = event.parsedJson;
this.emit(eventTypeName, eventData);
// const eventData = event.parsedJson;
const suiEvent = new SuiEvent({
suiMaster: this._suiMaster,
debug: this._debug,
data: event,
});
this.emit(eventTypeName, suiEvent);
}

@@ -190,0 +235,0 @@ }

{
"name": "suidouble",
"version": "0.0.17",
"version": "0.0.18",
"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",

@@ -24,3 +24,3 @@ "main": "index.js",

"dependencies": {
"@mysten/sui.js": "^0.34.0",
"@mysten/sui.js": "^0.35.1",
"@wallet-standard/core": "^1.0.3"

@@ -27,0 +27,0 @@ },

@@ -12,2 +12,3 @@ # suidouble

- [Fetching Events](#fetching-events)
- [Subscribe to Events](#subscribing-to-events)
- [Executing smart contract method](#executing-smart-contract-method)

@@ -142,2 +143,31 @@ - [Fetching objects](#fetching-objects)

##### subscribing to events
You can subscribe to Sui's contract events on package's module level. No types-etc filters for now ( @todo? )
```javascript
const module = await contract.getModule('suidouble_chat');
await module.subscribeEvents();
module.addEventListener('ChatResponseCreated', (suiEvent)=>{
// received message emited by
// emit(ChatResponseCreated { id: object::uid_to_inner(&chat_response_id), top_message_id: object::uid_to_inner(&id), seq_n: 0 });
// in suidouble_chat 's smart contract
console.log(suiEvent.typeName); // == 'ChatResponseCreated'
console.log(suiEvent.parsedJson);
});
module.addEventListener('ChatTopMessageCreated', (suiEvent)=>{
// received message emited by
// emit(ChatTopMessageCreated { id: object::uid_to_inner(&id), top_response_id: object::uid_to_inner(&chat_response_id), });
// in suidouble_chat 's smart contract
console.log(suiEvent.typeName); // == 'ChatTopMessageCreated'
console.log(suiEvent.parsedJson);
});
```
Don't forget to unsubscribe from events when you don't need them anymore:
```javascript
await module.unsubscribeEvents();
```
##### executing smart contract method

@@ -144,0 +174,0 @@

@@ -28,3 +28,3 @@ 'use strict'

test('init suiMaster and connect it to local test validator', async t => {
suiMaster = new SuiMaster({provider: suiLocalTestValidator, as: 'somebody', debug: false});
suiMaster = new SuiMaster({provider: suiLocalTestValidator, as: 'somebody', debug: true});
await suiMaster.initialize();

@@ -154,2 +154,30 @@

test('subscribe to module events', async t => {
const module = await contract.getModule('suidouble_chat');
await module.subscribeEvents();
let gotEventChatTopMessageCreated = false;
let gotEventChatResponseCreated = false;
module.addEventListener('ChatTopMessageCreated', (event)=>{
gotEventChatTopMessageCreated = event;
});
module.addEventListener('ChatResponseCreated', (event)=>{
gotEventChatResponseCreated = event.detail; // .detail is reference to event itself. To support CustomEvent pattern
});
await contract.moveCall('suidouble_chat', 'post', [chatShopObjectId, 'the message', 'metadata']);
await new Promise((res)=>setTimeout(res, 300)); // got events without timeout, but just to be sure.
t.ok(gotEventChatTopMessageCreated);
t.ok(gotEventChatResponseCreated);
// just some checks that events have data by contract's architecture
t.ok(gotEventChatResponseCreated.parsedJson.top_message_id == gotEventChatTopMessageCreated.parsedJson.id);
t.ok(gotEventChatTopMessageCreated.parsedJson.top_response_id == gotEventChatResponseCreated.parsedJson.id);
// unsubscribing from events, to close websocket
await module.unsubscribeEvents();
});
test('execute contract methods', async t => {

@@ -295,3 +323,3 @@ const moveCallResult = await contract.moveCall('suidouble_chat', 'post', [chatShopObjectId, 'the message', 'metadata']);

test('stops local test node', async t => {
SuiLocalTestValidator.stop();
await SuiLocalTestValidator.stop();
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc