Deploy a Smart Contract

Kafka
Kafka
Apr 14, 2018 · 4 min read

Nebulas Wallet Part 6

"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;
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Paste entire smart contract into the code section
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

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