-
Notifications
You must be signed in to change notification settings - Fork 89
integrate 0x v2 #278
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
Merged
integrate 0x v2 #278
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
c66e398
integrate 0x v2
mintdart c858fe7
use a different button for signing swap
mintdart fbf382e
update query params
mintdart 7c473f6
update query params
mintdart 22ca0c8
update query params
mintdart 7fee618
update aggregator name and supported chains
mintdart 9112df4
remove allowanceTarget check
mintdart c77f0d3
fix slippageBps
mintdart a11fc47
handle native tokens swap
mintdart 6394919
hardcode approval address
mintdart 7c9a89a
fixes
mintdart e6065fe
Merge branch 'master' into 0x-v2
mintdart d893bfc
integrate allowance-holder api
mintdart a2d40ce
fix
mintdart 04ed99a
update chains and refactor allowance code
mintdart f7928bc
update api key
mintdart 7c1b013
use a constant instead and label clearly
0xngmi 01c28be
remove infinite approval from v2 for now
0xngmi c98f50b
add support for more chains and disable v1
mintdart 0a178ef
update comment
mintdart 6812ae4
update signature
mintdart f6dc46d
fix
mintdart 33c1f7e
fix
mintdart a17aa56
fix
mintdart dca5bd9
disable allowance api
mintdart 63a4302
fix
mintdart 02939bb
rename
mintdart cb53a25
test
mintdart fea71bd
Revert "test"
mintdart 5c2d160
Merge branch 'master' into 0x-v2
mintdart bba1867
Update getAllowance.ts
mintdart 662d2e1
migrate from ethers
mintdart 6bc60a3
fix
mintdart 137948a
refactor
mintdart 1f9c6d5
update supported chains list
mintdart 0bb147e
test
mintdart 3c83410
Revert "test"
mintdart b5714d2
refactor
mintdart 3a6fab6
Merge branch 'master' into 0x-v2
0xngmi d08e01f
fix chain id
0xngmi 1805e63
fix unused imports
0xngmi 38617f0
fix
mintdart 3040cb0
fix
mintdart 282ab62
fix
mintdart 66e7cac
refactor
mintdart 694f60e
use mutation key
mintdart 00abc59
Revert "use mutation key"
mintdart 3a8ca8d
add support to charge fees in api
mintdart d9a64bb
Revert "add support to charge fees in api"
mintdart 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { numberToHex, size, zeroAddress, concat} from 'viem'; | ||
import { sendTx } from '../utils/sendTx'; | ||
|
||
export const name = 'Matcha/0x v2'; | ||
export const token = 'ZRX'; | ||
export const isOutputAvailable = false; | ||
|
||
export const chainToId = { | ||
ethereum: '1', | ||
bsc: '56', | ||
polygon: '137', | ||
optimism: '10', | ||
arbitrum: '42161', | ||
avax: '43114', | ||
base: '8453', | ||
linea: '59144', | ||
scroll: '534352', | ||
blast: '81457', | ||
mantle: '5000', | ||
mode: '34443' | ||
// missing unichain | ||
}; | ||
|
||
const nativeToken = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'; | ||
const feeCollectorAddress = '0x9Ab6164976514F1178E2BB4219DA8700c9D96E9A'; | ||
const permit2Address = '0x000000000022d473030f116ddee9f6b43ac78ba3'; | ||
|
||
export async function getQuote(chain: string, from: string, to: string, amount: string, extra) { | ||
// amount should include decimals | ||
|
||
const tokenFrom = from === zeroAddress ? nativeToken : from; | ||
const tokenTo = to === zeroAddress ? nativeToken : to; | ||
|
||
if (extra.amountOut && extra.amountOut !== '0') { | ||
throw new Error('Invalid query params'); | ||
} | ||
|
||
const amountParam = `sellAmount=${amount}`; | ||
|
||
const taker = extra.userAddress === zeroAddress ? '0x1000000000000000000000000000000000000000' : extra.userAddress; | ||
|
||
// only expects integer | ||
const slippage = (extra.slippage * 100) | 0; | ||
|
||
const data = await fetch( | ||
`https://api.0x.org/swap/permit2/quote?chainId=${chainToId[chain]}&buyToken=${tokenTo}&${amountParam}&sellToken=${tokenFrom}&slippageBps=${slippage}&taker=${taker}&tradeSurplusRecipient=${feeCollectorAddress}`, | ||
{ | ||
headers: { | ||
'0x-api-key': process.env.OX_API_KEY as string, | ||
'0x-version': 'v2' | ||
} | ||
} | ||
).then(async (r) => { | ||
if (r.status !== 200) { | ||
throw new Error('Failed to fetch'); | ||
} | ||
|
||
const data = await r.json(); | ||
|
||
return data; | ||
}); | ||
|
||
if ( | ||
data.permit2 !== null && | ||
data.permit2.eip712.domain.verifyingContract.toLowerCase() !== permit2Address.toLowerCase() | ||
) { | ||
throw new Error(`Approval address does not match`); | ||
} | ||
|
||
return { | ||
amountReturned: data?.buyAmount || 0, | ||
amountIn: data?.sellAmount || 0, | ||
tokenApprovalAddress: permit2Address, | ||
estimatedGas: data.transaction.gas, | ||
rawQuote: { ...data, gasLimit: data.transaction.gas }, | ||
isSignatureNeededForSwap: true, | ||
logo: 'https://www.gitbook.com/cdn-cgi/image/width=40,height=40,fit=contain,dpr=2,format=auto/https%3A%2F%2F1690203644-files.gitbook.io%2F~%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FKX9pG8rH3DbKDOvV7di7%252Ficon%252F1nKfBhLbPxd2KuXchHET%252F0x%2520logo.png%3Falt%3Dmedia%26token%3D25a85a3e-7f72-47ea-a8b2-e28c0d24074b' | ||
}; | ||
} | ||
|
||
export async function signatureForSwap({ rawQuote, signTypedDataAsync }) { | ||
const signature = await signTypedDataAsync(rawQuote.permit2.eip712).catch((err) => { | ||
console.log(err) | ||
}); | ||
return signature; | ||
} | ||
|
||
export async function swap({ fromAddress, rawQuote, signature }) { | ||
// signature not needed if using allowance holder api | ||
const signatureLengthInHex = signature | ||
? numberToHex(size(signature), { | ||
signed: false, | ||
size: 32 | ||
}) | ||
: null; | ||
const data = signature | ||
? concat([rawQuote.transaction.data, signatureLengthInHex, signature]) | ||
: rawQuote.transaction.data; | ||
const tx = await sendTx({ | ||
from: fromAddress, | ||
to: rawQuote.transaction.to, | ||
data, | ||
value: rawQuote.transaction.value | ||
}); | ||
|
||
return tx; | ||
} | ||
mintdart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export const getTxData = ({ rawQuote }) => rawQuote?.transaction?.data; | ||
|
||
export const getTx = ({ rawQuote }) => ({ | ||
to: rawQuote.transaction.to, | ||
data: rawQuote.transaction.data, | ||
value: rawQuote.transaction.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
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.
Uh oh!
There was an error while loading. Please reload this page.