Skip to content

Commit c87dc62

Browse files
chore: split into two files
1 parent 2eea3fc commit c87dc62

File tree

2 files changed

+154
-65
lines changed

2 files changed

+154
-65
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Draft: Migration document from Contractkit
2+
3+
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/).
4+
5+
## Initialization
6+
7+
```diff
8+
- import Web3 from "web3";
9+
- import { newKitFromWeb3 } from "@celo/contractkit";
10+
11+
- const web3 = new Web3("https://alfajores-forno.celo-testnet.org");
12+
- const kit = newKitFromWeb3(web3);
13+
+ import { providers } from 'ethers'
14+
+
15+
+ const provider = new providers.JsonRpcProvider('https://alfajores-forno.celo-testnet.org')
16+
```
17+
18+
## Basic usage
19+
20+
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.
21+
22+
### Get address
23+
24+
```diff
25+
- const accounts = await kit.web3.eth.getAccounts();
26+
+ const accounts = await provider.listAccounts();
27+
const defaultAccount = accounts[0];
28+
```
29+
30+
### Get wallet
31+
32+
```diff
33+
+ import { Wallet } from 'ethers'
34+
35+
- const wallet = kit.getWallet();
36+
+ const wallet = new Wallet('0x...', provider);
37+
```
38+
39+
### Provider methods
40+
41+
```diff
42+
- const provider = kit.connection.web3.currentProvider
43+
- kit.connection.getBlock(...)
44+
- kit.connection.getTransaction(...)
45+
provider.getBlock(...)
46+
provider.getTransaction(...)
47+
```
48+
49+
### Signer methods
50+
51+
```diff
52+
- const provider = kit.connection.web3.currentProvider
53+
- const signer = provider.getSigner(kit.connection.defaultAccount)
54+
+ const signer = provider.getSigner(address)
55+
+ signer.sendTransaction(...)
56+
+ signer.signMessage(...)
57+
```
58+
59+
### Contract interaction
60+
61+
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.
62+
63+
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).
64+
65+
```ts
66+
// this address is constant
67+
const REGISTRY_CONTRACT_ADDRESS = '0x000000000000000000000000000000000000ce10'
68+
const registry = new Contract(REGISTRY_CONTRACT_ADDRESS, registryAbi, wallet)
69+
70+
async function getToken(token: string) {
71+
const goldTokenAddress = await registry.getAddressForString(token)
72+
return goldTokenAddress
73+
}
74+
async function CeloTokens(): Promise<[string, string][]> {
75+
return Promise.all(
76+
['GoldToken', 'StableToken', 'StableTokenEUR', 'StableTokenBRL'].map(async (token) => [
77+
token,
78+
await getToken(token),
79+
])
80+
)
81+
}
82+
```
83+
84+
#### Balance
85+
86+
```diff
87+
+ import { tokenAbi } from './abi.json'
88+
89+
- const contract = await kit.contracts.getGoldToken();
90+
+ const tokenAddress = '0x...' // Grabbed from the registry or from the explorer
91+
+ const contract = new ethers.Contract(tokenAddress, tokenAbi, signer);
92+
const balance = await contract.balanceOf(wallet.address);
93+
```
94+
95+
#### Transfer
96+
97+
Then, use the address of the token that you need and call the transfer method of the contract.
98+
99+
```diff
100+
+ import { tokenAbi } from './abi.json'
101+
102+
- const contract = await kit.contracts.getGoldToken();
103+
+ const tokenAddress = '0x...' // Grabbed from the registry or from the explorer
104+
+ const contract = new ethers.Contract(tokenAddress, tokenAbi, signer);
105+
const txReceipt = await contract.transfer('0x...', amount);
106+
```
107+
108+
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/).
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
# Draft: Migration document from Contractkit
22

3-
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/).
3+
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/).
44

55
## Initialization
66

7-
With ethers:
8-
9-
```diff
10-
- import Web3 from "web3";
11-
- import { newKitFromWeb3 } from "@celo/contractkit";
12-
13-
- const web3 = new Web3("https://alfajores-forno.celo-testnet.org");
14-
- const kit = newKitFromWeb3(web3);
15-
+ import { providers } from 'ethers'
16-
+
17-
+ const provider = new providers.JsonRpcProvider('https://alfajores-forno.celo-testnet.org')
18-
```
19-
20-
With viem:
21-
227
```diff
238
- import Web3 from "web3";
249
- 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
4025

4126
### Get address
4227

43-
With ethers:
44-
45-
```diff
46-
- const accounts = await kit.web3.eth.getAccounts();
47-
+ const accounts = await provider.listAccounts();
48-
const defaultAccount = accounts[0];
49-
```
50-
5128
With viem:
5229

5330
```diff
@@ -58,32 +35,21 @@ const defaultAccount = accounts[0];
5835

5936
### Get wallet
6037

61-
With ethers:
62-
63-
```diff
64-
+ import { Wallet } from 'ethers'
65-
66-
- const wallet = kit.getWallet();
67-
+ const wallet = new Wallet('0x...', provider);
68-
```
69-
7038
With viem:
7139

7240
> [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
7341
74-
### Provider methods
75-
76-
With ethers:
77-
7842
```diff
79-
- const provider = kit.connection.web3.currentProvider
80-
- kit.connection.getBlock(...)
81-
- kit.connection.getTransaction(...)
82-
provider.getBlock(...)
83-
provider.getTransaction(...)
43+
+ const walletClient = createWalletClient({
44+
+ transport: http(celoAlfajores.rpcUrls.default.http[0] as string),
45+
+ chain: celoAlfajores,
46+
+ });
47+
+ const provider = new JsonRpcProvider(celoAlfajores.rpcUrls.default.http[0]);
48+
+ const wallet = new Wallet(privateKey, provider);
49+
+ const account = getAccount(wallet);
8450
```
8551

86-
With viem:
52+
### Provider methods
8753

8854
```diff
8955
- const provider = kit.connection.web3.currentProvider
@@ -95,18 +61,6 @@ With viem:
9561

9662
### Signer methods
9763

98-
With ethers:
99-
100-
```diff
101-
- const provider = kit.connection.web3.currentProvider
102-
- const signer = provider.getSigner(kit.connection.defaultAccount)
103-
+ const signer = provider.getSigner(address)
104-
+ signer.sendTransaction(...)
105-
+ signer.signMessage(...)
106-
```
107-
108-
With viem:
109-
11064
```diff
11165
- const provider = kit.connection.web3.currentProvider
11266
- const signer = provider.getSigner(kit.connection.defaultAccount)
@@ -117,28 +71,55 @@ With viem:
11771

11872
### Contract interaction
11973

120-
I'll show the most "basic" interaction, which is a CELO transfer:
74+
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.
75+
76+
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).
77+
78+
```ts
79+
// this address is constant
80+
const REGISTRY_CONTRACT_ADDRESS = '0x000000000000000000000000000000000000ce10'
81+
const registry = new Contract(REGISTRY_CONTRACT_ADDRESS, registryAbi, wallet)
82+
83+
async function getToken(token: string) {
84+
const tokenAddress = await registry.getAddressForString(token)
85+
return tokenAddress
86+
}
87+
async function CeloTokens(): Promise<[string, string][]> {
88+
return Promise.all(
89+
['GoldToken', 'StableToken', 'StableTokenEUR', 'StableTokenBRL'].map(async (token) => [
90+
token,
91+
await getToken(token),
92+
])
93+
)
94+
}
95+
```
12196

122-
With ethers:
97+
#### Balance
12398

12499
```diff
125100
+ import { tokenAbi } from './abi.json'
126-
- const CELO = await kit.contracts.getGoldToken();
127-
- const txReceipt = await CELO.transfer('0x...', amount)
128-
+ const tokenAddress = '0x...'
129-
+ const contract = new ethers.Contract(tokenAddress, tokenAbi, signer);
130-
+ const txReceipt = await contract.transfer('0x...', amount);
101+
102+
- const contract = await kit.contracts.getGoldToken();
103+
- const balance = await contract.balanceOf(wallet.address);
104+
+ const tokenAddress = '0x...' // Grabbed from the registry or from the explorer
105+
+ const balance = await client.readContract({
106+
+ abi: tokenAbi,
107+
+ address: tokenAddress,
108+
+ functionName: "balanceOf",
109+
+ args: [account.address],
110+
+ });
131111
```
132112

133-
With viem:
113+
#### Transfer
114+
115+
Then, use the address of the token that you need and call the transfer method of the contract.
134116

135117
```diff
136118
+ import { tokenAbi } from './abi.json'
137119
- const CELO = await kit.contracts.getGoldToken();
138120
- const txReceipt = await CELO.transfer('0x...', amount)
139121
+ const tokenAddress = '0x...'
140-
+ const transfer = await client.simulateContract({abi, address: tokenAddress, functionName: 'transfer' })
141-
+ const txReceipt = await transfer('0x...', amount);
122+
+ const transfer = await walletClient.simulateContract({abi, address: tokenAddress, functionName: 'transfer', args: ['0x...', amount] })
142123
```
143124

144125
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/).

0 commit comments

Comments
 (0)