-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Show push fees & ask user consent for push
- Loading branch information
1 parent
6dad261
commit 080ee02
Showing
8 changed files
with
237 additions
and
28 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,68 @@ | ||
import redstone from 'redstone-api'; | ||
import { initArweave } from './common'; | ||
|
||
interface CoinGeckoPriceResult { | ||
arweave: { | ||
[key: string]: number; | ||
}; | ||
[key: string]: { | ||
[key: string]: number; | ||
}; | ||
} | ||
|
||
export async function getArPrice() { | ||
try { | ||
const data = (await ( | ||
await fetch( | ||
`https://api.coingecko.com/api/v3/simple/price?ids=arweave&vs_currencies=usd` | ||
) | ||
).json()) as CoinGeckoPriceResult; | ||
|
||
return data.arweave.usd as number; | ||
} catch (e) { | ||
const response = await redstone.getPrice('AR'); | ||
|
||
if (!response.value) { | ||
return 0; | ||
} | ||
|
||
return (response.source as any).coingecko as number; | ||
} | ||
} | ||
|
||
export async function getWinstonPriceForBytes(bytes: number) { | ||
try { | ||
const response = await fetch(`https://arweave.net/price/${bytes}`); | ||
const winston = await response.text(); | ||
|
||
return +winston; | ||
} catch (error: any) { | ||
throw new Error(error); | ||
} | ||
} | ||
|
||
export function formatBytes(bytes: number): string { | ||
if (bytes === 0) return '0 Bytes'; | ||
|
||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
const k = 1024; | ||
const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
|
||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; | ||
} | ||
|
||
export async function calculateEstimate(bytes: number) { | ||
const formattedSize = formatBytes(bytes); | ||
|
||
const costInWinston = await getWinstonPriceForBytes(bytes); | ||
const costInAR = +initArweave().ar.winstonToAr(costInWinston.toString()); | ||
|
||
const costFor1ARInUSD = await getArPrice(); | ||
const costInUSD = costInAR * costFor1ARInUSD; | ||
|
||
return { | ||
formattedSize, | ||
costInAR: costInAR.toPrecision(5), | ||
costInUSD: costInUSD.toPrecision(5), | ||
}; | ||
} |
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
Oops, something went wrong.