github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/inner_call_tests/bank_vault_inner_contract.js (about) 1 "use strict"; 2 3 var DepositeContent = function (text) { 4 if (text) { 5 var o = JSON.parse(text); 6 this.balance = new BigNumber(o.balance); 7 this.expiryHeight = new BigNumber(o.expiryHeight); 8 } else { 9 this.balance = new BigNumber(0); 10 this.expiryHeight = new BigNumber(0); 11 } 12 }; 13 14 DepositeContent.prototype = { 15 toString: function () { 16 return JSON.stringify(this); 17 } 18 }; 19 20 var BankVaultContractS = function () { 21 LocalContractStorage.defineMapProperty(this, "bankVault", { 22 parse: function (text) { 23 return new DepositeContent(text); 24 }, 25 stringify: function (o) { 26 return o.toString(); 27 } 28 }); 29 }; 30 31 // save value to contract, only after height of block, users can takeout 32 BankVaultContractS.prototype = { 33 init: function () { 34 //TODO: 35 }, 36 getRandom: function(address, randA) { 37 console.log("second bank"); 38 var rand = _native_math.random(); 39 console.log("rand_second:", rand); 40 41 var c = new Blockchain.Contract(address); 42 var args = ""; 43 c.value(0).call("getRandom", randA, rand); 44 return rand; 45 }, 46 save: function (address, height) { 47 console.log("---------- inner in second address:",address, height); 48 var from = Blockchain.transaction.from; 49 var value = Blockchain.transaction.value; 50 var bk_height = new BigNumber(Blockchain.block.height); 51 52 var orig_deposit = this.bankVault.get(from); 53 if (orig_deposit) { 54 value = value.plus(orig_deposit.balance); 55 } 56 57 var deposit = new DepositeContent(); 58 deposit.balance = value; 59 deposit.expiryHeight = bk_height.plus(height); 60 61 this.bankVault.put(from, deposit); 62 this.transferEvent(true, height); 63 var c = new Blockchain.Contract(address); 64 c.value(2).call("save", height); 65 this.transferEvent(true, height); 66 }, 67 saveMem: function (address, mem) { 68 var m = new ArrayBuffer(mem); 69 var c = new Blockchain.Contract(address); 70 c.value(0).call("saveMem", mem); 71 this.transferEvent(true, 0, mem); 72 }, 73 saveValue: function(address, val) { 74 var c = new Blockchain.Contract(address); 75 c.value(val).call("saveValue", val); 76 }, 77 saveErr: function(address, flag) { 78 if (flag == 1) { 79 throw("saveErr in bank_vault_inner_contract"); 80 return; 81 } 82 var c = new Blockchain.Contract(address); 83 c.value(3).call("saveErr", address, flag); 84 // this.transferEvent(true, address, 0, mem); 85 }, 86 saveTimeOut: function(address, flag) { 87 if (flag == 1) { 88 while(1) { 89 90 } 91 } 92 var c = new Blockchain.Contract(address); 93 c.value(0).call("saveTimeOut", address, flag); 94 }, 95 transferEvent: function (status, height, mem) { 96 Event.Trigger("bank_vault_contract_second", { 97 Status: status, 98 BankVaultContractSecond: { 99 height: height, 100 mem: mem, 101 magic: "children one" 102 } 103 }); 104 }, 105 takeout: function (value) { 106 var from = Blockchain.transaction.from; 107 var bk_height = new BigNumber(Blockchain.block.height); 108 var amount = new BigNumber(value); 109 110 var deposit = this.bankVault.get(from); 111 if (!deposit) { 112 throw new Error("No deposit before."); 113 } 114 115 if (bk_height.lt(deposit.expiryHeight)) { 116 throw new Error("Can not takeout before expiryHeight."); 117 } 118 119 if (amount.gt(deposit.balance)) { 120 throw new Error("Insufficient balance."); 121 } 122 123 var result = Blockchain.transfer(from, amount); 124 if (!result) { 125 throw new Error("transfer failed."); 126 } 127 Event.Trigger("BankVault", { 128 Transfer: { 129 from: Blockchain.transaction.to, 130 to: from, 131 value: amount.toString() 132 } 133 }); 134 135 deposit.balance = deposit.balance.sub(amount); 136 this.bankVault.put(from, deposit); 137 }, 138 139 balanceOf: function () { 140 var from = Blockchain.transaction.from; 141 return this.bankVault.get(from); 142 }, 143 144 verifyAddress: function (address) { 145 // 1-valid, 0-invalid 146 var result = Blockchain.verifyAddress(address); 147 return { 148 valid: result == 0 ? false : true 149 }; 150 } 151 }; 152 153 module.exports = BankVaultContractS;