github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/inner_call_tests/bank_vault_final_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 BankVaultContract = 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  BankVaultContract.prototype = {
    33  	init: function () {
    34  		//TODO:
    35  	},
    36  	getRandom: function(randA, randB) {
    37  		console.log("second bank");
    38          var rand = _native_math.random();
    39  		console.log("rand_last:", rand);
    40  		if (rand == randA || rand == randB) {
    41  			throw("check the rand is equal");
    42  		}
    43  		
    44      },
    45  	save: function (height) {
    46  		var from = Blockchain.transaction.from;
    47  		var value = Blockchain.transaction.value;
    48  		var bk_height = new BigNumber(Blockchain.block.height);
    49  
    50  		var orig_deposit = this.bankVault.get(from);
    51  		if (orig_deposit) {
    52  			value = value.plus(orig_deposit.balance);
    53  		}
    54  
    55  		var deposit = new DepositeContent();
    56  		deposit.balance = value;
    57  		deposit.expiryHeight = bk_height.plus(height);
    58  		this.bankVault.put(from, deposit);
    59  		this.transferEvent(true, height);
    60  	},
    61  	saveMem: function (mem) {
    62          var m = new ArrayBuffer(mem);
    63  
    64          this.transferEvent(true, 0, mem);
    65  	},
    66  	saveErr: function(address, flag) {
    67          if (flag == 2) {
    68              throw("saveErr in bank_vault_contract");
    69              return;
    70          }
    71          this.transferEvent(true, 0, 3);
    72  	},
    73  	saveValue: function(val) {
    74  		console.log("inner last saveValue:", val);
    75      },
    76  	saveTimeOut: function(address, flag) {
    77          if (flag == 2) {
    78              while(1) {
    79  				
    80  			}
    81          }
    82          this.transferEvent(true, 0, 3);
    83  	},
    84  	transferEvent: function (status, height, mem) {
    85          Event.Trigger("bank_vault_contract", {
    86              Status: status,
    87              Transfer: {
    88  				height: height,
    89  				mem: mem,
    90                  magic: "children last one"
    91              }
    92          });
    93      },
    94  	takeout: function (value) {
    95  		var from = Blockchain.transaction.from;
    96  		var bk_height = new BigNumber(Blockchain.block.height);
    97  		var amount = new BigNumber(value);
    98  
    99  		var deposit = this.bankVault.get(from);
   100  		if (!deposit) {
   101  			throw new Error("No deposit before.");
   102  		}
   103  
   104  		if (bk_height.lt(deposit.expiryHeight)) {
   105  			throw new Error("Can not takeout before expiryHeight.");
   106  		}
   107  
   108  		if (amount.gt(deposit.balance)) {
   109  			throw new Error("Insufficient balance.");
   110  		}
   111  
   112  		var result = Blockchain.transfer(from, amount);
   113  		if (!result) {
   114  			throw new Error("transfer failed.");
   115  		}
   116  		Event.Trigger("BankVault", {
   117  			Transfer: {
   118  				from: Blockchain.transaction.to,
   119  				to: from,
   120  				value: amount.toString()
   121  			}
   122  		});
   123  
   124  		deposit.balance = deposit.balance.sub(amount);
   125  		this.bankVault.put(from, deposit);
   126  	},
   127  
   128  	balanceOf: function () {
   129  		var from = Blockchain.transaction.from;
   130  		return this.bankVault.get(from);
   131  	},
   132  
   133  	verifyAddress: function (address) {
   134  		// 1-valid, 0-invalid
   135  		var result = Blockchain.verifyAddress(address);
   136  		return {
   137  			valid: result == 0 ? false : true
   138  		};
   139  	}
   140  };
   141  
   142  module.exports = BankVaultContract;