-
Notifications
You must be signed in to change notification settings - Fork 1
/
web3.ts
57 lines (49 loc) · 1.92 KB
/
web3.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
import Web3 from "web3";
import { CeloTxReceipt, Connection } from "@celo/connect";
import { LocalWallet } from "@celo/wallet-local";
import "dotenv/config";
import { AbiItem } from "web3-utils";
import { ERC20ABI } from "./erc20Abi";
import { PRIVATE_KEY, RECIPIENT, USDC_TOKEN_ADDRESS, USDC_ADAPTER_ADDRESS } from "./constants";
/**
* Boilerplate to create a web3js client and web3js-compatible wallet
*/
const web3 = new Web3("https://alfajores-forno.celo-testnet.org"); // Celo testnet
const celoWallet = new LocalWallet();
celoWallet.addAccount(`0x${PRIVATE_KEY}`);
const sender = celoWallet.getAccounts()[0];
const connection = new Connection(web3, celoWallet);
/**
* Set up ERC20 contract
*/
const contract = new web3.eth.Contract(ERC20ABI as AbiItem[], USDC_TOKEN_ADDRESS);
/**
* Makes a transaction to transfer ERC20 tokens using a fee currency
*/
async function erc20Transfer() {
console.log(`Initiating fee currency transaction...`);
const [symbol, decimals, tokenBalance] = await Promise.all([
contract.methods.symbol().call(),
contract.methods.decimals().call(),
contract.methods.balanceOf(sender).call(),
]);
console.log(`${symbol} balance of ${sender}: ${tokenBalance * Math.pow(10, -decimals)}`);
const transactionObject = contract.methods.transfer(
RECIPIENT,
web3.utils.toBN(0.01 * Math.pow(10, decimals))
);
const transactionReceipt = (await connection
.sendTransaction({
from: sender,
to: USDC_TOKEN_ADDRESS,
feeCurrency: USDC_ADAPTER_ADDRESS,
data: transactionObject.encodeABI(),
})
.then((tx) => tx.waitReceipt())
.catch((err) => console.error(err))) as CeloTxReceipt;
console.log(`Done! Transaction hash: ${transactionReceipt.transactionHash}`);
}
// Initiate ERC20 transfer with fee currency
erc20Transfer().catch((err) => {
console.error("An error occurred:", err);
});