1

I'm trying to deploy a new contract via truffle-contract but for some reason it keeps giving me a Error: Invalid JSON RPC response: ""

import Web3 from 'web3'
import contract from 'truffle-contract'

import MyContract from '../build/MyContract.json'

const Contract = contract(MyContract)
const web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/...'))

MyContract.defaults({
  from: 0x...,
  gasPrice: '21000000000',
  gas: 4712388
})

MyContract.setProvider(web3.currentProvider)

async function deployContract() {
  let contractInstance = await MyContract.new()
  console.log(contractInstance.address)
}


deployContract()
  • The deployment transaction needs signing. Infura is not going to do that for you. That's why you should use an alternative like truffle-hdwallet-provider instead of the plain HttpProvider. This provider will sign the transaction for you, before sending it off to Infura. – Henk Aug 2 '18 at 8:36
  • @Henk am I not signing it using the MyContract.defaults and specifying the from field? – JorahFriendzone Aug 2 '18 at 9:02
  • No, because you're not providing a private key or a mnemonic anywhere to sign with – Henk Aug 2 '18 at 9:03
  • @Henk I realized I am signing it, in my truffle.js file. I have my private keys and providers defined there. Any reason I'm still getting this error? – JorahFriendzone Aug 2 '18 at 9:15
  • Yes, you might be creating providers there, but you're not using them in the code you posted. You are creating a new Web3 instance which has no access to your private keys. Use the Web3 instance truffle injects for you. Try removing this line: const web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/...')) – Henk Aug 2 '18 at 9:17

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.