8.0.0 ā€¢ Published 28 days ago

@celo/contractkit v8.0.0

Weekly downloads
4,821
License
Apache-2.0
Repository
github
Last release
28 days ago

ContractKit

Celo's ContractKit is a library to help developers and validators to interact with the Celo blockchain.

ContractKit supports the following functionality:

  • Connect to a node
  • Access web3 object to interact with node's Json RPC API
  • Send Transaction with celo's extra fields: (feeCurrency)
  • Simple interface to interact with CELO and cUSD
  • Simple interface to interact with Celo Core contracts
  • Utilities

User Guide

:::tip

You might not need the full ContractKit. Consider using @celo/connect which powers much of ContractKit such as building and sending Transactions, signing, etc, but does not give access to any celo Contract Wrappers. Or if a subset of Wrappers, setting the feeCurrency and account info is all your dapp needs consider replacing your imports of Contractkit with @celo/contractkit/lib/mini-kit

:::

How we work

We are a GitHub-first team, which means we have a strong preference for communicating via GitHub. Please use GitHub to:

šŸž File a bug report

šŸ’¬ Ask a question

āœØ Suggest a feature

šŸ§‘ā€šŸ’» Contribute!

šŸš” Report a security vulnerability

!TIP

Please avoid messaging us via Slack, Telegram, or email. We are more likely to respond to you on GitHub than if you message us anywhere else. We actively monitor GitHub, and will get back to you shortly šŸŒŸ

Getting Started

To install:

npm install @celo/contractkit
// or
yarn add @celo/contractkit

You will need Node.js v18.14.2. or greater.

To start working with contractkit you need a kit instance:

import { newKit } from '@celo/contractkit' // or import { newKit } from '@celo/contractkit/lib/mini-kit'

// Remotely connect to the Alfajores testnet
const kit = newKit('https://alfajores-forno.celo-testnet.org')

To access balances:

// returns an object with {lockedGold, pending, cUSD, cEUR, cREAL}

const balances = await kit.getTotalBalance()

// returns an object with {cUSD, cEUR, cREAL}
const balances = await miniKit.getTotalBalance()

If you don't need the balances of all tokens use the balanceOf method

const stableTokenWrapper = await kit.getStableToken(StableToken.cREAL)

const cRealBalance = stableTokenWrapper.balanceOf(accountAddress)

Setting Default Tx Options

kit allows you to set default transaction options:

import { newKit, CeloContract } from '@celo/contractkit/lib/mini-kit'

async function getKit(myAddress: string, privateKey: string) {
  const kit = newKit('https://alfajores-forno.celo-testnet.org')

  // default from account
  kit.defaultAccount = myAddress

  // add the account private key for tx signing when connecting to a remote node
  kit.connection.addAccount(privateKey)

  // paid gas in celo dollars
  await kit.setFeeCurrency('0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1')

  return kit
}

Interacting with CELO & cUSD

Celo has two initial coins: CELO and cUSD (stableToken). Both implement the ERC20 standard, and to interact with them is as simple as:

// get the CELO contract
const celoToken = await kit.contracts.getGoldToken()

// get the cUSD contract
const stableToken = await kit.contracts.getStableToken()

const celoBalance = await celoToken.balanceOf(someAddress)
const cusdBalance = await stableToken.balanceOf(someAddress)

To send funds:

const oneGold = kit.connection.web3.utils.toWei('1', 'ether')
const tx = await goldToken.transfer(someAddress, oneGold).send({
  from: myAddress,
})

const hash = await tx.getHash()
const receipt = await tx.waitReceipt()

If you would like to pay fees in cUSD, (or other cStables like cEUR, cUSD).

kit.setFeeCurrency('0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1') // Default to paying fees in cUSD

const stableTokenContract = kit.contracts.getStableToken()

const tx = await stableTokenContract
  .transfer(recipient, weiTransferAmount)
  .send({ from: myAddress, gasPrice })

const hash = await tx.getHash()

const receipt = await tx.waitReceipt()

Interacting with Core Contracts

There are many core contracts.

  • AccountsWrapper
  • AttestationsWrapper
  • BlockchainParametersWrapper
  • DoubleSigningSlasherWrapper
  • DowntimeSlasherWrapper
  • ElectionWrapper
  • EpochRewardsWrapper
  • Erc20Wrapper
  • EscrowWrapper
  • FreezerWrapper
  • GasPriceMinimumWrapper
  • GoldTokenWrapper
  • GovernanceWrapper
  • LockedGoldWrapper
  • MultiSigWrapper
  • ReserveWrapper
  • SortedOraclesWrapper
  • StableTokenWrapper
  • ValidatorsWrapper

Wrappers Through Kit

When using the kit you can access core contracts like

kit.contracts.get{ContractName}

E.G. kit.contracts.getAccounts(), kit.contracts.getValidators()

Stand Alone Wrappers

You can also initialize contracts wrappers directly. They require a Connection and their contract:

// MiniContractKit only gives access to a limited set of Contracts, so we import Multisig

import { newKit } from '@celo/contractkit/lib/mini-kit'
import { MultiSigWrapper } from '@celo/contractkit/lib/wrappers/MultiSig'
import { newMultiSig } from '@celo/abis/web3/MultiSig'

const miniKit = newKit('https://alfajores-forno.celo-testnet.org/')

// Alternatively import { Connection } from '@celo/connect'
// const connection = new Connection(web3)

const contract = newMultiSig(web3)

const multisigWrapper = new MultiSigWrapper(miniKit.connection, contract)

Accessing web3 contract wrappers

MiniContractKit does not provide access to the web3 contracts

Some user might want to access web3 native contract wrappers.

To do so, you can:

const feeCurrencyWhitelist = await kit._web3Contracts.getContract(CeloContract.FeeCurrencyWhitelist)

We expose native wrappers for all Celo core contracts.

The complete list of Celo Core contracts is:

  • Accounts
  • Attestations
  • BlockchainParameters
  • DoubleSigningSlasher
  • DowntimeSlasher
  • Election
  • EpochRewards
  • ERC20
  • Escrow
  • FederatedAttestations
  • FeeCurrencyWhitelist
  • FeeHandler
  • Freezer
  • GasPriceMinimum
  • GoldToken
  • Governance
  • LockedGold
  • MentoFeeHandlerSeller
  • UniswapFeeHandlerSeller
  • MultiSig
  • OdisPayments
  • Random
  • Registry
  • Reserve
  • SortedOracles
  • StableToken
  • StableTokenEUR
  • StableTokenBRL
  • Validators

This can also be found in packages/sdk/contractkit/src/base.ts

A Note About Contract Addresses

Celo Core Contracts addresses, can be obtained by looking at the Registry contract. That's how kit obtains them.

We expose the registry API, which can be accessed by:

const goldTokenAddress = await kit.registry.addressFor(CeloContract.GoldToken)

Sending Custom Transactions

Celo transaction object is not the same as Ethereum's. There are three new fields present:

  • feeCurrency (address of the ERC20 contract to use to pay for gas and the gateway fee)
  • gatewayFeeRecipient (coinbase address of the full serving the light client's trasactions)
  • gatewayFee (value paid to the gateway fee recipient, denominated in the fee currency)

:::note The gatewayFeeRecipient, and gatewayFee fields are currently not used by the protocol. :::

This means that using web3.eth.sendTransaction or myContract.methods.transfer().send() should be avoided to take advantage of paying transaction fees in alternative currencies.

Instead, kit provides an utility method to send transaction in both scenarios. If you use contract wrappers, there is no need to use this.

For a raw transaction:

const tx = kit.sendTransaction({
  from: myAddress,
  to: someAddress,
  value: oneGold,
})
const hash = await tx.getHash()
const receipt = await tx.waitReceipt()

When interacting with a web3 contract object:

const celoNativeToken = await kit._web3Contracts.getGoldToken()
const oneGold = kit.connection.web3.utils.toWei('1', 'ether')

const txo = await celoNativeToken.methods.transfer(someAddress, oneGold)
const tx = await kit.sendTransactionObject(txo, { from: myAddress })
const hash = await tx.getHash()
const receipt = await tx.waitReceipt()

More Information

You can find more information about the ContractKit in the Celo docs at https://docs.celo.org/developer-guide/contractkit.

Debugging

If you need to debug kit, we use the well known debug node library.

So set the environment variable DEBUG as:

DEBUG="kit:*,
@alebusse/account-lib@poofcash/poof-kit@poofcash/rewardscelo@poofcash/savingscelo@poofcash/truffle-plugin-blockscout-verify@poofcash/tx-manager@poofcash/use-contractkit@ledgerhq/live-commoncelovotes@celo/phone-number-privacy-signercelo-metamask-signercelogovernance@steakwallet/chains@infinitebrahmanuniverse/nolb-_celcelo1155romulus-kit@everything-registry/sub-chunk-161@hapchain/truffle-plugin-blockscout-verifyimpeditdoloribus@gibrangul95/account-libinstant-burnducimustempore@emag3m/celocli@emag3m/celogive@emag3m/celogiveegnardinitest1@facuintive/account-lib@fortmatic/ledger-bridge-celo@frausch/account-lib@jachaval/account-libkotani-evm@komenci/kit@jichaoli/account-lib@keep-network/tbtccelo-election-comparison@soloseng/celo-devchain@silalvado/account-lib@ryan-bitgo/account-libnamfacilispesabasekit@pontigaston/celocli-sec@shardlabs/ganache-core@summa-tx/relay-sol@mobius-money/swappamotif-truffle-blockscout-verify@node-fi/node-router@nomspace/nom@nomspace/nomkit@nomspace/nomspace@nomspace/use-contractkit@nategraf/phone-number-privacy-common@tradegen/sdk@transak/celo-mainnetpossimusqui@terminal-fi/savingscelo-cli@terminal-fi/savingscelo-with-ube@terminal-fi/swappa@terminal-fi/celo-devchain@terminal-fi/savingscelosavingscelosavingscelo-clisavingscelo-with-ube@ubeswap/use-contractkit@valora/exchanges@xeggo/sdktessellated-geometry-celo-network-monitorcelopaycelo-devchain@aloe2/sdk-coin-celo@alexbh/celocli@bitgo/sdk-coin-celodal-use-contractkitubeswap-headerubeswap-swapubeswap-swap-dev@clabs/celogiveundeetundeinventore@bitgo-beta/sdk-coin-celo@brianli/use-contractkittruffle-kekchain-verifytruffle-plugin-blockscout-verify-flydax@celo/celocli@celo/dappkit@celo/payments-cli@celo/phone-number-privacy-monitor@celo/explorer@celo/governance@celo/identity@celo/phone-number-privacy-common@celo/phone-number-privacy-common-devtruffle-blockscout-verifyuse-contractkit@did-kr-cg/celo-metamask-signer@dexfair/celo-dappkit-rn@dexfair/celo-web-signer
8.0.0

28 days ago

8.0.0-beta.0

1 month ago

7.2.0

2 months ago

7.1.0

2 months ago

7.1.0-beta.0

2 months ago

7.1.0-beta.1

2 months ago

7.0.0

2 months ago

7.0.0-beta.2

3 months ago

6.0.1-beta.1

3 months ago

6.0.1-beta.0

3 months ago

6.0.0

4 months ago

6.0.0-beta.1

4 months ago

6.0.0-beta.2

4 months ago

6.0.0-beta.0

5 months ago

5.2.1-beta.0

5 months ago

5.2.1

5 months ago

4.1.1-beta.1

9 months ago

5.0.0-beta.1

8 months ago

5.0.4

7 months ago

5.0.3

7 months ago

5.0.2

7 months ago

5.0.1

8 months ago

5.0.0

8 months ago

5.2.0-beta.0

5 months ago

5.0.3-beta.1

7 months ago

5.1.0-beta.0

6 months ago

5.1.0

6 months ago

5.0.2-beta.1

7 months ago

5.2.0

5 months ago

4.1.1

8 months ago

4.1.0-beta.1

12 months ago

4.1.0

12 months ago

4.0.0

1 year ago

4.0.0-beta.3

1 year ago

4.0.0-beta.2

1 year ago

4.0.0-beta.1

1 year ago

3.2.0

1 year ago

3.1.0

1 year ago

3.1.0-beta.1

1 year ago

3.1.0-beta.2

1 year ago

3.2.0-beta.1

1 year ago

2.3.0-beta.2

2 years ago

3.0.0-beta.1

1 year ago

2.3.0

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.4.0-beta.8

1 year ago

2.4.0-beta.7

1 year ago

2.4.0-beta.9

1 year ago

2.4.0-beta.4

1 year ago

2.4.0-beta.3

1 year ago

2.4.0-beta.6

1 year ago

2.4.0-beta.5

1 year ago

2.4.0-beta.2

1 year ago

2.4.0-beta.1

1 year ago

2.3.0-beta.1

2 years ago

2.2.0-beta.1

2 years ago

2.2.0

2 years ago

2.2.0-alpha.2

2 years ago

2.2.0-alpha.1

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

2.0.0-rc.3

2 years ago

2.0.0-alpha.3

2 years ago

2.0.0-alpha.4

2 years ago

2.0.0-alpha.5

2 years ago

2.0.0-alpha.2

2 years ago

2.0.0-beta.2

2 years ago

2.0.0-beta.1

2 years ago

2.0.0-rc.2

2 years ago

2.0.0-rc.1

2 years ago

2.0.0-alpha1

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.0-beta

2 years ago

1.4.0

2 years ago

1.3.3

2 years ago

1.3.0-dev

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.2

3 years ago

1.2.0

3 years ago

1.2.2-beta

3 years ago

1.1.1-beta

3 years ago

1.1.0

3 years ago

1.1.0-beta.1

3 years ago

1.0.2

3 years ago

1.0.2-beta-1

3 years ago

1.0.2-beta

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.0-beta4

3 years ago

1.0.0-beta3

3 years ago

1.0.0-beta

3 years ago

0.4.18

3 years ago

0.4.17

3 years ago

0.4.16

4 years ago

0.4.14

4 years ago

0.4.13

4 years ago

0.4.12

4 years ago

0.4.11

4 years ago

0.4.9

4 years ago

0.4.8

4 years ago

0.4.7

4 years ago

0.4.6

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.8

4 years ago

0.3.7

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2-test1

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.15-beta

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12-fixed

4 years ago

0.2.12

4 years ago

0.2.11-baklava2

4 years ago

0.2.11-baklava1

4 years ago

0.2.10-dev

4 years ago

0.2.10

4 years ago

0.2.10-beta1

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.5

4 years ago

0.2.6

4 years ago

0.2.4

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.1-alpha

5 years ago

0.1.0

5 years ago

0.0.5-beta3

5 years ago

0.0.5-beta2

5 years ago

0.0.5-beta1

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2-epsilon

5 years ago

0.0.2-delta

5 years ago

0.0.2-gamma

5 years ago

0.0.2-beta

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago