2

Using Ethers, I'm creating an instance of my deployed contract as follows:

import MySwapV2FactoryABI from "src/abis/MySwapV2Factory.json";

const provider = new ethers.providers.JsonRpcProvider(Constants.Endpoint.FXCORE_ENDPOINT);
const factory: Contract = new ethers.Contract(Constants.Address.MYSWAPV2FACTORY_ADDRESS, MySwapV2FactoryABI, provider);

I have generated my contract types from my contract ABIs through using the typechain library, and I have the type for MySwapV2Factory

How can I make my factory variable a MySwapV2Factory type? Such as: const factory: MySwapV2Factory = new ethers.Contract(Constants.Address.MYSWAPV2FACTORY_ADDRESS, MySwapV2FactoryABI, provider);?

I'm still a novice with using Web3 libraries, so thank you in advance!

1 Answer 1

4

Typechain's Example

Typechain has a code example of this in their repo here (0xfbB26A55a5757326368Ea292fe9678EafAE9970a). I'll copy it over here first in case that's enough, and then talk through the process a bit.

import { BigNumber } from 'ethers';
import { Wallet } from 'ethers';

import { DummyTokenFactory } from 'typechain-out-dir/DummyTokenFactory';
import { DummyToken } from 'typechain-out-dir/DummyToken';
import { Erc20TokenFactory } from 'typechain-out-dir/Erc20TokenFactory';

const provider = getYourProvider(...);

// use the concrete contract factory if you need to operate on the bytecode (ie. deploy)
async function deployTestToken(ownerPK: string): Promise<DummyToken> {
    const owner = new Wallet(ownerPK, provider);
    return new DummyTokenFactory(owner).deploy();
}

// to call existing contracts, a factory for both the concrete contract and for the interface
// can be used since the ABI is the same
async function getTokenBalance(walletAddress: string, tokenAddress: string): Promise<BigNumber> {
    const token = Erc20TokenFactory.connect(tokenAddress, provider);
    return token.balanceOf(walletAddress);
}

Talking Through the Process

We'll assume you've installed Typechain, that it's configured properly, etc.

If your repo is pure Ethers (as opposed to a framework like Hardhat), then you'll need to run Typechain from the command line to generate typings. This means you'll also need the JSON artifacts of the contract(s) that you're working with. If you don't have it, if it's a verified contract on Etherscan or Sourcify, you can get the JSON from there, and paste it into your directory as a JSON file. Since you import the ABI in your code above, I use the path there for the snippet here.

typechain --target ethers-v5 --out-dir typings/ src/abis/MySwapFactoryV2.json

This will generate the Typechain typings in a directory called typings/ in your project, feel free to customize that to whatever you want.

In your case, there should be a MySwapV2Factory.ts in that directory. In your code, make sure to import it:

import { MySwapV2Factory } from 'typings/MySwapV2Factory';

If you've made it this far, the rest is simple! You initialize the factory like this (I've borrowed the arguments you used above):

const factory = new MySwapV2Factory(
    Constants.Address.MYSWAPV2FACTORY_ADDRESS, 
    MySwapV2FactoryABI, 
    provider
);

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.