Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Get BigNumber.js app toolkit helper (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
JForsaken authored Apr 1, 2022
1 parent fc887f2 commit 8be775e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/app-toolkit/app-toolkit.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { StaticJsonRpcProvider } from '@ethersproject/providers';
import { BigNumber as BigNumberJS } from 'bignumber.js';
import { ethers } from 'ethers';

import { IContractFactory } from '~contract/contracts';
import { EthersMulticall } from '~multicall';
Expand All @@ -13,10 +15,9 @@ import { AppToolkitHelperRegistry } from './app-toolkit.helpers';
export const APP_TOOLKIT = Symbol('APP_TOOLKIT');

export interface IAppToolkit {
// Network Related
get globalContracts(): IContractFactory;

get helpers(): AppToolkitHelperRegistry;

getNetworkProvider(network: Network): StaticJsonRpcProvider;

getMulticall(network: Network): EthersMulticall;
Expand All @@ -36,4 +37,10 @@ export interface IAppToolkit {
getAppContractPositions<T = DefaultDataProps>(
...appTokenDefinition: AppGroupsDefinition[]
): Promise<ContractPosition<T>[]>;

// Global Helpers

get helpers(): AppToolkitHelperRegistry;

getBigNumber(source: BigNumberJS.Value | ethers.BigNumber): BigNumberJS;
}
20 changes: 16 additions & 4 deletions src/app-toolkit/app-toolkit.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { BigNumber as BigNumberJS } from 'bignumber.js';
import { ethers } from 'ethers';

import { ContractFactory } from '~contract';
import { EthersMulticall, MULTICALL_ADDRESSES } from '~multicall';
Expand All @@ -24,14 +26,12 @@ export class AppToolkit implements IAppToolkit {
this.contractFactory = new ContractFactory((network: Network) => this.networkProviderService.getProvider(network));
}

// Network Related

get globalContracts() {
return this.contractFactory;
}

get helpers() {
return this.helperRegistry;
}

getNetworkProvider(network: Network) {
return this.networkProviderService.getProvider(network);
}
Expand Down Expand Up @@ -59,11 +59,23 @@ export class AppToolkit implements IAppToolkit {
}

// Positions

getAppTokenPositions<T = DefaultDataProps>(...appTokenDefinition: AppGroupsDefinition[]) {
return this.positionService.getAppTokenPositions<T>(...appTokenDefinition);
}

getAppContractPositions<T = DefaultDataProps>(...appTokenDefinition: AppGroupsDefinition[]) {
return this.positionService.getAppContractPositions<T>(...appTokenDefinition);
}

// Global Helpers

get helpers() {
return this.helperRegistry;
}

getBigNumber(source: BigNumberJS.Value | ethers.BigNumber): BigNumberJS {
if (source instanceof ethers.BigNumber) return new BigNumberJS(source.toString());
return new BigNumberJS(source);
}
}
4 changes: 2 additions & 2 deletions src/app-toolkit/helpers/balance/token-balance.helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject, Injectable } from '@nestjs/common';
import BigNumber from 'bignumber.js';
import BigNumberJS from 'bignumber.js';
import { isArray, pick } from 'lodash';

import { IAppToolkit, APP_TOOLKIT } from '~app-toolkit/app-toolkit.interface';
Expand Down Expand Up @@ -75,7 +75,7 @@ export const drillBalance = <T extends Token>(
balanceUSD,
tokens: tokens.map((underlyingToken, i) => {
const pricePerShare = isArray(token.pricePerShare) ? token.pricePerShare[i] : token.pricePerShare;
const underlyingBalanceRawBN = new BigNumber(balanceRaw)
const underlyingBalanceRawBN = new BigNumberJS(balanceRaw)
.div(10 ** token.decimals)
.times(10 ** underlyingToken.decimals)
.times(pricePerShare);
Expand Down

0 comments on commit 8be775e

Please sign in to comment.