# web3x-codegen

> Contract interface code generator for web3x.

Latest version **4.0.6** (published 2019-11-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install web3x-codegen
pnpm add web3x-codegen
yarn add web3x-codegen
bun add web3x-codegen
```

Provides the command `web3x-codegen`.

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.6 |
| Published | 2019-11-25 |
| First published | 2019-06-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 29.7 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 213 |
| Maintainers | xf00f |
| Keywords | ethereum, typescript, web3, codegen |

## Links

- npm: https://www.npmjs.com/package/web3x-codegen
- Repository: https://github.com/xf00f/web3x
- Issues: https://github.com/xf00f/web3x/issues
- npm.io page: https://npm.io/package/web3x-codegen

## Dependencies (4)

- [got](https://npm.io/package/got.md) ^9.3.0
- [web3x](https://npm.io/package/web3x.md) ^4.0.6
- [fs-extra](https://npm.io/package/fs-extra.md) ^7.0.1
- [typescript](https://npm.io/package/typescript.md) ^3.2.2

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 4.0.6 (latest) — 2019-11-25
- 4.0.4 — 2019-08-26
- 4.0.1 — 2019-06-23
- 4.0.0 — 2019-06-23
- 1.0.0 — 2019-06-19

## README

# web3x-codegen

[![Version](https://img.shields.io/npm/v/web3x-codegen.svg)](https://www.npmjs.com/package/web3x-codegen)
[![Downloads](https://img.shields.io/npm/dw/web3x-codegen.svg)](https://www.npmjs.com/package/web3x-codegen)
[![GitHub Stars](https://img.shields.io/github/stars/xf00f/web3x.svg)](https://github.com/xf00f/web3x/stargazers)
[![GitHub Issues](https://img.shields.io/github/issues/xf00f/web3x.svg)](https://github.com/xf00f/web3x/issues)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/xf00f/web3x/blob/master/web3x-codegen/LICENSE)

Contract interface code generator for `web3x`.

Interacting with contracts without type safety is tedious at best, and dangerous at worst. `web3x-codegen` generates typings for contract ABIs either local, or remote from a simple configuration file called `contracts.json`.

### Installing

```
yarn add -D web3x-codegen
```

### Defining contracts.json

An example `contracts.json` looks like:

```json
{
  "outputPath": "./src/contracts",
  "contracts": {
    "DaiContract": {
      "source": "etherscan",
      "net": "mainnet",
      "address": "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
    },
    "MyTruffleContract": {
      "source": "truffle",
      "buildFile": "../truffle-project/build/contracts/MyContract.json"
    },
    "MyRawAbiContract": {
      "source": "files",
      "abiFile": "../my-contract/abi.json",
      "initDataFile": "../my-contract/init-code.bin"
    }
  }
}
```

Run the code generator:

```
yarn web3x-codegen
```

The generator will create 3 contracts:

- For the first it uses etherscan to download the contract ABI and initialisation code at the given address, and generates the interface at `./src/contracts/DaiContract.ts`.
- For the second it specifies a truffle build output and generates its interface at `./src/contracts/MyTruffleContract.ts`.
- For the third it reads a raw ABI file and compiled initialisation code from local files, and generates its interface at `./src/contracts/MyRawAbiContract.ts`. The `initDataFile` property is optional but you won't be able to easily deploy the contract without it.

For an example of the code generated, take a look at this [example](../web3x-node-example/src/contracts/DaiContract.ts).

### Using generated contracts

The following code demonstrates how to use the generated contract class. It's a similar API as used in web3.js, only now with type safety.

```typescript
import { Address } from 'web3x/address';
import { fromWei } from 'web3x/utils';
import { WebsocketProvider } from 'web3x/providers';
import { Eth } from 'web3x/eth';
import { DaiContract } from './contracts/DaiContract';

const DAI_CONTRACT_ADDRESS = Address.fromString('0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359');

async function main() {
  const provider = new WebsocketProvider('wss://mainnet.infura.io/ws');
  const eth = new Eth(provider);

  try {
    const contract = new DaiContract(eth, DAI_CONTRACT_ADDRESS);
    const daiBalance = await contract.methods.balanceOf(Address.ZERO).call();
    console.log(`Balance of 0 address DAI: ${fromWei(daiBalance, 'ether')}`);
  } finally {
    provider.disconnect();
  }
}

main().catch(console.error);
```

Deploying contracts is trivial as well, as the bytecode is imported by `web3x-codegen` and included as part of the contract class.
The following code deploys an exact replica of the DAI contract on mainnet, only now you can mint your own funds.

```typescript
import { Address } from 'web3x/address';
import { fromWei, toWei } from 'web3x/utils';
import { WebsocketProvider } from 'web3x/providers';
import { Eth } from 'web3x/eth';
import { DaiContract } from './contracts/DaiContract';

async function main() {
  const from = Address.fromString('0x903ddd91207f737255ca93eb5885c0e087be0fc3');
  const gasPrice = 50000;
  const provider = new WebsocketProvider('wss://mainnet.infura.io/ws');
  const eth = new Eth(provider);

  try {
    const contract = new DaiContract(eth);
    await contract
      .deploy('xf00f token')
      .send({ from, gasPrice })
      .getReceipt();
    await contract.methods
      .mint(toWei(1000, 'ether'))
      .send({ from, gasPrice })
      .getReceipt();
    const balance = await contract.methods.balanceOf(from).call();
    console.log(`Balance of ${from}: ${fromWei(balance, 'ether')}`);
  } finally {
    provider.disconnect();
  }
}

main().catch(console.error);
```

## Packages

- [web3x-codegen](https://www.npmjs.com/package/web3x-codegen)

---
_Source: https://npm.io/package/web3x-codegen · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
