1

I have created a contract, wrote a migration and a test. When I run truffle test the contract is deployed locally and then the tests are run against it. All is working fine.

I also added a function to the contract that looks like this (which I also call in the test):

function entryExists(uint256 id, string memory type)
        public
        view
        returns (bool)
    {

Now, I would like to locally deploy that contract (perhaps truffle develop could be of help?) and call that function from let's say a nodeJS script. How would I do that? I can't seem to find anything about that anywhere but it feels like such a basic thing.

Thanks in advance to anyone helping.

1

When Ganache is running, your local testnet Ethereum node acts like any other Ethereum node. You can call JSON-RPC APIs against it as long as you know the TCP/IP port.

In this case the process would be

  1. Write a JavaSCript file
  2. Import web3.js
  3. Make web3.js to connect HTTPProvider that is your testnet node running locally
  4. Read ABI files that describe your Solidity contract
  5. Import one of testnet accouts that has ETH on it
  6. Create a web3.js Contract instance from ABI file and the deployed address
  7. Use Contract instance to call your method from the testnet account with ETH on it
| improve this answer | |
  • 1
    Thanks! I initially tried that but failed to read an already inserted entry because I used call() on the insertion function instead of send() which caused the entry to NOT be inserted. Kinda obvious that you have to spend some gas on functions that change stuff on the chain. My bad. Thanks again for confirming my approach and taking time out of your day to aid me. – dacx Aug 15 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.