Deploy a Smart Contract

Kafka
Nebulasio
Published in
4 min readApr 14, 2018

Nebulas Wallet Part 6

Watch the YouTube Tutorial.

Now we were going to learn about deploying a smart contract to Nebulas. First things first, we need a smart contract so I will use a example smart contract from Nebulas.

Example Smart Contract ( below )

"use strict";

var DepositeContent = function (text) {
if (text) {
var o = JSON.parse(text);
this.balance = new BigNumber(o.balance);
this.expiryHeight = new BigNumber(o.expiryHeight);
} else {
this.balance = new BigNumber(0);
this.expiryHeight = new BigNumber(0);
}
};

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

var BankVaultContract = function () {
LocalContractStorage.defineMapProperty(this, "bankVault", {
parse: function (text) {
return new DepositeContent(text);
},
stringify: function (o) {
return o.toString();
}
});
};

// save value to contract, only after height of block, users can takeout
BankVaultContract.prototype = {
init: function () {
//TODO:
},

save: function (height) {
var from = Blockchain.transaction.from;
var value = Blockchain.transaction.value;
var bk_height = new BigNumber(Blockchain.block.height);

var orig_deposit = this.bankVault.get(from);
if (orig_deposit) {
value = value.plus(orig_deposit.balance);
}

var deposit = new DepositeContent();
deposit.balance = value;
deposit.expiryHeight = bk_height.plus(height);

this.bankVault.put(from, deposit);
},

takeout: function (value) {
var from = Blockchain.transaction.from;
var bk_height = new BigNumber(Blockchain.block.height);
var amount = new BigNumber(value);

var deposit = this.bankVault.get(from);
if (!deposit) {
throw new Error("No deposit before.");
}

if (bk_height.lt(deposit.expiryHeight)) {
throw new Error("Can not takeout before expiryHeight.");
}

if (amount.gt(deposit.balance)) {
throw new Error("Insufficient balance.");
}

var result = Blockchain.transfer(from, amount);
if (!result) {
throw new Error("transfer failed.");
}
Event.Trigger("BankVault", {
Transfer: {
from: Blockchain.transaction.to,
to: from,
value: amount.toString()
}
});

deposit.balance = deposit.balance.sub(amount);
this.bankVault.put(from, deposit);
},

balanceOf: function () {
var from = Blockchain.transaction.from;
return this.bankVault.get(from);
},

verifyAddress: function (address) {
// 1-valid, 0-invalid
var result = Blockchain.verifyAddress(address);
return {
valid: result == 0 ? false : true
};
}
};

module.exports = BankVaultContract;

Don’t be scared it’s just a smart contract.

Let’s begin

Open up the index.html

Navigate to the CONTRACT tab and find the DEPLOY tab (Example below)

Now we need to copy the example smart contract above into the code : section

Paste entire smart contract into the code section

NOTE: The entire smart contract must be copied for this to work.

Next make sure the Programming Language is set to JavaScript and for this example leave Arguments field empty.

This example smart contract is written with the Javascript language. Learn more about smart contracts from the wiki

Now select your KeyFile by Clicking SELECT WALLET FILE… Then Click Unlock. This is what you will see after:

Now were going to Click TEST. This will check the contract for errors.

The test result read that there is an insufficient balance. The reason for this is there is no NAS in the account so it’s expected to receive this error. This is a great feature because we can test our contract for error before sending it to the mainnet.

Let’s continue by Clicking the Submit button

We receive a TX Hash and a Contract Address. We should save the TxHash and Contract Address so we can look up the transaction.

Let’s search for our contract.

Scroll up to the top of the page and Click the Search tab

Now enter in the TxHash we saved and Click the Search Button

Now we should see the original smart contract below

Piece of cake. Right!

We learned how Deploy a Smart Contract on Nebulas and Search for the Contract.

Next tutorial we be learning how to Call a Contract

--

--

Kafka
Nebulasio

“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.