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

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  class DepositeContent {
    20      balance: BigNumber;
    21      expiryHeight: BigNumber;
    22  
    23      constructor(text?: string) {
    24          if (text) {
    25              let o = JSON.parse(text);
    26              this.balance = new BigNumber(o.balance);
    27              this.expiryHeight = new BigNumber(o.expiryHeight);
    28          } else {
    29              this.balance = new BigNumber(0);
    30              this.expiryHeight = new BigNumber(0);
    31          }
    32      }
    33  
    34      toString(): string {
    35          return JSON.stringify(this);
    36      }
    37  
    38  }
    39  
    40  class BankVaultContract {
    41      constructor() {
    42          LocalContractStorage.defineMapProperty(this, "bankVault", {
    43              parse(text: string): DepositeContent {
    44                  return new DepositeContent(text);
    45              },
    46  
    47              stringify(o: DepositeContent): string {
    48                  return o.toString();
    49              },
    50          });
    51      }
    52  
    53      // init.
    54      init() {
    55          // pass.
    56      }
    57      // save.
    58      save(height: number) {
    59          let from = Blockchain.transaction.from;
    60          let value = Blockchain.transaction.value;
    61          let bk_height = new BigNumber(Blockchain.block.height);
    62  
    63          let orig_deposit = this.bankVault.get(from);
    64  
    65          if (orig_deposit) {
    66              value = value.plus(orig_deposit.balance);
    67          }
    68  
    69          let deposit = new DepositeContent();
    70          deposit.balance = value;
    71          deposit.expiryHeight = bk_height.plus(height);
    72  
    73          this.bankVault.put(from, deposit);
    74      }
    75  
    76      takeout(value: number) {
    77          let from = Blockchain.transaction.from;
    78          let bk_height = new BigNumber(Blockchain.block.height);
    79          let amount = new BigNumber(value);
    80  
    81          let deposit = this.bankVault.get(from);
    82          if (!deposit) {
    83              throw new Error("No deposit before.");
    84          }
    85  
    86          if (bk_height.lt(deposit.expiryHeight)) {
    87              throw new Error("Can't takeout before expiryHeight.");
    88          }
    89  
    90          if (amount.gt(deposit.balance)) {
    91              throw new Error("Insufficient balance.");
    92          }
    93  
    94          let result = Blockchain.transfer(from, amount);
    95          if (result == false) {
    96              throw new Error("transfer failed.");
    97          }
    98  
    99          deposit.balance = deposit.balance.sub(amount);
   100          this.bankVault.put(from, deposit);
   101      }
   102  
   103      balanceOf() {
   104          let from = Blockchain.transaction.from;
   105          return this.bankVault.get(from);
   106      }
   107  }
   108  
   109  module.exports = BankVaultContract;