1
+ import assert from "assert" ;
1
2
import SynthPrices from "../libs/synthPrices" ;
2
3
import { AppState , PriceSample } from ".." ;
4
+ import * as uma from "@uma/sdk" ;
3
5
4
6
type Config = {
5
7
cryptowatchApiKey ?: string ;
@@ -15,30 +17,73 @@ export default function (config: Config, appState: Dependencies) {
15
17
const { web3, emps, synthPrices } = appState ;
16
18
const getSynthPrices = SynthPrices ( config , web3 ) ;
17
19
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 ) {
19
53
const result : PriceSample = await getSynthPrices . getCurrentPrice ( empAddress ) ;
20
54
synthPrices . latest [ empAddress ] = result ;
21
55
return result ;
22
56
}
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 ) ) ;
25
60
}
26
61
27
62
async function update ( ) {
28
63
// synth prices are looked up by emp address, not synthetic token address
29
64
const empAddresses = Array . from ( await emps . active . keys ( ) ) ;
30
- await updatePrices ( empAddresses ) . then ( ( results ) => {
65
+ await updateLatestPrices ( empAddresses ) . then ( ( results ) => {
31
66
results . forEach ( ( result ) => {
32
67
if ( result . status === "rejected" ) console . error ( "Error updating synthetic price: " + result . reason . message ) ;
33
68
} ) ;
34
69
} ) ;
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
+ } ) ;
35
76
}
36
77
37
78
return {
38
79
update,
39
80
utils : {
40
- updatePrice,
41
- updatePrices,
81
+ getOrCreateHistoryTable,
82
+ getLatestPriceFromTable,
83
+ updatePriceHistories,
84
+ updatePriceHistory,
85
+ updateLatestPrice,
86
+ updateLatestPrices,
42
87
} ,
43
88
} ;
44
89
}
0 commit comments