/
mutation.go
55 lines (48 loc) · 1.83 KB
/
mutation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package repository
import (
"time"
"github.com/syntropynet/osmosis-publisher/pkg/repository"
IBCTypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
_ "github.com/lib/pq"
"gorm.io/gorm/clause"
)
func (r *Repository) SaveIBCDenom(ibc IBCTypes.DenomTrace) error {
ibcDenom := IBCDenom{
IBC: ibc.IBCDenom(),
Path: ibc.Path,
BaseDenom: ibc.BaseDenom,
}
result := r.dbCon.Clauses(clause.OnConflict{DoNothing: true}).Model(&IBCDenom{}).Create(&ibcDenom)
return result.Error
}
func (r *Repository) SaveTokenPrice(price repository.TokenPrice) error {
ibcDenom := TokenPrice{
LastUpdated: price.LastUpdated.UnixNano(),
Value: price.Value,
Name: price.Name,
Base: price.Base,
}
result := r.dbCon.Clauses(clause.OnConflict{DoUpdates: clause.AssignmentColumns([]string{"value", "last_updated"})}).Model(&TokenPrice{}).Create(&ibcDenom)
return result.Error
}
func (r *Repository) SavePool(pool repository.Pool) error {
newPool := Pool{
Timestamp: pool.Timestamp,
Height: pool.Height,
PoolId: pool.PoolId,
Liquidity: pool.Liquidity.String(),
Volume: pool.Volume.String(),
}
result := r.dbCon.Clauses(clause.OnConflict{DoUpdates: clause.AssignmentColumns([]string{"liquidity", "volume", "timestamp"})}).Model(&Pool{}).Create(&newPool)
return result.Error
}
// PruneTokenPrices will remove all token prices prior timestamp.
func (r *Repository) PruneTokenPrices(timestamp time.Time) (int, error) {
result := r.dbCon.Model(&TokenPrice{}).Delete(&TokenPrice{}, "last_updated < ?", timestamp.UnixNano())
return int(result.RowsAffected), result.Error
}
// PrunePools will remove all pools prior block height.
func (r *Repository) PrunePools(height uint64) (int, error) {
result := r.dbCon.Model(&Pool{}).Delete(&Pool{}, "height < ?", height)
return int(result.RowsAffected), result.Error
}