diff --git a/README.md b/README.md index 18b7fee02..922471552 100644 --- a/README.md +++ b/README.md @@ -278,55 +278,48 @@ Now you can create & deploy & call smart contracts directly over HTTP/console ju If you want to create & deploy a smart contracts: ``` -'use strict'; +"use strict"; -var BankVaultContract = function () { - LocalContractStorage.defineMapProperty(this, "bankVault"); +var BankVaultContract = function() { + LocalContractStorage.defineMapProperty(this, "bankVault"); }; // save value to contract, only after height of block, users can takeout BankVaultContract.prototype = { - init: function() { - //TODO: - }, - save: function(height) { - var deposit = this.bankVault.get(Blockchain.transaction.from); - if (deposit != null) { - deposit = JSON.parse(deposit) - } - var value = new BigNumber(Blockchain.transaction.value); - if (deposit != null && deposit.balance.length > 0) { - var balance = new BigNumber(deposit.balance); - value = value.plus(balance); - } - var content = { - balance: value.toString(), - height: Blockchain.block.height + height - } - this.bankVault.put(Blockchain.transaction.from, JSON.stringify(content)) - }, - takeout: function(amount) { - var deposit = this.bankVault.get(Blockchain.transaction.from); - if (deposit == null) { - return 0; - } else { - deposit = JSON.parse(deposit) - } - if (Blockchain.block.height < deposit.height) { - return 0; - } - var balance = new BigNumber(deposit.balance); - var value = new BigNumber(amount); - if (balance.lessThan(value)) { - return 0; - } - var result = Blockchain.transfer(Blockchain.transaction.from, value); - if (result > 0) { - deposit.balance = balance.dividedBy(value).toString() - this.bankVault.put(Blockchain.transaction.from, JSON.stringify(deposit)) - } - return result - } + init:function() {}, + save:function(height) { + var deposit = this.bankVault.get(Blockchain.transaction.from); + var value = new BigNumber(Blockchain.transaction.value); + if (deposit != null && deposit.balance.length > 0) { + var balance = new BigNumber(deposit.balance); + value = value.plus(balance); + } + var content = { + balance:value.toString(), + height:Blockchain.block.height + height + }; + this.bankVault.put(Blockchain.transaction.from, content); + }, + takeout:function(amount) { + var deposit = this.bankVault.get(Blockchain.transaction.from); + if (deposit == null) { + return 0; + } + if (Blockchain.block.height < deposit.height) { + return 0; + } + var balance = new BigNumber(deposit.balance); + var value = new BigNumber(amount); + if (balance.lessThan(value)) { + return 0; + } + var result = Blockchain.transfer(Blockchain.transaction.from, value); + if (result > 0) { + deposit.balance = balance.dividedBy(value).toString(); + this.bankVault.put(Blockchain.transaction.from, deposit); + } + return result; + } }; module.exports = BankVaultContract; @@ -337,7 +330,7 @@ module.exports = BankVaultContract; ``` -curl -i -H 'Accept: application/json' -X POST http://localhost:8090/v1/transaction -H 'Content-Type: application/json' -d '{"from":"0x8a209cec02cbeab7e2f74ad969d2dfe8dd24416aa65589bf","to":"0x8a209cec02cbeab7e2f74ad969d2dfe8dd24416aa65589bf","nonce":1,"source":"'usestrict';varBankVaultContract=function(){ LocalContractStorage.defineMapProperty(this,"bankVault");};//savevaluetocontract,onlyafterheightofblock,userscantakeoutBankVaultContract.prototype={ init:function(){ //TODO: }, save:function(height){ vardeposit=this.bankVault.get(Blockchain.transaction.from); if(deposit!=null){ deposit=JSON.parse(deposit) } varvalue=newBigNumber(Blockchain.transaction.value); if(deposit!=null&&deposit.balance.length>0){ varbalance=newBigNumber(deposit.balance); value=value.plus(balance); } varcontent={ balance:value.toString(), height:Blockchain.block.height+height } this.bankVault.put(Blockchain.transaction.from,JSON.stringify(content)) }, takeout:function(amount){ vardeposit=this.bankVault.get(Blockchain.transaction.from); if(deposit==null){ return0; }else{ deposit=JSON.parse(deposit) } if(Blockchain.block.height0){ deposit.balance=balance.dividedBy(value).toString() this.bankVault.put(Blockchain.transaction.from,JSON.stringify(deposit)) } returnresult }};module.exports=BankVaultContract;", "args":"[\"TEST001\", 123,[{\"name\":\"robin\",\"count\":2},{\"name\":\"roy\",\"count\":3},{\"name\":\"leon\",\"count\":4}]]"}' +curl -i -H 'Accept: application/json' -X POST http://localhost:8191/v1/transaction -H 'Content-Type: application/json' -d '{"from":"8a209cec02cbeab7e2f74ad969d2dfe8dd24416aa65589bf","to":"8a209cec02cbeab7e2f74ad969d2dfe8dd24416aa65589bf","nonce":1,"source":"\"use strict\";var BankVaultContract=function(){LocalContractStorage.defineMapProperty(this,\"bankVault\")};BankVaultContract.prototype={init:function(){},save:function(height){var deposit=this.bankVault.get(Blockchain.transaction.from);var value=new BigNumber(Blockchain.transaction.value);if(deposit!=null&&deposit.balance.length>0){var balance=new BigNumber(deposit.balance);value=value.plus(balance)}var content={balance:value.toString(),height:Blockchain.block.height+height};this.bankVault.put(Blockchain.transaction.from,content)},takeout:function(amount){var deposit=this.bankVault.get(Blockchain.transaction.from);if(deposit==null){return 0}if(Blockchain.block.height0){deposit.balance=balance.dividedBy(value).toString();this.bankVault.put(Blockchain.transaction.from,deposit)}return result}};module.exports=BankVaultContract;", "args":""}' ``` If you succeed in deploying a smart contract, you will get the contract address & transaction hash as response. Then you can call this samrt contract: @@ -347,7 +340,7 @@ Then you can call this samrt contract: ``` -curl -i -H 'Accept: application/json' -X POST http://localhost:8090/v1/call -H 'Content-Type: application/json' -d '{"from":"0x8a209cec02cbeab7e2f74ad969d2dfe8dd24416aa65589bf","to":"8f5aad7e7ad59c9d9eaa351b3f41f887e49d13f37974a02c", "nonce":2,"function":"save","args":"[0]"}' +curl -i -H 'Accept: application/json' -X POST http://localhost:8191/v1/call -H 'Content-Type: application/json' -d '{"from":"8a209cec02cbeab7e2f74ad969d2dfe8dd24416aa65589bf","to":"4df690cad7727510f386cddb9416f601de69e48ac662c44c","nonce":2,"function":"save","args":"[0]"}' ``` ## Contribution diff --git a/nf/nvm/test/bank_vault_contract.js b/nf/nvm/test/bank_vault_contract.js index 75bd2cbcb..8b0291d69 100644 --- a/nf/nvm/test/bank_vault_contract.js +++ b/nf/nvm/test/bank_vault_contract.js @@ -11,9 +11,6 @@ BankVaultContract.prototype = { }, save: function(height) { var deposit = this.bankVault.get(Blockchain.transaction.from); - if (deposit != null) { - deposit = JSON.parse(deposit) - } var value = new BigNumber(Blockchain.transaction.value); if (deposit != null && deposit.balance.length > 0) { var balance = new BigNumber(deposit.balance); @@ -23,14 +20,12 @@ BankVaultContract.prototype = { balance: value.toString(), height: Blockchain.block.height + height } - this.bankVault.put(Blockchain.transaction.from, JSON.stringify(content)) + this.bankVault.put(Blockchain.transaction.from, content); }, takeout: function(amount) { var deposit = this.bankVault.get(Blockchain.transaction.from); if (deposit == null) { return 0; - } else { - deposit = JSON.parse(deposit) } if (Blockchain.block.height < deposit.height) { return 0; @@ -42,10 +37,10 @@ BankVaultContract.prototype = { } var result = Blockchain.transfer(Blockchain.transaction.from, value); if (result > 0) { - deposit.balance = balance.dividedBy(value).toString() - this.bankVault.put(Blockchain.transaction.from, JSON.stringify(deposit)) + deposit.balance = balance.dividedBy(value).toString(); + this.bankVault.put(Blockchain.transaction.from, deposit); } - return result + return result; } };