1
votes

i'm using truffle to test a contract written with solidity, it's very simple, i'm basically following the loom tutorial (crypto zombie).

this is a basic function i've written in a createzombie.sol file,

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;
}

and this is the javascript file i'm using to test the contract:

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);

  });
});

when i run sudo truffle test in the terminal, i get the following error:

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

(i precise i'm a complete beginner)

thanks a lot for your future answer !

EDIT: an other example of a problem that may be related, this is an other example of a very basic smart contract

// 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 {
}

}

i know i use uint for names instead of Bytes32 or string, it's just easier when using javascript, i just want to test simple codes. Anyway, truffle is able to compile and deploy the contract, when i run truffle console and write let inst = await Balek.deployed(); it works but i can't call anything from the contract, every time i write the followings in the console it doesn't work:

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

i have the error: Uncaught Error: Returned error: VM Exception while processing transaction: revert

In fact, i've never successfully called a function of a smart contract. All this errors seem related one to another for me, whenever i try to call a function of a successfully deployed contract, it doesn't work, even if i can deploy the contract ...

W T F :d

1

1 Answers

0
votes

Truffle doesn't use the .send() function to execute a contract method. That's used in web3.js, which is a different library.

You can execute the contract method by calling the corresponding JS function directly:

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

Truffle automatically (based on the contract ABI) recognizes whether it should send a (read-write) transaction, or a (read-only) call.

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