question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is the intended use of the Factory non-type .ts file documented?

See original GitHub issue

I’m curious what the purpose of the ...Factory.ts file is in a project like this:

types/Greeter.d.ts
types/GreeterFactory.ts
types/index.d.ts

Is that documented somewhere? I’m not sure if this question is ethers-specific or not. Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
quezakcommented, Jun 24, 2020

I’ve added a simple readme including what I’ve wrote above plus some basic info about contract type declarations. Thanks @pcowgill for the suggestion!

2reactions
quezakcommented, Jun 23, 2020

Yes. I’ll write a short overview here, @pcowgill please tell us what should be described more.

The factory classes are an extension of ethers’ ContractFactory. They serve two main purposes:

  • wrap passing contract ABI and bytecode to the ContractFactory class, so you don’t have to load and parse the JSON manually
  • provide a correctly typed interface to ContractFactory (since it returns plain Contract instances).

Another case are abstract contracts or solidity interfaces, because they have no bytecode. For those, a simplified factory is generated that doesn’t extends ContractFactory, and only includes the static connect method, so you can easily connect to a deployed instance without having to pass the ABI manually.

Very simple example:

// suppose you have an `Erc20Token` solidity interface and a `DummyToken` contract implementing it

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

import { DummyTokenFactory } from '@contracts/DummyTokenFactory';
import { DummyToken } from '@contracts/DummyToken';
import { Erc20TokenFactory } from '@contracts/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.functions.balanceOf(address);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript libraries should be written in TypeScript
I completely agree with the sentiment that typed libraries are more stable and self-documenting. You can autogenerate documentation from ...
Read more >
What's new in ReSharper - JetBrains
XML documentation in decompiled / metadata view file header. ... Improved performance of fix in scope, as it now uses the results of ......
Read more >
3a2425a5aed1bef93dab954745...
testRenamingUpdatesTheStub() 82b3364: fixed tests (documentation format) ... 34f02fe: added mako file type factory ad41d5b: Merge branch ...
Read more >
Programming, Coding and Algorithms Questions and Answers
Programming, Coding and Algorithms Questions and Answers. Coding is a complex process that requires precision and attention to detail.
Read more >
1:11 vo0?31 o0~P~ - DTIC
The chronicles of disasters is as old as recorded history, ranging from the ... extraordinarily complex task was a leading purpose of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found