-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(locksmith) - add pricingController (#11911)
add pricingController
- Loading branch information
1 parent
7e47a1c
commit 1fa92ee
Showing
4 changed files
with
128 additions
and
125 deletions.
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
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,119 @@ | ||
import networks from '@unlock-protocol/networks' | ||
import { getFees, getGasCost } from '../utils/pricing' | ||
|
||
interface Price { | ||
decimals: number | ||
symbol: string | ||
price: number | ||
timestamp: number | ||
confidence: number | ||
creditCardEnabled: boolean | ||
} | ||
|
||
export interface Options { | ||
amount?: number | ||
address?: string | ||
network: number | ||
} | ||
|
||
export async function getDefiLammaPrice({ | ||
network, | ||
address, | ||
amount = 1, | ||
}: Options): Promise< | ||
Partial< | ||
Price & { | ||
priceInAmount: number | ||
} | ||
> | ||
> { | ||
const networkConfig = networks[network] | ||
if (!network) { | ||
return {} | ||
} | ||
const items: string[] = [] | ||
const coingecko = `coingecko:${networkConfig.nativeCurrency?.coingecko}` | ||
const mainnetTokenAddress = networkConfig.tokens?.find( | ||
(item) => item.address?.toLowerCase() === address?.toLowerCase() | ||
)?.mainnetAddress | ||
|
||
if (mainnetTokenAddress) { | ||
items.push(`ethereum:${mainnetTokenAddress}`) | ||
} | ||
|
||
if (address) { | ||
items.push(`${networkConfig.chain}:${address}`) | ||
} | ||
|
||
if (!address && coingecko) { | ||
items.push(coingecko) | ||
} | ||
|
||
const endpoint = `https://coins.llama.fi/prices/current/${items.join(',')}` | ||
const response = await fetch(endpoint) | ||
|
||
if (!response.ok) { | ||
return {} | ||
} | ||
|
||
const json: Record<'coins', Record<string, Price>> = await response.json() | ||
const item = Object.values(json.coins).filter( | ||
(item) => item.confidence > 0.95 | ||
)[0] | ||
|
||
if (!item) { | ||
return {} | ||
} | ||
|
||
const priceInAmount = item.price * amount | ||
|
||
return { | ||
...item, | ||
priceInAmount, | ||
} | ||
} | ||
|
||
/** | ||
* Get lock total charges with fees | ||
* @returns | ||
*/ | ||
export const getTotalCharges = async ({ | ||
amount, | ||
network, | ||
address, | ||
}: { | ||
network: number | ||
amount: number | ||
address?: string | ||
}) => { | ||
const [pricing, gasCost] = await Promise.all([ | ||
getDefiLammaPrice({ | ||
network, | ||
amount, | ||
address, | ||
}), | ||
getGasCost({ network }), | ||
]) | ||
|
||
if (pricing.priceInAmount === undefined) { | ||
return { | ||
total: 0, | ||
subtotal: 0, | ||
gasCost, | ||
unlockServiceFee: 0, | ||
creditCardProcessingFee: 0, | ||
isCreditCardPurchasable: false, | ||
} | ||
} | ||
const subtotal = Math.round(pricing.priceInAmount * 100) | ||
const fees = getFees({ | ||
subtotal, | ||
gasCost, | ||
}) | ||
const result = { | ||
...fees, | ||
subtotal, | ||
isCreditCardPurchasable: fees.total > 50, | ||
} | ||
return result | ||
} |
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