Transaction Sending – How to Send a Transaction Without Using Metamask?

contract-deploymentrinkebyweb3js

I have designed a contract deployed on the Rinkeby with web3. I have also implemented a react app to interact with the blockchain.
I want to use my application without metamask but I got this error in Chrome "No "from" address specified in neither the given options, nor the default options." when I try to send a transaction like this:

await tracker.methods
      .createAsset(name, description, manufacturer, price)
      .send({
        from: accounts[0],
        value: web3.utils.toWei(this.state.amountToStake, "ether"),
        gas: "1000000"
      });

Based on the code below can someone tell what I am doing wrong?

web3 script:

import Web3 from "web3";
let web3; 
const provider = new Web3.providers.HttpProvider( // creating my own provider
"https://rinkeby.infura.io/v3/..."
);
web3 = new Web3(provider);

export default web3;

deploy script:

const HDWalletProvider = require("truffle-hdwallet-provider");
const Web3 = require("web3");

const compiledTracker = require("./build/AssetTracker.json");

const provider = new HDWalletProvider("mnemonic",
 "https://rinkeby.infura.io/v3/...");

const web3 = new Web3(provider);

const deploy = async () => {
const accounts = await web3.eth.getAccounts();


const result = await new web3.eth.Contract(
JSON.parse(compiledTracker.interface))
    .deploy({ data: compiledTracker.bytecode })
    .send({ gas: "2000000", from: accounts[0] });

Note: Everything was working fine with MetaMask but I need to send a transaction without MetaMask that's why I defined a provider with Rinkeby.

Best Answer

You are using a direct connection to Infura.

const provider = new Web3.providers.HttpProvider( // creating my own provider
    "https://rinkeby.infura.io/v3/..."
);
web3 = new Web3(provider);

Infura doesn't provide account management. If you want to use Infura and create transaction in the client side you have to use a library to sign transaction.

You can use HDWalletProvider that will use the wallet derived from the mnemonic. For example your deploy script should work fine.

const provider = new HDWalletProvider("mnemonic", "https://rinkeby.infura.io/v3/...");
const web3 = new Web3(provider);
Related Topic