|
| 1 | +const path = require('path'); |
| 2 | +const fs = require('fs-extra'); |
| 3 | +var ethers = require('ethers'); |
| 4 | + |
| 5 | +// RPCNODE details |
| 6 | +const { tessera, besu } = require("../keys.js"); |
| 7 | +const host = besu.rpcnode.url; |
| 8 | +const accountPrivateKey = besu.rpcnode.accountPrivateKey; |
| 9 | + |
| 10 | +// abi and bytecode generated from simplestorage.sol: |
| 11 | +// > solcjs --bin --abi simplestorage.sol |
| 12 | +const contractJsonPath = path.resolve(__dirname, '../../','contracts','Counter.json'); |
| 13 | +const contractJson = JSON.parse(fs.readFileSync(contractJsonPath)); |
| 14 | +const contractAbi = contractJson.abi; |
| 15 | +const contractBytecode = contractJson.evm.bytecode.object |
| 16 | + |
| 17 | +async function getValueAtAddress(provider, deployedContractAbi, deployedContractAddress){ |
| 18 | + const contract = new ethers.Contract(deployedContractAddress, deployedContractAbi, provider); |
| 19 | + const res = await contract.getCount(); |
| 20 | + console.log("Obtained value at deployed contract is: "+ res); |
| 21 | + return res |
| 22 | +} |
| 23 | + |
| 24 | +// You need to use the accountAddress details provided to Quorum to send/interact with contracts |
| 25 | +async function incrementValueAtAddress(provider, wallet, deployedContractAbi, deployedContractAddress){ |
| 26 | + const contract = new ethers.Contract(deployedContractAddress, deployedContractAbi, provider); |
| 27 | + const contractWithSigner = contract.connect(wallet); |
| 28 | + const tx = await contractWithSigner.incrementCounter(); |
| 29 | + // verify the updated value |
| 30 | + await tx.wait(); |
| 31 | + // const res = await contract.get(); |
| 32 | + // console.log("Obtained value at deployed contract is: "+ res); |
| 33 | + return tx; |
| 34 | +} |
| 35 | + |
| 36 | +async function decrementValueAtAddress(provider, wallet, deployedContractAbi, deployedContractAddress){ |
| 37 | + const contract = new ethers.Contract(deployedContractAddress, deployedContractAbi, provider); |
| 38 | + const contractWithSigner = contract.connect(wallet); |
| 39 | + const tx = await contractWithSigner.decrementCounter(); |
| 40 | + // verify the updated value |
| 41 | + await tx.wait(); |
| 42 | + // const res = await contract.get(); |
| 43 | + // console.log("Obtained value at deployed contract is: "+ res); |
| 44 | + return tx; |
| 45 | +} |
| 46 | + |
| 47 | +async function createContract(provider, wallet, contractAbi, contractByteCode) { |
| 48 | + const feeData = await provider.getFeeData(); |
| 49 | + const factory = new ethers.ContractFactory(contractAbi, contractByteCode, wallet); |
| 50 | + const contract = await factory.deploy({ |
| 51 | + chainId: 1337, |
| 52 | + type: 2, |
| 53 | + maxPriorityFeePerGas: feeData["maxPriorityFeePerGas"], |
| 54 | + maxFeePerGas: feeData["maxFeePerGas"], |
| 55 | + }); |
| 56 | + // The contract is NOT deployed yet; we must wait until it is mined |
| 57 | + const deployed = await contract.waitForDeployment(); |
| 58 | + //The contract is deployed now |
| 59 | + return contract |
| 60 | +}; |
| 61 | + |
| 62 | +async function main(){ |
| 63 | + const provider = new ethers.JsonRpcProvider(host); |
| 64 | + const wallet = new ethers.Wallet(accountPrivateKey, provider); |
| 65 | + |
| 66 | + createContract(provider, wallet, contractAbi, contractBytecode) |
| 67 | + .then(async function(contract){ |
| 68 | + console.log(contract); |
| 69 | + contractAddress = await contract.getAddress(); |
| 70 | + console.log("Use the smart contracts 'get' function to read the contract's initialized value .. " ) |
| 71 | + await getValueAtAddress(provider, contractAbi, contractAddress); |
| 72 | + console.log("Use the smart contracts 'increment' function to update that value .. " ); |
| 73 | + await incrementValueAtAddress(provider, wallet, contractAbi, contractAddress ); |
| 74 | + console.log("Verify the updated value that was set .. " ) |
| 75 | + await getValueAtAddress(provider, contractAbi, contractAddress); |
| 76 | + console.log("Use the smart contracts 'decrement' function to update that value .. " ); |
| 77 | + await decrementValueAtAddress(provider, wallet, contractAbi, contractAddress ); |
| 78 | + console.log("Verify the updated value that was set .. " ) |
| 79 | + await getValueAtAddress(provider, contractAbi, contractAddress); |
| 80 | + }) |
| 81 | + .catch(console.error); |
| 82 | +} |
| 83 | + |
| 84 | +if (require.main === module) { |
| 85 | + main(); |
| 86 | +} |
| 87 | + |
| 88 | +module.exports = exports = main |
0 commit comments