New Nebulas Smart Contract feature

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.

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