-
Notifications
You must be signed in to change notification settings - Fork 1
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
Feature/sc 2663/wrap amm quote requests in api #27
Merged
jackmellis
merged 3 commits into
main
from
feature/sc-2663/wrap-amm-quote-requests-in-api
May 23, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,14 @@ | ||
import fetchSpotPrice from './fetchSpotPrice'; | ||
|
||
/** Returns the current price of ETH in USDC terms */ | ||
const fetchEthPrice = async ({ network }: { network?: number }) => { | ||
const { price } = await fetchSpotPrice({ | ||
network, | ||
tokenAddress: 'ETH', | ||
quoteToken: 'USDC', | ||
}); | ||
|
||
return price; | ||
}; | ||
|
||
export default fetchEthPrice; |
This file contains 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,22 @@ | ||
import { MarketplacePrice } from '@nftx/types'; | ||
import fetchQuote, { | ||
BuyArgs, | ||
MintArgs, | ||
RedeemArgs, | ||
SellArgs, | ||
TokenArgs, | ||
SwapArgs, | ||
} from './fetchQuote'; | ||
|
||
/** Returns an off-chain price for a transaction */ | ||
function fetchPrice(args: BuyArgs): Promise<MarketplacePrice>; | ||
function fetchPrice(args: SellArgs): Promise<MarketplacePrice>; | ||
function fetchPrice(args: SwapArgs): Promise<MarketplacePrice>; | ||
function fetchPrice(args: MintArgs): Promise<MarketplacePrice>; | ||
function fetchPrice(args: RedeemArgs): Promise<MarketplacePrice>; | ||
function fetchPrice(args: TokenArgs): Promise<MarketplacePrice>; | ||
function fetchPrice(args: any) { | ||
return fetchQuote({ ...args, quoteType: 'price' }); | ||
} | ||
|
||
export default fetchPrice; |
This file contains 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 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,46 @@ | ||
import type { QuoteToken } from '@nftx/types'; | ||
import fetchPrice from './fetchPrice'; | ||
import { WeiPerEther } from '@nftx/constants'; | ||
import { UnknownError } from '@nftx/errors'; | ||
|
||
/** Returns the spot price for buying a token */ | ||
const fetchSpotPrice = async ({ | ||
tokenAddress, | ||
amount = WeiPerEther, | ||
network, | ||
quoteToken, | ||
}: { | ||
tokenAddress: QuoteToken; | ||
network?: number; | ||
quoteToken?: QuoteToken; | ||
amount?: bigint; | ||
}) => { | ||
// We start by attempting to quote 1 whole token, but if it fails (e.g. due to insufficient liquidity), | ||
// we can try to quote a smaller fraction of the token until we find a price that works. | ||
let fraction = WeiPerEther; | ||
let error: any = new UnknownError('Failed to fetch spot price'); | ||
|
||
do { | ||
try { | ||
const quote = await fetchPrice({ | ||
network, | ||
type: 'erc20', | ||
buyToken: tokenAddress, | ||
sellToken: quoteToken || 'ETH', | ||
buyAmount: fraction, | ||
}); | ||
|
||
// Extrapolate the price to determine the spot price for 1 whole token, then scale it by the desired amount. | ||
quote.price = quote.vTokenPrice = (quote.price * amount) / fraction; | ||
|
||
return quote; | ||
} catch (e) { | ||
error = e; | ||
fraction /= 100n; | ||
} | ||
} while (fraction > 0n); | ||
|
||
throw error; | ||
}; | ||
|
||
export default fetchSpotPrice; |
This file contains 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,37 @@ | ||
import { WeiPerEther, Zero } from '@nftx/constants'; | ||
import { QuoteToken } from '@nftx/types'; | ||
import fetchPrice from './fetchPrice'; | ||
|
||
/** Returns the difference between a buy and sell of a single token */ | ||
const fetchSpread = async ({ | ||
tokenAddress, | ||
network, | ||
quoteToken, | ||
}: { | ||
tokenAddress: QuoteToken; | ||
network?: number; | ||
quoteToken?: QuoteToken; | ||
}) => { | ||
try { | ||
const { price: buyPrice } = await fetchPrice({ | ||
network, | ||
type: 'erc20', | ||
buyToken: tokenAddress, | ||
sellToken: quoteToken || 'ETH', | ||
buyAmount: WeiPerEther, | ||
}); | ||
const { price: sellPrice } = await fetchPrice({ | ||
network, | ||
type: 'erc20', | ||
sellToken: tokenAddress, | ||
buyToken: quoteToken || 'ETH', | ||
sellAmount: WeiPerEther, | ||
}); | ||
|
||
return buyPrice - sellPrice; | ||
} catch { | ||
return Zero; | ||
} | ||
}; | ||
|
||
export default fetchSpread; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
Isn't it easier/more readable just to have an interface with all the props?
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.
I did it this way so ts can tell you which props you actually need to provide for each type of txn, otherwise it's a whole mess of optional props and it'd be difficult to know which things you need to pass and when