Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Testing basic contract on truffle #5179

Closed
pbirbarah opened this issue Jun 5, 2022 Discussed in #5178 · 14 comments
Closed

Testing basic contract on truffle #5179

pbirbarah opened this issue Jun 5, 2022 Discussed in #5178 · 14 comments

Comments

@pbirbarah
Copy link

Discussed in https://github.com/orgs/trufflesuite/discussions/5178

Originally posted by pbirbarah June 5, 2022
this is my first contract and I am using truffle with ganache for testing and I am facing some issues:
image

The contract is supposed to just send payment back to the person who contacts it.
How do I do that? I have the contract deployed, created an instance const my_contract = await Test_Payment.deployed()

when I try my_contract.test I get an error that this is not a function I am confused how to proceed.

@kimanikelly
Copy link

Hi, @pbirbarah do you have a public repository or full code snippet of Test_Payment.sol? I would like to assist you with the blocker you're facing thank you.

@pbirbarah
Copy link
Author

thanks a lot. please use this link https://www.toptal.com/developers/hastebin/vofixocoxi.php

@kimanikelly
Copy link

@pbirbarah Sounds great thank you, I will let you know what I find and we can go over the solution

@kimanikelly
Copy link

Hi, @pbirbarah I performed unit tests for the test function and found it to be properly working while sending 0.001 ETH as the value. The environment I'm using is Hardhat so the syntax might be a little different. I added the code for the test process and a screenshot of the test passing including the before and after ETH balance of the connected signer that is invoking the function. I do have concerns about how the test function operates, the signer invoking the test function has to send 0.001 ETH to TestPayment.sol and in turn, TestPayment.sol sends the 0.001 ETH back to the msg.sender taking in account the gas fees to invoke the function is being subtracted from signers balance.

import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { expect, use } from "chai";
import { solidity } from "ethereum-waffle";
import { ethers } from "hardhat";
import { TestPayment } from "../typechain";

use(solidity);

describe.only("Test Payment", function () {
  let testPayment: TestPayment;
  let signers: SignerWithAddress[];

  beforeEach(async () => {
    signers = await ethers.getSigners();

    const TestPayment = await ethers.getContractFactory(
      "TestPayment",
      signers[0]
    );

    testPayment = (await TestPayment.deploy()) as TestPayment;
  });

  it("Should invoke the test() function", async () => {
    // Return the ETH balance of TestPayment before invoke test()
    const preContractBal = await ethers.provider.getBalance(
      testPayment.address
    );

    // Expect the ETH balance to equal 0
    expect(preContractBal).to.equal(0);

    const signerPreBal = await signers[0].getBalance();

    // Invoke test() with .001 ETH
    await testPayment.test({ value: BigInt(1e15) });

    const signerPostBal = await signers[0].getBalance();

    // Return the ETH balance of TestPayment after invoke test()
    const postContractBal = await ethers.provider.getBalance(
      testPayment.address
    );

    // Expect the ETH balance to equal 0
    expect(postContractBal).to.equal(0);

    console.log({
      signerPreBal: signerPreBal,
      signerPostBal: signerPostBal,
    });
  });
});

Screen Shot 2022-06-07 at 11 21 18 AM

@kimanikelly
Copy link

@pbirbarah How are you deploying your contract do you have a code snippet?

@pbirbarah
Copy link
Author

I am using truffle console with ganache so it's different.

after deploying the contract on ganache, I can write this to double check it is deployed.
const my_contract = await Test_Payment.deployed()
then I try and define the sender address using
address my_address = '0x56cFBBDB11D385da02db4eef2784E021435d6731' and I get an error
image

@kimanikelly
Copy link

@pbirbarah Alright I will try with Truffle and let you know

@kimanikelly
Copy link

@pbirbarah Before I deployed the contract and invoked the test function on localhost through truffle console and ganache-cli I was able to successfully invoke test through the test suite by configuring a init test file and executing the truffle test command

const TestPayment = artifacts.require("TestPayment");

contract("TestPayment", (accounts) => {
  it("Should invoke the test() function", async () => {
    const testPayment = await TestPayment.deployed();

    await testPayment.test({ value: 1e15 });
  });
});

Screen Shot 2022-06-07 at 4 38 27 PM

@kimanikelly
Copy link

@pbirbarah I was able to successfully deploy TestPayment.sol locally with truffle migrate and ganache-cli then invoke the test function

Screen Shot 2022-06-07 at 5 32 44 PM

@pbirbarah
Copy link
Author

this is very helpful thank you. How can I specify which address to send from using the console? and can I check the balance on the address using the console?

@kimanikelly
Copy link

@pbirbarah The Truffle console supports the accounts variable and returns an array of public addresses connected to ganache-cli. You can specify which address invokes the test function by adding the from property to the function call and designating the index inside the accounts array.

Screen Shot 2022-06-07 at 7 34 15 PM

c

@kimanikelly
Copy link

@pbirbarah Here is how you can check the ETH balance from one of the accounts

Screen Shot 2022-06-07 at 7 38 20 PM

@pbirbarah
Copy link
Author

that is perfect! thanks!

@kimanikelly
Copy link

@pbirbarah No problem take care

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants