-
Notifications
You must be signed in to change notification settings - Fork 90
Uniswap #239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vrtnd
wants to merge
17
commits into
master
Choose a base branch
from
uniswap
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Uniswap #239
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d719ea4
Add Uniswap
vrtnd 0bb4333
fix slippage
vrtnd 2dcb29a
fix polygon
vrtnd d67b5f6
Hide ip for uniswap queries
vrtnd afac8b4
Fix
vrtnd 154a445
Merge branch 'master' into uniswap
0xngmi 2571006
Merge remote-tracking branch 'origin/master' into uniswap
vrtnd 32391e8
Add uniswap permit2 support
vrtnd c4eff2b
Fix types
vrtnd 063b851
Use jsonrpcprovider for uniswap
vrtnd 7ccb965
fix types
vrtnd 4bf6da2
update chakra
vrtnd f1937cb
fix import
vrtnd 2b62496
Merge branch 'master' into uniswap
vrtnd 9ea8969
Use uniswap v2 api
vrtnd 8c12a47
Merge remote-tracking branch 'origin/uniswap' into uniswap
vrtnd 8b9b01d
Add sleep for gecko api
vrtnd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import { AllowanceData, AllowanceProvider, AllowanceTransfer, PERMIT2_ADDRESS } from '@uniswap/permit2-sdk'; | ||
import { ethers } from 'ethers'; | ||
import { sendTx } from '../../utils/sendTx'; | ||
import { rpcUrls } from '../../rpcs'; | ||
import { redirectQuoteReq } from '../utils'; | ||
|
||
export const chainToId = { | ||
ethereum: 1, | ||
arbitrum: 42161, | ||
optimism: 10 | ||
}; | ||
|
||
const providers = { | ||
ethereum: new ethers.providers.JsonRpcProvider(rpcUrls[1].default, { | ||
name: 'ethereum', | ||
chainId: 1 | ||
}), | ||
arbitrum: new ethers.providers.JsonRpcProvider(rpcUrls[42161].default, { | ||
name: 'arbitrum', | ||
chainId: 42161 | ||
}), | ||
optimism: new ethers.providers.JsonRpcProvider(rpcUrls[10].default, { | ||
name: 'optimism', | ||
chainId: 10 | ||
}) | ||
}; | ||
|
||
export const name = 'Uniswap'; | ||
export const token = 'UNI'; | ||
|
||
const fetchQuote = async ({ chain, from, to, amount, userAddress, signature = null, permit = null }) => { | ||
console.log( | ||
signature | ||
? { | ||
permitSignature: signature, | ||
permitAmount: permit.details.amount, | ||
permitExpiration: permit.details.expiration, | ||
permitSigDeadline: permit.sigDeadline, | ||
permitNonce: permit.details.nonce | ||
} | ||
: {} | ||
); | ||
const quote = await fetch('https://api.uniswap.org/v2/quote', { | ||
headers: { | ||
origin: 'https://app.uniswap.org/' | ||
}, | ||
referrer: 'https://app.uniswap.org/', | ||
referrerPolicy: 'strict-origin-when-cross-origin', | ||
body: JSON.stringify({ | ||
tokenInChainId: chainToId[chain], | ||
tokenIn: from, | ||
tokenOutChainId: chainToId[chain], | ||
tokenOut: to, | ||
amount, | ||
sendPortionEnabled: false, | ||
type: 'EXACT_INPUT', | ||
configs: [ | ||
{ | ||
protocols: ['V2', 'V3', 'MIXED'], | ||
enableUniversalRouter: true, | ||
routingType: 'CLASSIC', | ||
recipient: userAddress, | ||
...(signature | ||
? { | ||
permitSignature: signature, | ||
permitAmount: permit.details.amount, | ||
permitExpiration: permit.details.expiration, | ||
permitSigDeadline: permit.sigDeadline, | ||
permitNonce: permit.details.nonce | ||
} | ||
: {}) | ||
} | ||
] | ||
}), | ||
method: 'POST', | ||
mode: 'cors', | ||
credentials: 'omit' | ||
}).then((r) => r.json()); | ||
|
||
return quote; | ||
}; | ||
|
||
export async function getQuote(chain: string, from: string, to: string, amount: string, extra) { | ||
const { quote } = await fetchQuote({ | ||
chain, | ||
from, | ||
to, | ||
userAddress: extra.userAddress, | ||
amount, | ||
permit: extra?.permit, | ||
signature: extra?.signature | ||
}); | ||
const routerAddress = quote?.methodParameters?.to; | ||
|
||
return { | ||
amountReturned: quote?.quote, | ||
estimatedGas: quote?.quoteGasAdjustedDecimals, | ||
tokenApprovalAddress: PERMIT2_ADDRESS, | ||
rawQuote: { | ||
tx: { | ||
data: quote?.methodParameters?.calldata, | ||
value: quote?.methodParameters?.value, | ||
to: routerAddress | ||
}, | ||
gasLimit: quote?.gasUseEstimateQuote | ||
} | ||
}; | ||
} | ||
|
||
export async function swap({ chain, signer, rawQuote, ...params }) { | ||
const fromAddress = await signer.getAddress(); | ||
const routerAddress = rawQuote?.tx?.to; | ||
const { from, to } = params; | ||
const amount = params?.route?.fromAmount; | ||
|
||
const signPermitAndSwap = async (provider: ethers.Wallet) => { | ||
const allowanceProvider = new AllowanceProvider(providers[chain], PERMIT2_ADDRESS); | ||
const allowance: AllowanceData = await allowanceProvider?.getAllowanceData(from, fromAddress, routerAddress); | ||
const deadline = (new Date().getTime() / 1000 + 300).toFixed(0); | ||
const permitDetails = { | ||
nonce: allowance.nonce, | ||
token: from, | ||
amount, | ||
expiration: deadline | ||
}; | ||
const { domain, types, values } = AllowanceTransfer.getPermitData( | ||
{ | ||
spender: routerAddress, | ||
sigDeadline: deadline, | ||
details: permitDetails | ||
}, | ||
PERMIT2_ADDRESS, | ||
chainToId[chain] | ||
); | ||
const signature = await provider._signTypedData(domain, types, values); | ||
const permit = { | ||
signature, | ||
details: permitDetails, | ||
sigDeadline: deadline, | ||
spender: routerAddress | ||
}; | ||
|
||
const quote = await redirectQuoteReq('Uniswap', chain, from, to, amount, { signature, permit }); | ||
|
||
const res = await sendTx(signer, chain, { | ||
from: fromAddress, | ||
...quote?.rawQuote?.tx, | ||
...(chain === 'optimism' && { gasLimit: rawQuote.gasLimit }) | ||
}); | ||
|
||
return res; | ||
}; | ||
|
||
return from === ethers.constants.AddressZero | ||
? await sendTx(signer, chain, { | ||
from: fromAddress, | ||
...rawQuote?.tx, | ||
...(chain === 'optimism' && { gasLimit: rawQuote.gasLimit }) | ||
}) | ||
: await signPermitAndSwap(signer); | ||
} | ||
|
||
export const getTxData = ({ rawQuote }) => rawQuote?.tx?.data; | ||
|
||
export const getTx = ({ rawQuote }) => ({ | ||
to: rawQuote?.tx?.to, | ||
data: rawQuote?.tx?.data, | ||
value: rawQuote?.tx?.value | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why change versions here?