Skip to content

Commit

Permalink
Add Migration Guide for ContractKit v2 (#307)
Browse files Browse the repository at this point in the history
* Add Migration Guide for contractKit v2

* Improve flow of guide

* add to sidebar and fix up descriptions

* some edits

* edits

* minor edits

* update gtag location

* update docusaurus version

Co-authored-by: Josh <critesjosh@gmail.com>
  • Loading branch information
aaronmgdr and critesjosh authored Mar 28, 2022
1 parent ff56321 commit f8bd8d2
Show file tree
Hide file tree
Showing 6 changed files with 1,874 additions and 848 deletions.
20 changes: 10 additions & 10 deletions docs/developer-resources/contractkit/migrating-to-contractkit-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ slug: /developer-guide/contractkit/migrating-to-contractkit-v1

# Migrating to ContractKit v1.0

How to migrate to the newest version of ContractKit and make use of its latest features.
How to migrate to from prerelease of ContractKit to v1 and make use of its features.

___

Expand Down Expand Up @@ -74,32 +74,32 @@ import { newBlockExplorer } from '@celo/explorer/lib/block-explorer'
### Older versions of ContractKit:

```javascript
// version ^0.4.0
// version ^0.4.0
const ContractKit = require('@celo/contractkit')

// Older versions of ContractKit create a new Web3 instance internally
// Older versions of ContractKit create a new Web3 instance internally
const kit = ContractKit.newKit('https://forno.celo.org')
```

### Version 1.x.y

```javascript
// Since ContractKit no longer instantiates web3, you'll need to explicitly require it
const Web3 = require('web3')
const web3 = new Web3('https://forno.celo.org')
// Since ContractKit no longer instantiates web3, you'll need to explicitly require it
const Web3 = require('web3')
const web3 = new Web3('https://forno.celo.org')

// Require ContractKit and newKitFromWeb3
const { ContractKit, newKitFromWeb3 } = require('@celo/contractkit')
// Require ContractKit and newKitFromWeb3
const { ContractKit, newKitFromWeb3 } = require('@celo/contractkit')
let contractKit = newKitFromWeb3(web3)
```
## Accessing Web3 functions

You can access `web3` functions through the `connection` module.

```javascript
// version ^0.4.0
// version ^0.4.0
let amount = kit.web3.utils.fromWei("1000000", "ether")

// version 1.x.y
let amount = kit.connection.web3.utils.fromWei("1000000", "ether")
```
Expand Down
233 changes: 233 additions & 0 deletions docs/developer-resources/contractkit/migrating-to-contractkit-v2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
---
title: Migrating to ContractKit v2.0
description: How to migrate from v1 to v2 ContractKit suite of packages and make use of their latest features.
slug: /developer-guide/contractkit/migrating-to-contractkit-v2
---

How to migrate from v1 to v2 of the Celo SDK suite of packages and make use of their latest features.

___

## Why v2?

### Bundlesize

The primary motivation in creating v2 was reduced bundlesize and increased modularity. The massive package size for `@celo/contractkit` has been an elephant in the room and source of dissonance for looking to build mobile first dApps. As of 1.5.2 bundlephobia list the minified size at 3.7MB. 2.0.0 comes in at 1.7MB. still big yet we have a few more tricks. First the packages have been all marked as `sideEffects:false`, a `kit` instance is no longer required to any classes in the contractkit package, and the introduction of `MiniContractKit`.

### Modularity

#### `sideEffects:false`

Tells your bundler it can safely only include the code that is explicitly used, reducing bundlesize.

#### `kit` no longer needed by everything

In v1 Almost everything required a `kit` instance to be passed to its constructor. Effectively this meant it was impossible to use any of the classes in @celo/contractkit alone.

In v2 AddressRegistry, Wrappers, WrapperCache, and more can all be constructed using mostly just a `Connection` (sometimes other arguments too).

#### `MiniContractKit`

The prize of no longer needing a full `kit` is that it became possible to create a slimed down minimal viable ContractKit.

`MiniContractKit` provides a subset of ContractKit features with the same interface. For many dapps it will be a drop in opt-in change (eg `import {newKit, ContractKit} from "@celo/contractkit/lib/mini/kit`). It reduces size by only including access to `Accounts, GasPriceMinimum, StableToken*, Exchange* and GoldToken` wrappers and contracts. It can `setFeeCurrency`, look up info about the current account and, like full Contractkit, it delegates most functionality to `connection`.

## Get Started

Upgrade your project packages to the latest (in this case release beta, but anything over 2 will work when it's out of beta). For example, with ContractKit and Celo utils:

`yarn add @celo/contractkit@beta @celo/utils@beta`

If you are directly importing any other `@celo/*` packages, upgrade them as well.

If you need them, append `@celo/phone-utils@beta` `@celo/cryptographic-utils@beta`.

*(see section on breaks in [@celo/utils](#celoutils) to know if you need them)*

## Breaking changes

Because of how we publish packages, all packages will be upgraded to v2. However not all packages will have breaking changes. Breaking changes are limited to:

- [@celo/contractkit](#celocontractkit)
- [@celo/utils](#celoutils)

### @celo/contractkit

Most changes are about eliminating the need to construct an entire kit to use other classes and functions.

#### IdentityMetadataWrapper (idendity/metadata.ts)

This had functions that took a `kit` as a parameter. Now it takes an AccountsWrapper class.

This change was done so that `kit` was not required to be passed into all the classes and functions that use `IdentityMetadataWrapper`.

*v1

```typescript
IdentityMetadataWrapper.fetchFromURL(kit, url)

IdentityMetadataWrapper.fromFile(kit, path)

IdentityMetadataWrapper.verifySignerForAddress(kit, hash, signature, address)
IdentityMetadataWrapper.fromRawString(kit, rawData)
```

*v2

```typescript
const accounts = await kit.contracts.getAccounts()

IdentityMetadataWrapper.fetchFromURL(accounts, url)

IdentityMetadataWrapper.fromFile(accounts, path)

IdentityMetadataWrapper.verifySignerForAddress(accounts, hash, signature, address)
IdentityMetadataWrapper.fromRawString(accounts, rawData)
```

#### AddressRegistry

Now takes an `Connection` instance instead of a `kit` instance.

#### CeloTokens

No longer requires `kit`, instead it requires a class implementing `ContractCacheType` to be passed in. Examples are `WrapperCache` or `CeloTokensCache`.

#### Wrappers

**Note: If you were constructing wrappers with `kit.contracts.getX` no change is required.**

Rather than take the full Kit Wrappers, now construct it like:

```javascript
// Most Common
constructor(connection: Connection, contract: Contract)
// The Voting Contracts (Governance, Election, Validator, LockedGold, Slashers, and Attestations
constructor(connection: Connection, contract: Contract, wrapperCache: WrapperCache)
// Sorted Oracles
constructor(connection: Connection, contract: Contract, addressRegistry: AddressRegistry)
```

The `WrapperCache` takes care of this while constructing them and most likely there will not be many situations where wrappers were constructed directly given they needed a `kit` before.

##### AccountsWrapper

`authorizeValidatorSigner` method now requires a `ValidatorsWrapper` be passed in as final argument.

*v1*

```ts
const accountsInstance = await kit.contracts.getAccountsWrapper()

accountsInstance.authorizeValidatorSigner(signer, sig)
```

*v2*

```ts

const accountsInstance = await kit.contracts.getAccountsWrapper()
const validatorsInstance = await kit.contracts.getValidatorsWrapper()

accountsInstance.authorizeValidatorSigner(signer, sig, validatorsInstance)

```

#### Web3ContractCache

Instead of a `kit` instance, it requires only a `AddressRegistry` (uses AddressRegistry's web3 instances).

### @celo/utils

Most of the size savings came from removing functionality from `@celo/utils` into two new packages `@celo/phone-utils` and `@celo/cryptographic-utils`

So depending on what you used you will need to add one or both to your package.json.

#### Phone Utils

If your packages imports any of the following from `@celo/utils` you will need to change the import to `@celo/phone-utils`

##### from countries.ts

- `CountryNames`
- `LocalizedCountry`
- `Countries`

##### from getCountryEmoji.ts

- `getCountryEmoji`

##### from getPhoneHash.ts

- default (getPhoneHash)

##### from inputValidation.ts

- `validatePhone`
- `validateInput`

##### from io.ts

- `AttestationRequestType`
- `AttestationResponseType`
- `AttestationResponse`
- `AttestationServiceTestRequestType`
- `AttestationServiceTestRequest`
- `E164PhoneNumberType`
- `E164Number`
- `GetAttestationRequestType`

##### from phoneNumbers.ts

- `getCountryCode`
- `getRegionCode`
- `getRegionCodeFromCountryCode`
- `getDisplayPhoneNumber`
- `getDisplayNumberInternational`
- `getE164DisplayNumber`
- `getE164Number`
- `isE164NumberStrict`
- `parsePhoneNumber`
- `getExampleNumber`

#### Cryptographic-utils

If your packages imports any of the following from `@celo/utils` you will need to change the import to `@celo/cryptographic-utils`

##### from account.ts

- `generateKeys`
- `generateKeysFromSeed`
- `generateDeterministicInviteCode`
- `generateSeed`
- `generateMnemonic`
- `validateMnemonic`
- `invalidMnemonicWords`
- `normalizeMnemonic`
- `formatNonAccentedCharacters`
- `getAllLanguages`
- `mnemonicLengthFromStrength`
- `detectMnemonicLanguage`
- `suggestMnemonicCorrections`

##### from bls.ts

- `BLS_PUBLIC_KEY_SIZE`
- `BLS_POP_SIZE`
- `blsPrivateKeyToProcessedPrivateKey`
- `getBlsPublicKey`
- `getBlsPoP`

##### from commentEncryption.ts

- `EncryptionStatus`
- `encryptData`
- `decryptData`
- `encryptComment`
- `decryptComment`

##### from dataEncryption.ts

- `compressedPubKey`
- `decompressPublicKey`
- `deriveDek`
12 changes: 6 additions & 6 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,6 @@ module.exports = {
},
],
},
gtag: {
// You can also use your "G-" Measurement ID here.
trackingID: "G-0CXEKQ81V2",
// Optional fields.
anonymizeIP: true, // Should IPs be anonymized?
},
algolia: {
appId: "55M4I38S60",
apiKey: "baed78b52be14ac907688f1dd70b41d5",
Expand Down Expand Up @@ -433,6 +427,12 @@ module.exports = {
],
rehypePlugins: [katex],
},
gtag: {
// You can also use your "G-" Measurement ID here.
trackingID: "G-0CXEKQ81V2",
// Optional fields.
anonymizeIP: true, // Should IPs be anonymized?
},
theme: {
customCss: require.resolve("./src/css/custom.css"),
},
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
},
"dependencies": {
"@crowdin/cli": "^3.7.0",
"@docusaurus/core": "^2.0.0-beta.14",
"@docusaurus/plugin-client-redirects": "^2.0.0-beta.14",
"@docusaurus/preset-classic": "^2.0.0-beta.14",
"@docusaurus/remark-plugin-npm2yarn": "^2.0.0-beta.14",
"@docusaurus/theme-live-codeblock": "^2.0.0-beta.14",
"@docusaurus/theme-search-algolia": "^2.0.0-beta.14",
"@docusaurus/core": "^2.0.0-beta.18",
"@docusaurus/plugin-client-redirects": "^2.0.0-beta.18",
"@docusaurus/preset-classic": "^2.0.0-beta.18",
"@docusaurus/remark-plugin-npm2yarn": "^2.0.0-beta.18",
"@docusaurus/theme-live-codeblock": "^2.0.0-beta.18",
"@docusaurus/theme-search-algolia": "^2.0.0-beta.18",
"@mdx-js/react": "^1.6.21",
"clsx": "^1.1.1",
"docusaurus-plugin-fathom": "^1.1.0",
Expand All @@ -46,7 +46,7 @@
]
},
"devDependencies": {
"@docusaurus/module-type-aliases": "^2.0.0-beta.14",
"@docusaurus/module-type-aliases": "^2.0.0-beta.18",
"@tsconfig/docusaurus": "^1.0.2",
"@types/react": "^17.0.11",
"@types/react-helmet": "^6.1.1",
Expand Down
5 changes: 5 additions & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,11 @@ const sidebars = {
label: "Migrate to ContractKit v1",
id: "developer-resources/contractkit/migrating-to-contractkit-v1",
},
{
type: "doc",
label: "Migrate to ContractKit v2",
id: "developer-resources/contractkit/migrating-to-contractkit-v2",
},
],
},
{
Expand Down
Loading

0 comments on commit f8bd8d2

Please sign in to comment.