diff --git a/README.md b/README.md index f3b9908..512b2af 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,7 @@ -> [!WARNING] -> Currently this demo only works with the `STBLTEST` adapter and token, for which there is no -> public faucet. If and when USDC deploys the adapter and the Celo community votes to make USDC an -> accepted fee currency, we'll have to update the addresses in [`constants.ts`](./constants.ts). - # Fee Currencies on Celo > [!TIP] -> Code examples for -> [docs.celo.org > Paying for Gas in Tokens](https://docs.celo.org/protocol/transaction/erc20-transaction-fees#alfajores-testnet). +> Code examples for [docs.celo.org > Paying for Gas in Tokens](https://docs.celo.org/protocol/transaction/erc20-transaction-fees#alfajores-testnet). > Visit that page for more information. Fee currency transactions are unique to Celo. Fee currency transactions are made using a custom @@ -22,28 +16,55 @@ JS/TS client libraries. ### `viem` (Recommended ✅) -We recommend you build with `viem`, if you are starting from scratch. See demo in -[`viem.ts`](/viem.ts). +We recommend you build with `viem`, if you are starting from scratch. For a demo, see +[`viem.ts`](/viem.ts). You can run the demo as follows: + +```sh +$ yarn ts-node viem.ts + +Initiating fee currency transaction... +USDC balance of 0x303C22e6ef01CbA9d03259248863836CB91336D5: 10 +Done! Transaction hash: 0x5242bb5340baa534f14285feac24fd8eac57cdd779b9a5cb2e88bbb5c3e6bb3d +✨ Done in 4.66s. +``` ### `web3js` + `@celo/connect` (Discouraged ❌) -We don't recommend you build with `web3js` and `@celo/connect`, if you are starting from scratch. +To make fee currency transactions with `web3js` you need to use it in conjunction with +`@celo/connect`. Bu, we don't recommend you build with `web3js` and `@celo/connect`, if you are +starting from scratch. That's because `@celo/connect` only supports `web3js@1.10`, and not the +latest version `web3js@4.5.0`. Instead, we recommend you build with `viem`. -That's because `@celo/connect` only supports `web3js@1.10`, and not the latest version -`web3js@4.5.0`. +But, for completeness, we included a demo in +[`web3.ts`](/web3.ts) so you can see usage patterns. You can run the demo as follows: -Instead, we recommend you build with `viem`. But, for completeness, we included a demo in -[`web3.ts`](/web3.ts) so you can see usage patterns. +```sh +$ yarn ts-node web3.ts + +Initiating fee currency transaction... +USDC balance of 0x303c22e6ef01cba9d03259248863836cb91336d5: 9.989236 +Done! Transaction hash: 0x49fa99caa6c2a4aade1d3a979fedea5037542a7a7a39c70eeec14b7661a9b030 +✨ Done in 4.78s. +``` ### `@celo/contractkit` (Discouraged ❌) We don't recommend you build with `@celo/contractkit`, if you are starting from scratch. - That's because `@celo/contractkit` is a library for developers who want to interact with [core contracts](https://docs.celo.org/contract-addresses) like `Governance.sol` or `Election.sol`. +Instead, we recommend you build with `viem`. + +But, for completeness, we included a demo in [`contractkit.ts`](/contractkit.ts) so you can see +usage patterns. You can run the demo as follows: + +```sh +$ yarn ts-node contractkit.ts -Instead, we recommend you build with `viem`. But, for completeness, we included a demo in -[`contractkit.ts`](/contractkit.ts) so you can see usage patterns. +Initiating fee currency transaction... +USDC balance of 0x303C22e6ef01CbA9d03259248863836CB91336D5: 9.957260999999999 +Done! Transaction hash: 0x6a2c32283971ca90463fb58b00d4fe4c4d75b340bc3f646667a4bdc368f31a65 +✨ Done in 5.85s. +``` ### Raw transaction (Experts-only 🟠) @@ -57,22 +78,20 @@ envelopes. But, if you would really like to construct fee currency transactions this way, you can learn more about the transaction arguments and format here: `celo-org/txtypes`. -## Demo usage +## Demos ### Requirements - The private key of a Celo wallet with at least 1 USDC on the Alfajores testnet (you can get free USDC testnet tokens from [faucet.circle.com](https://faucet.circle.com/)) -### Installing Node.js - -We use [Node.js](https://nodejs.org/en/) to run the project locally. You need to install the -**Node.js version** specified in [.nvmrc](./.nvmrc). To do so, run: +- We use [Node.js](https://nodejs.org/en/) to run the project locally. You need to install the + **Node.js version** specified in [.nvmrc](./.nvmrc). To do so, run: -```sh -$ nvm install -$ nvm use -``` + ```sh + $ nvm install + $ nvm use + ``` ### Installing dependencies @@ -109,7 +128,7 @@ When you execute a script, you will see something akin to this output: $ yarn ts-node viem.ts Initiating fee currency transaction... -STBLTEST balance of 0x303C22e6ef01CbA9d03259248863836CB91336D5: 59.946671 -Done! Transaction hash: 0x8646968a8fe025fc7fd47a4e754953ddb4fe2936b43c06d82313463cb7851071 -✨ Done in 8.23s. +USDC balance of 0x303C22e6ef01CbA9d03259248863836CB91336D5: 10 +Done! Transaction hash: 0x5242bb5340baa534f14285feac24fd8eac57cdd779b9a5cb2e88bbb5c3e6bb3d +✨ Done in 4.66s. ``` diff --git a/constants.ts b/constants.ts index d74e905..1be543e 100644 --- a/constants.ts +++ b/constants.ts @@ -1,9 +1,24 @@ import "dotenv/config"; -export const RECIPIENT = "0x22579CA45eE22E2E16dDF72D955D6cf4c767B0eF"; // arbitrary address -export const cUSD_CONTRACT_ADDRESS = "0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1"; // ERC20 -export const USDC_CONTRACT_ADDRESS = "0x780c1551C2Be3ea3B1f8b1E4CeDc9C3CE40da24E"; // ERC20 -export const USDC_ADAPTER_ADDRESS = "0xDB93874fE111F5a87Acc11Ff09Ee9450Ac6509AE"; // Adapter +/** + * Arbitrary recipient address. Feel free to change this to any address you like. + */ +export const RECIPIENT = "0x22579CA45eE22E2E16dDF72D955D6cf4c767B0eF"; + +/** + * cUSD: Fee currency that doesn't require an adapter + */ +export const cUSD_TOKEN_ADDRESS = "0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1"; + +/** + * USDC: Fee currency that requires an adapter + */ +export const USDC_TOKEN_ADDRESS = "0x2F25deB3848C207fc8E0c34035B3Ba7fC157602B"; +export const USDC_ADAPTER_ADDRESS = "0x4822e58de6f5e485eF90df51C41CE01721331dC0"; + +/** + * Private key of the account that will send the demo transaction + */ export const PRIVATE_KEY = process.env.PRIVATE_KEY; if (!PRIVATE_KEY) { throw new Error( diff --git a/contractkit.ts b/contractkit.ts index ed047cb..8f0bc8c 100644 --- a/contractkit.ts +++ b/contractkit.ts @@ -1,5 +1,5 @@ import { newKit } from "@celo/contractkit"; -import { PRIVATE_KEY, RECIPIENT, USDC_CONTRACT_ADDRESS, USDC_ADAPTER_ADDRESS } from "./constants"; +import { PRIVATE_KEY, RECIPIENT, USDC_TOKEN_ADDRESS, USDC_ADAPTER_ADDRESS } from "./constants"; import { ERC20ABI } from "./erc20Abi"; import { AbiItem, CeloTxReceipt } from "@celo/connect"; @@ -12,7 +12,7 @@ kit.connection.addAccount(`0x${PRIVATE_KEY}`); /** * Set up ERC20 contract */ -const contract = new kit.web3.eth.Contract(ERC20ABI as AbiItem[], USDC_CONTRACT_ADDRESS); +const contract = new kit.web3.eth.Contract(ERC20ABI as AbiItem[], USDC_TOKEN_ADDRESS); /** * Makes a transaction to transfer ERC20 tokens using a fee currency @@ -39,7 +39,7 @@ async function erc20Transfer() { const transactionReceipt = (await kit .sendTransaction({ from: sender, - to: USDC_CONTRACT_ADDRESS, + to: USDC_TOKEN_ADDRESS, feeCurrency: USDC_ADAPTER_ADDRESS, data: transactionObject.encodeABI(), }) diff --git a/viem.ts b/viem.ts index 5c42687..c943d1c 100644 --- a/viem.ts +++ b/viem.ts @@ -12,7 +12,7 @@ import { celoAlfajores } from "viem/chains"; import { PRIVATE_KEY, RECIPIENT, - USDC_CONTRACT_ADDRESS, + USDC_TOKEN_ADDRESS, USDC_ADAPTER_ADDRESS, } from "./constants"; @@ -33,7 +33,7 @@ const sender = privateKeyToAccount(`0x${PRIVATE_KEY}`); * Set up ERC20 contract */ const contract = getContract({ - address: USDC_CONTRACT_ADDRESS, + address: USDC_TOKEN_ADDRESS, abi: erc20Abi, client: { public: read, wallet: write }, }); diff --git a/web3.ts b/web3.ts index e06af58..15a66b6 100644 --- a/web3.ts +++ b/web3.ts @@ -4,7 +4,7 @@ import { LocalWallet } from "@celo/wallet-local"; import "dotenv/config"; import { AbiItem } from "web3-utils"; import { ERC20ABI } from "./erc20Abi"; -import { PRIVATE_KEY, RECIPIENT, USDC_CONTRACT_ADDRESS, USDC_ADAPTER_ADDRESS } from "./constants"; +import { PRIVATE_KEY, RECIPIENT, USDC_TOKEN_ADDRESS, USDC_ADAPTER_ADDRESS } from "./constants"; /** * Boilerplate to create a web3js client and web3js-compatible wallet @@ -18,7 +18,7 @@ const connection = new Connection(web3, celoWallet); /** * Set up ERC20 contract */ -const contract = new web3.eth.Contract(ERC20ABI as AbiItem[], USDC_CONTRACT_ADDRESS); +const contract = new web3.eth.Contract(ERC20ABI as AbiItem[], USDC_TOKEN_ADDRESS); /** * Makes a transaction to transfer ERC20 tokens using a fee currency @@ -41,7 +41,7 @@ async function erc20Transfer() { const transactionReceipt = (await connection .sendTransaction({ from: sender, - to: USDC_CONTRACT_ADDRESS, + to: USDC_TOKEN_ADDRESS, feeCurrency: USDC_ADAPTER_ADDRESS, data: transactionObject.encodeABI(), })