Truffle-Test
松露測試錯誤
我想創建一個去中心化的應用程序來出售我的代幣。所以,在這種情況下。我創建一個函式來結束我的代幣銷售。所以,我在下面創建一個函式。
堅固性程式碼在這裡: -
function endSale() public { require(msg.sender == admin); require(tokenContract.transfer(admin,tokenContract.balanceOf(this))); selfdestruct(admin); }
松露java腳本測試程式碼在這裡: -
it('ends token sale', function() { `enter preformatted text here` return DappToken.deployed().then(function(instance) { // Grab token instance first tokenInstance = instance; return DappTokenSale.deployed(); }).then(function(instance) { // Then grab token sale instance tokenSaleInstance = instance; // Try to end sale from account other than the admin return tokenSaleInstance.endSale({ from: buyer }); }).then(assert.fail).catch(function(error) { assert(error.message.indexOf('revert' >= 0, 'must be admin to end sale')); // End sale as admin return tokenSaleInstance.endSale({ from: admin }); }).then(function(receipt) { return tokenInstance.balanceOf(admin); }).then(function(balance) { assert.equal(balance.toNumber(), 999990, 'returns all unsold dapp tokens to admin'); // Check that token price was reset when selfDestruct was called return tokenSaleInstance.tokenPrice(); }).then(function(price) { assert.equal(price.toNumber(), 0, 'token price was reset'); }); });
當我通過 truffle test 命令對其進行測試時,出現以下錯誤:-
Error: Attempting to run transaction which calls a contract function, but recipient address 0x970c30c59d21df9fe660a266faf2ba0871dc25ad is not a contract address at Object.InvalidResponse (/home/anupam/.config/yarn/global/node_modules/truffle/build/webpack:/~/web3/lib/web3/errors.js:38:1) at /home/anupam/.config/yarn/global/node_modules/truffle/build/webpack:/~/web3/lib/web3/requestmanager.js:86:1 at /home/anupam/.config/yarn/global/node_modules/truffle/build/webpack:/~/truffle-provider/wrapper.js:134:1 at XMLHttpRequest.request.onreadystatechange (/home/anupam/.config/yarn/global/node_modules/truffle/build/webpack:/~/web3/lib/web3/httpprovider.js:128:1) at XMLHttpRequestEventTarget.dispatchEvent (/home/anupam/.config/yarn/global/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:64:1) at XMLHttpRequest._setReadyState (/home/anupam/.config/yarn/global/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:354:1) at XMLHttpRequest._onHttpResponseEnd (/home/anupam/.config/yarn/global/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:509:1) at IncomingMessage.<anonymous> (/home/anupam/.config/yarn/global/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:469:1) at endReadableNT (_stream_readable.js:1064:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickCallback (internal/process/next_tick.js:180:9)
因此,任何人都可以幫助我解決此類錯誤。請告訴我我在程式碼中的錯誤位置。
嘗試
selfdestruct
從 endSale 函式中刪除函式呼叫。
您不能
tokenSaleInstance.tokenPrice()
在之後呼叫,tokenSaleInstance.endSale({ from: admin })
因為在那之後契約不再存在。正如@ThorkilVærge 所建議的,如果您需要價格,您可以移除 selfdestruct。另一種選擇是在呼叫之前檢索價格
endSale()
。