0

I have a dapp that allows a user to input some info via the GUI, and this info is stored to the blockchain(testnet for now) when the user presses a button. This naturally incurs a cost, which causes metamask to pop up and ask for confirmation. It works fine. However, I am wondering if there is a way to avoid this popup, and for the transaction to be paid for 'in the background', say, by a script on the server, which I could make a call to...? Firstly, the reason is that I don't want to user to pay for the transaction.

One idea had involved a script which I use to deploy a contract from the server, which I usually just run from the terminal when testing(seen below). I found it interesting that this script can get access the metamask account(via the seed words for the account, and use of an Infura node) and perform the deployment, paying the fee, without any need for popups, because I'm providing the seed words.

I was wondering then, if it might be possible to make a call to a similar script, from the front end, when the user presses the button, which will then pay for the transaction? I'm not quite sure how to go about it yet, but thought maybe someone may have some valuable knowledge regarding this.

  • I am using nodejs, reactjs and web3js

deploy.js

const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require('web3');
const Factory = require('./build/Factory.json');

// args: mnemonic, url of network to connect to(Rinkeby Infura API key...)
const provider = new HDWalletProvider(
    MY_SEED_WORDS_STR,
    MY_INFURA_API_URL
);

// Completely enabled for the Rinkeby network. Using truffle hdwallet and Infura etc.
const web3 = new Web3(provider);

const deploy = async () => {
    const accounts = await web3.eth.getAccounts();
    console.log('Attempting to deploy from account: ', accounts[0]);
    const factory = await new web3.eth.Contract(JSON.parse(Factory.interface))
        .deploy({data: Factory.bytecode})
        .send( {gas: '1000000', from: accounts[0] });
    console.log('Contract deployed to: ', factory.options.address);
};

deploy();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.