github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/sample_contract.js (about) 1 'use strict'; 2 3 var SampleContract = function () { 4 LocalContractStorage.defineProperties(this, { 5 name: null, 6 count: null 7 }); 8 LocalContractStorage.defineMapProperty(this, "allocation"); 9 }; 10 11 SampleContract.prototype = { 12 init: function (name, count, allocation) { 13 this.name = name; 14 this.count = count; 15 allocation.forEach(function (item) { 16 this.allocation.put(item.name, item.count); 17 }, this); 18 // console.log('init: this.name = ' + this.name); 19 // console.log('init: this.count = ' + this.count); 20 console.log('init: Blockchain.block.coinbase = ' + Blockchain.block.coinbase); 21 console.log('init: Blockchain.block.hash = ' + Blockchain.block.hash); 22 console.log('init: Blockchain.block.height = ' + Blockchain.block.height); 23 console.log('init: Blockchain.transaction.from = ' + Blockchain.transaction.from); 24 console.log('init: Blockchain.transaction.to = ' + Blockchain.transaction.to); 25 console.log('init: Blockchain.transaction.value = ' + Blockchain.transaction.value); 26 console.log('init: Blockchain.transaction.nonce = ' + Blockchain.transaction.nonce); 27 console.log('init: Blockchain.transaction.hash = ' + Blockchain.transaction.hash); 28 }, 29 dump: function () { 30 console.log('dump: this.name = ' + this.name); 31 console.log('dump: this.count = ' + this.count); 32 }, 33 $dump: function () { 34 return this.dump(); 35 }, 36 dump_1: function () { 37 return this.dump(); 38 }, 39 verify: function (expectedName, expectedCount, expectedAllocation) { 40 if (!Object.is(this.name, expectedName)) { 41 throw new Error("name is not the same, expecting " + expectedName + ", actual is " + this.name + "."); 42 } 43 if (!Object.is(this.count, expectedCount)) { 44 throw new Error("count is not the same, expecting " + expectedCount + ", actual is " + this.count + "."); 45 } 46 47 expectedAllocation.forEach(function (expectedItem) { 48 var count = this.allocation.get(expectedItem.name); 49 if (!Object.is(count, expectedItem.count)) { 50 throw new Error("count of " + expectedItem.name + " is not the same, expecting " + expectedItem.count + ", actual is " + count + "."); 51 } 52 }, this); 53 } 54 }; 55 56 module.exports = SampleContract;