Skip to content

Commit

Permalink
Update scripts (#275)
Browse files Browse the repository at this point in the history
* updated scripts to use hre

* ver bump
  • Loading branch information
joshuafernandes authored Sep 4, 2023
1 parent 33aa81b commit 6e5b295
Show file tree
Hide file tree
Showing 20 changed files with 9,067 additions and 1,592 deletions.
18 changes: 9 additions & 9 deletions files/besu/smart_contracts/scripts/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ module.exports = {
},
},
accounts: {
"0xfe3b557e8fb62b89f4916b721be55ceb828dbd73": {
privateKey:
"0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63",
a: {
address: "0xfe3b557e8fb62b89f4916b721be55ceb828dbd73",
privateKey: "0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63",
},
"0x627306090abaB3A6e1400e9345bC60c78a8BEf57": {
privateKey:
"0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3",
b: {
address: "0x627306090abaB3A6e1400e9345bC60c78a8BEf57",
privateKey: "0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3",
},
"0xf17f52151EbEF6C7334FAD080c5704D77216b732": {
privateKey:
"0xae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f",
c: {
address: "0xf17f52151EbEF6C7334FAD080c5704D77216b732",
privateKey: "0xae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f",
},
},
};
88 changes: 88 additions & 0 deletions files/besu/smart_contracts/scripts/public/hre_1559_public_tx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const path = require('path');
const fs = require('fs-extra');
var ethers = require('ethers');

// RPCNODE details
const { tessera, besu } = require("../keys.js");
const host = besu.rpcnode.url;
const accountPrivateKey = besu.rpcnode.accountPrivateKey;

// abi and bytecode generated from simplestorage.sol:
// > solcjs --bin --abi simplestorage.sol
const contractJsonPath = path.resolve(__dirname, '../../','contracts','Counter.json');
const contractJson = JSON.parse(fs.readFileSync(contractJsonPath));
const contractAbi = contractJson.abi;
const contractBytecode = contractJson.evm.bytecode.object

async function getValueAtAddress(provider, deployedContractAbi, deployedContractAddress){
const contract = new ethers.Contract(deployedContractAddress, deployedContractAbi, provider);
const res = await contract.getCount();
console.log("Obtained value at deployed contract is: "+ res);
return res
}

// You need to use the accountAddress details provided to Quorum to send/interact with contracts
async function incrementValueAtAddress(provider, wallet, deployedContractAbi, deployedContractAddress){
const contract = new ethers.Contract(deployedContractAddress, deployedContractAbi, provider);
const contractWithSigner = contract.connect(wallet);
const tx = await contractWithSigner.incrementCounter();
// verify the updated value
await tx.wait();
// const res = await contract.get();
// console.log("Obtained value at deployed contract is: "+ res);
return tx;
}

async function decrementValueAtAddress(provider, wallet, deployedContractAbi, deployedContractAddress){
const contract = new ethers.Contract(deployedContractAddress, deployedContractAbi, provider);
const contractWithSigner = contract.connect(wallet);
const tx = await contractWithSigner.decrementCounter();
// verify the updated value
await tx.wait();
// const res = await contract.get();
// console.log("Obtained value at deployed contract is: "+ res);
return tx;
}

async function createContract(provider, wallet, contractAbi, contractByteCode) {
const feeData = await provider.getFeeData();
const factory = new ethers.ContractFactory(contractAbi, contractByteCode, wallet);
const contract = await factory.deploy({
chainId: 1337,
type: 2,
maxPriorityFeePerGas: feeData["maxPriorityFeePerGas"],
maxFeePerGas: feeData["maxFeePerGas"],
});
// The contract is NOT deployed yet; we must wait until it is mined
const deployed = await contract.waitForDeployment();
//The contract is deployed now
return contract
};

async function main(){
const provider = new ethers.JsonRpcProvider(host);
const wallet = new ethers.Wallet(accountPrivateKey, provider);

createContract(provider, wallet, contractAbi, contractBytecode)
.then(async function(contract){
console.log(contract);
contractAddress = await contract.getAddress();
console.log("Use the smart contracts 'get' function to read the contract's initialized value .. " )
await getValueAtAddress(provider, contractAbi, contractAddress);
console.log("Use the smart contracts 'increment' function to update that value .. " );
await incrementValueAtAddress(provider, wallet, contractAbi, contractAddress );
console.log("Verify the updated value that was set .. " )
await getValueAtAddress(provider, contractAbi, contractAddress);
console.log("Use the smart contracts 'decrement' function to update that value .. " );
await decrementValueAtAddress(provider, wallet, contractAbi, contractAddress );
console.log("Verify the updated value that was set .. " )
await getValueAtAddress(provider, contractAbi, contractAddress);
})
.catch(console.error);
}

if (require.main === module) {
main();
}

module.exports = exports = main
55 changes: 55 additions & 0 deletions files/besu/smart_contracts/scripts/public/hre_eth_tx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const path = require('path');
const fs = require('fs-extra');
var ethers = require('ethers');

// member1 details
const { accounts, besu } = require("../keys.js");
const host = besu.rpcnode.url;
// one of the seeded accounts
const accountAPrivateKey = accounts.a.privateKey;

async function main(){
const provider = new ethers.JsonRpcProvider(host);

const walletA = new ethers.Wallet(accountAPrivateKey, provider);
var accountABalance = await provider.getBalance(walletA.address);
console.log("Account A has balance of: " + accountABalance);

// create a new account to use to transfer eth to
const walletB = ethers.Wallet.createRandom()
var accountBBalance = await provider.getBalance(walletB.address);
console.log("Account B has balance of: " + accountBBalance);

const nonce = await provider.getTransactionCount(walletA.address);
const feeData = await provider.getFeeData();
const gasLimit = await provider.estimateGas({from: walletA.address, value: ethers.parseEther("0.01")});

// send some eth from A to B
const txn = {
nonce: nonce,
from: walletA.address,
to: walletB.address,
value: 0x10, //amount of eth to transfer
gasPrice: feeData.gasPrice, //ETH per unit of gas
gasLimit: gasLimit //max number of gas units the tx is allowed to use
};

console.log("create and sign the txn")
const signedTx = await walletA.sendTransaction(txn);
await signedTx.wait();
console.log("tx transactionHash: " + signedTx.hash);

//After the transaction there should be some ETH transferred
accountABalance = await provider.getBalance(walletA.address);
console.log("Account A has balance of: " + accountABalance);
accountBBalance = await provider.getBalance(walletB.address);
console.log("Account B has balance of: " + accountBBalance);

}

if (require.main === module) {
main();
}

module.exports = exports = main

Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,25 @@ async function createContract(provider, wallet, contractAbi, contractByteCode, c
const factory = new ethers.ContractFactory(contractAbi, contractByteCode, wallet);
const contract = await factory.deploy(contractInit);
// The contract is NOT deployed yet; we must wait until it is mined
const deployed = await contract.deployTransaction.wait()
const deployed = await contract.waitForDeployment();
//The contract is deployed now
return contract
};

async function main(){
const provider = new ethers.providers.JsonRpcProvider(host);
const provider = new ethers.JsonRpcProvider(host);
const wallet = new ethers.Wallet(accountPrivateKey, provider);

createContract(provider, wallet, contractAbi, contractBytecode, 47)
.then(async function(contract){
console.log("Contract deployed at address: " + contract.address);
contractAddress = await contract.getAddress();
console.log("Contract deployed at address: " + contractAddress);
console.log("Use the smart contracts 'get' function to read the contract's constructor initialized value .. " )
await getValueAtAddress(provider, contractAbi, contract.address);
await getValueAtAddress(provider, contractAbi, contractAddress);
console.log("Use the smart contracts 'set' function to update that value to 123 .. " );
await setValueAtAddress(provider, wallet, contractAbi, contract.address, 123 );
await setValueAtAddress(provider, wallet, contractAbi, contractAddress, 123 );
console.log("Verify the updated value that was set .. " )
await getValueAtAddress(provider, contractAbi, contract.address);
await getValueAtAddress(provider, contractAbi, contractAddress);
// await getAllPastEvents(host, contractAbi, tx.contractAddress);
})
.catch(console.error);
Expand Down
114 changes: 0 additions & 114 deletions files/besu/smart_contracts/scripts/public/public_tx.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const host = besu.rpcnode.url;
async function main(){
const web3 = new Web3(host);
//pre seeded account - test account only
const privateKeyA = accounts['0x627306090abaB3A6e1400e9345bC60c78a8BEf57'].privateKey;

const privateKeyA = accounts.a.privateKey;
const accountA = web3.eth.accounts.privateKeyToAccount(privateKeyA);
var accountABalance = web3.utils.fromWei(await web3.eth.getBalance(accountA.address));
console.log("Account A has balance of: " + accountABalance);
Expand Down
Loading

0 comments on commit 6e5b295

Please sign in to comment.