-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
store calculated VWAP for each tokens with 10min interval
- Loading branch information
Showing
4 changed files
with
112 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package vwap | ||
|
||
// VWAPData represents the specific token's VWAP data. | ||
type VWAPData struct { | ||
TokenName string `json:"token_name"` // VWAP data belongs token name | ||
VWAP float64 `json:"vwap"` // calculated VWAP value | ||
Timestamp int `json:"timestamp"` // timestamp of the VWAP value (UNIX, 10 min interval) | ||
} | ||
|
||
var vwapDataMap map[string][]VWAPData | ||
|
||
func init() { | ||
lastPrices = make(map[string]float64) | ||
vwapDataMap = make(map[string][]VWAPData) | ||
} | ||
|
||
// store stores the VWAP data for the token or updates the existing data. | ||
// | ||
// Parameters: | ||
// | ||
// - tokenName: the token name | ||
// - vwap: the VWAP value to store | ||
// - timestamp: the timestamp of the VWAP value | ||
func store(tokenName string, vwap float64, timestamp int) { | ||
// adjust the timestamp to the 10 minutes interval. | ||
adjustedTimestamp := timestamp - (timestamp % 600) | ||
|
||
// get the VWAP data for the token | ||
lst, ok := vwapDataMap[tokenName] | ||
if !ok { | ||
lst = []VWAPData{} | ||
} | ||
|
||
// check last VWAP data for the list | ||
if len(lst) > 0 { | ||
last := lst[len(lst)-1] | ||
if last.Timestamp == adjustedTimestamp { | ||
last.VWAP = vwap | ||
vwapDataMap[tokenName] = lst | ||
return | ||
} | ||
} | ||
|
||
lst = append(lst, VWAPData{tokenName, vwap, adjustedTimestamp}) | ||
vwapDataMap[tokenName] = lst | ||
} |
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,40 @@ | ||
package vwap | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestVWAPStorage(t *testing.T) { | ||
vwapDataMap = make(map[string][]VWAPData) | ||
lastPrices = make(map[string]float64) | ||
|
||
trades := generateMockTrades() | ||
|
||
for _, trade := range trades { | ||
VWAP([]TradeData{trade}) | ||
} | ||
|
||
expectedVWAPData := map[string][]VWAPData{ | ||
"gno.land/r/demo/foo": { | ||
{TokenName: "gno.land/r/demo/foo", VWAP: 1.2, Timestamp: 1623200400}, | ||
{TokenName: "gno.land/r/demo/foo", VWAP: 1.5, Timestamp: 1623201000}, | ||
}, | ||
"gno.land/r/demo/bar": { | ||
{TokenName: "gno.land/r/demo/bar", VWAP: 2.1, Timestamp: 1623200400}, | ||
{TokenName: "gno.land/r/demo/bar", VWAP: 2.3, Timestamp: 1623201000}, | ||
}, | ||
} | ||
|
||
assert.Equal(t, expectedVWAPData, vwapDataMap) | ||
} | ||
|
||
func generateMockTrades() []TradeData { | ||
return []TradeData{ | ||
{TokenName: "gno.land/r/demo/foo", Volume: 100, Ratio: 1.2, Timestamp: 1623200400}, | ||
{TokenName: "gno.land/r/demo/bar", Volume: 200, Ratio: 2.1, Timestamp: 1623200400}, | ||
{TokenName: "gno.land/r/demo/foo", Volume: 150, Ratio: 1.5, Timestamp: 1623201000}, | ||
{TokenName: "gno.land/r/demo/bar", Volume: 250, Ratio: 2.3, Timestamp: 1623201000}, | ||
} | ||
} |
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