the NodeJS Oracle – Coinmonks – Medium

0 22


Some time ago I had to write an Ethereum based contract which acted as a trust. The owner of that trust was able to send Ether to that contract and the contract itself then transferred some Ether on the 15th of every month to the member’s wallets. The amount of Ether transferred to each member was based on the length of the membership and the current USD exchange rate.

Programming such a trust was doable but I despaired of the nature of Solidity too often. That’s why I decided to implement a smarter way of developing smart contracts in Solidity. And I’m going to show you how such a trust can be implemented in a smarter way — with less code, less stress and more autonomy of the contract itself.

First, look at the painful “how is it done today” code

Developing an Ethereum based contract can become quite time consuming due to the fact, that Ethereum’s own programming language Solidity is some kind strange and limited. That’s why most of the smart contracts written in Solidity are simple and the business logic is implemented outside.

Daniel Joszef wrote a nice post about it back in 2018:

Solidity became such a hit phenomenon specifically because it makes people believe they can code Ethereum. After all, a smart contract isn’t that complex (it cannot be too complex, given the platform limitations)… but then, this is coupled with a language structure that is far from obvious (did you even know Solidity had pointers, and even worse, pointer arithmetic?), and a million plus one ways to mess up.

Now let’s come back to the simple trust which I developed some time ago: there was an owner and some members of that trust. And you need to payout some money on the 15th of each month based on the current ETH to USD exchange rate.

The whole contract starts quite straight forward:

And in function NewMembership the first punch hits your developer’s liver: what is the current date? I just want to know the current year and the month but calculating these values requires me to search the internet for a solution. Luckily I found a great 216-lines-of-Solidity-code work done by Piper Merriam back in 2017.

Since Solidity itself cannot call the outside world (yes! There’s another world outside of the blockchain) you as a developer need to leave the Ethereum blockchain to get the ETH to USD exchange rate. An oracle is the agent that finds and verifies this information. Oraclize is the most used oracle by Solidity developers and the development is straight forward:

Summarizing:

  • We have 287 lines of Solidity code which pass the compiler — CHECK
  • We have spent lots & lots of time for testing with Truffle, Remix etc. — CHECK
  • We have a solution for dates — CHECK
  • We get the ETH to USD exchange rate — CHECK
  • We have spent 0.001923 ETH gas fee — CHECK
  • We have a smart contract which pays out some money on the 15th of each month — WAIT

The contract is unable to invoke itself on the 15th of each month. We need another outside-blockchain solution which must make sure to invoke the contract on every 15th… This all didn’t make me happy. That’s why I decided to find a smarter way of writing smart contracts in Solidity.

Introducing the smarter way of writing smart contracts in Solidity

I’m a huge fan of Ethereum. Solidity is OK — but not great. Implementing real-world business logic with Solidity is time consuming and sometimes impossible. I believe, that if there was more real business logic implemented in smart contracts then we all would tap the full potential of Ethereum as a crypto platform.

I love NodeJS and JavaScript. Whenever I wrote complicated Solidity code I asked myself “why the hell is it so complicated? In JavaScript it would be so easy…

For example finding out how many days it takes until the next 15th of a month:

Or calculating the duration of the trust membership, then quering Kraken regarding the current ETH to USD exchange rate and finally calculating the trust’s payout value with regards to the duration of the membership:

But that’s NodeJS code. You can’t use it in Solidity? No. Not unless there is a NodeJS oracle. There was none. Until I decided to implement one back in November 2018. I call it CaptainJS’ #ScriptIt and it allows you to integrate NodeJS code directly into Solidity, compile the whole code and store the business logic plus the data to the Ethereum blockchain.

The result is that combining NodeJS code and Solidity doesn’t only increase the potential of Solidity, it massively reduces your development and testing time.

Let’s write the same trust with Solidity and NodeJS code now. Such a contract must be usingCaptainJS . Then the whole magic can start:

The difference starts right at the constructor . Inside the constructor we will force the contract to callback itself on the next 15th of a month. So, once we deployed it it will automatically call itself. There will be no external invocation necessary. You can send Ether to the trust, add members to the trust (externally). But the trust will execute its business logic itself:

In function DaysUntilNext15th you embed the NodeJS code with a string . Then you invoke usingCaptainJS‘s Run method, transfer the code with a unique Id to identify your callback and inform the NodeJS container to load module moment.

When this JavaScript code got executed inside a NodeJS container the #ScriptIt oracle will invoke function CaptainsResult in case of no error. And the rest of the code flow is

  • if today is the 15th then invoke function Payout
  • otherwise call this contract back on the 15th of next month (with function CallbackOn15th

A callback request is invoked by calling RingShipsBell which results into an #ScriptIt callback of function RingRing.

Summarizing:

  • We have 82 lines of Solidity code (including NodeJS code) which pass the compiler — CHECK
  • We have spent some time testing NodeJS code locally before embedding it into Solidity and some short tests with Truffle, Remix etc. — CHECK
  • We have a solution for dates — CHECK
  • We get the ETH to USD exchange rate — CHECK
  • We have spent 0.001462 ETH gas fee — CHECK
  • We have a smart contract which pays out some money on the 15th of each month — CHECK

You might also like

Pin It on Pinterest

Share This

Share this post with your friends!

WhatsApp chat