-
Notifications
You must be signed in to change notification settings - Fork 1
/
contractkit.ts
55 lines (45 loc) · 1.8 KB
/
contractkit.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
import { newKit } from "@celo/contractkit";
import { PRIVATE_KEY, RECIPIENT, USDC_TOKEN_ADDRESS, USDC_ADAPTER_ADDRESS } from "./constants";
import { ERC20ABI } from "./erc20Abi";
import { AbiItem, CeloTxReceipt } from "@celo/connect";
/**
* Boilerplate to create a ContractKit instance and ContractKit-compatible wallet
*/
const kit = newKit("https://alfajores-forno.celo-testnet.org"); // Celo testnet
kit.connection.addAccount(`0x${PRIVATE_KEY}`);
/**
* Set up ERC20 contract
*/
const contract = new kit.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 accounts = await kit.connection.getAccounts();
const sender = accounts[0];
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,
kit.web3.utils.toBN(0.01 * Math.pow(10, decimals))
);
const transactionReceipt = (await kit
.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);
});