fix(api-server): add total supply endpoint#1020
Conversation
|
|
||
| use crate::ApiState; | ||
|
|
||
| const TOKEN_DECIMALS: u32 = 18; |
There was a problem hiding this comment.
We seem to have this value scattered across the codebase.
irys/crates/types/src/storage_pricing.rs
Line 21 in 0a17fee
Probably needs to be pulled into a consensus param. Fine to hardcode now for expediency.
DanMacDonald
left a comment
There was a problem hiding this comment.
estimation code should be tweaked to be block height based. Also, I think the GenesisRelativeTimestamp type was removed as rewards really shouldn't be timestamp based.
| }); | ||
|
|
||
| let (emitted_amount, calculation_method) = if use_estimate { | ||
| let elapsed = |
There was a problem hiding this comment.
This logic is no longer timestamp based...
irys/crates/actors/src/block_producer.rs
Lines 1253 to 1270 in 0a17fee
^ it should be block height based, this is how we simulate it.
| #[serde(rename_all = "camelCase")] | ||
| pub struct SupplyResponse { | ||
| pub total_supply: String, | ||
| pub total_supply_billions: String, |
There was a problem hiding this comment.
we don't need these billion fields, I'd just remove them
| #[derive(Debug, Deserialize)] | ||
| pub struct SupplyQuery { | ||
| #[serde(default)] | ||
| pub estimate: bool, |
There was a problem hiding this comment.
change this to default to the estimate
| pub genesis_supply_billions: String, | ||
| pub emitted_supply: String, | ||
| pub emitted_supply_billions: String, | ||
| pub timestamp_millis: u128, |
There was a problem hiding this comment.
also don't need this field, as it's not time-based
9447027 to
5c89cea
Compare
6a0ee91 to
5c89cea
Compare
JesseTheRobot
left a comment
There was a problem hiding this comment.
LGTM, but this needs approval from @DanMacDonald before merging.
DanMacDonald
left a comment
There was a problem hiding this comment.
This looks good, though I suspect the merge with master will be a mildly annoying refactor.
Describe the changes
Before:
No HTTP endpoint existed to query total token supply (genesis allocation + emissions).
After:
Implements
/v1/supplyGET endpoint returning total supply, genesis allocation, emitted tokens, and inflation progress. Supports two modes: actual (sums block rewards from canonical chain) and estimated (uses exponential decay formula).Changes
API Server (
crates/api-server)src/routes/supply.rs(195 lines) with supply calculation logicSupply Endpoint (
src/routes/supply.rs)supply()handler with query parameter?estimate=true|false(default: false)SupplyResponsewith camelCase JSON fields:totalSupply,totalSupplyBillionsgenesisSupply,genesisSupplyBillionsemittedSupply,emittedSupplyBillionstimestampMillis,blockHeightinflationCap,inflationCapBillionsinflationProgressPercentcalculationMethod("actual" or "estimated")Calculation Modes
reward_amountfrom all blocks (O(n), 100% accurate)?estimate=true): UsesHalvingCurve::total_emitted_estimated()formula (O(1), relatively accurate)API Usage
Example Response:
{ "totalSupply": "10500000000000000000000000000", "totalSupplyBillions": "10.500000", "genesisSupply": "10000000000000000000000000000", "genesisSupplyBillions": "10.000000", "emittedSupply": "500000000000000000000000000", "emittedSupplyBillions": "0.500000", "timestampMillis": 1732217580000, "blockHeight": 12345, "inflationCap": "1300000000000000000000000000", "inflationCapBillions": "1.300000", "inflationProgressPercent": "38.46", "calculationMethod": "actual" }Related Issue(s)
Please link to the issue(s) that will be closed with this PR.
Checklist
Additional Context
Add any other context about the pull request here.