Skip to content

Commit

Permalink
Merge pull request #8 from uniswap-integration/feature/ethereum-provider
Browse files Browse the repository at this point in the history
feat: support passing in ethereum provider directly
  • Loading branch information
joshstevens19 authored Jul 1, 2021
2 parents c8fd8b8 + 9ec7648 commit ba8c640
Show file tree
Hide file tree
Showing 28 changed files with 318 additions and 117 deletions.
154 changes: 133 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class UniswapPair {
private _uniswapPairContext:
| UniswapPairContextForChainId
| UniswapPairContextForProviderUrl
| UniswapPairContextForEthereumProvider
)
```
Expand All @@ -88,6 +89,12 @@ interface UniswapPairContextBase {
settings?: UniswapPairSettings | undefined;
}

export interface UniswapPairContextForEthereumProvider
extends UniswapPairContextBase {
// can take any ethers provider, web3 provider or custom ethereum provider
ethereumProvider: any;
}

export interface UniswapPairContextForChainId extends UniswapPairContextBase {
chainId: ChainId | number;
}
Expand Down Expand Up @@ -136,6 +143,47 @@ export class UniswapPairSettings {
}
```
### Ethereum provider
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 } from 'simple-uniswap-sdk';

const uniswapPair = new UniswapPair({
// the contract address of the token you want to convert FROM
fromTokenContractAddress: '0x419D0d8BdD9aF5e606Ae2232ed285Aff190E711b',
// the contract address of the token you want to convert TO
toTokenContractAddress: '0x1985365e9f78359a9B6AD760e32412f4a445E862',
// the ethereum address of the user using this part of the dApp
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
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%
// would be 0.007
slippage: 0.005,
// if not supplied it will use 20 a deadline minutes
deadlineMinutes: 20,
// if not supplied it will try to use multihops
// if this is true it will require swaps to direct
// pairs
disableMultihops: false,
// for example if you only wanted to turn on quotes for v3 and not v3
// you can only support the v3 enum same works if you only want v2 quotes
// if you do not supply anything it query both v2 and v3
uniswapVersions: [UniswapVersion.v2, UniswapVersion.v3],
}),
});

// now to create the factory you just do
const uniswapPairFactory = await uniswapPair.createFactory();
```
### With only the chainId
This will use a infura endpoint without you having to pass in a node
```ts
import { UniswapPair, ChainId, UniswapVersion } from 'simple-uniswap-sdk';

Expand All @@ -147,8 +195,44 @@ const uniswapPair = new UniswapPair({
// the ethereum address of the user using this part of the dApp
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
chainId: ChainId.MAINNET,
// you can pass in the provider url as well if you want
// 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%
// would be 0.007
slippage: 0.005,
// if not supplied it will use 20 a deadline minutes
deadlineMinutes: 20,
// if not supplied it will try to use multihops
// if this is true it will require swaps to direct
// pairs
disableMultihops: false,
// for example if you only wanted to turn on quotes for v3 and not v3
// you can only support the v3 enum same works if you only want v2 quotes
// if you do not supply anything it query both v2 and v3
uniswapVersions: [UniswapVersion.v2, UniswapVersion.v3],
}),
});

// now to create the factory you just do
const uniswapPairFactory = await uniswapPair.createFactory();
```
### With your own provider url
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 } from 'simple-uniswap-sdk';

const uniswapPair = new UniswapPair({
// the contract address of the token you want to convert FROM
fromTokenContractAddress: '0x419D0d8BdD9aF5e606Ae2232ed285Aff190E711b',
// the contract address of the token you want to convert TO
toTokenContractAddress: '0x1985365e9f78359a9B6AD760e32412f4a445E862',
// 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 Down Expand Up @@ -201,11 +285,12 @@ export enum ErrorCodes {
toTokenContractAddressNotValid = 9,
ethereumAddressRequired = 10,
ethereumAddressNotValid = 11,
youMustSupplyAChainId = 12,
invalidPairContext = 12,
invalidFromOrToContractToken = 13,
uniswapVersionNotSupported = 14,
uniswapVersionsMustNotBeAnEmptyArray = 15,
canNotFindProviderUrl = 16,
wrongEthersProviderContext = 17,
}
```
Expand Down Expand Up @@ -406,6 +491,8 @@ const etherTradeExample = async () => {
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
// you can pass in the provider url as well if you want
// providerUrl: YOUR_PROVIDER_URL,
// OR if you want to inject your own ethereum provider (no need for chainId if so)
// ethereumProvider: YOUR_WEB3_ETHERS_OR_CUSTOM_ETHEREUM_PROVIDER,
chainId: ChainId.MAINNET,
});

Expand Down Expand Up @@ -506,6 +593,8 @@ const web3TradeExample = async () => {
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
// you can pass in the provider url as well if you want
// providerUrl: YOUR_PROVIDER_URL,
// OR if you want to inject your own ethereum provider (no need for chainId if so)
// ethereumProvider: YOUR_WEB3_ETHERS_OR_CUSTOM_ETHEREUM_PROVIDER,
chainId: ChainId.RINKEBY,
});

Expand Down Expand Up @@ -645,6 +734,8 @@ const uniswapPair = new UniswapPair({
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
// you can pass in the provider url as well if you want
// providerUrl: YOUR_PROVIDER_URL,
// OR if you want to inject your own ethereum provider (no need for chainId if so)
// ethereumProvider: YOUR_WEB3_ETHERS_OR_CUSTOM_ETHEREUM_PROVIDER,
chainId: ChainId.MAINNET,
});

Expand Down Expand Up @@ -915,6 +1006,8 @@ const uniswapPair = new UniswapPair({
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
// you can pass in the provider url as well if you want
// providerUrl: YOUR_PROVIDER_URL,
// OR if you want to inject your own ethereum provider (no need for chainId if so)
// ethereumProvider: YOUR_WEB3_ETHERS_OR_CUSTOM_ETHEREUM_PROVIDER,
chainId: ChainId.MAINNET,
});

Expand Down Expand Up @@ -2357,6 +2450,8 @@ const uniswapPair = new UniswapPair({
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
// you can pass in the provider url as well if you want
// providerUrl: YOUR_PROVIDER_URL,
// OR if you want to inject your own ethereum provider (no need for chainId if so)
// ethereumProvider: YOUR_WEB3_ETHERS_OR_CUSTOM_ETHEREUM_PROVIDER,
chainId: ChainId.MAINNET,
});

Expand Down Expand Up @@ -3625,6 +3720,8 @@ const uniswapPair = new UniswapPair({
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
// you can pass in the provider url as well if you want
// providerUrl: YOUR_PROVIDER_URL,
// OR if you want to inject your own ethereum provider (no need for chainId if so)
// ethereumProvider: YOUR_WEB3_ETHERS_OR_CUSTOM_ETHEREUM_PROVIDER,
chainId: ChainId.MAINNET,
});

Expand Down Expand Up @@ -3675,6 +3772,8 @@ const uniswapPair = new UniswapPair({
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
// you can pass in the provider url as well if you want
// providerUrl: YOUR_PROVIDER_URL,
// OR if you want to inject your own ethereum provider (no need for chainId if so)
// ethereumProvider: YOUR_WEB3_ETHERS_OR_CUSTOM_ETHEREUM_PROVIDER,
chainId: ChainId.MAINNET,
});

Expand All @@ -3695,10 +3794,10 @@ console.log(fromToken);
### providerUrl
This exposes the provider url it is using
This exposes the provider url it is using will be undefined if you injected a ethereum provider
```ts
get providerUrl(): string
get providerUrl(): string | undefined
```
#### Usage
Expand All @@ -3715,6 +3814,8 @@ const uniswapPair = new UniswapPair({
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
// you can pass in the provider url as well if you want
// providerUrl: YOUR_PROVIDER_URL,
// OR if you want to inject your own ethereum provider (no need for chainId if so)
// ethereumProvider: YOUR_WEB3_ETHERS_OR_CUSTOM_ETHEREUM_PROVIDER,
chainId: ChainId.MAINNET,
});

Expand Down Expand Up @@ -3757,7 +3858,10 @@ const tokenContractAddress = '0x419D0d8BdD9aF5e606Ae2232ed285Aff190E711b';

const tokenFactoryPublic = new TokenFactoryPublic(
toTokenContractAddress,
ChainId.MAINNET
// this can take the same interface as pair context aka
// `ChainIdAndProvider` | `EthereumProvider`
// so you can pass in a providerUrl or a ethereumProvider
{ chainId: ChainId.MAINNET }
);

const token = await tokenFactoryPublic.getToken();
Expand Down Expand Up @@ -3792,7 +3896,10 @@ const tokenContractAddress = '0x419D0d8BdD9aF5e606Ae2232ed285Aff190E711b';

const tokenFactoryPublic = new TokenFactoryPublic(
toTokenContractAddress,
ChainId.MAINNET
// this can take the same interface as pair context aka
// `ChainIdAndProvider` | `EthereumProvider`
// so you can pass in a providerUrl or a ethereumProvider
{ chainId: ChainId.MAINNET }
);

const ethereumAddress = '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9';
Expand Down Expand Up @@ -3822,7 +3929,10 @@ const tokenContractAddress = '0x419D0d8BdD9aF5e606Ae2232ed285Aff190E711b';

const tokenFactoryPublic = new TokenFactoryPublic(
toTokenContractAddress,
ChainId.MAINNET
// this can take the same interface as pair context aka
// `ChainIdAndProvider` | `EthereumProvider`
// so you can pass in a providerUrl or a ethereumProvider
{ chainId: ChainId.MAINNET }
);

const ethereumAddress = '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9';
Expand All @@ -3847,12 +3957,11 @@ import { TokenFactoryPublic, ChainId } from 'simple-uniswap-sdk';

const tokenContractAddress = '0x419D0d8BdD9aF5e606Ae2232ed285Aff190E711b';

const tokenFactoryPublic = new TokenFactoryPublic(
toTokenContractAddress,
ChainId.MAINNET
const tokenFactoryPublic = new TokenFactoryPublic(toTokenContractAddress, {
chainId: ChainId.MAINNET,
// you can pass in the provider url as well if you want
// providerUrl: YOUR_PROVIDER_URL,
);
});

const totalSupply = await tokenFactoryPublic.totalSupply();
console.log(totalSupply);
Expand All @@ -3876,9 +3985,10 @@ const tokenContractAddress = '0x419D0d8BdD9aF5e606Ae2232ed285Aff190E711b';

const tokenFactoryPublic = new TokenFactoryPublic(
tokenContractAddress,
ChainId.MAINNET
// you can pass in the provider url as well if you want
// providerUrl: YOUR_PROVIDER_URL,
// this can take the same interface as pair context aka
// `ChainIdAndProvider` | `EthereumProvider`
// so you can pass in a providerUrl or a ethereumProvider
{ chainId: ChainId.MAINNET }
);

// the contract address for which you are allowing to move tokens on your behalf
Expand Down Expand Up @@ -3920,9 +4030,10 @@ const tokenContractAddress = '0x419D0d8BdD9aF5e606Ae2232ed285Aff190E711b';

const tokenFactoryPublic = new TokenFactoryPublic(
tokenContractAddress,
ChainId.MAINNET
// you can pass in the provider url as well if you want
// providerUrl: YOUR_PROVIDER_URL,
// this can take the same interface as pair context aka
// `ChainIdAndProvider` | `EthereumProvider`
// so you can pass in a providerUrl or a ethereumProvider
{ chainId: ChainId.MAINNET }
);

const ethereumAddress = '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9';
Expand Down Expand Up @@ -3966,9 +4077,10 @@ export interface Token {
import { TokensFactoryPublic, ChainId } from 'simple-uniswap-sdk';

const tokensFactoryPublic = new TokensFactoryPublic(
ChainId.MAINNET
// you can pass in the provider url as well if you want
// providerUrl: YOUR_PROVIDER_URL,
// this can take the same interface as pair context aka
// `ChainIdAndProvider` | `EthereumProvider`
// so you can pass in a providerUrl or a ethereumProvider
{ chainId: ChainId.MAINNET }
);

const tokens = await tokensFactoryPublic.getTokens([
Expand Down
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": "2.3.3",
"version": "2.4.0",
"description": "Simple easy to understand SDK for uniswap which looks over best v2 and v3 to find you the best swap quote",
"main": "dist/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/__TEST-SCRIPT__/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const routeTest = async () => {
// console.log(error.message);
// }

// const ethers = new EthersProvider(ChainId.MAINNET);
// const ethers = new EthersProvider({ chainId: ChainId.MAINNET });
// ethers.provider.estimateGas(trade.transaction);

process.stdin.resume();
Expand Down
3 changes: 2 additions & 1 deletion src/common/errors/error-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export enum ErrorCodes {
toTokenContractAddressNotValid = 9,
ethereumAddressRequired = 10,
ethereumAddressNotValid = 11,
youMustSupplyAChainId = 12,
invalidPairContext = 12,
invalidFromOrToContractToken = 13,
uniswapVersionNotSupported = 14,
uniswapVersionsMustNotBeAnEmptyArray = 15,
canNotFindProviderUrl = 16,
wrongEthersProviderContext = 17,
}
15 changes: 9 additions & 6 deletions src/ethers-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { UniswapContractContextV2 } from './uniswap-contract-context/uniswap-con

describe('EthersProvider', () => {
describe('with chain id', () => {
const ethersProvider = new EthersProvider(ChainId.MAINNET);
const ethersProvider = new EthersProvider({ chainId: ChainId.MAINNET });

it('getContract', () => {
const result = ethersProvider.getContract<PairContractContext>(
Expand Down Expand Up @@ -38,14 +38,17 @@ describe('EthersProvider', () => {
});

describe('with chain id and providerUrl', () => {
const ethersProvider = new EthersProvider(
ChainId.MAINNET,
MOCK_PROVIDER_URL()
);
const ethersProvider = new EthersProvider({
chainId: ChainId.MAINNET,
providerUrl: MOCK_PROVIDER_URL(),
});

it('should throw error if chainId not be found', () => {
expect(() => {
new EthersProvider(10293, MOCK_PROVIDER_URL());
new EthersProvider({
chainId: 10293,
providerUrl: MOCK_PROVIDER_URL(),
});
}).toThrowError(
new UniswapError(
'Can not find chain name for 10293',
Expand Down
Loading

0 comments on commit ba8c640

Please sign in to comment.