0

i'm using infura and i'm creating a smart contract that can get the total balance of token on a wallet and can transfer token balance

This is my Transactions.sol

// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract Transactions {
    uint256 transactionCount;
    event Transfer(address from, address receiver, uint amount, string message, uint256 timestamp, string keyword);

    mapping(address => uint256) public balances; // Define a mapping to track the balances of each address

    IERC20 private token;

    constructor(address _tokenAddress) {
        token = IERC20(_tokenAddress);
    }

    struct TransferStruct {
        address sender;
        address receiver;
        uint amount;
        string message;
        uint256 timestamp;
        string keyword;
    }

    TransferStruct[] transactions;

    function addToBlockchain(address payable receiver, uint amount, string memory message, string memory keyword) public {
        transactionCount += 1;
        transactions.push(TransferStruct(msg.sender, receiver, amount, message, block.timestamp, keyword));

        // Update the sender and receiver balances
        balances[msg.sender] -= amount;
        balances[receiver] += amount;

        emit Transfer(msg.sender, receiver, amount, message, block.timestamp, keyword);
    }

    function getAllTransactions() public view returns (TransferStruct[] memory) {
        return transactions;
    }

    function getTransactionCount() public view returns (uint256) {
        return transactionCount;
    }
    
    function getBalance() public view returns (uint256) {
        return token.balanceOf(msg.sender);
    }

    function balanceOf(address account) public view returns (uint256) {
        return token.balanceOf(account);
    }

    function sendTokens(address to, uint256 amount) public {
        require(token.allowance(msg.sender, address(this)) >= amount, "Contract not authorized to transfer tokens");
        require(token.transferFrom(msg.sender, to, amount), "Token transfer failed");
    }
}

This is my Hardhat.config.js file

const path = require('path');
require('@nomiclabs/hardhat-waffle');

module.exports = {
  solidity: '0.8.0',
  networks: {
    sepolia: {
      url: 'https://sepolia.infura.io/v3/f37bc94c8c524217b597e4f291784097',
      accounts: ['MY API KEY IS HERE'],
    },
    mainnet: {
      url: 'https://mainnet.infura.io/v3/f37bc94c8c524217b597e4f291784097',
      accounts: ['MY API KEY IS HERE'],
    },
  },
  paths: {
    sources: './contracts',
    tests: './test',
    cache: './cache',
    artifacts: './artifacts',
    deploy: './deploy',
  },
  solidity: {
    compilers: [
      {
        version: '0.8.0',
      },
    ],
  },
  namedAccounts: {
    deployer: 0,
  },
  mocha: {
    timeout: 20000,
  },
  // Add this section to specify the path to your deploy.js file
  scripts: {
    deploy: 'npx hardhat run scripts/deploy.js --network sepolia',
  },
};

This is my deploy.js file

const { ethers } = require('hardhat');

const main = async () => {
  const TransactionsFactory = await hre.ethers.getContractFactory("Transactions");
  const transactionsContract = await TransactionsFactory.deploy();

  await transactionsContract.deployed();

  console.log("Transactions address: ", transactionsContract.address);
};

const runMain = async () => {
  try {
    await main();
    process.exit(0);
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
};

runMain();

it compiled successfully but this is the error i get when i try to deploy it.

Error: missing argument:  in Contract constructor (count=0, expectedCount=1, code=MISSING_ARGUMENT, version=contracts/5.7.0)
    at Logger.makeError (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\logger\src.ts\index.ts:269:28)
    at Logger.throwError (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\logger\src.ts\index.ts:281:20)
    at Logger.checkArgumentCount (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\logger\src.ts\index.ts:340:18)
    at ContractFactory.<anonymous> (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\contracts\src.ts\index.ts:1237:16)
    at step (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\contracts\lib\index.js:48:23)
    at Object.next (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\contracts\lib\index.js:29:53)
    at C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\contracts\lib\index.js:23:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\contracts\lib\index.js:19:12)
    at ContractFactory.deploy (C:\Users\ABE\Desktop\test\smart_contract\node_modules\@ethersproject\contracts\lib\index.js:1138:16)
    at main (C:\Users\ABE\Desktop\test\smart_contract\scripts\deploy.js:5:58)
    at runMain (C:\Users\ABE\Desktop\test\smart_contract\scripts\deploy.js:14:5) {
  reason: 'missing argument:  in Contract constructor',
  code: 'MISSING_ARGUMENT',
  count: 0,
  expectedCount: 1
}

I've updated and re-install hardhat but its still the same

1 Answers1

0

You need to pass arguments for smart contract constructor when deploying contract. Your Transaction contract constructor requires token adress so you need to pass it.

const { ethers } = require('hardhat');

const main = async () => {
  const TransactionsFactory = await hre.ethers.getContractFactory("Transactions");
  const tokenAddress = "YOUR_TOKEN_CONTRACT_ADDRESS";
  const transactionsContract = await TransactionsFactory.deploy(tokenAddress);

  await transactionsContract.deployed();

  console.log("Transactions address: ", transactionsContract.address);
};

const runMain = async () => {
  try {
    await main();
    process.exit(0);
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
};

runMain();
Meerkat
  • 324
  • 1
  • 10
  • i haven't deploy any token contract so i don't have contract address. i want to deploy so i could get a token address – Jason Bowden May 04 '23 at 04:17
  • Could you explain what you're going to implement with smart contract? I don't quite understand what your code does. – Meerkat May 04 '23 at 04:41
  • I want it to get token balance when user connect and transfer token – Jason Bowden May 04 '23 at 04:44
  • IERC20 token already has balances for addresses. Why did you need to have balances in your contract too? – Meerkat May 04 '23 at 04:49
  • I think you should implement ERC20 contract to create a new token or pass the token address to your constructor as you did and use that token's interface to get balances and transfer tokens. – Meerkat May 04 '23 at 04:56
  • i don't know how to do that.. would you please show me? – Jason Bowden May 04 '23 at 05:05