New Nebulas Smart Contract feature

Kafka
Kafka
Aug 15, 2018 · 2 min read

Imagine being able to call a contract from a contract. Will here we are today. Today we can do just that. So think about this. We can use a function in contract one to call a function in contract two. Nebulas calls this Inter-contract calls.

What does this mean?

It means we could call a function in another contract from an external contract.

Below is the Main Smart Contract we will use for the explanation.

"use strict";

var item = function(text) {
if (text) {
var obj = JSON.parse(text);
this.key = obj.key;
this.value = obj.value;
this.author = obj.text;
} else {
this.key = "";
this.author = "";
this.value = "";
}
};

item.prototype = {
toString: function () {
return JSON.stringify(this);
}
};

var mainContract = function () {
LocalContractStorage.defineMapProperty(this, "repo", {
parse: function (text) {
return new item(text);
},
stringify: function (o) {
return o.toString();
}
});
};

mainContract.prototype = {
init: function () {
// todo
},

save: function (key, value) {
console.log("reach child contract");

key = key.trim();
value = value.trim();
if (key === "" || value === ""){
throw new Error("empty key / value");
}
if (value.length > 128 || key.length > 128){
throw new Error("key / value exceed limit length")
}

var from = Blockchain.transaction.from;
var item = this.repo.get(key);

if (item){
throw new Error("value has been taken");
}

item = new item();
item.author = from;
item.key = key;
item.value = value;
this.repo.put(key, item);
},

get: function (key) {
key = key.trim();
if ( key === "" ) {
throw new Error("empty key")
}
return this.repo.get(key);
},

throwErr: function() {
throw("err for test");
}
};
module.exports = mainContract;

Below is the secondary smart contract. This contract will call the functions of mainContract: SAVE and Get.

Lets break down this feature a bit.

The save call will interact with the mainContract and it will commence the save function on the mainContract.

Image for post
Image for post

Currently, this is only on the Nebulas TestNet but will be implemented on the MainNet in the coming weeks.

Here a link to the wiki of Nebulas if you want to check it out. https://github.com/nebulasio/wiki/blob/master/smart_contract.md

Nebulasio

Nebulas is an autonomous metanet.

Kafka

Written by

Kafka

“Genius” is 1% inspiration and 99% perspiration. Accordingly, a ‘genius’ is often merely a talented person who has done all of his homework — T.E.

Nebulasio

Nebulasio

Nebulas is an autonomous metanet. The future of collaboration is coming.

Kafka

Written by

Kafka

“Genius” is 1% inspiration and 99% perspiration. Accordingly, a ‘genius’ is often merely a talented person who has done all of his homework — T.E.

Nebulasio

Nebulasio

Nebulas is an autonomous metanet. The future of collaboration is coming.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store