Giter Site home page Giter Site logo

Comments (9)

hickscorp avatar hickscorp commented on May 22, 2024 1

@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.

krzkaczor avatar krzkaczor commented on May 22, 2024

@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.

NicolasMahe avatar NicolasMahe commented on May 22, 2024

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.

sebastianst avatar sebastianst commented on May 22, 2024

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.

hickscorp avatar hickscorp commented on May 22, 2024

@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.

sebastianst avatar sebastianst commented on May 22, 2024

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.

krzkaczor avatar krzkaczor commented on May 22, 2024

@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.

hickscorp avatar hickscorp commented on May 22, 2024

@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.

krzkaczor avatar krzkaczor commented on May 22, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.