forked from matter-labs/l2-intro-pre-ethdenver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
use-erc20.ts
74 lines (59 loc) · 2.13 KB
/
use-erc20.ts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Wallet, Provider, Contract } from "zksync-web3";
import * as ethers from "ethers";
import { HardhatRuntimeEnvironment } from "hardhat/types";
// load env file
import dotenv from "dotenv";
dotenv.config();
const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY || "";
if (!PRIVATE_KEY)
throw "⛔️ Private key not detected! Add it to the .env file!";
// load contract artifact. Make sure to compile first!
import * as ContractArtifact from "../artifacts-zk/contracts/zkToken.sol/zkToken.json";
// Address of the contract on zksync testnet
const TOKEN_ADDRESS = "";
// 0x address of the wallet that will receive a transfer
const DESTINATION_WALLET = "";
if (!TOKEN_ADDRESS) throw "⛔️ ERC20 token address not provided";
// An example of a deploy script that will deploy and call a simple contract.
export default async function (hre: HardhatRuntimeEnvironment) {
console.log(`Running script to transfer token ${TOKEN_ADDRESS}`);
// Initialize the signer.
// @ts-ignore
const provider = new Provider(hre.userConfig.networks?.zkSyncTestnet?.url);
const signer = new Wallet(PRIVATE_KEY, provider);
const tokenContract = new Contract(
TOKEN_ADDRESS,
ContractArtifact.abi,
signer
);
const AMOUNT = "12";
console.log(
`Account ${signer.address} balance is: ${await tokenContract.balanceOf(
signer.address
)} tokens`
);
console.log(
`Account ${DESTINATION_WALLET} balance is: ${await tokenContract.balanceOf(
DESTINATION_WALLET
)} tokens`
);
// transfer tokens
const transferHandle = await tokenContract.transfer(
DESTINATION_WALLET,
ethers.utils.parseEther(AMOUNT)
);
// Wait until the transaction is processed on zkSync
await transferHandle.wait();
console.log(`Transfer completed in trx ${transferHandle.hash}`);
console.log(
`Account ${signer.address} balance now is: ${await tokenContract.balanceOf(
signer.address
)} tokens`
);
console.log(
`Account ${DESTINATION_WALLET} balance now is: ${await tokenContract.balanceOf(
DESTINATION_WALLET
)} tokens`
);
console.log(`Current token supply is ${await tokenContract.totalSupply()}`);
}