-
Notifications
You must be signed in to change notification settings - Fork 75
FIX - web3js refactor #1244
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
Merged
Merged
FIX - web3js refactor #1244
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f6b99b7
fix: web3js refactor + llms
nhussein11 9cbbfab
Update smart-contracts/libraries/web3-js.md
eshaben d343844
Update smart-contracts/libraries/web3-js.md
eshaben ed1557f
Update smart-contracts/libraries/web3-js.md
eshaben 14657ae
Update smart-contracts/libraries/web3-js.md
eshaben 098f1d2
edit file location
eshaben 5503599
remove unnecessary title
eshaben aa23da1
update where to go next section
eshaben 3edfc9e
llms
eshaben 984316a
Merge branch 'staging/product-ia' into nhussein11/fix-web3js
eshaben 25c7e81
fix: unneeded tip
nhussein11 da361de
fix: wording
nhussein11 62eb455
fix: chain name to polkadotTestNet
nhussein11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 60 additions & 44 deletions
104
.snippets/code/smart-contracts/libraries/web3-js/deploy.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,82 +1,98 @@ | ||
| import { readFileSync } from 'fs'; | ||
| import { Web3 } from 'web3'; | ||
| const { writeFileSync, existsSync, readFileSync } = require('fs'); | ||
| const { join } = require('path'); | ||
| const { Web3 } = require('web3'); | ||
|
|
||
| const scriptsDir = __dirname; | ||
| const abisDir = join(__dirname, '../abis'); | ||
| const artifactsDir = join(__dirname, '../artifacts'); | ||
|
|
||
| const createProvider = (rpcUrl, chainId, chainName) => { | ||
| const web3 = new Web3(rpcUrl); | ||
| return web3; | ||
| }; | ||
|
|
||
| const getAbi = (contractName) => { | ||
| try { | ||
| return JSON.parse(readFileSync(`${contractName}.json`), 'utf8'); | ||
| const abiPath = join(abisDir, `${contractName}.json`); | ||
| return JSON.parse(readFileSync(abiPath, 'utf8')); | ||
| } catch (error) { | ||
| console.error( | ||
| `β Could not find ABI for contract ${contractName}:`, | ||
| error.message | ||
| `Could not find ABI for contract ${contractName}:`, | ||
| error.message, | ||
| ); | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| const getByteCode = (contractName) => { | ||
| try { | ||
| return `0x${readFileSync(`${contractName}.polkavm`).toString('hex')}`; | ||
| const bytecodePath = join(artifactsDir, `${contractName}.bin`); | ||
| const bytecode = readFileSync(bytecodePath, 'utf8').trim(); | ||
| return bytecode.startsWith('0x') ? bytecode : `0x${bytecode}`; | ||
| } catch (error) { | ||
| console.error( | ||
| `β Could not find bytecode for contract ${contractName}:`, | ||
| error.message | ||
| `Could not find bytecode for contract ${contractName}:`, | ||
| error.message, | ||
| ); | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| export const deploy = async (config) => { | ||
| const deployContract = async (contractName, privateKey, providerConfig) => { | ||
| console.log(`Deploying ${contractName}...`); | ||
| try { | ||
| // Initialize Web3 with RPC URL | ||
| const web3 = new Web3(config.rpcUrl); | ||
| const web3 = createProvider( | ||
| providerConfig.rpc, | ||
| providerConfig.chainId, | ||
| providerConfig.name, | ||
| ); | ||
|
|
||
| // Prepare account | ||
| const account = web3.eth.accounts.privateKeyToAccount(config.privateKey); | ||
| const formattedPrivateKey = privateKey.startsWith('0x') ? privateKey : `0x${privateKey}`; | ||
| const account = web3.eth.accounts.privateKeyToAccount(formattedPrivateKey); | ||
| web3.eth.accounts.wallet.add(account); | ||
| web3.eth.defaultAccount = account.address; | ||
|
|
||
| // Load abi | ||
| const abi = getAbi('Storage'); | ||
|
|
||
| // Create contract instance | ||
| const abi = getAbi(contractName); | ||
| const bytecode = getByteCode(contractName); | ||
| const contract = new web3.eth.Contract(abi); | ||
|
|
||
| // Prepare deployment | ||
| const deployTransaction = contract.deploy({ | ||
| data: getByteCode('Storage'), | ||
| arguments: [], // Add constructor arguments if needed | ||
| const deployTx = contract.deploy({ | ||
| data: bytecode, | ||
| }); | ||
|
|
||
| // Estimate gas | ||
| const gasEstimate = await deployTransaction.estimateGas({ | ||
| from: account.address, | ||
| }); | ||
|
|
||
| // Get current gas price | ||
| const gas = await deployTx.estimateGas(); | ||
| const gasPrice = await web3.eth.getGasPrice(); | ||
|
|
||
| // Send deployment transaction | ||
| const deployedContract = await deployTransaction.send({ | ||
| console.log(`Estimated gas: ${gas}`); | ||
| console.log(`Gas price: ${web3.utils.fromWei(gasPrice, 'gwei')} gwei`); | ||
|
|
||
| const deployedContract = await deployTx.send({ | ||
| from: account.address, | ||
| gas: gasEstimate, | ||
| gas: gas, | ||
| gasPrice: gasPrice, | ||
| }); | ||
|
|
||
| // Log and return contract details | ||
| console.log(`Contract deployed at: ${deployedContract.options.address}`); | ||
| return deployedContract; | ||
| const address = deployedContract.options.address; | ||
| console.log(`Contract ${contractName} deployed at: ${address}`); | ||
|
|
||
| const addressesFile = join(scriptsDir, 'contract-address.json'); | ||
| const addresses = existsSync(addressesFile) | ||
| ? JSON.parse(readFileSync(addressesFile, 'utf8')) | ||
| : {}; | ||
|
|
||
| addresses[contractName] = address; | ||
| writeFileSync(addressesFile, JSON.stringify(addresses, null, 2), 'utf8'); | ||
| } catch (error) { | ||
| console.error('Deployment failed:', error); | ||
| throw error; | ||
| console.error(`Failed to deploy contract ${contractName}:`, error); | ||
| } | ||
| }; | ||
|
|
||
| // Example usage | ||
| const deploymentConfig = { | ||
| rpcUrl: 'INSERT_RPC_URL', | ||
| privateKey: 'INSERT_PRIVATE_KEY', | ||
| contractName: 'INSERT_CONTRACT_NAME', | ||
| const providerConfig = { | ||
| rpc: 'https://testnet-passet-hub-eth-rpc.polkadot.io', // TODO: replace to `https://services.polkadothub-rpc.com/testnet` when ready | ||
| chainId: 420420422, | ||
| name: 'polkadot-hub-testnet', | ||
nhussein11 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| deploy(deploymentConfig) | ||
| .then((contract) => console.log('Deployment successful')) | ||
| .catch((error) => console.error('Deployment error')); | ||
| const privateKey = 'INSERT_PRIVATE_KEY'; | ||
|
|
||
| deployContract('Storage', privateKey, providerConfig); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.