code.vegaprotocol.io/vega@v0.79.0/datanode/entities/asset.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 entities 17 18 import ( 19 "encoding/json" 20 "fmt" 21 "time" 22 23 v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" 24 pb "code.vegaprotocol.io/vega/protos/vega" 25 26 "github.com/shopspring/decimal" 27 ) 28 29 type _Asset struct{} 30 31 type AssetID = ID[_Asset] 32 33 type Asset struct { 34 ID AssetID 35 ChainID string `db:"chain_id"` 36 Name string 37 Symbol string 38 Decimals int 39 Quantum decimal.Decimal 40 Source string 41 ERC20Contract string 42 TxHash TxHash 43 VegaTime time.Time 44 LifetimeLimit decimal.Decimal 45 WithdrawThreshold decimal.Decimal 46 Status AssetStatus 47 } 48 49 func (a Asset) ToProto() *pb.Asset { 50 pbAsset := &pb.Asset{ 51 Id: a.ID.String(), 52 Details: &pb.AssetDetails{ 53 Name: a.Name, 54 Symbol: a.Symbol, 55 Decimals: uint64(a.Decimals), 56 Quantum: a.Quantum.String(), 57 }, 58 Status: pb.Asset_Status(a.Status), 59 } 60 if a.Source != "" { 61 pbAsset.Details.Source = &pb.AssetDetails_BuiltinAsset{ 62 BuiltinAsset: &pb.BuiltinAsset{ 63 MaxFaucetAmountMint: a.Source, 64 }, 65 } 66 } else if a.ERC20Contract != "" { 67 pbAsset.Details.Source = &pb.AssetDetails_Erc20{ 68 Erc20: &pb.ERC20{ 69 ContractAddress: a.ERC20Contract, 70 LifetimeLimit: a.LifetimeLimit.String(), 71 WithdrawThreshold: a.WithdrawThreshold.String(), 72 ChainId: a.ChainID, 73 }, 74 } 75 } 76 77 return pbAsset 78 } 79 80 func (a Asset) Cursor() *Cursor { 81 ac := AssetCursor{ 82 ID: a.ID, 83 } 84 return NewCursor(ac.String()) 85 } 86 87 func (a Asset) ToProtoEdge(_ ...any) (*v2.AssetEdge, error) { 88 return &v2.AssetEdge{ 89 Node: a.ToProto(), 90 Cursor: a.Cursor().Encode(), 91 }, nil 92 } 93 94 type AssetCursor struct { 95 ID AssetID `json:"id"` 96 } 97 98 func (ac AssetCursor) String() string { 99 bs, err := json.Marshal(ac) 100 if err != nil { 101 panic(fmt.Errorf("couldn't marshal asset cursor: %w", err)) 102 } 103 return string(bs) 104 } 105 106 func (ac *AssetCursor) Parse(cursorString string) error { 107 if cursorString == "" { 108 return nil 109 } 110 111 return json.Unmarshal([]byte(cursorString), ac) 112 }