code.vegaprotocol.io/vega@v0.79.0/datanode/entities/deposit.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 "code.vegaprotocol.io/vega/protos/vega" 25 26 "github.com/shopspring/decimal" 27 ) 28 29 type _Deposit struct{} 30 31 type DepositID = ID[_Deposit] 32 33 type Deposit struct { 34 ID DepositID 35 Status DepositStatus 36 PartyID PartyID 37 Asset AssetID 38 Amount decimal.Decimal 39 ForeignTxHash string 40 CreditedTimestamp time.Time 41 CreatedTimestamp time.Time 42 TxHash TxHash 43 VegaTime time.Time 44 } 45 46 func DepositFromProto(deposit *vega.Deposit, txHash TxHash, vegaTime time.Time) (*Deposit, error) { 47 var err error 48 var amount decimal.Decimal 49 50 if amount, err = decimal.NewFromString(deposit.Amount); err != nil { 51 return nil, fmt.Errorf("invalid amount: %w", err) 52 } 53 54 return &Deposit{ 55 ID: DepositID(deposit.Id), 56 Status: DepositStatus(deposit.Status), 57 PartyID: PartyID(deposit.PartyId), 58 Asset: AssetID(deposit.Asset), 59 Amount: amount, 60 ForeignTxHash: deposit.TxHash, 61 CreditedTimestamp: NanosToPostgresTimestamp(deposit.CreditedTimestamp), 62 CreatedTimestamp: NanosToPostgresTimestamp(deposit.CreatedTimestamp), 63 TxHash: txHash, 64 VegaTime: vegaTime, 65 }, nil 66 } 67 68 func (d Deposit) ToProto() *vega.Deposit { 69 return &vega.Deposit{ 70 Id: d.ID.String(), 71 Status: vega.Deposit_Status(d.Status), 72 PartyId: d.PartyID.String(), 73 Asset: d.Asset.String(), 74 Amount: d.Amount.String(), 75 TxHash: d.ForeignTxHash, 76 CreditedTimestamp: d.CreditedTimestamp.UnixNano(), 77 CreatedTimestamp: d.CreatedTimestamp.UnixNano(), 78 } 79 } 80 81 func (d Deposit) Cursor() *Cursor { 82 cursor := DepositCursor{ 83 VegaTime: d.VegaTime, 84 ID: d.ID, 85 } 86 return NewCursor(cursor.String()) 87 } 88 89 func (d Deposit) ToProtoEdge(_ ...any) (*v2.DepositEdge, error) { 90 return &v2.DepositEdge{ 91 Node: d.ToProto(), 92 Cursor: d.Cursor().Encode(), 93 }, nil 94 } 95 96 type DepositCursor struct { 97 VegaTime time.Time `json:"vegaTime"` 98 ID DepositID `json:"id"` 99 } 100 101 func (dc DepositCursor) String() string { 102 bs, err := json.Marshal(dc) 103 if err != nil { 104 // This should never happen. 105 panic(fmt.Errorf("couldn't marshal deposit cursor: %w", err)) 106 } 107 return string(bs) 108 } 109 110 func (dc *DepositCursor) Parse(cursorString string) error { 111 if cursorString == "" { 112 return nil 113 } 114 return json.Unmarshal([]byte(cursorString), dc) 115 }