Inspiration

In order to build contracts dynamically on the server a NodeJs wrapper was derived.

Possible use cases:

  • Construct contracts dynamically (e.g. with dynamic list of interfaces or features) and build in the cloud.
  • Allow building contracts on low-powered clients such as embedded devices/phones/apps etc without downloading the SDK
  • Various automation scenarios.

What it does

  • When the Docker image is started, a NodeJS server is started, exposed by default on port 8080.
  • The NodeJS server listens for POST requests to the /contract endpoint. The request schema is defined in the CompileRequest interface.
  • When a request is recieved, the server:
    • Creates a temporary folder
    • Initializes the build environment by constructing an artificial npm package and installing dependencies
    • Injects the users code into the folder
    • Builds the contract as WebAssembly, and then extracts it via the filesystem.
    • Returns the results to the user.

How we built it

  • Compiling Near Javascript/Typescript code into WebAssembly is a complex process that is likely to change in upcoming versions. Rather than re-implementing this variable process, it is more reasonable to wrap the CLI build process directly.
  • The CLI build process is dependent on system architecture and somewhat dependent on system libraries. A hypothetical NPM wrapper library is not likely to be portable. Therefore, this project opted to create a Docker image which exposes a compiler API service.
  • The project was done in cooperation with a contractor in the context of a freelance job (~50 hours). The private repository and all rights were transferred over and as of now it is publicly available under the MIT license.

Challenges we ran into

Near JS is still in Alpha status so it has some features still in development. For example, it turned out that in the latest version (0.6.0) compiler output was suppressed - this means the user would not get any compilation errors in the response. To overcome this a pull request was derived to add an enhancement to Near JS SDK to relay the output to the user: https://github.com/near/near-sdk-js/pull/303

Accomplishments that we're proud of

  • Dynamic version support
  • Arbitrary package dependency support
  • Versatile deployment scenarios through Docker/NodeJs
Share this project:

Updates