Solidity

如何解決“未發出事件”

  • October 29, 2021

我一直按照 Dapp 大學教程中的說明進行操作。但我使用的是 Windows 10,Dapp 大學是 Mac。

我從 Dapp 大學下載了這個帶有智能合約 ERC20 的松露。

參考: https ://github.com/dappuniversity/token_sale

這是solidity的參考(我只將文件重命名為 bruce.solhttps://github.com/dappuniversity/token_sale/blob/master/contracts/DappToken.sol

我執行truffle test --network development它的地方顯示:

Using network 'local'.



 Contract: BruceToken
   √ initializes the contract with the correct values
   √ allocates the initial supply upon deployment
   1) transfers token ownership
   > No events were emitted
   2) approves tokens for delegated transfer
   > No events were emitted
   3) handles delegated token transfers
   > No events were emitted

 Contract: BruceTokenSale
   √ initializes the contract with the correct values
   4) facilitates token buying

   Events emitted during test:
   ---------------------------

   Transfer(_from: <indexed>, _to: <indexed>, _value: 750000)

   ---------------------------
   5) ends token sale
   > No events were emitted


 3 passing (32s)
 5 failing

 1) Contract: BruceToken
      transfers token ownership:
    AssertionError: error message must contain revert
     at test\BruceToken.js:39:7
     at <anonymous>
     at process._tickCallback (internal/process/next_tick.js:188:7)

 2) Contract: BruceToken
      approves tokens for delegated transfer:
    TypeError: Cannot read property 'call' of undefined
     at test\BruceToken.js:62:36
     at <anonymous>
     at process._tickCallback (internal/process/next_tick.js:188:7)

 3) Contract: BruceToken
      handles delegated token transfers:
    AssertionError: cannot transfer value larger than approved amount
     at test\BruceToken.js:97:7
     at <anonymous>
     at process._tickCallback (internal/process/next_tick.js:188:7)

 4) Contract: BruceTokenSale
      facilitates token buying:
    AssertionError: cannot purchase more tokens than available
     at test\BruceTokenSale.js:61:7
     at <anonymous>
     at process._tickCallback (internal/process/next_tick.js:188:7)

 5) Contract: BruceTokenSale
      ends token sale:

     returns all unsold bruce tokens to admin
     + expected - actual

     -250000
     +999990

     at test\BruceTokenSale.js:82:14
     at <anonymous>
     at process._tickCallback (internal/process/next_tick.js:188:7)



PS D:\Blvnp\DappUniversity\brucev2token_sale-master>
PS D:\Blvnp\DappUniversity\brucev2token_sale-master>
PS D:\Blvnp\DappUniversity\brucev2token_sale-master>
PS D:\Blvnp\DappUniversity\brucev2token_sale-master>
PS D:\Blvnp\DappUniversity\brucev2token_sale-master> ^C
PS D:\Blvnp\DappUniversity\brucev2token_sale-master> truffle test --network development
Could not connect to your Ethereum client. Please check that your Ethereum client:
   - is running
   - is accepting RPC connections (i.e., "--rpc" option is used in geth)
   - is accessible over the network
   - is properly configured in your Truffle configuration file (truffle.js)

PS D:\Blvnp\DappUniversity\brucev2token_sale-master> truffle test --network development
Using network 'development'.



 Contract: BruceToken
   √ initializes the contract with the correct values (165ms)
   √ allocates the initial supply upon deployment (44ms)
   √ transfers token ownership (214ms)
   1) approves tokens for delegated transfer
   > No events were emitted
   2) handles delegated token transfers

   Events emitted during test:
   ---------------------------

   Transfer(_from: <indexed>, _to: <indexed>, _value: 100)

   ---------------------------

 Contract: BruceTokenSale
   √ initializes the contract with the correct values (41ms)
   √ facilitates token buying (401ms)
   √ ends token sale (501ms)


 6 passing (2s)
 2 failing

 1) Contract: BruceToken
      approves tokens for delegated transfer:
    TypeError: Cannot read property 'call' of undefined
     at test\BruceToken.js:62:36
     at <anonymous>
     at process._tickCallback (internal/process/next_tick.js:188:7)

 2) Contract: BruceToken
      handles delegated token transfers:
    AssertionError: cannot transfer value larger than approved amount
     at test\BruceToken.js:97:7
     at <anonymous>
     at process._tickCallback (internal/process/next_tick.js:188:7)

現在這是我的 truffle.js:

module.exports = {
 networks: {
   development: {
     host: "127.0.0.1",
     port: 7545,
     network_id: "*"
   },
   local: {
     host: "192.168.*.***",
     port: 1201,
     gas: 2000000,
     gasPrice: 10000000000,
     network_id: "*"
   }
 }
};

沒有發出事件不是錯誤。所以你無法修復它。

當測試失敗時,Truffle 會向您顯示測試期間發出的事件,以便您輕鬆調試。

如果您來到這裡是為了尋找一種方法來列印測試期間發出的事件,您可以使用 –show-events標誌執行此操作:

truffle test --show-events

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