/
04i_contract_interaction
59 lines (39 loc) · 1.97 KB
/
04i_contract_interaction
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const { ethers } = require("ethers");
const provider = new ethers.JsonRpcProvider(`https://sepolia.infura.io/v3/8b9750710d56460d940aeff47967c4ba`);
const privateKey = 'cd82a7d0d6e528322e8c26f9ccbc18767543786d073c48ef38a753f29b1e8f39';
const wallet = new ethers.Wallet(privateKey, provider);
const abiWETH = [
"function balanceOf(address) public view returns(uint)",
"function deposit() public payable",
"function transfer(address, uint) public returns (bool)",
"function withdraw(uint) public",
];
const addressWETH = "0xb16F35c0Ae2912430DAc15764477E179D9B9EbEa"; // weth contract address
const contract = new ethers.Contract(addressWETH, abiWETH, wallet);
const main = async () => {
const address = await wallet.getAddress();
console.log(`Read WETH balance`);
const balanceWETH = await contract.balanceOf(address);
console.log(`WETH balance before deposit: ${ethers.formatEther(balanceWETH)}`);
const balanceETH = await provider.getBalance(wallet)
if(ethers.formatEther(balanceETH) > 0.0015){
console.log(`Balance of Eth: ${ethers.formatEther(balanceETH)}`);
console.log("Call the deposit() function to deposit 0.001 ETH")
const tx = await contract.deposit({value: ethers.parseEther('0.001')})
await tx.wait()
console.log(tx)
const balanceWETH_deposit = await contract.balanceOf(address)
console.log(`WETH balance after deposit: ${ethers.formatEther(balanceWETH_deposit)}`)
console.log("Call the transfer() function to transfer 0.001 WETH to Vitalik")
const tx2 = await contract.transfer("vitalik.eth", ethers.parseEther("0.001"))
await tx2.wait()
const balanceWETH_transfer = await contract.balanceOf(address)
console.log(`WETH balance after transfer: ${ethers.formatEther(balanceWETH_transfer)}`)
}else{
// If ETH is not enough
console.log("ETH is not enough, go to the faucet to get some Sepolia ETH")
console.log("1. Chainlink faucet: https://faucets.chain.link")
console.log("2. Paradigm faucet: https://faucet.paradigm.xyz/")
}
}
main();