# @two-point-five/web3-sdk

> This is an official 2p5 Web3 SDK for connecting to blockchain networks.

Latest version **0.3.3-preview** (published 2023-07-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @two-point-five/web3-sdk
pnpm add @two-point-five/web3-sdk
yarn add @two-point-five/web3-sdk
bun add @two-point-five/web3-sdk
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.3-preview |
| Published | 2023-07-11 |
| First published | 2023-02-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=14.0.0 |
| Dependencies | 4 |
| Unpacked size | 65.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | gadingnst, sofianhw |

## Links

- npm: https://www.npmjs.com/package/@two-point-five/web3-sdk
- Repository: https://github.com/two-point-five/web3-sdk
- Homepage: https://github.com/two-point-five/web3-sdk#readme
- Issues: https://github.com/two-point-five/web3-sdk/issues
- npm.io page: https://npm.io/package/@two-point-five/web3-sdk

## Dependencies (4)

- [ethereumjs-util](https://npm.io/package/ethereumjs-util.md) ^7.1.5
- [cookie-universal](https://npm.io/package/cookie-universal.md) ^2.2.2
- [web3-provider-engine](https://npm.io/package/web3-provider-engine.md) ^16.0.4
- [@two-point-five/provider-engine](https://npm.io/package/@two-point-five/provider-engine.md) ^0.1.0

## Recent versions

- 0.3.3-preview (latest) — 2023-07-11
- 0.3.2-preview — 2023-07-11
- 0.3.1-preview — 2023-06-23
- 0.3.0-preview — 2023-06-19
- 0.2.3-preview — 2023-06-19
- 0.2.2-preview — 2023-06-19
- 0.2.1-preview — 2023-05-17
- 0.2.0-preview — 2023-02-24
- 0.1.1-preview — 2023-02-24
- 0.1.0-preview — 2023-02-22

## README

# Web3 SDK
This is an official 2p5 Web3 SDK for connecting to blockchain networks.

# Table of Contents
- [Web3 SDK](#web3-sdk)
- [Table of Contents](#table-of-contents)
  - [Prerequisite](#prerequisite)
  - [How to setup](#how-to-setup)
  - [What's inside?](#whats-inside)
    - [Apps and Packages](#apps-and-packages)
      - [Apps](#apps)
      - [Packages](#packages)
    - [Utilities](#utilities)
    - [Build](#build)
    - [Develop](#develop)
    - [Remote Caching](#remote-caching)
  - [Useful Links](#useful-links)
- [How to use](#how-to-use)
  - [With Web3.js](#with-web3js)
## Prerequisite
1. [NodeJS 18](https://nodejs.org/en/)
2. [2p5 Wallet](https://github.com/two-point-five/wallet)

## How to setup
1. Run `npm install`

## What's inside?
This turborepo uses [npm](https://www.npmjs.com/) as a package manager. It includes the following packages/apps:

### Apps and Packages
#### Apps
- `docs`: a [Next.js](https://nextjs.org/) app for creating `web3-sdk` documentation.
- `example-election`: another [Next.js](https://nextjs.org/) app for experimenting `web3-sdk` package.
- `example-smartcontract`: a [Hardhat](https://hardhat.org) project for contract development in `example-election` project.

#### Packages
- `~web3-sdk`: Main Package.
- `eslint-config-custom`: `eslint` configurations (includes `eslint-config-next` and `eslint-config-prettier`)
- `tsconfig`: `tsconfig.json`s used throughout the monorepo

Each package/app is 100% [TypeScript](https://www.typescriptlang.org/).

### Utilities

This turborepo has some additional tools already setup for you:

- [TypeScript](https://www.typescriptlang.org/) for static type checking
- [ESLint](https://eslint.org/) for code linting
- [Prettier](https://prettier.io) for code formatting

### Build

To build all apps and packages, run the following command:

```
cd web3-sdk
npm run build
```

### Develop

To develop all apps and packages, run the following command:

```
cd web3-sdk
npm run dev
```

if you want to test the package, you should start the project [2p5 Wallet](https://github.com/two-point-five/wallet) first. and make sure the variable `TPF_BASEURL_DEVELOPMENT` in file `packages/~web3-sdk/src/modules/widgets/WidgetManager.ts` is same as the url in `2p5 Wallet` project that you start before.

### Remote Caching

Turborepo can use a technique known as [Remote Caching](https://turbo.build/repo/docs/core-concepts/remote-caching) to share cache artifacts across machines, enabling you to share build caches with your team and CI/CD pipelines.

By default, Turborepo will cache locally. To enable Remote Caching you will need an account with Vercel. If you don't have an account you can [create one](https://vercel.com/signup), then enter the following commands:

```
cd my-turborepo
npx turbo login
```

This will authenticate the Turborepo CLI with your [Vercel account](https://vercel.com/docs/concepts/personal-accounts/overview).

Next, you can link your Turborepo to your Remote Cache by running the following command from the root of your turborepo:

```
npx turbo link
```

## Useful Links

Learn more about the power of Turborepo:

- [Pipelines](https://turbo.build/repo/docs/core-concepts/monorepos/running-tasks)
- [Caching](https://turbo.build/repo/docs/core-concepts/caching)
- [Remote Caching](https://turbo.build/repo/docs/core-concepts/remote-caching)
- [Filtering](https://turbo.build/repo/docs/core-concepts/monorepos/filtering)
- [Configuration Options](https://turbo.build/repo/docs/reference/configuration)
- [CLI Usage](https://turbo.build/repo/docs/reference/command-line-reference)

# How to use
## With Web3.js
```js
import Web3 from 'web3';
import TPFWeb, { withTPFProvider } from '@two-point-five/web3-sdk';

function initiateWeb3() {
  /**
   * string `test` is your dApp ID, but we haven't implemented it for now, so just fill it freely
   * string `goerli` is blockchain network. we have: "mainnet" | "goerli" | "sandbox2p5". choose one.
   * */
  const tpf = new TPFWeb('test', 'goerli', {
    environment: 'staging'
  });

  // pass the provider into Web3 instance
  const web3 = new Web3(tpf.provider);
  web3.eth.handleRevert = true;
  return web3;
}

/** example to get public address and balance */
async function getAccounts() {
  const web3 = initiateWeb3();
  try {
    const accounts = await web3.eth.getAccounts();
    const balance = await web3.eth.getBalance(accounts[0]);
    console.log({ accounts, balance });
  } catch (err) {
    console.error({ err });
  }
}
/**
 * of course, you can use `web3` instance like usual.
 * @see: https://web3js.readthedocs.io/en/v1.10.0/web3.html#example-local-geth-node
*/

// get TPF Provider
const provider = withTPFProvider(web3.currentProvider)
console.log(provider.isWalletConnected());
console.log(provider.getAccessToken());
```

---
_Source: https://npm.io/package/@two-point-five/web3-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
