code.vegaprotocol.io/vega@v0.79.0/core/integration/stubs/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 stubs 17 18 import ( 19 "context" 20 "errors" 21 22 "code.vegaprotocol.io/vega/core/assets" 23 "code.vegaprotocol.io/vega/core/assets/common" 24 "code.vegaprotocol.io/vega/core/types" 25 "code.vegaprotocol.io/vega/libs/num" 26 ) 27 28 type AssetStub struct { 29 registered map[string]*assets.Asset 30 permissive bool 31 defaultDP uint64 32 } 33 34 func NewAssetStub() *AssetStub { 35 return &AssetStub{ 36 registered: map[string]*assets.Asset{}, 37 permissive: true, 38 defaultDP: 0, 39 } 40 } 41 42 func (a *AssetStub) Get(id string) (*assets.Asset, error) { 43 r, ok := a.registered[id] 44 if ok { 45 // pre-registered, so simply return 46 return r, nil 47 } 48 if !a.permissive { 49 // we're in strict mode, unknown assets should result in errors 50 return nil, errors.New("unknown asset") 51 } 52 // permissive, we return the default decimal asset 53 stub := NewIsAssetStub(id, a.defaultDP, nil) 54 return stub, nil 55 } 56 57 func (a *AssetStub) Register(id string, decimals uint64, quantum *num.Decimal) { 58 a.registered[id] = NewIsAssetStub(id, decimals, quantum) 59 } 60 61 func (a *AssetStub) SetPermissive() { 62 a.permissive = true 63 } 64 65 func (a *AssetStub) SetStrict() { 66 a.permissive = false 67 } 68 69 func (AssetStub) Enable(_ context.Context, assetID string) error { 70 return nil 71 } 72 73 func (a *AssetStub) ApplyAssetUpdate(_ context.Context, assetID string) error { 74 return nil 75 } 76 77 type isAssetStub struct { 78 ID string 79 DecimalPlaces uint64 80 Status types.AssetStatus 81 Quantum *num.Decimal 82 } 83 84 func NewIsAssetStub(id string, dp uint64, quantum *num.Decimal) *assets.Asset { 85 return assets.NewAsset(&isAssetStub{ 86 ID: id, 87 DecimalPlaces: dp, 88 Status: types.AssetStatusProposed, 89 Quantum: quantum, 90 }) 91 } 92 93 func (a isAssetStub) Type() *types.Asset { 94 quantum := num.DecimalFromFloat(5000) 95 if a.Quantum != nil { 96 if a.Quantum.IsZero() { 97 quantum = num.DecimalOne() 98 } else { 99 quantum = *a.Quantum 100 } 101 } 102 return &types.Asset{ 103 ID: a.ID, 104 Details: &types.AssetDetails{ 105 Decimals: a.DecimalPlaces, 106 Quantum: quantum, 107 }, 108 } 109 } 110 111 func (a *isAssetStub) SetPendingListing() { 112 a.Status = types.AssetStatusPendingListing 113 } 114 115 func (a *isAssetStub) SetRejected() { 116 a.Status = types.AssetStatusRejected 117 } 118 119 func (a *isAssetStub) SetEnabled() { 120 a.Status = types.AssetStatusEnabled 121 } 122 123 func (isAssetStub) GetAssetClass() common.AssetClass { 124 return common.Builtin 125 } 126 127 func (isAssetStub) IsValid() bool { 128 return true 129 } 130 131 func (isAssetStub) Validate() error { 132 return nil 133 } 134 135 func (isAssetStub) SetValid() {} 136 137 func (a isAssetStub) String() string { 138 return a.ID 139 }