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