From c87dc62e3e8c0907a26388143daee7175d899637 Mon Sep 17 00:00:00 2001 From: Nicolas Brugneaux Date: Mon, 27 Mar 2023 19:27:09 +0200 Subject: [PATCH] chore: split into two files --- .../sdk/contractkit/MIGRATION-TO-ETHERS.md | 108 +++++++++++++++++ .../{MIGRATION.md => MIGRATION-TO-VIEM.md} | 111 ++++++++---------- 2 files changed, 154 insertions(+), 65 deletions(-) create mode 100644 packages/sdk/contractkit/MIGRATION-TO-ETHERS.md rename packages/sdk/contractkit/{MIGRATION.md => MIGRATION-TO-VIEM.md} (53%) diff --git a/packages/sdk/contractkit/MIGRATION-TO-ETHERS.md b/packages/sdk/contractkit/MIGRATION-TO-ETHERS.md new file mode 100644 index 00000000000..e79a2680d09 --- /dev/null +++ b/packages/sdk/contractkit/MIGRATION-TO-ETHERS.md @@ -0,0 +1,108 @@ +# Draft: Migration document from Contractkit + +Hello devs 🌱 this is a migration path away from contractkit following the [public deprecation notice](https://forum.celo.org/t/sunsetting-contractkit/5337/1) of contractkit. This aims to give examples to help you move to [ethers](https://docs.ethers.org/). + +## Initialization + +```diff +- import Web3 from "web3"; +- import { newKitFromWeb3 } from "@celo/contractkit"; + +- const web3 = new Web3("https://alfajores-forno.celo-testnet.org"); +- const kit = newKitFromWeb3(web3); ++ import { providers } from 'ethers' ++ ++ const provider = new providers.JsonRpcProvider('https://alfajores-forno.celo-testnet.org') +``` + +## Basic usage + +While we cannot here show all the use-cases of contrackit or ethers or viem, let's try to give an overview of how they can be used for different goals. + +### Get address + +```diff +- const accounts = await kit.web3.eth.getAccounts(); ++ const accounts = await provider.listAccounts(); +const defaultAccount = accounts[0]; +``` + +### Get wallet + +```diff ++ import { Wallet } from 'ethers' + +- const wallet = kit.getWallet(); ++ const wallet = new Wallet('0x...', provider); +``` + +### Provider methods + +```diff +- const provider = kit.connection.web3.currentProvider +- kit.connection.getBlock(...) +- kit.connection.getTransaction(...) +provider.getBlock(...) +provider.getTransaction(...) +``` + +### Signer methods + +```diff +- const provider = kit.connection.web3.currentProvider +- const signer = provider.getSigner(kit.connection.defaultAccount) ++ const signer = provider.getSigner(address) ++ signer.sendTransaction(...) ++ signer.signMessage(...) +``` + +### Contract interaction + +I'll show the most "basic" interaction, which is a transfer. On CELO, it comes with a twist, you can transfer 4 currencies, CELO, cUSD, cEUR, and cREAL. + +You can get the addresses on these tokens by heading to the explorer and getting their abi and addresses, or you can also use our [registry contract](https://docs.celo.org/developer/contractkit/contracts-wrappers-registry). + +```ts +// this address is constant +const REGISTRY_CONTRACT_ADDRESS = '0x000000000000000000000000000000000000ce10' +const registry = new Contract(REGISTRY_CONTRACT_ADDRESS, registryAbi, wallet) + +async function getToken(token: string) { + const goldTokenAddress = await registry.getAddressForString(token) + return goldTokenAddress +} +async function CeloTokens(): Promise<[string, string][]> { + return Promise.all( + ['GoldToken', 'StableToken', 'StableTokenEUR', 'StableTokenBRL'].map(async (token) => [ + token, + await getToken(token), + ]) + ) +} +``` + +#### Balance + +```diff ++ import { tokenAbi } from './abi.json' + +- const contract = await kit.contracts.getGoldToken(); ++ const tokenAddress = '0x...' // Grabbed from the registry or from the explorer ++ const contract = new ethers.Contract(tokenAddress, tokenAbi, signer); +const balance = await contract.balanceOf(wallet.address); +``` + +#### Transfer + +Then, use the address of the token that you need and call the transfer method of the contract. + +```diff ++ import { tokenAbi } from './abi.json' + +- const contract = await kit.contracts.getGoldToken(); ++ const tokenAddress = '0x...' // Grabbed from the registry or from the explorer ++ const contract = new ethers.Contract(tokenAddress, tokenAbi, signer); +const txReceipt = await contract.transfer('0x...', amount); +``` + +For more in depth examples, I highly recommend checking out the extensive documentations of both [ethers](https://docs.ethers.org/) and [viem](https://viem.sh/). diff --git a/packages/sdk/contractkit/MIGRATION.md b/packages/sdk/contractkit/MIGRATION-TO-VIEM.md similarity index 53% rename from packages/sdk/contractkit/MIGRATION.md rename to packages/sdk/contractkit/MIGRATION-TO-VIEM.md index 68cc5d56e20..27cc837b286 100644 --- a/packages/sdk/contractkit/MIGRATION.md +++ b/packages/sdk/contractkit/MIGRATION-TO-VIEM.md @@ -1,24 +1,9 @@ # Draft: Migration document from Contractkit -Hello devs 🌱 this is a migration path away from contractkit following the [public deprecation notice](https://forum.celo.org/t/sunsetting-contractkit/5337/1) of contractkit. This aims to give examples to help you move to either [ethers](https://docs.ethers.org/) or [viem](https://viem.sh/). +Hello devs 🌱 this is a migration path away from contractkit following the [public deprecation notice](https://forum.celo.org/t/sunsetting-contractkit/5337/1) of contractkit. This aims to give examples to help you move to [viem](https://viem.sh/). ## Initialization -With ethers: - -```diff -- import Web3 from "web3"; -- import { newKitFromWeb3 } from "@celo/contractkit"; - -- const web3 = new Web3("https://alfajores-forno.celo-testnet.org"); -- const kit = newKitFromWeb3(web3); -+ import { providers } from 'ethers' -+ -+ const provider = new providers.JsonRpcProvider('https://alfajores-forno.celo-testnet.org') -``` - -With viem: - ```diff - import Web3 from "web3"; - import { newKitFromWeb3 } from "@celo/contractkit"; @@ -40,14 +25,6 @@ While we cannot here show all the use-cases of contrackit or ethers or viem, let ### Get address -With ethers: - -```diff -- const accounts = await kit.web3.eth.getAccounts(); -+ const accounts = await provider.listAccounts(); -const defaultAccount = accounts[0]; -``` - With viem: ```diff @@ -58,32 +35,21 @@ const defaultAccount = accounts[0]; ### Get wallet -With ethers: - -```diff -+ import { Wallet } from 'ethers' - -- const wallet = kit.getWallet(); -+ const wallet = new Wallet('0x...', provider); -``` - With viem: > [viem does not currently support](<[source](https://viem.sh/docs/ethers-migration.html#viem-11)>) client-side signing (it's coming shortly!) – until then, you can use an Ethers Wallet -### Provider methods - -With ethers: - ```diff -- const provider = kit.connection.web3.currentProvider -- kit.connection.getBlock(...) -- kit.connection.getTransaction(...) -provider.getBlock(...) -provider.getTransaction(...) ++ const walletClient = createWalletClient({ ++ transport: http(celoAlfajores.rpcUrls.default.http[0] as string), ++ chain: celoAlfajores, ++ }); ++ const provider = new JsonRpcProvider(celoAlfajores.rpcUrls.default.http[0]); ++ const wallet = new Wallet(privateKey, provider); ++ const account = getAccount(wallet); ``` -With viem: +### Provider methods ```diff - const provider = kit.connection.web3.currentProvider @@ -95,18 +61,6 @@ With viem: ### Signer methods -With ethers: - -```diff -- const provider = kit.connection.web3.currentProvider -- const signer = provider.getSigner(kit.connection.defaultAccount) -+ const signer = provider.getSigner(address) -+ signer.sendTransaction(...) -+ signer.signMessage(...) -``` - -With viem: - ```diff - const provider = kit.connection.web3.currentProvider - const signer = provider.getSigner(kit.connection.defaultAccount) @@ -117,28 +71,55 @@ With viem: ### Contract interaction -I'll show the most "basic" interaction, which is a CELO transfer: +I'll show the most "basic" interaction, which is a transfer. On CELO, it comes with a twist, you can transfer 4 currencies, CELO, cUSD, cEUR, and cREAL. + +You can get the addresses on these tokens by heading to the explorer and getting their abi and addresses, or you can also use our [registry contract](https://docs.celo.org/developer/contractkit/contracts-wrappers-registry). + +```ts +// this address is constant +const REGISTRY_CONTRACT_ADDRESS = '0x000000000000000000000000000000000000ce10' +const registry = new Contract(REGISTRY_CONTRACT_ADDRESS, registryAbi, wallet) + +async function getToken(token: string) { + const tokenAddress = await registry.getAddressForString(token) + return tokenAddress +} +async function CeloTokens(): Promise<[string, string][]> { + return Promise.all( + ['GoldToken', 'StableToken', 'StableTokenEUR', 'StableTokenBRL'].map(async (token) => [ + token, + await getToken(token), + ]) + ) +} +``` -With ethers: +#### Balance ```diff + import { tokenAbi } from './abi.json' -- const CELO = await kit.contracts.getGoldToken(); -- const txReceipt = await CELO.transfer('0x...', amount) -+ const tokenAddress = '0x...' -+ const contract = new ethers.Contract(tokenAddress, tokenAbi, signer); -+ const txReceipt = await contract.transfer('0x...', amount); + +- const contract = await kit.contracts.getGoldToken(); +- const balance = await contract.balanceOf(wallet.address); ++ const tokenAddress = '0x...' // Grabbed from the registry or from the explorer ++ const balance = await client.readContract({ ++ abi: tokenAbi, ++ address: tokenAddress, ++ functionName: "balanceOf", ++ args: [account.address], ++ }); ``` -With viem: +#### Transfer + +Then, use the address of the token that you need and call the transfer method of the contract. ```diff + import { tokenAbi } from './abi.json' - const CELO = await kit.contracts.getGoldToken(); - const txReceipt = await CELO.transfer('0x...', amount) + const tokenAddress = '0x...' -+ const transfer = await client.simulateContract({abi, address: tokenAddress, functionName: 'transfer' }) -+ const txReceipt = await transfer('0x...', amount); ++ const transfer = await walletClient.simulateContract({abi, address: tokenAddress, functionName: 'transfer', args: ['0x...', amount] }) ``` For more in depth examples, I highly recommend checking out the extensive documentations of both [ethers](https://docs.ethers.org/) and [viem](https://viem.sh/).