0.10.0 • Published 8 days ago

@scio-labs/use-inkathon v0.10.0

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
8 days ago

inkathon Devtooling Banner

useInkathon – React Hooks & Utility Library

License: GPL v3 Built with ink! TypeScript React

This library provides typesafe React hooks and utility functions that simplify the process of working with Substrate-based networks and ink! smart contracts. It abstracts away the complexities of polkadot{.js} but still lets you access the full power of the underlying API.

The project is part of a Scio Labs initiative to improve the developer experience in the ink! ecosystem and a proud member of the Aleph Zero EFP. 💜

Other projects include:

Join the discussion in our Telegram Group 💬

Checkout our TypeDoc Documentation 📃


  1. Getting started 🚀
  2. Features ✨
  3. Contract Registry 🗳️
    1. How it works
    2. Typed Contracts
  4. Examples 📚
  5. Package Development 🛠

Getting started 🚀

!IMPORTANT
If you are looking for a boilerplate to start your dApp project from scratch, checkout ink!athon.

  1. Go to your existing project and install the package from the npm registry:
pnpm add @scio-labs/use-inkathon
# or
npm install @scio-labs/use-inkathon
# or
yarn add @scio-labs/use-inkathon
  1. Wrap it around your app or parent component:
import { development, UseInkathonProvider } from '@scio-labs/use-inkathon'
<UseInkathonProvider appName="My dApp" defaultChain={development}>
  <Component {...pageProps} />
</UseInkathonProvider>
  1. Use the primary useInkathon hook for connecting the wallet or accessing the API:
import { useInkathon } from '@scio-labs/use-inkathon'
const { api, connect, activeChain, activeAccount, … } = useInkathon()

Features ✨

At its core, this library serves as a wrapper for polkadot{.js}, potentially saving you over 100 lines of code. This includes:

!NOTE
Checkout our TypeDoc Documentation for more details.

Contract Registry 🗳️

Often when working with smart contracts in the frontend, you have to import the contract metadata multiple times across a project, then determine the matching deployment address for the active chain, and create a ContractPromise instance manually each time.

The idea of a Contract Registry is to define contract metadata & addresses only once and use them everywhere with a simple hook:

const { contract } = useRegisteredContract('greeter')

How it works

Start by defining an async getDeployments function that returns SubstrateDeployment[] metadata objects for each contract deployment on each chain.

!NOTE
Checkout an advanced example within the ink!athon boilerplate here where metadata is imported dynamically based on defined chains and contract identifiers.

import { alephzeroTestnet, SubstrateDeployment } from '@scio-labs/use-inkathon'

export const getDeployments = async (): Promise<SubstrateDeployment[]> => {
  return [
    {
      contractId: 'greeter',
      networkId: alephzeroTestnet.network,
      abi: await import(`../deployments/metadata.json`),
      address: '5HPwzKmJ6wgs18BEcLdH5P3mULnfnowvRzBtFcgQcwTLVwFc',
    },
  ]
}

The function's result passed to the UseInkathonProvider provider:

<UseInkathonProvider
  appName="My dApp"
  defaultChain={alephzeroTestnet}
  deployments={getDeployments()}
>
  <Component {...pageProps} />
</UseInkathonProvider>

Then access the contract as above:

const { contract } = useRegisteredContract('greeter')

Typed Contracts

!NOTE
Make sure to also install @727-ventures/typechain-types, bn.js, and @types/bn.js as dependencies in your project. Find a complete setup & usage example in the ink!athon boilerplate.

If you are using typechain-polkadot to generate type-safe contracts, you can use the useRegisteredTypedContract hook instead:

import GreeterContract from '[…]/typed-contracts/contracts/greeter'

// …

const { typedContract } = useRegisteredTypedContract('greeter', GreeterContract)
const result = await typedContract.query.greet()

!IMPORTANT
Currently, only queries are supported until typechain-polkadot#138 is merged. Alternatively, we're considering switching to the prosopo/typechain-polkadot fork completely.

Examples 📚

Within this repository:

Live examples:

Package Development 🛠

If you want to contribute, please read our Contributor Guidelines 🙏

Pre-requisites:

# Install dependencies
pnpm i

# Enable pre-commit hooks
pnpm simple-git-hooks

# Run tsup in development mode (watching)
pnpm dev

# Run package compilation in parallel with vanilla React example
pnpm run dev:react-example

# Build the package
pnpm build

Unfortunately, importing this package locally into other local projects requires some manual steps. You need to build & pack this package into a .tgz-build and then update this dependency in your other project. These steps must be repeated each time you make changes to this package.

# 1. [THIS PACKAGE] Generate a .tgz package of the build
pnpm tsup && pnpm pack

# 2. [OTHER PROJECT] Add it as a dependency in your other project like:
#    `"@scio-labs/use-inkathon": "file:../scio-labs-use-inkathon-0.0.X.tgz"`
pnpm add ../use-inkathon/scio-labs-use-inkathon-0.0.X.tgz

# 3. [OTHER PROJECT] Subsequent updates can be done via
pnpm update @scio-labs/use-inkathon --force
0.10.0

8 days ago

0.9.0

30 days ago

0.8.1

3 months ago

0.8.0

3 months ago

0.7.0

4 months ago

0.6.3

5 months ago

0.6.2

5 months ago

0.0.1-alpha.50

10 months ago

0.0.1-alpha.52

9 months ago

0.0.1-alpha.51

10 months ago

0.1.0

7 months ago

0.3.0

7 months ago

0.1.1

7 months ago

0.5.0

6 months ago

0.0.1-alpha.49

10 months ago

0.0.1

9 months ago

0.0.3

9 months ago

0.2.0

7 months ago

0.0.2

9 months ago

0.0.9

7 months ago

0.4.4

6 months ago

0.0.8

8 months ago

0.4.1

6 months ago

0.0.5

9 months ago

0.4.0

6 months ago

0.0.4

9 months ago

0.6.1

5 months ago

0.4.3

6 months ago

0.0.7

8 months ago

0.6.0

6 months ago

0.4.2

6 months ago

0.0.6

9 months ago

0.0.1-alpha.45

11 months ago

0.0.1-alpha.44

11 months ago

0.0.1-alpha.47

11 months ago

0.0.1-alpha.46

11 months ago

0.0.1-alpha.48

11 months ago

0.0.1-alpha.43

11 months ago

0.0.1-alpha.42

12 months ago

0.0.1-alpha.27

1 year ago

0.0.1-alpha.29

1 year ago

0.0.1-alpha.28

1 year ago

0.0.1-alpha.41

1 year ago

0.0.1-alpha.40

1 year ago

0.0.1-alpha.34

1 year ago

0.0.1-alpha.33

1 year ago

0.0.1-alpha.36

1 year ago

0.0.1-alpha.35

1 year ago

0.0.1-alpha.38

1 year ago

0.0.1-alpha.37

1 year ago

0.0.1-alpha.39

1 year ago

0.0.1-alpha.30

1 year ago

0.0.1-alpha.32

1 year ago

0.0.1-alpha.31

1 year ago

0.0.1-alpha.23

1 year ago

0.0.1-alpha.22

1 year ago

0.0.1-alpha.25

1 year ago

0.0.1-alpha.24

1 year ago

0.0.1-alpha.26

1 year ago

0.0.1-alpha.21

1 year ago

0.0.1-alpha.20

1 year ago

0.0.1-alpha.16

1 year ago

0.0.1-alpha.18

1 year ago

0.0.1-alpha.17

1 year ago

0.0.1-alpha.19

1 year ago

0.0.1-alpha.15

1 year ago

0.0.1-alpha.14

1 year ago

0.0.1-alpha.13

1 year ago

0.0.1-alpha.12

1 year ago

0.0.1-alpha.11

1 year ago

0.0.1-alpha.10

1 year ago

0.0.1-alpha.9

1 year ago

0.0.1-alpha.8

1 year ago

0.0.1-alpha.7

1 year ago

0.0.1-alpha.6

1 year ago

0.0.1-alpha.5

1 year ago

0.0.1-alpha.4

1 year ago

0.0.1-alpha.3

1 year ago

0.0.1-alpha.2

1 year ago

0.0.1-alpha.1

1 year ago