Skip to content

Commit

Permalink
chore: split into two files
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasbrugneaux committed Jun 1, 2023
1 parent 7005eaf commit a8898e5
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 65 deletions.
108 changes: 108 additions & 0 deletions packages/sdk/contractkit/MIGRATION-TO-ETHERS.md
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/).
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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/).

0 comments on commit a8898e5

Please sign in to comment.