Skip to content

Commit

Permalink
extend custom network
Browse files Browse the repository at this point in the history
  • Loading branch information
joshstevens19 committed Oct 25, 2021
1 parent 19c5b51 commit e737fa7
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 52 deletions.
41 changes: 32 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,32 @@ export interface CloneUniswapContractDetails {
v3Override?: CloneUniswapContractDetailsV3 | undefined;
}

export interface Token {
chainId: ChainId;
contractAddress: string;
decimals: number;
symbol: string;
name: string;
}

export interface NativeCurrencyInfo {
name: string;
symbol: string;
}

export interface CustomNetwork {
nameNetwork: string;
multicallContractAddress: string;
nativeCurrency: NativeCurrencyInfo;
nativeWrappedTokenInfo: Token;
// defined your base tokens here!
baseTokens?: {
usdt?: Token | undefined;
dai?: Token | undefined;
comp?: Token | undefined;
usdc?: Token | undefined;
wbtc?: Token | undefined;
};
}

export class UniswapPairSettings {
Expand Down Expand Up @@ -182,9 +205,9 @@ export class UniswapPairSettings {
}
```
### Ethereum provider
### With only the chainId
This will use your ethereum provider you pass in. This will work with any web3 provider, ethers provider or custom provider. For example when using MetaMask you can pass in the window.ethereum and it work. You must supply the ethereum address and the wallet be approved to use for the dApp and unlocked before passing it in. The uniswap sdk makes those assumptions without them it will not work as MetaMask is not allowed access to your dApp. Any change of network or ethereum address change you will need to handle in your dApp and regenerate the uniswap pair context. Most the time the contract addresses for your tokens are different anyway.
This will use a infura endpoint without you having to pass in a node
```ts
import { UniswapPair, ChainId, UniswapVersion, ETH } from 'simple-uniswap-sdk';
Expand All @@ -196,7 +219,7 @@ const uniswapPair = new UniswapPair({
toTokenContractAddress: '0x1985365e9f78359a9B6AD760e32412f4a445E862',
// the ethereum address of the user using this part of the dApp
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
ethereumProvider: YOUR_WEB3_ETHERS_OR_CUSTOM_ETHEREUM_PROVIDER,
chainId: ChainId.MAINNET,
settings: new UniswapPairSettings({
// if not supplied it will use `0.005` which is 0.5%
// please pass it in as a full number decimal so 0.7%
Expand All @@ -219,9 +242,9 @@ const uniswapPair = new UniswapPair({
const uniswapPairFactory = await uniswapPair.createFactory();
```
### With only the chainId
### With your own provider url
This will use a infura endpoint without you having to pass in a node
This will use your node you pass in you must pass us the chainId as well, this stops the ethers instance calling pointless `JSONRPC` calls to get the chain id before every `JSONRPC` call.
```ts
import { UniswapPair, ChainId, UniswapVersion, ETH } from 'simple-uniswap-sdk';
Expand All @@ -234,6 +257,7 @@ const uniswapPair = new UniswapPair({
// the ethereum address of the user using this part of the dApp
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
chainId: ChainId.MAINNET,
providerUrl: YOUR_PROVIDER_URL,
settings: new UniswapPairSettings({
// if not supplied it will use `0.005` which is 0.5%
// please pass it in as a full number decimal so 0.7%
Expand All @@ -256,9 +280,9 @@ const uniswapPair = new UniswapPair({
const uniswapPairFactory = await uniswapPair.createFactory();
```
### With your own provider url
### Custom Ethereum provider
This will use your node you pass in you must pass us the chainId as well, this stops the ethers instance calling pointless `JSONRPC` calls to get the chain id before every `JSONRPC` call.
This will use your ethereum provider you pass in. This will work with any web3 provider, ethers provider or custom provider. For example when using MetaMask you can pass in the window.ethereum and it work. You must supply the ethereum address and the wallet be approved to use for the dApp and unlocked before passing it in. The uniswap sdk makes those assumptions without them it will not work as MetaMask is not allowed access to your dApp. Any change of network or ethereum address change you will need to handle in your dApp and regenerate the uniswap pair context. Most the time the contract addresses for your tokens are different anyway.
```ts
import { UniswapPair, ChainId, UniswapVersion, ETH } from 'simple-uniswap-sdk';
Expand All @@ -270,8 +294,7 @@ const uniswapPair = new UniswapPair({
toTokenContractAddress: '0x1985365e9f78359a9B6AD760e32412f4a445E862',
// the ethereum address of the user using this part of the dApp
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
chainId: ChainId.MAINNET,
providerUrl: YOUR_PROVIDER_URL,
ethereumProvider: YOUR_WEB3_ETHERS_OR_CUSTOM_ETHEREUM_PROVIDER,
settings: new UniswapPairSettings({
// if not supplied it will use `0.005` which is 0.5%
// please pass it in as a full number decimal so 0.7%
Expand Down
28 changes: 24 additions & 4 deletions src/common/tokens/eth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChainId } from '../../enums/chain-id';
import { NativeCurrencyInfo } from '../../factories/pair/models/custom-network';
import { Token } from '../../factories/token/models/token';
import { ErrorCodes } from '../errors/error-codes';
import { UniswapError } from '../errors/uniswap-error';
Expand All @@ -24,12 +25,20 @@ export const isNativeEth = (contractAddress: string): boolean => {
return contractAddress.includes(ETH_PREFIX);
};

export const turnTokenIntoEthForResponse = (token: Token): Token => {
export const turnTokenIntoEthForResponse = (
token: Token,
nativeCurrencyInfo: NativeCurrencyInfo | undefined
): Token => {
const clone = deepClone(token);
// clear down contract address
clone.contractAddress = 'NO_CONTRACT_ADDRESS';
clone.symbol = ETH_SYMBOL;
clone.name = ETH_NAME;
if (nativeCurrencyInfo) {
clone.symbol = nativeCurrencyInfo.symbol;
clone.name = nativeCurrencyInfo.name;
} else {
clone.symbol = ETH_SYMBOL;
clone.name = ETH_NAME;
}

return clone;
};
Expand Down Expand Up @@ -102,7 +111,18 @@ export class ETH {
* Get ETH token info by chain id
* @param chainId The chain id
*/
public static info(chainId: ChainId | number): Token {
public static info(
chainId: ChainId | number,
customNetworkNativeWrappedTokenInfo: Token | undefined = undefined
): Token {
if (customNetworkNativeWrappedTokenInfo) {
return {
...customNetworkNativeWrappedTokenInfo,
contractAddress: appendEthToContractAddress(
customNetworkNativeWrappedTokenInfo.contractAddress
),
};
}
switch (chainId) {
case ChainId.MAINNET:
return this.MAINNET();
Expand Down
13 changes: 10 additions & 3 deletions src/common/utils/trade-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import { ETH } from '../tokens/eth';
export function getTradePath(
chainId: ChainId,
fromToken: Token,
toToken: Token
toToken: Token,
customNetworkNativeWrappedTokenInfo: Token | undefined
): TradePath {
if (fromToken.contractAddress === ETH.info(chainId).contractAddress) {
if (
fromToken.contractAddress ===
ETH.info(chainId, customNetworkNativeWrappedTokenInfo).contractAddress
) {
return TradePath.ethToErc20;
}

if (toToken.contractAddress === ETH.info(chainId).contractAddress) {
if (
toToken.contractAddress ===
ETH.info(chainId, customNetworkNativeWrappedTokenInfo).contractAddress
) {
return TradePath.erc20ToEth;
}

Expand Down
17 changes: 17 additions & 0 deletions src/factories/pair/models/custom-network.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import { Token } from '../../token/models/token';

export interface NativeCurrencyInfo {
name: string;
symbol: string;
}

export interface CustomNetwork {
nameNetwork: string;
multicallContractAddress: string;
nativeCurrency: NativeCurrencyInfo;
nativeWrappedTokenInfo: Token;
// defined your base tokens here!
baseTokens?: {
usdt?: Token | undefined;
dai?: Token | undefined;
comp?: Token | undefined;
usdc?: Token | undefined;
wbtc?: Token | undefined;
};
}
22 changes: 17 additions & 5 deletions src/factories/pair/uniswap-pair.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ export class UniswapPairFactory {
private _fromTokenFactory = new TokenFactory(
this._uniswapPairFactoryContext.fromToken.contractAddress,
this._uniswapPairFactoryContext.ethersProvider,
this._uniswapPairFactoryContext.settings.customNetwork?.multicallContractAddress,
this._uniswapPairFactoryContext.settings.customNetwork,
this._uniswapPairFactoryContext.settings.cloneUniswapContractDetails
);

private _toTokenFactory = new TokenFactory(
this._uniswapPairFactoryContext.toToken.contractAddress,
this._uniswapPairFactoryContext.ethersProvider,
this._uniswapPairFactoryContext.settings.customNetwork?.multicallContractAddress
this._uniswapPairFactoryContext.settings.customNetwork
);

private _uniswapRouterFactory = new UniswapRouterFactory(
Expand Down Expand Up @@ -354,7 +354,10 @@ export class UniswapPairFactory {
bestRouteQuote.uniswapVersion
)
: undefined,
toToken: turnTokenIntoEthForResponse(this.toToken),
toToken: turnTokenIntoEthForResponse(
this.toToken,
this._uniswapPairFactoryContext.settings?.customNetwork?.nativeCurrency
),
toBalance: new BigNumber(bestRouteQuotes.toBalance)
.shiftedBy(this.toToken.decimals * -1)
.toFixed(),
Expand Down Expand Up @@ -487,7 +490,10 @@ export class UniswapPairFactory {
toBalance: new BigNumber(bestRouteQuotes.toBalance)
.shiftedBy(this.toToken.decimals * -1)
.toFixed(),
fromToken: turnTokenIntoEthForResponse(this.fromToken),
fromToken: turnTokenIntoEthForResponse(
this.fromToken,
this._uniswapPairFactoryContext.settings?.customNetwork?.nativeCurrency
),
fromBalance: {
hasEnough: bestRouteQuotes.hasEnoughBalance,
balance: bestRouteQuotes.fromBalance,
Expand All @@ -507,7 +513,13 @@ export class UniswapPairFactory {
*/
private tradePath(): TradePath {
const network = this._uniswapPairFactoryContext.ethersProvider.network();
return getTradePath(network.chainId, this.fromToken, this.toToken);
return getTradePath(
network.chainId,
this.fromToken,
this.toToken,
this._uniswapPairFactoryContext.settings.customNetwork
?.nativeWrappedTokenInfo
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/factories/pair/uniswap-pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class UniswapPair {

const tokensFactory = new TokensFactory(
this._ethersProvider,
this._uniswapPairContext.settings?.customNetwork?.multicallContractAddress
this._uniswapPairContext.settings?.customNetwork
);
const tokens = await tokensFactory.getTokens([
this._uniswapPairContext.fromTokenContractAddress,
Expand Down
Loading

0 comments on commit e737fa7

Please sign in to comment.