Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 2.2.0 #65

Merged
merged 21 commits into from May 19, 2023
Merged

Release 2.2.0 #65

merged 21 commits into from May 19, 2023

Conversation

evgenykuzyakov
Copy link
Contributor

2.2.0

  • BETA: Introduce VM.require. This is a new API that allows to retrieve a module source from the Social DB, execute it and return the results.

Note, this should be considered a beta feature and the API and the functionality might change.

VM.require(src) takes a src the path for the source code of the module.
The src argument may contain the source version by including the blockHeight. E.g. VM.require("mob.near/widget/Module.Abc@91698491") to get the source code at block height 91698491.

Module example:

function Magic(props) {
  return <div>Magic: {props.children}</div>;
}

return { Magic };

Widget example:

const { Magic } = VM.require("mob.near/widget/Module.Magic");

return <Magic>Hello World</Magic>;
  • Add WebSocket object and support native events for function arguments (needed to get data). Websockets are automatically closed when a VM instance is stopped.

Evgeny Kuzyakov and others added 21 commits May 2, 2023 10:26
Add `WebSocket` object and support native events for function arguments (needed to get `data`). Websockets are automatically closed when a VM instance is stopped.

Example using WebSocket for the FT transfer events. It handles reconnects.

```jsx
if (state.ws === undefined) {
  const eventsFilter = {
    status: "SUCCESS",
    event: {
      event: "ft_transfer",
    },
  };

  function startWebSocket(processEvents) {
    let ws = State.get().ws;

    if (ws) {
      ws.close();
      return;
    }

    ws = new WebSocket("wss://events.near.stream/ws");

    ws.onopen = () => {
      console.log(`Connection to WS has been established`);
      ws.send(
        JSON.stringify({
          secret: "near-social-events",
          filter: eventsFilter,
          fetch_past_events: 10,
        })
      );
    };
    ws.onclose = () => {
      State.update({ ws: null });
      console.log(`WS Connection has been closed`);
      State.get().startWebSocket(processEvents);
    };
    ws.onmessage = (e) => {
      const data = JSON.parse(e.data);
      processEvents(data.events);
    };
    ws.onerror = (err) => {
      State.update({ ws: null });
      console.log("WebSocket error", err);
    };

    State.update({ ws });
  }

  function processEvent(event) {
    return {
      time: new Date(parseFloat(event.block_timestamp) / 1e6),
      event: event.event,
      accountId: event.account_id,
      predecessorId: event.predecessor_id,
    };
  }

  function processEvents(events) {
    events = events.flatMap(processEvent);
    events.reverse();

    State.update((state) => {
      const prevActions = state.actions || [];
      state.actions = [
        ...events.filter(
          (event) =>
            prevActions.length === 0 ||
            event.time.getTime() > prevActions[0].time.getTime()
        ),
        ...prevActions,
      ].slice(0, 10);
      return state;
    });
  }

  State.init({
    startWebSocket,
  });
  state.startWebSocket(processEvents);
}

return state.actions;
```

**Note, this PR is based on top of a fix for the legacy function call creation.** https://github.com/NearSocial/VM/tree/fix/legacy-function-call-creator 

Fixes #35
chore: switch to pagoda's  higher capcity public archival node
_BETA_: Introduce `VM.require`. This is a new API that allows to retrieve a module source from the Social DB, execute it and return the results.

Note, this should be considered a beta feature and the API and the functionality might change.

`VM.require(src)` takes a `src` the path for the source code of the module.
The `src` argument may contain the source version by including the `blockHeight`. E.g. `VM.require("mob.near/widget/Module.Abc@91698491")` to get the source code at block height `91698491`.

Module example:
```jsx
function Magic(props) {
  return <div>Magic: {props.children}</div>;
}

return { Magic };
```

Widget example:
```jsx
const { Magic } = VM.require("mob.near/widget/Module.Magic");

return <Magic>Hello World</Magic>;
```
Copy link
Contributor

@calebjacob calebjacob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The require feature looks great! Excited to use it.

@evgenykuzyakov evgenykuzyakov merged commit bf9fd1d into master May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants