0

I want to initiate transactions using the following stack. However, Infura doesn't support web3.eth.sendTransaction(tx) because it requires the server to hold private keys. Here is a link to how it should work https://example.walletconnect.org/ and the repo https://github.com/WalletConnect/walletconnect-example-dapp.

So far this is what I have.

        const provider = new WalletConnectProvider({
            infuraId: "**************",
        })

        await provider.enable();

        const web3 = new Web3(provider);

        web3.eth.getAccounts().then((account) => {
            console.log("kk", account)
            setAccount(account)
        }).catch((err) => {
            console.log("Failed to get accounts", err)
        })

        web3.eth.getBalance(account[0]).then((balance) => {
            setAccountBalance(balance)
        }).catch((err) => {
            console.log("ayre", err)
        })

1 Answer 1

1

you can use the contract function and that will work. For example if you want to initiate an ETH transaction and you have the function buy() in your contract used for purchasing goods, you can call it with your contract instance:

Declare the contract instance:

const instance = new web3.eth.Contract(contract.abi, contractAddress)

Use the contract instance to call your function

instance.methods.yourContractFunction().send({from: accounts[0],value:price}).then("confirmation",(confirmation) => {
        console.log(confirmation)
      }).catch("error", (error) => {
        console.log(error)
      })
3
  • I thought contracts are used for ETH tokens, also do I have to write solidity code for this?
    – Ibra
    Commented Oct 11, 2021 at 23:19
  • I suggest you check out this tutorial: dappuniversity.com/articles/web3-js-intro. It will explain how to use contract functions and call them with web3.
    – Icarus23
    Commented Oct 12, 2021 at 3:46
  • I want to be able to send a transaction without using Ethereum-tx and I don't have he wallet's private keys. Check out my follow up question ethereum.stackexchange.com/questions/112153/…
    – Ibra
    Commented Oct 25, 2021 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.