-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2eea3fc
commit c87dc62
Showing
2 changed files
with
154 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters