Skip to content

Commit

Permalink
should be able to quote in native currency string (Uniswap#123)
Browse files Browse the repository at this point in the history
* should be able to quote in native currency string

* allow for other strings to map to native currency objects

* fix comment
  • Loading branch information
snreynolds authored Jul 20, 2022
1 parent 9786bba commit 9623259
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 26 deletions.
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,23 @@ Calldata: 0x414bf389000000000000000000000000dac17f958d2ee523a2206206994597c13d83
./bin/cli quote --tokenIn 0x2791bca1f2de4661ed88a30c99a7a9449aa84174 --tokenOut 0x7ceb23fd6bc0add59e62ac25578270cff1b9f619 --amount 5 --exactIn --minSplits 1 --protocols v3 --router alpha --chainId 137
```

## Celo Mainnet

```
./bin/cli quote --tokenIn CELO --tokenOut 0x765DE816845861e75A25fCA122bb6898B8B1282a --amount 5 --exactIn --minSplits 1 --protocols v3 --router alpha --chainId 42220
```

## Adding a new Chain

The main components to complete are:
* Deploy contracts on chain, add the pools to subgraph
* Populate v3 providers in `src/providers/v3/subgraph-provider` and `src/providers/v3/static-subgraph-provider`
* Populate chainId and addresses in `src/util/chains.ts` and `src/util/addresses.ts`
* Populate token providers in `src/providers/caching-token-provider` and `src/providers/token-provider.ts`
* Populate gas constants in `src/routers/alpha-router/gas-models/*`
* Populate bases in `src/routers/legacy-router/bases.ts`
* Populate `test/integ/routers/alpha-router/alpha-router.integration.test.ts` and `src/providers/v2/static-subgraph-provider.ts`
* Populate `src/routers/alpha-router/*`
* Add a log to `/CHANGELOG.md`
* Run `npm run integ-test` successfully

- Deploy contracts on chain, add the pools to subgraph
- Populate v3 providers in `src/providers/v3/subgraph-provider` and `src/providers/v3/static-subgraph-provider`
- Populate chainId and addresses in `src/util/chains.ts` and `src/util/addresses.ts`
- Populate token providers in `src/providers/caching-token-provider` and `src/providers/token-provider.ts`
- Populate gas constants in `src/routers/alpha-router/gas-models/*`
- Populate bases in `src/routers/legacy-router/bases.ts`
- Populate `test/integ/routers/alpha-router/alpha-router.integration.test.ts` and `src/providers/v2/static-subgraph-provider.ts`
- Populate `src/routers/alpha-router/*`
- Add a log to `/CHANGELOG.md`
- Run `npm run integ-test` successfully
29 changes: 14 additions & 15 deletions cli/commands/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import dotenv from 'dotenv';
import _ from 'lodash';
import {
ID_TO_CHAIN_ID,
NativeCurrencyName,
nativeOnChain,
parseAmount,
SwapRoute,
} from '../../src';
import { TO_PROTOCOL } from '../../src/util';
import { NATIVE_NAMES_BY_ID, TO_PROTOCOL } from '../../src/util';
import { BaseCommand } from '../base-command';

dotenv.config();
Expand Down Expand Up @@ -85,20 +84,20 @@ export class Quote extends BaseCommand {
const tokenProvider = this.tokenProvider;
const router = this.router;

const tokenAccessor = await tokenProvider.getTokens([
tokenInStr,
tokenOutStr,
]);
// if the tokenIn str is 'ETH' or 'MATIC' or in NATIVE_NAMES_BY_ID
const tokenIn: Currency = NATIVE_NAMES_BY_ID[chainId]!.includes(tokenInStr)
? nativeOnChain(chainId)
: (await tokenProvider.getTokens([tokenInStr])).getTokenByAddress(
tokenInStr
)!;

// if the tokenIn str is 'ETH' or 'MATIC' or NATIVE_CURRENCY_STRING
const tokenIn: Currency =
tokenInStr in NativeCurrencyName
? nativeOnChain(chainId)
: tokenAccessor.getTokenByAddress(tokenInStr)!;
const tokenOut: Currency =
tokenOutStr in NativeCurrencyName
? nativeOnChain(chainId)
: tokenAccessor.getTokenByAddress(tokenOutStr)!;
const tokenOut: Currency = NATIVE_NAMES_BY_ID[chainId]!.includes(
tokenOutStr
)
? nativeOnChain(chainId)
: (await tokenProvider.getTokens([tokenOutStr])).getTokenByAddress(
tokenOutStr
)!;

let swapRoutes: SwapRoute | null;
if (exactIn) {
Expand Down
58 changes: 57 additions & 1 deletion src/util/chains.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Currency, Ether, NativeCurrency, Token } from '@uniswap/sdk-core';

import { WGLMR_MOONBEAM, WXDAI_GNOSIS } from '../providers';

export enum ChainId {
MAINNET = 1,
ROPSTEN = 3,
Expand Down Expand Up @@ -129,6 +129,62 @@ export enum NativeCurrencyName {
GNOSIS = 'XDAI',
MOONBEAM = 'GLMR',
}
export const NATIVE_NAMES_BY_ID: { [chainId: number]: string[] } = {
[ChainId.MAINNET]: [
'ETH',
'ETHER',
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
],
[ChainId.RINKEBY]: [
'ETH',
'ETHER',
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
],
[ChainId.GÖRLI]: [
'ETH',
'ETHER',
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
],
[ChainId.KOVAN]: [
'ETH',
'ETHER',
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
],
[ChainId.ROPSTEN]: [
'ETH',
'ETHER',
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
],
[ChainId.OPTIMISM]: [
'ETH',
'ETHER',
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
],
[ChainId.OPTIMISTIC_KOVAN]: [
'ETH',
'ETHER',
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
],
[ChainId.ARBITRUM_ONE]: [
'ETH',
'ETHER',
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
],
[ChainId.ARBITRUM_RINKEBY]: [
'ETH',
'ETHER',
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
],
[ChainId.POLYGON]: ['MATIC', '0x0000000000000000000000000000000000001010'],
[ChainId.POLYGON_MUMBAI]: [
'MATIC',
'0x0000000000000000000000000000000000001010',
],
[ChainId.CELO]: ['CELO'],
[ChainId.CELO_ALFAJORES]: ['CELO'],
[ChainId.GNOSIS]: ['XDAI'],
[ChainId.MOONBEAM]: ['GLMR'],
};

export const NATIVE_CURRENCY: { [chainId: number]: NativeCurrencyName } = {
[ChainId.MAINNET]: NativeCurrencyName.ETHER,
Expand Down

0 comments on commit 9623259

Please sign in to comment.