-
Notifications
You must be signed in to change notification settings - Fork 19
/
asset.go
123 lines (105 loc) · 3.35 KB
/
asset.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package sqlsubscribers
import (
"context"
"fmt"
"math"
"time"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/protos/vega"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
type AssetEvent interface {
events.Event
Asset() vega.Asset
}
type AssetStore interface {
Add(context.Context, entities.Asset) error
}
type Asset struct {
subscriber
store AssetStore
}
func NewAsset(store AssetStore) *Asset {
return &Asset{
store: store,
}
}
func (a *Asset) Types() []events.Type {
return []events.Type{events.AssetEvent}
}
func (a *Asset) Push(ctx context.Context, evt events.Event) error {
return a.consume(ctx, evt.(AssetEvent))
}
func (a *Asset) consume(ctx context.Context, ae AssetEvent) error {
err := a.addAsset(ctx, ae.Asset(), ae.TxHash(), a.vegaTime)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (a *Asset) addAsset(ctx context.Context, va vega.Asset, txHash string, vegaTime time.Time) error {
quantum, err := decimal.NewFromString(va.Details.Quantum)
if err != nil {
return errors.Errorf("bad quantum '%v'", va.Details.Quantum)
}
var source, erc20Contract string
lifetimeLimit := decimal.Zero
withdrawalThreshold := decimal.Zero
switch src := va.Details.Source.(type) {
case *vega.AssetDetails_BuiltinAsset:
source = src.BuiltinAsset.MaxFaucetAmountMint
case *vega.AssetDetails_Erc20:
erc20Contract = src.Erc20.ContractAddress
if src.Erc20.LifetimeLimit != "" {
res, err := decimal.NewFromString(src.Erc20.LifetimeLimit)
if err != nil {
return fmt.Errorf("couldn't parse lifetime_limit: %w", err)
}
lifetimeLimit = res
}
if src.Erc20.WithdrawThreshold != "" {
res, err := decimal.NewFromString(src.Erc20.WithdrawThreshold)
if err != nil {
return fmt.Errorf("couldn't parse withdraw_threshold: %w", err)
}
withdrawalThreshold = res
}
default:
return errors.Errorf("unknown asset source: %v", source)
}
if va.Details.Decimals > math.MaxInt {
return errors.Errorf("decimals value will cause integer overflow: %d", va.Details.Decimals)
}
decimals := int(va.Details.Decimals)
asset := entities.Asset{
ID: entities.AssetID(va.Id),
Name: va.Details.Name,
Symbol: va.Details.Symbol,
Decimals: decimals,
Quantum: quantum,
Source: source,
ERC20Contract: erc20Contract,
TxHash: entities.TxHash(txHash),
VegaTime: vegaTime,
LifetimeLimit: lifetimeLimit,
WithdrawThreshold: withdrawalThreshold,
Status: entities.AssetStatus(va.Status),
}
return errors.WithStack(a.store.Add(ctx, asset))
}