Cant seem to send a transaction

I have been working on a little dapp project and I’m just having a hard time getting a transaction to send, seems like it has something to do with signing the TX

What im trying to do:

1.) Have user input number into a input field
2.) When the “Add KM” button is pushed take the number entered in the field and send it to the smart contract’s mint function to mint the inputted amount of ERC20 Tokens to the sending user.

the error I get when button clicked:

Uncaught TypeError: t.slice is not a function
at Object.h [as fromPrivate] (web3.min.js:1)
at e.privateKeyToAccount (web3.min.js:1)
at HTMLButtonElement.inputKmWalked (main.js:50)
at HTMLButtonElement.dispatch (jquery-3.4.1.min.js:2)
at HTMLButtonElement.v.handle (jquery-3.4.1.min.js:2)

my code:

main.js

const web3 = new Web3(Web3.givenProvider || "localhost:7545");
Web3.providers.HttpProvider("http://localhost:8000"));
const conAddr = "0xA6aFA054a30df6eb3aF74E7d5d30b005a319711f";
var contractInstance;

$(document).ready(function() {
    window.ethereum.enable().then(function(accounts){
      contractInstance = new web3.eth.Contract(abi, conAddr
        , {_address: accounts[0]}
      );


      console.log(contractInstance);
      let userAdd = contractInstance.options.address;
      const fixUserAdd = web3.utils.toChecksumAddress(userAdd);

      console.log(fixUserAdd);

  $("#add_km_button").click(inputKmWalked);

  function inputKmWalked(kmWalked, user){
    var user = fixUserAdd;
    var kmWalked = document.getElementById("km_input").value;
    console.log(kmWalked);
    console.log(user);
    console.log(web3.eth.personal)
    var key = web3.eth.accounts.privateKeyToAccount({from: accounts[0]});


//    var sendKm = {
//
//      data: "Getting Moovs",
//      from: accounts[0],
//      gas: 1000000,
//      gasprice: 20000000
//  }

  var tx = contractInstance.methods.getKm(user, kmWalked);
  const gas = web3.eth.tx.estimateGas(user);
  const gasPrice = web3.eth.getGasPrice();
  const data = tx.encodeABI();
  const nonce = web3.eth.getTransactionCount(user);

  const signedTx = web3.eth.accounts.signTransaction(
    {
      to: contractInsatance.opptions.user,
      data,
      gas,
      gasPrice,
      nonce
    },
    web3.eth.personal
  );
    console.log("old data value: ${await contractInstance.methods.getKm().call()}");
    const receipt = web3.eth.sendSignedTransaction(signedTX.rawTransaction);
    console.log("transaction hash: ${receipt.transactionHash}");
    console.log("new data value: ${await contractInstance.methods.getKm().call()}");

      }
    })
})

MoovIt.sol

pragma solidity ^0.5.16;

//import "http://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20Capped.sol";
//import "http://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";
import "./ERC20Capped.sol";

contract MoovItTest is ERC20Capped {

  mapping (address => MoovItTest) private users;
  address[] private creators;

  constructor(string memory _name, string memory _symbol, uint8 _decimals, uint256 _cap)
    //ERC20(_name, _symbol, _decimals)
    ERC20Capped(_cap)
    public
    {
    //  _name = "MoovItTest";
    //  _symbol = "Moov";
    //  _decimals = 5;
  //    address(this);
  //    _mint(owner, 51010000000000);
    }
    address owner = msg.sender;

function getKm(address user, uint256 kmWalked) public {
  kmWalked;
  //Moovs = address(this);
  addMinter(user);
  _mint(user, kmWalked);
  //transferFrom(Moovs, user, kmWalked);
  creators.push(msg.sender);
}}

EDIT: Wasn’t entirely sure where to post this @Mauro recommended I post here :slight_smile:

Hello sir, I have moved your topic to the proper category, so the community can reach your questions easily.

If you have any more questions, please let us know so we can help you! :slightly_smiling_face:

Carlos Z.

thanks :slight_smile: been really stuck here and google hasn’t been too helpful.

Hey @Proniss

I would love to have a look, please create a repo on GitHub and upload there your code (HTML / Js and Solidity).

I will try to deploy and verify.

Cheers,
Dani

1 Like

@dan-i

I haven’t used git hub much, I hope this is what your looking for

Hey @Proniss

There are some typos and issues with the logic of you backend.
I will DM you with some comments on your code.

  • When upload on GitHub please always leave all the truffle folders so that I can deploy and test :slight_smile:

This is how your main.js should look like.

const web3 = new Web3(Web3.givenProvider);
var contractInstance;
var contractAddress = '0xa949F8771aB8acE1A9d3813df55936878566C13d';

$(document).ready(function() {
  window.ethereum.enable().then(function(accounts){
    contractInstance = new web3.eth.Contract(abi, contractAddress,{from:accounts[0]});
  })
  $("#add_km_button").click(inputKmWalked);
})

function inputKmWalked () {
  web3.eth.getAccounts((error, result) => {
    if(!error) {
      contractInstance.methods.getKm(result[0],inputKmWalked).send({from:result[0]});
    }
  })
}

There is an error in your smart contract somewhere, as I hit a revert when I try to call the function getKm() so you should have a look there.

Cheers,
Dani

1 Like