Skip to content

Commit

Permalink
contract:refine bank_vault_contract.js & update readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
qywang2012 committed Nov 23, 2017
1 parent 5c3999b commit 2ec4747
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 55 deletions.
85 changes: 39 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.height<deposit.height){ return0; } varbalance=newBigNumber(deposit.balance); varvalue=newBigNumber(amount); if(balance.lessThan(value)){ return0; } varresult=Blockchain.transfer(Blockchain.transaction.from,value); if(result>0){ 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.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;", "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:
Expand All @@ -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
Expand Down
13 changes: 4 additions & 9 deletions nf/nvm/test/bank_vault_contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
};

Expand Down

0 comments on commit 2ec4747

Please sign in to comment.