Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cToken constants missing from Compound typescript definitions #9

Open
0xbhagwan opened this issue Dec 6, 2020 · 2 comments
Open

cToken constants missing from Compound typescript definitions #9

0xbhagwan opened this issue Dec 6, 2020 · 2 comments

Comments

@0xbhagwan
Copy link

Hello,

Importing the library into a typescript project and accessing any of the properties defined in src/constants.ts such as Compound.cDAI as in the example in the readme throws the following error:

[tsserver 2339] [E] Property 'cDai' does not exist on type '{ (provider?:       
                           string | Provider, options?: CompoundOptions): CompoundInstance; eth: typeof

Reproducing

On a typescript file with strict mode enabled:

import Compound from "@compound-finance/compound-js";

console.log(Compound.cDai);

Cause

The generated type definition file at 'dist/nodejs/index.d.ts' the constants such as cDAI are missing:

...
declare const Compound: {
    (provider?: Provider | string, options?: CompoundOptions): CompoundInstance;
    eth: typeof eth;
    api: typeof api;
    util: typeof util;
    _ethers: typeof ethers;
    decimals: {
        cBAT: number;
        cCOMP: number;
        cDAI: number;
        cETH: number;
        cREP: number;
        cSAI: number;
        cUNI: number;
        cUSDC: number;
        cUSDT: number;
        cWBTC: number;
        cZRX: number;
        BAT: number;
        COMP: number;
        DAI: number;
        ETH: number;
        REP: number;
        SAI: number;
        UNI: number;
        USDC: number;
        USDT: number;
        WBTC: number;
        ZRX: number;
        KNC: number;
        LINK: number;
        BTC: number;
    };
    comp: {
        getCompBalance: typeof comp.getCompBalance;
        getCompAccrued: typeof comp.getCompAccrued;
    };
};
...

This is because on src/index.ts:93 the constant properties are assigned with Object.assign(Compound, constants); which does not preserve the types.

Proposed fix

Stop using object assign and use the spread operator instead, since types are preserved that way.

Then 'dist/nodejs/index.d.ts' becomes:

...
declare const Compound: {
    PriceFeed: string;
    Maximillion: string;
    CompoundLens: string;
    GovernorAlpha: string;
    Comptroller: string;
    Reservoir: string;
    KNC: string;
    LINK: string;
    BTC: string;
    cBAT: string;
    cCOMP: string;
    cDAI: string;
    cETH: string;
    cREP: string;
    cSAI: string;
    cUNI: string;
    cUSDC: string;
    cUSDT: string;
    cWBTC: string;
    cZRX: string;
    BAT: string;
    COMP: string;
    DAI: string;
    ETH: string;
    REP: string;
    SAI: string;
    UNI: string;
    USDC: string;
    USDT: string;
    WBTC: string;
    ZRX: string;
    eth: typeof eth;
    api: typeof api;
    util: typeof util;
    _ethers: typeof ethers;
    decimals: {
        cBAT: number;
        cCOMP: number;
        cDAI: number;
        cETH: number;
        cREP: number;
        cSAI: number;
        cUNI: number;
        cUSDC: number;
        cUSDT: number;
        cWBTC: number;
        cZRX: number;
        BAT: number;
        COMP: number;
        DAI: number;
        ETH: number;
        REP: number;
        SAI: number;
        UNI: number;
        USDC: number;
        USDT: number;
        WBTC: number;
        ZRX: number;
        KNC: number;
        LINK: number;
        BTC: number;
    };
    comp: {
        getCompBalance: typeof comp.getCompBalance;
        getCompAccrued: typeof comp.getCompAccrued;
    };
};
...
@0xbhagwan
Copy link
Author

As it can be seen on the proposed fix, the (provider?: Provider | string, options?: CompoundOptions): CompoundInstance; is lost when using the spread operator.

Instead of trying to replicate the behaviour of Object.assign() while preserving types, I added some type definitions for the exported Compound object at src/index.ts, as well as for constants, and decimals at (provider?: Provider | string, options?: CompoundOptions): CompoundInstance;.

Since I was already adding types I also went ahead and wrote interfaces for Account and AccountReponse so that the example at https://compound.finance/docs/compound-js#account can run on strict mode without type issues. Using the new keywoard when initiallizing the compound instance is no longer an issue for the typescript compiler.

All changes can be seen at #10.

@deeeed
Copy link

deeeed commented Jul 25, 2022

is there a way to access this fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants