1

I'm trying to understand the correct typing I should use for a web3 eth contract that is one of the parameters of my function:

import * as express from "express";
import { check, oneOf, validationResult } from "express-validator";
import * as web3 from "web3";

function publicApi(
  app: express.Application,
  blockPartiesPublic: <not sure what should go here>
) {
1
  • You can use TypeChain to automatically generate TypeScript bindings for your contracts. See my solidity-template for a boilerplate that comes with TypeChain pre-configured. Apr 28, 2022 at 9:39

2 Answers 2

3

I couldn't know for sure if you only want to know the web3.eth.Contract type. But if it's what I imagine you can use this:

import { Contract } from 'web3-eth-contract';

For example:

import { Contract } from 'web3-eth-contract';
import { AbiItem } from 'web3-utils';

const TokenInstance: Contract = new web3.eth.Contract(Token.abi as AbiItem, address)
0

If you have to stay with web3js solution only, then using web3.eth.Contract, as mentioned by @n3n3, is the correct way. I want to show another option by consuming eth contracts with github.com/0xweb-org/0xweb. It will generate TypeScript Classes for you with all read and write methods. You can install them as normal modules.

$ npm i 0xweb -g
$ 0xweb init

$ 0xweb install ./path/to/abi/BlockPartiesPublic.json --name BlockPartiesPublic --chain eth
import * as express from "express";
import { check, oneOf, validationResult } from "express-validator";
import { BlockPartiesPublic } from '@0xweb/eth/BlockPartiesPublic/BlockPartiesPublic'

function publicApi(
  app: express.Application,
  blockPartiesPublic: BlockPartiesPublic
) {
}

For validated contracts, you can also install classes directly from Etherscan and Co by specifying the address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.