Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contract Kit 2.0.0 #9127

Merged
merged 111 commits into from
Apr 26, 2022
Merged

Contract Kit 2.0.0 #9127

merged 111 commits into from
Apr 26, 2022

Conversation

aaronmgdr
Copy link
Member

@aaronmgdr aaronmgdr commented Dec 17, 2021

Description

Major Version change to SDK packages.

Goals

  1. Bundle Size of the flagship @celo/contractkit package is as of latest version 3.2MB minified Considering we are advertising this as being mobile that size is far to big. (15 seconds to download on 3g network)

  2. Minimize Breaking Changes
    This is not a complete rewrite. I want to make changes only where it made a big different to bundle size and the migration would not be too difficult

Result

As of Alpha 4 Bundlephobia lists the bundle size as 1.7MB minified (which is still very big) Most of this comes from A) our contract ABIs/wrappers and B) Web3 itself. Therefore I would say we have reached the limit of how small this will get without drastic changes. However for Dapps needing a subset of contractkit there are alternatives MiniContractKit and Connect

How We Got there

Prime source of bloat was due to external packages imported via @celo/utils. To fix I split off 2 packages

@celo/phone-utils

Functions which depend on either google-libphonenumber or country-data

@celo/cryptographic-utils

Functions which depend on bigi, bip39 bip32, bls12377js buffer-reverse

  • from './account'
  • from './bls'
  • from './commentEncryption'
  • from './dataEncryptionKey'

Refactoring

Third pillar was refactoring of WrapperCache AddressRegistry CeloTokens, and **Wrappers so that they did not need a full ContractKit as a Parameter. This means it is now possible to use just the parts of the Kit that one needs. For how this lead to breaking changes see that section.

MiniKit

Furthermore it enabled the Creation of MiniContractKit which is a Kit that only provides access to AccountsWrapper The Token Wrappers, The Exchange Wrappers, and GasPriceMinimumWrapper. It gives no access to nor uses the Web3ContractsCache

use Like

import {ContractKit, newKitFromWeb3} from "@celo/contractkit/lib/mini-kit"


const miniKit = newKitFromWeb3(web3)

miniKit.setFeeCurrency()

miniKit.fillGasPrice()

/// full access to `connection` eg
 miniKit.connection.addAccount(privateKey)
 miniKit.connection.isSyncing()
miniKit.connection.sendTransaction(tx)

MiniKit has no Access to

  • Epoch/Block number functions (Now available on ValidatorsWrapper
  • _web3_contracts_cache
  • networkConfig

CeloTokens#isStableTokenContract

This is now exported as a standalone function (kept on the instance for backwards compatibility)

ValidatorsWrapper

Functions that used the validator wrapper but lived on the ContractKit have been moved here (with forwarding to kit for backwards compatibility)

getEpochSize(): Promise<number> 

getFirstBlockNumberForEpoch(epochNumber: number): Promise<number> 
 
getLastBlockNumberForEpoch(epochNumber: number): Promise<number> 
 
getEpochNumberOfBlock(blockNumber: number): Promise<number> 

@celo/base

enum for StableTokens Token CeloTokens move there from @celo/contractkit/celo-tokens (reexports for backwards compatibility

@celo/connect

  • removed web3-utils ( some interfaces from there have been copied to abi-types.ts file )

Other changes

  • ALL Packages marked as sideEffects: false
  • removed lodash from @celo/utils. as far as i could tell it was not used
  • added "exclude": ["**/*.test.ts"] to tsconfig for the @celo/utils and phone-utils packages because before the lib folder had all our tests in it which was then published to NPM. which i think would increase the package size.

Tested

Related issues

Fixes Break up Utils Package #6189

Backwards compatibility

Breaking Changres

@celo/contractkit

Most changes here are about eliminating the need to construct an entire kit to use classes and functions. and thus giving us the dream of a modular sdk

AddressRegistry

now takes an Connection instance instead of a kit

CeloTokens

no longer requires kit instead requires a Class implementing ContractCacheType to be passed. Examples are WrapperCache or CeloTokensCache

Wrappers

Rather than take the full Kit Wrappers now construct like

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

Web3ContractCache

Instead of a kit requires only a AddressRegistry (uses AddressRegistry's web3 instances)

@celo/utils

Anyone who was importing {PhoneNumberUtils, getCountryEmoji, getPhoneHash, LocalizedCountry, Countries, validatePhone, validateInput } from "@celo/utils" must now import from @celo/phone-utils

Furthermore the io.ts file was split up and E164PhoneNumberType, AttestationServiceStatusResponseType, AttestationServiceTestRequestType, AttestationRequestType, GetAttestationRequestType, AttestationRequest, AttestationResponseType, AttestationResponse must be imported from @celo/phone-utils too.

This is because all those types indirectly depend on E164PhoneNumberType which uses isE164NumberStrict. One alternative is to change this to use isE164Number from celo/base which is regeex based instead but im not sure of the affects of that. but it would greatly simplify a lot here.

Documentation

Still in progress I Am adding readmes to all subpackages, making sure functions have typedoc annotations and new packages have typedoc setup.

I plan to write a blog post / migration guide on docs.celo.org

My goal is to have this all done before celo connect. and have beta 1 released before or durin.

Make it so nothing inside of the contractKit packgaes needs a kit to be passed into its constructor.

instead use Connection or WrapperCache or move functions close to their source.

This should alllow us to be more selective and decide to one ContractWrapper without needing to initialize a kit which itself would require initializing all bunch of other data.

This should lead to the ability to only import parts of contract kit. and lead to small bundle sizes
aaronmgdr and others added 20 commits April 21, 2022 16:39
* Add Basic Readme files to the sdk packages

* WIP readmes

* add  more  readmes, provide descriptions of wallet packages

* Set packages homepage to the sdk docs site

* mark functions as deprecated or internal

* Add details to typdoc comments,
set readmes for a few packages

* mark functions as internal and other cleanup

* lint

* clean up docs

* edits

* formatting

* formatting

* link to example repo

* improve readme for kit

* Update packages/sdk/connect/readme.md

Co-authored-by: Camila Rondinini <crondinini@gmail.com>

* Update packages/sdk/connect/readme.md

Co-authored-by: Camila Rondinini <crondinini@gmail.com>

* its a wrapper not technically a contract

* Update packages/sdk/contractkit/README.md

Co-authored-by: Camila Rondinini <crondinini@gmail.com>

* Add reference to upgrade Guide in Changelog

* Update packages/sdk/connect/src/utils/abi-utils.ts

Co-authored-by: Camila Rondinini <crondinini@gmail.com>

* Fix missing contract in readme example

* generate docs

Co-authored-by: Josh <critesjosh@gmail.com>
Co-authored-by: Camila Rondinini <crondinini@gmail.com>
@aaronmgdr aaronmgdr added the automerge Have PR merge automatically when checks pass label Apr 26, 2022
@mergify mergify bot merged commit 5cfd162 into master Apr 26, 2022
@mergify mergify bot deleted the aaronmgder/ck-2 branch April 26, 2022 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Have PR merge automatically when checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants