@@ -98,3 +98,96 @@ export function calcSpotPrice(
98
98
99
99
return price
100
100
}
101
+
102
+
103
+ export function getPoolSnapshotId ( poolAddress : string , timestamp : i32 ) : string {
104
+ const dayTimestamp = timestamp - ( timestamp % DAY ) // Todays Timestamp
105
+ return `${ poolAddress } -${ dayTimestamp } `
106
+ }
107
+
108
+ export function createPoolSnapshot ( poolId : string , timestamp : i32 ) : void {
109
+ log . warning ( 'Start create Pool Snapshot: {} {}' , [
110
+ poolId ,
111
+ timestamp . toString ( )
112
+ ] )
113
+ const dayTimestamp = timestamp - ( timestamp % DAY ) // Todays Timestamp
114
+
115
+ const pool = PoolEntity . load ( poolId )
116
+
117
+ log . warning ( 'got pool {} {}' , [ pool . id , poolId ] )
118
+ // Save pool snapshot
119
+ const snapshotId = poolId + '-' + dayTimestamp . toString ( )
120
+
121
+ log . warning ( 'Creatnig Pool Snapshot with id {} {} {}' , [
122
+ snapshotId ,
123
+ pool . totalShares . toString ( ) ,
124
+ pool . totalSwapFee . toString ( )
125
+ ] )
126
+ const snapshot = new PoolSnapshot ( snapshotId )
127
+
128
+ snapshot . pool = poolId
129
+
130
+ snapshot . totalShares = pool . totalShares
131
+ snapshot . swapVolume = ZERO_BD
132
+ snapshot . swapFees = pool . totalSwapFee
133
+ snapshot . timestamp = dayTimestamp
134
+ snapshot . save ( )
135
+ }
136
+
137
+ export function saveSwapToSnapshot (
138
+ poolAddress : string ,
139
+ timestamp : i32 ,
140
+ volume : BigDecimal ,
141
+ fees : BigDecimal
142
+ ) : void {
143
+ const dayTimestamp = timestamp - ( timestamp % DAY ) // Todays timestamp
144
+
145
+ // Save pool snapshot
146
+ const snapshotId = poolAddress + '-' + dayTimestamp . toString ( )
147
+ const snapshot = PoolSnapshot . load ( snapshotId )
148
+
149
+ if ( ! snapshot ) {
150
+ return
151
+ }
152
+
153
+ snapshot . swapVolume = snapshot . swapVolume . plus ( volume )
154
+ snapshot . swapFees = snapshot . swapFees . plus ( fees )
155
+ snapshot . save ( )
156
+ }
157
+
158
+ export function updatePoolSnapshotToken (
159
+ poolAddress : string ,
160
+ timestamp : i32 ,
161
+ poolTokenId : string ,
162
+ amount : BigDecimal ,
163
+ balance : BigDecimal ,
164
+ feeValue : BigDecimal
165
+ ) : void {
166
+ log . warning ( 'Start create Pool Snapshot Token: {} {}' , [
167
+ poolAddress ,
168
+ timestamp . toString ( )
169
+ ] )
170
+ const dayTimestamp = timestamp - ( timestamp % DAY ) // Todays timestamp
171
+
172
+ const snapshotId = poolAddress + '-' + dayTimestamp . toString ( )
173
+ log . warning ( 'Pool Snapshot Token: {} {} {} {}' , [
174
+ amount . toString ( ) ,
175
+ balance . toString ( ) ,
176
+ feeValue . toString ( ) ,
177
+ snapshotId + '-' + poolTokenId
178
+ ] )
179
+ const token = new PoolSnapshotTokenValue ( snapshotId + '-' + poolTokenId )
180
+
181
+ token . poolSnapshot = snapshotId
182
+ token . value = amount
183
+ token . tokenReserve = balance
184
+ token . tokenAddress = poolTokenId
185
+ token . feeValue = feeValue
186
+ if ( amount . lt ( ZERO_BD ) ) {
187
+ token . type = 'out'
188
+ } else {
189
+ token . type = 'in'
190
+ }
191
+ log . warning ( 'Snapshot Token ID: {}' , [ token . id ] )
192
+ token . save ( )
193
+ }
0 commit comments