-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathuse-paymaster.ts
71 lines (52 loc) · 2.4 KB
/
use-paymaster.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
import * as ethers from "ethers";
import { promises as fs } from "fs";
import { Provider, utils, Wallet } from "zksync-web3";
import { localConfig } from "../../config";
import { Buffer } from "../../entities";
import { Helper } from "../../helper";
import type { HardhatRuntimeEnvironment } from "hardhat/types";
const helper = new Helper();
export default async function (hre: HardhatRuntimeEnvironment) {
const bufferRoute = "src/playbook/";
const PAYMASTER_ADDRESS = await helper.getStringFromFile(bufferRoute + Buffer.paymaster);
const TOKEN_ADDRESS = await helper.getStringFromFile(bufferRoute + Buffer.customToken);
const EMPTY_WALLET_PRIVATE_KEY = await helper.getStringFromFile(bufferRoute + Buffer.emptyWalletPrivateKey);
const provider = new Provider(localConfig.L2Network);
const emptyWallet = new Wallet(EMPTY_WALLET_PRIVATE_KEY, provider);
function getToken(hre: HardhatRuntimeEnvironment, wallet: Wallet) {
const artifact = hre.artifacts.readArtifactSync("MyERC20");
return new ethers.Contract(TOKEN_ADDRESS, artifact.abi, wallet);
}
const ethBalance = await emptyWallet.getBalance();
if (!ethBalance.eq(0)) {
throw new Error("The wallet is not empty");
}
console.log(`Balance of the user before mint: ${await emptyWallet.getBalance(TOKEN_ADDRESS)}`);
const erc20 = getToken(hre, emptyWallet);
const gasPrice = await provider.getGasPrice();
const paymasterParams = utils.getPaymasterParams(PAYMASTER_ADDRESS, {
type: "ApprovalBased",
token: TOKEN_ADDRESS,
minimalAllowance: ethers.BigNumber.from(1),
innerInput: new Uint8Array(),
});
const gasLimit = await erc20.estimateGas.mint(emptyWallet.address, 100, {
customData: {
gasPerPubdata: utils.DEFAULT_GAS_PER_PUBDATA_LIMIT,
paymasterParams: paymasterParams,
},
});
gasPrice.mul(gasLimit.toString());
const mintTx = await erc20.mint(emptyWallet.address, 90, {
customData: {
paymasterParams: paymasterParams,
gasPerPubdata: utils.DEFAULT_GAS_PER_PUBDATA_LIMIT,
},
});
const receipt = await mintTx.wait();
console.log(`Balance of the user after mint: ${await emptyWallet.getBalance(TOKEN_ADDRESS)}`);
console.log(`Balance of the user after mint: ${await emptyWallet.getBalance(TOKEN_ADDRESS)}`);
await fs.writeFile(Buffer.paymasterTx, receipt.transactionHash);
console.log(`Transaction hash: ${receipt.transactionHash}`);
return receipt.transactionHash;
}