github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/contract_accept_func_with_args.js (about)

     1  
     2  "use strict";
     3  
     4  var DepositeContent = function (text) {
     5  	if (text) {
     6  		var o = JSON.parse(text);
     7  		this.balance = new BigNumber(o.balance);
     8  		this.expiryHeight = new BigNumber(o.expiryHeight);
     9  	} else {
    10  		this.balance = new BigNumber(0);
    11  		this.expiryHeight = new BigNumber(0);
    12  	}
    13  };
    14  
    15  DepositeContent.prototype = {
    16  	toString: function () {
    17  		return JSON.stringify(this);
    18  	}
    19  };
    20  
    21  var BankVaultContract = function () {
    22  	LocalContractStorage.defineMapProperty(this, "bankVault", {
    23  		parse: function (text) {
    24  			return new DepositeContent(text);
    25  		},
    26  		stringify: function (o) {
    27  			return o.toString();
    28  		}
    29  	});
    30  };
    31  
    32  // save value to contract, only after height of block, users can takeout
    33  BankVaultContract.prototype = {
    34  	init: function () {
    35  		//TODO:
    36  	},
    37  
    38  	// enables accepting NAS via 'binary'
    39  	accept: function (height) {		
    40  		var from = Blockchain.transaction.from;
    41  		var value = Blockchain.transaction.value;
    42  		var bk_height = new BigNumber(Blockchain.block.height);
    43  
    44  		var orig_deposit = this.bankVault.get(from);
    45  		if (orig_deposit) {
    46  			value = value.plus(orig_deposit.balance);
    47  		}
    48  
    49  		var deposit = new DepositeContent();
    50  		deposit.balance = value;
    51  		deposit.expiryHeight = bk_height.plus(height);
    52  
    53          this.bankVault.put(from, deposit);
    54  	}
    55  };
    56  
    57  module.exports = BankVaultContract;
    58