Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REMIX IDE - web3-lib.ts and deploy_with_web3.ts scripts are not working #24

Closed
nhussein11 opened this issue Jan 20, 2025 · 15 comments
Closed

Comments

@nhussein11
Copy link

@pgherveou I’m trying to run the web3-lib.ts and deploy_with_web3.ts scripts on the Polkadot Remix IDE to check if the code is accurate because I need to replicate the script locally. However, when I run the script, it doesn’t work; the terminal only logs:

Invalid JSON RPC response: undefined

web3-lib.ts code:

import Web3 from 'web3'
import { Contract, ContractSendMethod, Options } from 'web3-eth-contract'

export const deploy = async (contractName: string, args: Array<any>, from?: string, gas?: number): Promise<Options> => {

  const web3 = new Web3(web3Provider)
  console.log(`deploying ${contractName}`)
  // Note that the script needs the ABI which is generated from the compilation artifact.
  // Make sure contract is compiled and artifacts are generated
  const artifactsPath = `browser/contracts/artifacts/${contractName}.json`

  const metadata = JSON.parse(await remix.call('fileManager', 'getFile', artifactsPath))

  const accounts = await web3.eth.getAccounts()

  const contract: Contract = new web3.eth.Contract(metadata.abi)

  const contractSend: ContractSendMethod = contract.deploy({
    data: metadata.data.bytecode.object,
    arguments: args
  })

  const gasEstimate = await contractSend.estimateGas({
    from: from || accounts[0],
  })

  const newContractInstance = await contractSend.send({
    from: from || accounts[0],
    gas: gas || gasEstimate
  })
  return newContractInstance.options
}

And deploy_with_web3.ts code:

import { deploy } from './web3-lib'

(async () => {
  try {
    const result = await deploy('Counter', [])
    console.log(`address: ${result.address}`)
  } catch (e) {
    console.log(e.message)
  }
})()

In case it helps, this is the smart contract I'm using, Counter.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Counter {
    int256 private count;
    event CountChanged(int256 newCount);

    function increment() public {
        count += 1;
        emit CountChanged(count);
    }

    function decrement() public {
        count -= 1;
        emit CountChanged(count);
    }

    function getCount() public view returns (int256) {
        return count;
    }
}
@smiasojed
Copy link

I was checking this in December and it was working fine. @pgherveou maybe it is related to gas encoding changes?

@nhussein11
Copy link
Author

I think this might be related to how web3.js estimates gas when deploying the transaction. I've been able to deploy successfully using ethers.js, and the snippets are very similar. That's the only difference I've found, it's probably worth investigating that point @smiasojed @pgherveou

@smiasojed
Copy link

smiasojed commented Jan 23, 2025

Yes, the web.js and ether.js get different gas estimations for the same transaction. The change which has been done is in gas encoding area so this is why I pointed this out.

@nhussein11
Copy link
Author

@smiasojed Could you please let me know if there is a solution for this issue? I'm trying to run the script locally to document web3js tailored for asset hub smart contracts, and this is a blocker

@athei
Copy link
Member

athei commented Jan 23, 2025

It is likely that it will be fixed by this: paritytech/polkadot-sdk#7281

You can try to run latest master if you don't wanna wait until this gets deployed to Westend.

@nhussein11
Copy link
Author

@athei thanks! Could you please ping me when this is deployed to Westend?

@athei
Copy link
Member

athei commented Jan 24, 2025

It was deployed yesterday afternoon already: https://assethub-westend.subscan.io/extrinsic/10597534-0?event=10597534-1

Can you still reproduce?

@smiasojed
Copy link

smiasojed commented Jan 24, 2025

Please modify web3-lib.ts to:

export const deploy = async (contractName: string, args: Array<any>, from?: string, gas?: number): Promise<Options> => {

  const web3 = new Web3(web3Provider)
  console.log(`deploying ${contractName}`)
  // Note that the script needs the ABI which is generated from the compilation artifact.
  // Make sure contract is compiled and artifacts are generated
  const artifactsPath = `browser/contracts/artifacts/${contractName}.json`

  const metadata = JSON.parse(await remix.call('fileManager', 'getFile', artifactsPath))

  const accounts = await web3.eth.getAccounts()

  const contract: Contract = new web3.eth.Contract(metadata.abi)

  const contractSend: ContractSendMethod = contract.deploy({
    data: metadata.data.bytecode.object,
    arguments: args
  })

  const gasEstimate = await contractSend.estimateGas({
    from: from || accounts[0],
  })

  const gasPrice = await web3.eth.getGasPrice()

  const newContractInstance = await contractSend.send({
    from: from || accounts[0],
    gas: gasEstimate,
    gasPrice: gasPrice
  })
  return newContractInstance.options
}

@nhussein11 please let me know if it helps

@nhussein11
Copy link
Author

nhussein11 commented Jan 24, 2025

Thanks, @smiasojed , but I tried it out and still getting: Invalid JSON RPC response: undefined

I copied and pasted your snippet in the web3-lib.ts file in remix and ran deploy_with_web3.ts:

Image

It seems that it's failing in the following line:

 const accounts = await web3.eth.getAccounts()

Since I can log "metadata" but never reaching to log "accounts"

  const metadata = JSON.parse(await remix.call('fileManager', 'getFile', artifactsPath))
  console.log("metadata", metadata)

  const accounts = await web3.eth.getAccounts()
  console.log("accounts", accounts)

@smiasojed
Copy link

@nhussein11 Please select Westend network in Deploy & run transaction tab, without it you will get an error like you see.

@nhussein11
Copy link
Author

That worked! Many thanks @smiasojed!!

@nhussein11
Copy link
Author

@smiasojed will the REMIX IDE be updated with the script you shared previously?

@smiasojed
Copy link

yes, I am working on an update. I will do it next week.

@pgherveou
Copy link

Looked into it,
looks like web3 is creating a tx with unexpected big maxPriorityFeePerGas and maxFeePerGas field, I will try to find out why. When you se t gasPrice I guess it ignores these values and use the legacy tx format.

@pgherveou
Copy link

pgherveou commented Jan 28, 2025

Ok this seems to be the main reason
https://github.com/web3/web3.js/blob/4.x/docs/docs/guides/09_web3_config/index.md?plain=1#L439-L443

defaultMaxPriorityFeePerGas

The defaultMaxPriorityFeePerGas option is used to set the defaultMaxPriorityFeePerGas value for EIP-1559 smart contract transactions (transaction type 0x2).

The default value of defaultMaxPriorityFeePerGas is 2500000000 (2.5gwei) in hexstring format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants