github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/bank_vault_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  	save: function (height) {
    37  		var from = Blockchain.transaction.from;
    38  		var value = Blockchain.transaction.value;
    39  		var bk_height = new BigNumber(Blockchain.block.height);
    40  
    41  		var orig_deposit = this.bankVault.get(from);
    42  		if (orig_deposit) {
    43  			value = value.plus(orig_deposit.balance);
    44  		}
    45  
    46  		var deposit = new DepositeContent();
    47  		deposit.balance = value;
    48  		deposit.expiryHeight = bk_height.plus(height);
    49  		this.bankVault.put(from, deposit);
    50  		this._transferEvent(true, height);
    51  	},
    52  	_transferEvent: function (status, height, mem) {
    53          Event.Trigger("bank_vault_contract", {
    54              Status: status,
    55              Transfer: {
    56  				height: height,
    57  				mem: mem,
    58                  magic: "children last one"
    59              }
    60          });
    61      },
    62  	takeout: function (value) {
    63  		var from = Blockchain.transaction.from;
    64  		var bk_height = new BigNumber(Blockchain.block.height);
    65  		var amount = new BigNumber(value);
    66  
    67  		var deposit = this.bankVault.get(from);
    68  		if (!deposit) {
    69  			throw new Error("No deposit before.");
    70  		}
    71  
    72  		if (bk_height.lt(deposit.expiryHeight)) {
    73  			throw new Error("Can not takeout before expiryHeight.");
    74  		}
    75  
    76  		if (amount.gt(deposit.balance)) {
    77  			throw new Error("Insufficient balance.");
    78  		}
    79  
    80  		var result = Blockchain.transfer(from, amount);
    81  		if (!result) {
    82  			throw new Error("transfer failed.");
    83  		}
    84  		Event.Trigger("BankVault", {
    85  			Transfer: {
    86  				from: Blockchain.transaction.to,
    87  				to: from,
    88  				value: amount.toString()
    89  			}
    90  		});
    91  
    92  		deposit.balance = deposit.balance.sub(amount);
    93  		this.bankVault.put(from, deposit);
    94  	},
    95  
    96  	balanceOf: function () {
    97  		var from = Blockchain.transaction.from;
    98  		return this.bankVault.get(from);
    99  	}
   100  };
   101  
   102  module.exports = BankVaultContract;