Hi all, I'm stuck on a problem, hope anyone can help me.
I've made a test network with ganache, am able to connect to it through web3 on frontend but have to perform a call via nodejs on backend. I'm stuck receiving this error:
Error: Invalid JSON RPC response: ""
As you can see, I tried to debug with a lot of consoles, that prints out as follow.
This is my code
console.log("1: Address");
console.log(contractInfo.contract_address);
console.log("2: Contract");
contractInstance = new web3.eth.Contract(contractInfo.contract_abi, contractInfo.contract_address);
console.log("3: Methods");
console.log(contractInstance.methods);
console.log("4: call");
let a = contractInstance.methods.getActionIsPayed(id)
console.log("4b: a", a);
contractInstance.methods.getActionIsPayed(id).call({from: '0xEF0F001B80c8BdbDF7e48d5477d75e861d211eaa'})
.then((response) => {
console.log("5: response");
console.log(create);
res(create);
})
.catch((err) => {
console.log(err);
res("401");
});
Jun 27, 2022, 3:39 PM
These are the answers
1: Address
0x756Da73c0F6999DB4B81423D2E2146637Af6CD3C
2: Contract
3: Methods
{
buildAction: [Function: bound _createTxObject],
'0xe9ba0aa8': [Function: bound _createTxObject],
'buildAction(uint256)': [Function: bound _createTxObject],
createAction: [Function: bound _createTxObject],
'0xfa649172': [Function: bound _createTxObject],
'createAction(uint256,uint256)': [Function: bound _createTxObject],
getActionIsPayed: [Function: bound _createTxObject],
'0xf8f87f8d': [Function: bound _createTxObject],
'getActionIsPayed(uint256)': [Function: bound _createTxObject],
getActions: [Function: bound _createTxObject],
'0x328dd982': [Function: bound _createTxObject],
'getActions(uint256)': [Function: bound _createTxObject]
}
4: call
4b: a {
call: [Function: bound _executeMethod] {
request: [Function: bound _executeMethod]
},
send: [Function: bound _executeMethod] {
request: [Function: bound _executeMethod]
},
encodeABI: [Function: bound _encodeMethodABI],
estimateGas: [Function: bound _executeMethod],
createAccessList: [Function: bound _executeMethod],
arguments: [ '2' ],
_method: {
inputs: [ [Object] ],
name: 'getActionIsPayed',
outputs: [ [Object] ],
stateMutability: 'view',
type: 'function',
constant: true,
payable: undefined,
signature: '0xf8f87f8d'
},
_parent: Contract {
setProvider: [Function (anonymous)],
currentProvider: [Getter/Setter],
_requestManager: RequestManager {
provider: [HttpProvider],
providers: [Object],
subscriptions: Map(0) {}
},
givenProvider: null,
providers: {
WebsocketProvider: [Function: WebsocketProvider],
HttpProvider: [Function: HttpProvider],
IpcProvider: [Function: IpcProvider]
},
_provider: HttpProvider {
withCredentials: false,
timeout: 0,
headers: undefined,
agent: undefined,
connected: false,
host: 'http://localhost:8545',
httpAgent: [Agent]
},
setRequestManager: [Function (anonymous)],
BatchRequest: [Function: bound Batch],
extend: [Function: ex] {
formatters: [Object],
utils: [Object],
Method: [Function: Method]
},
clearSubscriptions: [Function (anonymous)],
options: { address: [Getter/Setter], jsonInterface: [Getter/Setter] },
handleRevert: [Getter/Setter],
defaultCommon: [Getter/Setter],
defaultHardfork: [Getter/Setter],
defaultChain: [Getter/Setter],
transactionPollingTimeout: [Getter/Setter],
transactionPollingInterval: [Getter/Setter],
transactionConfirmationBlocks: [Getter/Setter],
transactionBlockTimeout: [Getter/Setter],
blockHeaderTimeout: [Getter/Setter],
defaultAccount: [Getter/Setter],
defaultBlock: [Getter/Setter],
methods: {
buildAction: [Function: bound _createTxObject],
'0xe9ba0aa8': [Function: bound _createTxObject],
'buildAction(uint256)': [Function: bound _createTxObject],
createAction: [Function: bound _createTxObject],
'0xfa649172': [Function: bound _createTxObject],
'createAction(uint256,uint256)': [Function: bound _createTxObject],
getActionIsPayed: [Function: bound _createTxObject],
'0xf8f87f8d': [Function: bound _createTxObject],
'getActionIsPayed(uint256)': [Function: bound _createTxObject],
getActions: [Function: bound _createTxObject],
'0x328dd982': [Function: bound _createTxObject],
'getActions(uint256)': [Function: bound _createTxObject]
},
events: {
newActionCreated: [Function: bound ],
'0xd11c44ba953fddba8d7004c43ef1c4125a2d5e6dc0227de355ee9b9692ce4a05': [Function: bound ],
'newActionCreated(uint256)': [Function: bound ],
newActionPayed: [Function: bound ],
'0x390a6659d40234a1f75a3eef95ae4b7e83932ef00c81336c3c18e367f6b8070e': [Function: bound ],
'newActionPayed(uint256,address)': [Function: bound ],
allEvents: [Function: bound ]
},
_address: '0x756
1: Address
0x756Da73c0F6999DB4B81423D2E2146637Af6CD3C
2: Contract
3: Methods
{
buildAction: [Function: bound _createTxObject],
'0xe9ba0aa8': [Function: bound _createTxObject],
'buildAction(uint256)': [Function: bound _createTxObject],
createAction: [Function: bound _createTxObject],
'0xfa649172': [Function: bound _createTxObject],
'createAction(uint256,uint256)': [Function: bound _createTxObject],
getActionIsPayed: [Function: bound _createTxObject],
'0xf8f87f8d': [Function: bound _createTxObject],
'getActionIsPayed(uint256)': [Function: bound _createTxObject],
getActions: [Function: bound _createTxObject],
'0x328dd982': [Function: bound _createTxObject],
'getActions(uint256)': [Function: bound _createTxObject]
}
4: call
4b: a {
call: [Function: bound _executeMethod] {
request: [Function: bound _executeMethod]
},
send: [Function: bound _executeMethod] {
request: [Function: bound _executeMethod]
},
encodeABI: [Function: bound _encodeMethodABI],
estimateGas: [Function: bound _executeMethod],
createAccessList: [Function: bound _executeMethod],
arguments: [ '2' ],
_method: {
inputs: [ [Object] ],
name: 'getActionIsPayed',
outputs: [ [Object] ],
stateMutability: 'view',
type: 'function',
constant: true,
payable: undefined,
signature: '0xf8f87f8d'
},
_parent: Contract {
setProvider: [Function (anonymous)],
currentProvider: [Getter/Setter],
_requestManager: RequestManager {
provider: [HttpProvider],
providers: [Object],
subscriptions: Map(0) {}
},
givenProvider: null,
providers: {
WebsocketProvider: [Function: WebsocketProvider],
HttpProvider: [Function: HttpProvider],
IpcProvider: [Function: IpcProvider]
},
_provider: HttpProvider {
withCredentials: false,
timeout: 0,
headers: undefined,
agent: undefined,
connected: false,
host: 'http://localhost:8545',
httpAgent: [Agent]
},
setRequestManager: [Function (anonymous)],
BatchRequest: [Function: bound Batch],
extend: [Function: ex] {
formatters: [Object],
utils: [Object],
Method: [Function: Method]
},
clearSubscriptions: [Function (anonymous)],
options: { address: [Getter/Setter], jsonInterface: [Getter/Setter] },
handleRevert: [Getter/Setter],
defaultCommon: [Getter/Setter],
defaultHardfork: [Getter/Setter],
defaultChain: [Getter/Setter],
transactionPollingTimeout: [Getter/Setter],
transactionPollingInterval: [Getter/Setter],
transactionConfirmationBlocks: [Getter/Setter],
transactionBlockTimeout: [Getter/Setter],
blockHeaderTimeout: [Getter/Setter],
defaultAccount: [Getter/Setter],
defaultBlock: [Getter/Setter],
methods: {
buildAction: [Function: bound _createTxObject],
'0xe9ba0aa8': [Function: bound _createTxObject],
'buildAction(uint256)': [Function: bound _createTxObject],
createAction: [Function: bound _createTxObject],
'0xfa649172': [Function: bound _createTxObject],
'createAction(uint256,uint256)': [Function: bound _createTxObject],
getActionIsPayed: [Function: bound _createTxObject],
'0xf8f87f8d': [Function: bound _createTxObject],
'getActionIsPayed(uint256)': [Function: bound _createTxObject],
getActions: [Function: bound _createTxObject],
'0x328dd982': [Function: bound _createTxObject],
'getActions(uint256)': [Function: bound _createTxObject]
},
events: {
newActionCreated: [Function: bound ],
'0xd11c44ba953fddba8d7004c43ef1c4125a2d5e6dc0227de355ee9b9692ce4a05': [Function: bound ],
'newActionCreated(uint256)': [Function: bound ],
newActionPayed: [Function: bound ],
'0x390a6659d40234a1f75a3eef95ae4b7e83932ef00c81336c3c18e367f6b8070e': [Function: bound ],
'newActionPayed(uint256,address)': [Function: bound ],
allEvents: [Function: bound ]
},
_address: '0x756
Sorry for long messages, thanks to anyone that can help
Jun 27, 2022, 3:40 PM
are you sure that nodejs script connected to ganache rpc? do something simple to check it, like get balance or read contract
Jun 27, 2022, 3:45 PM
I'm trying to, this call is a simple view function, it should go easy... how can i get balance of the contract?
Jun 27, 2022, 3:45 PM
get balance of ganache-generated wallets and you’ll be sure about correct connections
const Web3 = require("web3")
const web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/v3/YOUR_PROJECT_ID"))
web3.eth.getBalance("0x52bc44d5378309EE2abF1539BF71dE1b7d7bE3b5", function(err, result) {
if (err) {
console.log(err)
} else {
console.log(web3.utils.fromWei(result, "ether") + " ETH")
}
})
const web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/v3/YOUR_PROJECT_ID"))
web3.eth.getBalance("0x52bc44d5378309EE2abF1539BF71dE1b7d7bE3b5", function(err, result) {
if (err) {
console.log(err)
} else {
console.log(web3.utils.fromWei(result, "ether") + " ETH")
}
})
Jun 27, 2022, 3:51 PM
Thanks a lot Tesla ... I found out my idiot error... I was still going to localhost:8545 even after deploy on docker... i'm a f**ing idiot :)
Jun 27, 2022, 3:51 PM
you are not alone
Jun 27, 2022, 3:52 PM
classic error, we all do lol
but if its a faucet with a frontend you could create a function that calls to getBalance of the contract and update the balance value once the tx for sending the tokens i confirmed
Jun 27, 2022, 3:57 PM
yes it’s nice solution, also be able to put some delay function, couse it’s blockchain and nothing will be happen immediately
Jun 27, 2022, 4:03 PM
u're right! that's why using etheres i do something like this
try{
const tx = await contract.Function(blabla)
await tx.wait()
(at this point the tx has been confirmed so i can keep doing things)
updateBalance()
}
catch(error){
//if tx failed show it here
}
try{
const tx = await contract.Function(blabla)
await tx.wait()
(at this point the tx has been confirmed so i can keep doing things)
updateBalance()
}
catch(error){
//if tx failed show it here
}
i got it from here, some time ago i used this app to learn how to swap a token on pcs https://amm.kiemtienonline360.com/
another way that doesnt need to have it deployed to bsc would be to deploy the router on remix too and use the address from remix
Jun 27, 2022, 4:51 PM