Truffle

如何將solidity庫與松露一起使用

  • February 28, 2020

我正在使用松露V5.1.13,當我執行truffle testres變數時包含交易收據,而不是預期的 2。我注意到如果我刪除了與庫有關的所有內容,事情會按預期工作。

為什麼會這樣?如何使用我的圖書館?

合約程式碼:

pragma solidity ^0.5.1;

library TestLib{
   struct DataSet {
       uint a;
   }

   function assign(DataSet storage self) internal {
       self.a = 2;
   }

   function getA(DataSet storage self) internal view returns(uint) {
       return self.a;
   }
}

contract TestContract {
   using TestLib for TestLib.DataSet;
   TestLib.DataSet set;

   function getBalanceInEth() public returns(uint) {
       set.assign();
       return set.getA();
   }
}

測試程式碼:

   const TestContract = artifacts.require('TestContract')

   contract('TestContract', async accounts => {
       let TestContractInstance

       beforeEach('setup contract for each test', async function() {
          TestContractInstance = await TestContract.deployed()
       })

       it('Should return 2', async () => {
          let res = await TestContractInstance.getBalanceInEth()
          console.log(res)
          assert.equal(res, 2)
       })
   })

遷移程式碼:

const TestContract = artifacts.require('TestContract')
const TestLib = artifacts.require('TestLib')
module.exports = function(deployer) {
 deployer.deploy(TestLib)
 deployer.link(TestLib, TestContract)
 deployer.deploy(TestContract)
}

console.log(res)給出:

{
 tx: '0x1f05b5fa7ed635aaa619cc548a0a37ff9d9d57398d85ecb3f8000ed7e124373c',
 receipt: {
   transactionHash: '0x1f05b5fa7ed635aaa619cc548a0a37ff9d9d57398d85ecb3f8000ed7e124373c',
   transactionIndex: 0,
   blockHash: '0xe6bc488fce167970b781588d1c70c0b4a452834c78daf4f052c1a045575fd959',
   blockNumber: 3,
   from: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
   to: '0xf12b5dd4ead5f743c6baa640b0216200e89b60da',
   gasUsed: 42158,
   cumulativeGasUsed: 42158,
   contractAddress: null,
   logs: [],
   status: true,
   logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
   rawLogs: []
 },
 logs: []
}

解決此問題的一種可能方法是將合約程式碼更改為以下內容:

contract TestContract {
   using TestLib for TestLib.DataSet;
   TestLib.DataSet set;

   function setBalanceInEth() public {
       set.assign();
   }

   function getBalanceInEth() public view returns(uint) {
       return set.getA();
   }
}

並相應地更改測試程式碼:

const TestContract = artifacts.require('TestContract')

contract('TestContract', async (accounts) => {
 let TestContractInstance

 beforeEach('setup contract for each test', async function() {
   TestContractInstance = await TestContract.deployed()
 })

 it('Should return 2', async () => {
   await TestContractInstance.setBalanceInEth()
   let res = await TestContractInstance.getBalanceInEth()
   console.log(res)
   assert.equal(res, 2)
 })
})

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