-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Trackerming
committed
Jun 19, 2024
1 parent
f97df20
commit 44c69b9
Showing
4 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ethers } from 'ethers'; | ||
import { getPrivateKey } from './deploy-contract-web3'; | ||
import { getContractInfo } from './call-contract-web3'; | ||
|
||
export function getWallet(rpc: string): ethers.Wallet { | ||
const provider = new ethers.JsonRpcProvider(rpc); | ||
// const signer = provider.getSigner(); | ||
const wallet = new ethers.Wallet(getPrivateKey(), provider); | ||
return wallet; | ||
} | ||
|
||
export async function callContract(wallet: ethers.Wallet, abi: any, contractAddress: string) { | ||
const contractInstance = new ethers.Contract(contractAddress, abi, wallet); | ||
const contractReader = new ethers.Contract(contractAddress, abi, wallet.provider); | ||
contractReader.on('Increment', (value) => { | ||
console.log(`listen in Increment, value ${value}`); | ||
}) | ||
let num = await contractReader.getNumber(); | ||
console.log(`ethers call contract before update num:${num}`) | ||
const addVal = 3; | ||
const incReceipt = await contractInstance.increment(addVal); | ||
await incReceipt.wait(); | ||
num = await contractReader.getNumber(); | ||
console.log(`ethers call contract update(inc ${addVal}), num is ${num}`); | ||
} |
This file contains 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,11 +1,14 @@ | ||
import { deploy } from './deploy-contract-web3'; | ||
import { getContractInfo, callContract, initWeb3, deployContract } from './call-contract-web3'; | ||
import { getWallet, callContract as callContractByEthers } from './call-contract-ethers'; | ||
// deploy contract | ||
(async function () { | ||
let { web3, account } = await initWeb3("wss://ethereum-sepolia-rpc.publicnode.com"); | ||
const rpc = "https://rpc2.sepolia.org"; | ||
let { web3, account } = await initWeb3(rpc); | ||
// let { contract, abi } = await deployContract(web3, account); | ||
let contract = "0x329f6ec542a232c44572659a8c47bd04beb02bc7"; | ||
let { abi } = getContractInfo(); | ||
if (contract) | ||
await callContract(web3, account, abi, contract); | ||
//await callContract(web3, account, abi, contract); | ||
await callContractByEthers(getWallet(rpc), abi, contract); | ||
})() |
This file contains 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