1+ import assert from "assert" ;
12import SynthPrices from "../libs/synthPrices" ;
23import { AppState , PriceSample } from ".." ;
4+ import * as uma from "@uma/sdk" ;
35
46type Config = {
57 cryptowatchApiKey ?: string ;
@@ -15,30 +17,73 @@ export default function (config: Config, appState: Dependencies) {
1517 const { web3, emps, synthPrices } = appState ;
1618 const getSynthPrices = SynthPrices ( config , web3 ) ;
1719
18- async function updatePrice ( empAddress : string ) {
20+ // if we have a new emp address, this will create a new price table structure to store historical price data
21+ function getOrCreateHistoryTable ( empAddress : string ) {
22+ if ( synthPrices . history [ empAddress ] == null ) {
23+ synthPrices . history [ empAddress ] = uma . tables . historicalPrices . SortedJsMap ( ) ;
24+ }
25+ return synthPrices . history [ empAddress ] ;
26+ }
27+
28+ // utility to grab last price based on address
29+ function getLatestPriceFromTable ( empAddress : string ) {
30+ const result = synthPrices . latest [ empAddress ] ;
31+ assert ( uma . utils . exists ( result ) , "no latest sythetic price found for: " + empAddress ) ;
32+ return result ;
33+ }
34+
35+ // pulls price from latest and stuffs it into historical table.
36+ async function updatePriceHistory ( empAddress : string ) {
37+ const table = getOrCreateHistoryTable ( empAddress ) ;
38+ const [ timestamp , price ] = getLatestPriceFromTable ( empAddress ) ;
39+ // if this timestamp exists in the table, dont bother re-adding it
40+ assert ( uma . utils . exists ( price ) , "No latest price available for: " + empAddress ) ;
41+ assert (
42+ ! ( await table . hasByTimestamp ( timestamp ) ) ,
43+ `Synthetic price already exists for emp address ${ empAddress } at timestamp: ${ timestamp } `
44+ ) ;
45+ return table . create ( { timestamp, price } ) ;
46+ }
47+
48+ async function updatePriceHistories ( addresses : string [ ] ) {
49+ return Promise . allSettled ( addresses . map ( updatePriceHistory ) ) ;
50+ }
51+
52+ async function updateLatestPrice ( empAddress : string ) {
1953 const result : PriceSample = await getSynthPrices . getCurrentPrice ( empAddress ) ;
2054 synthPrices . latest [ empAddress ] = result ;
2155 return result ;
2256 }
23- async function updatePrices ( empAddresses : string [ ] ) {
24- return Promise . allSettled ( empAddresses . map ( updatePrice ) ) ;
57+
58+ async function updateLatestPrices ( empAddresses : string [ ] ) {
59+ return Promise . allSettled ( empAddresses . map ( updateLatestPrice ) ) ;
2560 }
2661
2762 async function update ( ) {
2863 // synth prices are looked up by emp address, not synthetic token address
2964 const empAddresses = Array . from ( await emps . active . keys ( ) ) ;
30- await updatePrices ( empAddresses ) . then ( ( results ) => {
65+ await updateLatestPrices ( empAddresses ) . then ( ( results ) => {
3166 results . forEach ( ( result ) => {
3267 if ( result . status === "rejected" ) console . error ( "Error updating synthetic price: " + result . reason . message ) ;
3368 } ) ;
3469 } ) ;
70+ await updatePriceHistories ( empAddresses ) . then ( ( results ) => {
71+ results . forEach ( ( result ) => {
72+ if ( result . status === "rejected" )
73+ console . error ( "Error updating historical synthetic price: " + result . reason . message ) ;
74+ } ) ;
75+ } ) ;
3576 }
3677
3778 return {
3879 update,
3980 utils : {
40- updatePrice,
41- updatePrices,
81+ getOrCreateHistoryTable,
82+ getLatestPriceFromTable,
83+ updatePriceHistories,
84+ updatePriceHistory,
85+ updateLatestPrice,
86+ updateLatestPrices,
4287 } ,
4388 } ;
4489}
0 commit comments