code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/assets.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package steps 17 18 import ( 19 "context" 20 "fmt" 21 22 "code.vegaprotocol.io/vega/core/collateral" 23 "code.vegaprotocol.io/vega/core/integration/stubs" 24 "code.vegaprotocol.io/vega/core/types" 25 "code.vegaprotocol.io/vega/libs/num" 26 "code.vegaprotocol.io/vega/libs/ptr" 27 28 "github.com/cucumber/godog" 29 ) 30 31 func UpdateAsset(tbl *godog.Table, asset *stubs.AssetStub, col *collateral.Engine) error { 32 rows := parseAssetsTable(tbl) 33 for _, row := range rows { 34 aRow := assetRow{row: row} 35 aid := row.MustStr("id") 36 asset.Register( 37 aid, 38 row.MustU64("decimal places"), 39 aRow.maybeQuantum(), 40 ) 41 err := col.PropagateAssetUpdate(context.Background(), types.Asset{ 42 ID: aid, 43 Details: &types.AssetDetails{ 44 Quantum: aRow.quantum(), 45 Symbol: aid, 46 }, 47 }) 48 if err != nil { 49 if err == collateral.ErrAssetHasNotBeenEnabled { 50 return fmt.Errorf("asset %q has not been enabled", aid) 51 } 52 return fmt.Errorf("couldn't enable asset %q: %w", aid, err) 53 } 54 } 55 return nil 56 } 57 58 func RegisterAsset(tbl *godog.Table, asset *stubs.AssetStub, col *collateral.Engine) error { 59 rows := parseAssetsTable(tbl) 60 for _, row := range rows { 61 aRow := assetRow{row: row} 62 aid := row.MustStr("id") 63 asset.Register( 64 aid, 65 row.MustU64("decimal places"), 66 aRow.maybeQuantum(), 67 ) 68 err := col.EnableAsset(context.Background(), types.Asset{ 69 ID: aid, 70 Details: &types.AssetDetails{ 71 Quantum: aRow.quantum(), 72 Symbol: aid, 73 }, 74 }) 75 if err != nil { 76 if err == collateral.ErrAssetAlreadyEnabled { 77 return fmt.Errorf("asset %s was already enabled, perhaps when defining markets, order of steps should be swapped", aid) 78 } 79 return fmt.Errorf("couldn't enable asset %q: %w", aid, err) 80 } 81 } 82 return nil 83 } 84 85 func parseAssetsTable(table *godog.Table) []RowWrapper { 86 return StrictParseTable(table, []string{ 87 "id", 88 "decimal places", 89 }, []string{ 90 "quantum", 91 }) 92 } 93 94 type assetRow struct { 95 row RowWrapper 96 } 97 98 func (r assetRow) quantum() num.Decimal { 99 if !r.row.HasColumn("quantum") { 100 return num.DecimalOne() 101 } 102 return r.row.MustDecimal("quantum") 103 } 104 105 func (r assetRow) maybeQuantum() *num.Decimal { 106 if !r.row.HasColumn("quantum") { 107 return nil 108 } 109 110 return ptr.From(r.row.MustDecimal("quantum")) 111 } 112 113 func CreateNetworkTreasuryAccount(col *collateral.Engine, asset string) error { 114 _ = col.GetOrCreateNetworkTreasuryAccount(context.Background(), asset) 115 return nil 116 }