Skip to content

Commit

Permalink
feat: bring in some override token logic so we can get token info fro…
Browse files Browse the repository at this point in the history
…m tokens which handle bytes over strings on name decimals etc etc
  • Loading branch information
joshstevens19 committed Aug 20, 2021
1 parent 378ac48 commit 3c4365b
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-uniswap-sdk",
"version": "3.4.0",
"version": "3.4.1",
"description": "Simple easy to understand SDK for uniswap which looks over best v2 and v3 to find you the best swap quote",
"main": "dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
5 changes: 3 additions & 2 deletions src/common/tokens/comp.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ChainId } from '../../enums/chain-id';
import { Token } from '../../factories/token/models/token';
import { ErrorCodes } from '../errors/error-codes';
import { UniswapError } from '../errors/uniswap-error';

/**
* COMP token context CHANGE CONTRACT ADDRESS INFO ETC
*/
export class COMP {
public static MAINNET() {
public static MAINNET(): Token {
return {
chainId: ChainId.MAINNET,
contractAddress: '0xc00e94Cb662C3520282E6f5717214004A7f26888',
Expand All @@ -20,7 +21,7 @@ export class COMP {
* Get COMP token info by chain id
* @param chainId The chain id
*/
public static token(chainId: ChainId | number) {
public static token(chainId: ChainId | number): Token {
switch (chainId) {
case ChainId.MAINNET:
return this.MAINNET();
Expand Down
5 changes: 3 additions & 2 deletions src/common/tokens/dai.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ChainId } from '../../enums/chain-id';
import { Token } from '../../factories/token/models/token';
import { ErrorCodes } from '../errors/error-codes';
import { UniswapError } from '../errors/uniswap-error';

/**
* DAI token context CHANGE CONTRACT ADDRESS INFO ETC
*/
export class DAI {
public static MAINNET() {
public static MAINNET(): Token {
return {
chainId: ChainId.MAINNET,
contractAddress: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
Expand All @@ -20,7 +21,7 @@ export class DAI {
* Get DAI token info by chain id
* @param chainId The chain id
*/
public static token(chainId: ChainId | number) {
public static token(chainId: ChainId | number): Token {
switch (chainId) {
case ChainId.MAINNET:
return this.MAINNET();
Expand Down
10 changes: 5 additions & 5 deletions src/common/tokens/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ETH {
};
}

public static ROPSTEN() {
public static ROPSTEN(): Token {
return {
chainId: ChainId.ROPSTEN,
contractAddress: appendEthToContractAddress(
Expand All @@ -62,7 +62,7 @@ export class ETH {
};
}

public static RINKEBY() {
public static RINKEBY(): Token {
return {
chainId: ChainId.RINKEBY,
contractAddress: appendEthToContractAddress(
Expand All @@ -74,7 +74,7 @@ export class ETH {
};
}

public static GORLI() {
public static GORLI(): Token {
return {
chainId: ChainId.GÖRLI,
contractAddress: appendEthToContractAddress(
Expand All @@ -86,7 +86,7 @@ export class ETH {
};
}

public static KOVAN() {
public static KOVAN(): Token {
return {
chainId: ChainId.KOVAN,
contractAddress: appendEthToContractAddress(
Expand All @@ -102,7 +102,7 @@ export class ETH {
* Get ETH token info by chain id
* @param chainId The chain id
*/
public static info(chainId: ChainId | number) {
public static info(chainId: ChainId | number): Token {
switch (chainId) {
case ChainId.MAINNET:
return this.MAINNET();
Expand Down
17 changes: 17 additions & 0 deletions src/common/tokens/overrides/dForce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ChainId } from '../../../enums/chain-id';
import { Token } from '../../../factories/token/models/token';

/**
* DFORCE token contract
*/
export class DFORCE {
public static MAINNET(): Token {
return {
chainId: ChainId.MAINNET,
contractAddress: '0x431ad2ff6a9C365805eBaD47Ee021148d6f7DBe0',
decimals: 18,
symbol: 'DF',
name: 'dForce token',
};
}
}
12 changes: 12 additions & 0 deletions src/common/tokens/overrides/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Token } from '../../../factories/token/models/token';
import { DFORCE } from './dForce';
import { MAI } from './mkr';

const _tokenOverrideInfo: Token[] = [DFORCE.MAINNET(), MAI.MAINNET()];

export const isTokenOverrideInfo = (contractAddress: string) => {
return _tokenOverrideInfo.find(
(info) =>
info.contractAddress.toLowerCase() === contractAddress.toLowerCase()
);
};
17 changes: 17 additions & 0 deletions src/common/tokens/overrides/mkr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ChainId } from '../../../enums/chain-id';
import { Token } from '../../../factories/token/models/token';

/**
* MAI token contract
*/
export class MAI {
public static MAINNET(): Token {
return {
chainId: ChainId.MAINNET,
contractAddress: '0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2',
decimals: 18,
symbol: 'MAI',
name: 'MAI token',
};
}
}
5 changes: 3 additions & 2 deletions src/common/tokens/usdc.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ChainId } from '../../enums/chain-id';
import { Token } from '../../factories/token/models/token';
import { ErrorCodes } from '../errors/error-codes';
import { UniswapError } from '../errors/uniswap-error';

/**
* USDC token context CHANGE CONTRACT ADDRESS INFO ETC
*/
export class USDC {
public static MAINNET() {
public static MAINNET(): Token {
return {
chainId: ChainId.MAINNET,
contractAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
Expand All @@ -20,7 +21,7 @@ export class USDC {
* Get USDC token info by chain id
* @param chainId The chain id
*/
public static token(chainId: ChainId | number) {
public static token(chainId: ChainId | number): Token {
switch (chainId) {
case ChainId.MAINNET:
return this.MAINNET();
Expand Down
5 changes: 3 additions & 2 deletions src/common/tokens/usdt.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ChainId } from '../../enums/chain-id';
import { Token } from '../../factories/token/models/token';
import { ErrorCodes } from '../errors/error-codes';
import { UniswapError } from '../errors/uniswap-error';

/**
* USDT token context CHANGE CONTRACT ADDRESS INFO ETC
*/
export class USDT {
public static MAINNET() {
public static MAINNET(): Token {
return {
chainId: ChainId.MAINNET,
contractAddress: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
Expand All @@ -20,7 +21,7 @@ export class USDT {
* Get USDT token info by chain id
* @param chainId The chain id
*/
public static token(chainId: ChainId | number) {
public static token(chainId: ChainId | number): Token {
switch (chainId) {
case ChainId.MAINNET:
return this.MAINNET();
Expand Down
5 changes: 3 additions & 2 deletions src/common/tokens/wbtc.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ChainId } from '../../enums/chain-id';
import { Token } from '../../factories/token/models/token';
import { ErrorCodes } from '../errors/error-codes';
import { UniswapError } from '../errors/uniswap-error';

/**
* WBTC token context
*/
export class WBTC {
public static MAINNET() {
public static MAINNET(): Token {
return {
chainId: ChainId.MAINNET,
contractAddress: '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599',
Expand All @@ -20,7 +21,7 @@ export class WBTC {
* Get WBTC token info by chain id
* @param chainId The chain id
*/
public static token(chainId: ChainId | number) {
public static token(chainId: ChainId | number): Token {
switch (chainId) {
case ChainId.MAINNET:
return this.MAINNET();
Expand Down
10 changes: 5 additions & 5 deletions src/common/tokens/weth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class WETHContract {
};
}

public static ROPSTEN() {
public static ROPSTEN(): Token {
return {
chainId: ChainId.ROPSTEN,
contractAddress: '0xc778417E063141139Fce010982780140Aa0cD5Ab',
Expand All @@ -30,7 +30,7 @@ export class WETHContract {
};
}

public static RINKEBY() {
public static RINKEBY(): Token {
return {
chainId: ChainId.RINKEBY,
contractAddress: '0xc778417E063141139Fce010982780140Aa0cD5Ab',
Expand All @@ -40,7 +40,7 @@ export class WETHContract {
};
}

public static GORLI() {
public static GORLI(): Token {
return {
chainId: ChainId.GÖRLI,
contractAddress: '0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6',
Expand All @@ -50,7 +50,7 @@ export class WETHContract {
};
}

public static KOVAN() {
public static KOVAN(): Token {
return {
chainId: ChainId.KOVAN,
contractAddress: '0xd0A1E359811322d97991E03f863a0C30C2cF029C',
Expand All @@ -64,7 +64,7 @@ export class WETHContract {
* Get WETH token info by chain id
* @param chainId The chain id
*/
public static token(chainId: ChainId | number) {
public static token(chainId: ChainId | number): Token {
switch (chainId) {
case ChainId.MAINNET:
return this.MAINNET();
Expand Down
7 changes: 4 additions & 3 deletions src/factories/pair/uniswap-pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ export class UniswapPair {
const uniswapFactoryContext: UniswapPairFactoryContext = {
fromToken: tokens.find(
(t) =>
t.contractAddress ===
this._uniswapPairContext.fromTokenContractAddress
t.contractAddress.toLowerCase() ===
this._uniswapPairContext.fromTokenContractAddress.toLowerCase()
)!,
toToken: tokens.find(
(t) =>
t.contractAddress === this._uniswapPairContext.toTokenContractAddress
t.contractAddress.toLowerCase() ===
this._uniswapPairContext.toTokenContractAddress.toLowerCase()
)!,
ethereumAddress: this._uniswapPairContext.ethereumAddress,
settings: this._uniswapPairContext.settings || new UniswapPairSettings(),
Expand Down
6 changes: 6 additions & 0 deletions src/factories/token/token.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BigNumber } from 'ethers';
import { ContractContext as ERC20ContractContext } from '../../ABI/types/erc20-contract';
import { ContractContext } from '../../common/contract-context';
import { ETH, isNativeEth } from '../../common/tokens/eth';
import { isTokenOverrideInfo } from '../../common/tokens/overrides';
import { getAddress } from '../../common/utils/get-address';
import { UniswapVersion } from '../../enums/uniswap-version';
import { EthersProvider } from '../../ethers-provider';
Expand Down Expand Up @@ -35,6 +36,11 @@ export class TokenFactory {
if (isNativeEth(this._tokenContractAddress)) {
return ETH.info(this._ethersProvider.network().chainId);
} else {
const overridenToken = isTokenOverrideInfo(this._tokenContractAddress);
if (overridenToken) {
return overridenToken;
}

const SYMBOL = 0;
const DECIMALS = 1;
const NAME = 2;
Expand Down
Loading

0 comments on commit 3c4365b

Please sign in to comment.