Truffle

用松露測試觀看事件

  • September 17, 2020

給定以下可靠程式碼:

pragma solidity ^0.5.1;

contract Foo {

   event FooInc(uint256 _a);
   uint256 a;

   constructor() public {
       a = 42;
   }

   function inc() public returns(bool) {
       a += 1;
       emit FooInc(a);
       return true;
   }
}

和以下測試javascript程式碼:

let Foo = artifacts.require('Foo');

contract("foocontract", async accounts => {
   it("testing inc", async (done) => {
       let foo = await Foo.deployed();
       foo.allEvents().on('data', function(e) {
           console.log('allevents', e);
           done();
       });
       foo.inc().then(r => {
           console.log('done', r);         
           foo.getPastEvents('allEvents', undefined, (e, l) => {
               console.log("pastevents", e, l);
               done();
           });

       });
   });
});

foo.inc()承諾的收據被記錄下來。合約事件的兩個事件監聽器訪問器都不會觸發,儘管接收對象顯示事件已被觸發。

$ truffle test
Using network 'test'.


Compiling your contracts...
===========================
> Compiling ./contracts/Foo.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /tmp/test-2020127-266345-9s8aje.7jzss
> Compiled successfully using:
  - solc: 0.5.16+commit.9c3226ce.Emscripten.clang



 Contract: foocontract
done {
 tx: '0x158109501fa0cecffa6c7887d05c255cb5e76002c25881f5e684ab199a75cdff',
 receipt: {
   transactionHash: '0x158109501fa0cecffa6c7887d05c255cb5e76002c25881f5e684ab199a75cdff',
   transactionIndex: 0,
   blockHash: '0x55e2fce5a0257fad6d851ba35e03885509f98a6641e3e92a146e2fbaad9e3a18',
   blockNumber: 5,
   from: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
   to: '0x345ca3e014aaf5dca488057592ee47305d9b3e10',
   gasUsed: 28961,
   cumulativeGasUsed: 28961,
   contractAddress: null,
   logs: [ [Object] ],
   status: true,
   logsBloom: '0x00000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000010000000000000',
   rawLogs: [ [Object] ]
 },
 logs: [
   {
     logIndex: 0,
     transactionIndex: 0,
     transactionHash: '0x158109501fa0cecffa6c7887d05c255cb5e76002c25881f5e684ab199a75cdff',
     blockHash: '0x55e2fce5a0257fad6d851ba35e03885509f98a6641e3e92a146e2fbaad9e3a18',
     blockNumber: 5,
     address: '0x345cA3e014Aaf5dcA488057592ee47305D9B3e10',
     type: 'mined',
     id: 'log_8dfd46f8',
     event: 'FooInc',
     args: [Result]
   }
 ]
}

我正在使用truffle@v5.1.14它,它的文件建議它應該只是監聽 javascript 事件dataerror. 我在這裡想念什麼?

這適用於 Truffle v4.1.16:

const Foo = artifacts.require('Foo');

contract("foocontract", async accounts => {
   it("testing inc", async (done) => {
       const foo = await Foo.deployed();
       const receipt = await foo.inc();
       printLogs("receipt", receipt.logs);
       foo.allEvents().get(function(error, logs) {
           assert.equal(error, null);
           printLogs("listened", logs);
       });
   });
});

function printLogs(title, logs) {
   for (let i = 0; i < logs.length; i++) {
       console.log();
       console.log(`${title} event #${i + 1}:`);
       console.log(JSON.stringify(logs[i], null, 4));
   }
}

你可能想在你的 Truffle 版本上嘗試類似的東西。

請記住,Truffle v4.x 依賴於 web3.js v0.x,而 Truffle v5.x 依賴於 web3.js v1.x。

因此,監聽事件的方式可能存在一些差異。

松露 5,solc 0.7

const CrowdFundingWithDeadline = artifacts.require("TestCrowdFundingWithDeadline");

contract('CrowdFundingWithDeadline' , async accounts => {
   let contract;
   let account = accounts[0];
   let beneficiary = accounts[1];
   const ONE_ETH = 1000000000000000000;
   const Ongoing_State = 0;
   const Faild_State = 1;
   const Succeeded_State = 2;
   const Paidout_State = 3;

   beforeEach(async function () {
       contract = await CrowdFundingWithDeadline.new(
           'funding',
           1,
           10,
           beneficiary,
           {
               from: account,
               gas: 2000000
           }
       );
   });
   
   it('event is emitted', async function () {
       await contract.setCurrentTime(601);
       let res = await contract.finishCrowdFunding();
       let eventName = res.logs[0].event;
       let eventRes = res.logs[0].args;
       expect(Number.parseInt(eventRes.totalCollected)).to.equal(0);
       expect(eventRes.succeeded).to.equal(false);
   });
});

引用自:https://ethereum.stackexchange.com/questions/80162