Comments (9)
@sebastianst Thanks a lot. This is pretty much the same as we came up with as well.
I think the TypeChain should allow for "configurable" BigNumber
library. It is such a great tool, I think it would benefit from some little flexibility.
from typechain.
@eswarasai this is expected behavior. Targets like [email protected]
or truffle
will generate typings for API provided by given libraries. This means that decision to use a string
in public API was made by web3 team, not ethereum-ts
/TypeChain
.
You can always write automatic transformer on userland using a combination of conditional types and dynamically wrapping responses from web3. I know it's not the cleanest solution but this way we can stay out of discussions what API should TypeChain provide in generated wrappers.
I think it raises a broader question: should TypeChain allow to customize targets. For example, one can always write own target and just copy web3 target with some changes. I think we should at least document and support easily this use case.
from typechain.
It seems that [email protected] actually support BigNumber
as well as BN
for anything in Wei (transaction value and gas price at least).
Source:
https://web3js.readthedocs.io/en/1.0/web3-eth.html?highlight=BigNumber#sendtransaction
from typechain.
Note that web3.utils
etc. use BN
from BN.js internally. Some functions don't even support BigNumber
but only BN
, like web3.utils.toWei
, so I suggest to change the exported type to BN
to make it compatible to web3.js
.
from typechain.
@sebastianst How do you do that? The .ts
files generated by TypeChain import BigNumber
and there's no way to override that. Did you find a solution to this problem?
from typechain.
I ended up with some hacks in the scripts
section of the package.json
file:
"scripts": {
"postinstall": "patch node_modules/truffle-typings/index.d.ts < patches/truffle-typings.d.patch || true",
"build:contracts": "truffle compile",
"build:types": "typechain --target truffle './build/**/*.json'",
"postbuild:types": "sed -i -e 's/BigNumber/BN/g' -e '/bignumber\\.js/d' types/truffle-contracts/index.d.ts",
"build:js": "tsc -p .",
"build": "yarn build:contracts && yarn build:types && yarn build:js",
"test": "truffle test"
}
The patch looks like
--- /home/self/truffle-typings/index.d.ts 2019-02-04 13:23:01.173368580 +0100
+++ truffle.d.ts 2019-02-04 13:24:30.993372687 +0100
@@ -3,15 +3,19 @@
*/
/// <reference types="chai" />
/// <reference types="mocha" />
+
+declare type BN = import("web3-utils").BN;
+declare type Web3 = import("web3").default;
+
declare const assert: Chai.AssertStatic;
declare const expect: Chai.ExpectStatic;
+declare const web3: Web3;
+
declare function contract(name: string, test: (accounts: Truffle.Accounts) => void): void;
declare const artifacts: Truffle.Artifacts;
-declare const web3: any;
-
/**
* Namespace
*/
@@ -19,10 +23,10 @@
type Accounts = string[];
interface TransactionDetails {
- from: string;
- gas?: number | string;
- gasPrice?: number | string;
- value?: number | string;
+ from?: string;
+ gas?: BN | number | string;
+ gasPrice?: BN | number | string;
+ value?: BN | string;
}
export interface TransactionLog {
But I didn't test this for the last three months, so this probably has to be adjusted.
Does that answer your question?
from typechain.
@hickscorp @sebastianst I agree that we need "custom" target or something like this. Let me know if you want to work on this
from typechain.
@krzkaczor I'd be very happy to help but I've literally never worked on a tooling npm
package - I'm a bit scared that I'll go against good practices. If you would however like me to help with specs for whoever would like to take on the development part, happy to do a clean ticket recap'ing what should be done.
from typechain.
@hickscorp don't worry about it, you did a fine job on truffle-typings
repo ;)
Some background on this feature. TypeChain under the hood uses https://github.com/krzkaczor/ts-generator which abstracts away some parts regarding type generation. What we could do is allow typechain users to provide custom "plugin" for ts-generator. All existing targets are just plugins for ts-generator.
CLI usage could look something like:
typechain --target=./typechain-generator.ts --outDir app/contracts './node_modules/neufund-contracts/build/contracts/*.json'
from typechain.
Related Issues (20)
- Feature request: add support for viem HOT 1
- Cannot find namespaces for events when `alwaysGenerateOverloads` is turned on HOT 3
- [hardhat + ethers-v6]Fallback to BaseContract when using hre.ethers.getContractFactory.attach HOT 2
- Update Contributing file
- getting wrong type after .connect() HOT 1
- 'type' folder layout changed and unable to import type for ethers.
- Overrides for `staticCall` and `staticCallResult` should always be viewOverrides in ethers-v6
- ethers-v6: wrong type generated for listening to contract events HOT 4
- Use branded type instead of string for addresses
- explicit contract method args
- Accessing `factoryInstance.interface` should return the exact contract interface type
- Upgrade js-sha3 to audited sha3 library
- Types are not generated if hardhat config file is not typescript
- typechain-hardhat does not consider hardhat 'sources' config
- TypeScript: 'interface' is a reserved word in strict mode HOT 1
- Generated types are broken if contract function names clash with ethers built-ins
- I want to use strict type to get contract response HOT 1
- ethers-v6: AddressLike in encodeFunctionData is incorrect
- //cloud3@ethnet
- How to use zksync-ethers with typechain?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typechain.