簡體   English   中英

用松露測試我的智能合約時“TypeError: inst.createZombie(...).send is not a function”

[英]“TypeError: inst.createZombie(…).send is not a function” when testing my smart contract with truffle

我正在使用 truffle 來測試用solidity編寫的合同,這非常簡單,我基本上是在學習織機教程(加密僵屍)。

這是我在 createzombie.sol 文件中編寫的基本函數,

function createZombie() public payable {
  require(msg.value >= feeCreateZombie);
  zombie memory myZombie;
  myZombie.name = "newbie";
  myZombie.dna = uint(keccak256( abi.encodePacked(now,msg.sender))) % 5;
  myZombie.level = 1;
  myZombie.cooldown = now;
  zombieToOwner[zombies.push(myZombie) - 1] = msg.sender;
}

這是我用來測試合同的 javascript 文件:

const createZombie = artifacts.require('createzombie');

contract('basic tests', (accounts) => {

  it('should create a zombie', async () => {

    const inst = await createZombie.new();
    let result = await inst.createZombie({from: accounts[0]}).send();
    assert.equal(result.receipt.status, true);

  });
});

當我在終端中運行 sudo truffle test 時,出現以下錯誤:

 TypeError: inst.createZombie(...).send is not a function

(我確切地說我是一個完整的初學者)

非常感謝您未來的回答!

編輯:一個可能相關的問題的另一個例子,這是一個非常基本的智能合約的另一個例子

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;

contract Balek {

mapping(uint => address) public deck;

constructor() {
  deck[0] = msg.sender;
}

modifier reallySender(uint _name) {
  require(deck[_name] == msg.sender);
  _;
}

modifier onlyOwner(uint _name) {
  require(deck[0] == deck[_name]);
  _;
}

function transfer(address _to, uint _name) public reallySender(_name) {
  deck[_name] = _to;
}

function create(uint _name) public onlyOwner(_name) {
  deck[_name] = msg.sender;
}

fallback () external {
}

}

我知道我使用 uint 作為名稱而不是 Bytes32 或字符串,使用 javascript 時更容易,我只想測試簡單的代碼。 無論如何,當我運行 truffle 控制台並編寫 let inst = await Balek.deployed(); 時,truffle 能夠編譯和部署合約。 它有效,但我無法從合同中調用任何內容,每次我在控制台中編寫以下內容時它都不起作用:

let deck = await inst.deck;
let result = await inst.create(1);

我有錯誤:未捕獲的錯誤:返回的錯誤:處理事務時的 VM 異常:恢復

事實上,我從來沒有成功地調用過智能合約的函數。 所有這些錯誤對我來說似乎都是相互關聯的,每當我嘗試調用成功部署的合同的函數時,它都不起作用,即使我可以部署合同......

跆拳道:d

Truffle 不使用.send()函數來執行合約方法。 這在web3.js 中使用,這是一個不同的庫。

可以直接調用對應的JS函數來執行合約方法:

let result = await inst.createZombie({from: accounts[0]});

Truffle 自動(基於合約 ABI)識別它應該發送(讀寫)交易還是(只讀)調用。

文檔: https : //www.trufflesuite.com/docs/truffle/reference/contract-abstractions#making-a-transaction-via-a-contract-function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM