/
assets.go
116 lines (104 loc) · 3.02 KB
/
assets.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
// 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 steps
import (
"context"
"fmt"
"code.vegaprotocol.io/vega/core/collateral"
"code.vegaprotocol.io/vega/core/integration/stubs"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/libs/ptr"
"github.com/cucumber/godog"
)
func UpdateAsset(tbl *godog.Table, asset *stubs.AssetStub, col *collateral.Engine) error {
rows := parseAssetsTable(tbl)
for _, row := range rows {
aRow := assetRow{row: row}
aid := row.MustStr("id")
asset.Register(
aid,
row.MustU64("decimal places"),
aRow.maybeQuantum(),
)
err := col.PropagateAssetUpdate(context.Background(), types.Asset{
ID: aid,
Details: &types.AssetDetails{
Quantum: aRow.quantum(),
Symbol: aid,
},
})
if err != nil {
if err == collateral.ErrAssetHasNotBeenEnabled {
return fmt.Errorf("asset %q has not been enabled", aid)
}
return fmt.Errorf("couldn't enable asset %q: %w", aid, err)
}
}
return nil
}
func RegisterAsset(tbl *godog.Table, asset *stubs.AssetStub, col *collateral.Engine) error {
rows := parseAssetsTable(tbl)
for _, row := range rows {
aRow := assetRow{row: row}
aid := row.MustStr("id")
asset.Register(
aid,
row.MustU64("decimal places"),
aRow.maybeQuantum(),
)
err := col.EnableAsset(context.Background(), types.Asset{
ID: aid,
Details: &types.AssetDetails{
Quantum: aRow.quantum(),
Symbol: aid,
},
})
if err != nil {
if err == collateral.ErrAssetAlreadyEnabled {
return fmt.Errorf("asset %s was already enabled, perhaps when defining markets, order of steps should be swapped", aid)
}
return fmt.Errorf("couldn't enable asset %q: %w", aid, err)
}
}
return nil
}
func parseAssetsTable(table *godog.Table) []RowWrapper {
return StrictParseTable(table, []string{
"id",
"decimal places",
}, []string{
"quantum",
})
}
type assetRow struct {
row RowWrapper
}
func (r assetRow) quantum() num.Decimal {
if !r.row.HasColumn("quantum") {
return num.DecimalOne()
}
return r.row.MustDecimal("quantum")
}
func (r assetRow) maybeQuantum() *num.Decimal {
if !r.row.HasColumn("quantum") {
return nil
}
return ptr.From(r.row.MustDecimal("quantum"))
}
func CreateNetworkTreasuryAccount(col *collateral.Engine, asset string) error {
_ = col.GetOrCreateNetworkTreasuryAccount(context.Background(), asset)
return nil
}