-
Notifications
You must be signed in to change notification settings - Fork 19
/
collateral.go
322 lines (285 loc) · 10.4 KB
/
collateral.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// 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 types
import (
"fmt"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/libs/ptr"
"code.vegaprotocol.io/vega/libs/stringer"
proto "code.vegaprotocol.io/vega/protos/vega"
)
const (
systemOwner = "*"
noMarket = "!"
)
type AccountDetails struct {
Owner string
AssetID string
MarketID string
Type AccountType
}
func (ad *AccountDetails) ID() string {
idbuf := make([]byte, 256)
marketID, partyID := ad.MarketID, ad.Owner
if len(marketID) <= 0 {
marketID = noMarket
}
// market account
if len(partyID) <= 0 {
partyID = systemOwner
}
copy(idbuf, marketID)
ln := len(marketID)
copy(idbuf[ln:], partyID)
ln += len(partyID)
copy(idbuf[ln:], []byte(ad.AssetID))
ln += len(ad.AssetID)
idbuf[ln] = byte(ad.Type + 48)
return string(idbuf[:ln+1])
}
func (ad *AccountDetails) IntoProto() *proto.AccountDetails {
var marketID, owner *string
if ad.Owner != systemOwner {
owner = ptr.From(ad.Owner)
}
if ad.MarketID != noMarket {
marketID = ptr.From(ad.MarketID)
}
return &proto.AccountDetails{
Owner: owner,
MarketId: marketID,
AssetId: ad.AssetID,
Type: ad.Type,
}
}
type Account struct {
ID string
Owner string
Balance *num.Uint
Asset string
MarketID string // NB: this market may not always refer to a valid market id. instead in the case of transfers it just represents a hash corresponding to a dispatch metric.
Type AccountType
}
func (a Account) ToDetails() *AccountDetails {
return &AccountDetails{
Owner: a.Owner,
MarketID: a.MarketID,
AssetID: a.Asset,
Type: a.Type,
}
}
func (a Account) String() string {
return fmt.Sprintf(
"ID(%s) owner(%s) balance(%s) asset(%s) marketID(%s) type(%s)",
a.ID,
a.Owner,
stringer.PtrToString(a.Balance),
a.Asset,
a.MarketID,
a.Type.String(),
)
}
func (a *Account) Clone() *Account {
acccpy := *a
acccpy.Balance = acccpy.Balance.Clone()
return &acccpy
}
func AccountFromProto(a *proto.Account) *Account {
bal, _ := num.UintFromString(a.Balance, 10)
return &Account{
ID: a.Id,
Owner: a.Owner,
Balance: bal,
Asset: a.Asset,
MarketID: a.MarketId,
Type: a.Type,
}
}
func (a *Account) IntoProto() *proto.Account {
return &proto.Account{
Id: a.ID,
Owner: a.Owner,
Balance: num.UintToString(a.Balance),
Asset: a.Asset,
MarketId: a.MarketID,
Type: a.Type,
}
}
type Accounts []*Account
func (a Accounts) IntoProto() []*proto.Account {
out := make([]*proto.Account, 0, len(a))
for _, v := range a {
out = append(out, v.IntoProto())
}
return out
}
type TransferRequest struct {
FromAccount []*Account
ToAccount []*Account
Amount *num.Uint
MinAmount *num.Uint
Asset string
// Reference string
Type TransferType
TransferID *string
}
func (t *TransferRequest) IntoProto() *proto.TransferRequest {
return &proto.TransferRequest{
FromAccount: Accounts(t.FromAccount).IntoProto(),
ToAccount: Accounts(t.ToAccount).IntoProto(),
Amount: num.UintToString(t.Amount),
MinAmount: num.UintToString(t.MinAmount),
Asset: t.Asset,
// Reference: t.Reference,
}
}
type LedgerMovement struct {
Entries []*LedgerEntry
Balances []*PostTransferBalance
}
func (t *LedgerMovement) IntoProto() *proto.LedgerMovement {
return &proto.LedgerMovement{
Entries: LedgerEntries(t.Entries).IntoProto(),
Balances: PostTransferBalances(t.Balances).IntoProto(),
}
}
type LedgerMovements []*LedgerMovement
func (a LedgerMovements) IntoProto() []*proto.LedgerMovement {
out := make([]*proto.LedgerMovement, 0, len(a))
for _, v := range a {
out = append(out, v.IntoProto())
}
return out
}
type PostTransferBalance struct {
Account *Account
Balance *num.Uint
}
func (t *PostTransferBalance) IntoProto() *proto.PostTransferBalance {
var acc *proto.AccountDetails
if t.Account != nil {
acc = t.Account.ToDetails().IntoProto()
}
return &proto.PostTransferBalance{
Account: acc,
Balance: t.Balance.String(),
}
}
type PostTransferBalances []*PostTransferBalance
func (a PostTransferBalances) IntoProto() []*proto.PostTransferBalance {
out := make([]*proto.PostTransferBalance, 0, len(a))
for _, v := range a {
out = append(out, v.IntoProto())
}
return out
}
type LedgerEntry struct {
FromAccount *AccountDetails
ToAccount *AccountDetails
Amount *num.Uint
Type TransferType
Timestamp int64
FromAccountBalance *num.Uint
ToAccountBalance *num.Uint
TransferID *string
}
func (l *LedgerEntry) IntoProto() *proto.LedgerEntry {
return &proto.LedgerEntry{
FromAccount: l.FromAccount.IntoProto(),
ToAccount: l.ToAccount.IntoProto(),
Amount: num.UintToString(l.Amount),
Type: l.Type,
Timestamp: l.Timestamp,
FromAccountBalance: num.UintToString(l.FromAccountBalance),
ToAccountBalance: num.UintToString(l.ToAccountBalance),
TransferId: l.TransferID,
}
}
type LedgerEntries []*LedgerEntry
func (a LedgerEntries) IntoProto() []*proto.LedgerEntry {
out := make([]*proto.LedgerEntry, 0, len(a))
for _, v := range a {
out = append(out, v.IntoProto())
}
return out
}
type AccountType = proto.AccountType
const (
// Default value.
AccountTypeUnspecified AccountType = proto.AccountType_ACCOUNT_TYPE_UNSPECIFIED
// Per asset network treasury.
AccountTypeNetworkTreasury AccountType = proto.AccountType_ACCOUNT_TYPE_NETWORK_TREASURY
// Insurance pool accounts contain insurance pool funds for a market.
AccountTypeInsurance AccountType = proto.AccountType_ACCOUNT_TYPE_INSURANCE
// Settlement accounts exist only during settlement or mark-to-market.
AccountTypeSettlement AccountType = proto.AccountType_ACCOUNT_TYPE_SETTLEMENT
// Global insurance account for the asset.
AccountTypeGlobalInsurance AccountType = proto.AccountType_ACCOUNT_TYPE_GLOBAL_INSURANCE
// Margin accounts contain margin funds for a party and each party will
// have multiple margin accounts, one for each market they have traded in
//
// Margin account funds will alter as margin requirements on positions change.
AccountTypeMargin AccountType = proto.AccountType_ACCOUNT_TYPE_MARGIN
// Margin account for isolated margin mode.
AccountTypeOrderMargin AccountType = proto.AccountType_ACCOUNT_TYPE_ORDER_MARGIN
// General accounts contains general funds for a party. A party will
// have multiple general accounts, one for each asset they want
// to trade with
//
// General accounts are where funds are initially deposited or withdrawn from,
// it is also the account where funds are taken to fulfil fees and initial margin requirements.
AccountTypeGeneral AccountType = proto.AccountType_ACCOUNT_TYPE_GENERAL
// Infrastructure accounts contain fees earned by providing infrastructure on Vega.
AccountTypeFeesInfrastructure AccountType = proto.AccountType_ACCOUNT_TYPE_FEES_INFRASTRUCTURE
// Liquidity accounts contain fees earned by providing liquidity on Vega markets.
AccountTypeFeesLiquidity AccountType = proto.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY
// This account is created to hold fees earned by placing orders that sit on the book
// and are then matched with an incoming order to create a trade - These fees reward parties
// who provide the best priced liquidity that actually allows trading to take place.
AccountTypeFeesMaker AccountType = proto.AccountType_ACCOUNT_TYPE_FEES_MAKER
// This account is created to maintain liquidity providers funds commitments.
AccountTypeBond AccountType = proto.AccountType_ACCOUNT_TYPE_BOND
// External account represents an external source (deposit/withdrawal).
AccountTypeExternal AccountType = proto.AccountType_ACCOUNT_TYPE_EXTERNAL
// Global reward accounts contain rewards per asset.
AccountTypeGlobalReward AccountType = proto.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD
// Global account to hold pending transfers.
AccountTypePendingTransfers AccountType = proto.AccountType_ACCOUNT_TYPE_PENDING_TRANSFERS
// Asset account for paid taker fees.
AccountTypeMakerPaidFeeReward AccountType = proto.AccountType_ACCOUNT_TYPE_REWARD_MAKER_PAID_FEES
// Asset account for received maker fees.
AccountTypeMakerReceivedFeeReward AccountType = proto.AccountType_ACCOUNT_TYPE_REWARD_MAKER_RECEIVED_FEES
// Asset account for received LP fees.
AccountTypeLPFeeReward AccountType = proto.AccountType_ACCOUNT_TYPE_REWARD_LP_RECEIVED_FEES
// Asset account for market proposers.
AccountTypeMarketProposerReward AccountType = proto.AccountType_ACCOUNT_TYPE_REWARD_MARKET_PROPOSERS
// Market account for holding in-flight spot passive orders funds.
AccountTypeHolding AccountType = proto.AccountType_ACCOUNT_TYPE_HOLDING
// Market account per LP to receive liquidity fees.
AccountTypeLPLiquidityFees AccountType = proto.AccountType_ACCOUNT_TYPE_LP_LIQUIDITY_FEES
AccountTypeLiquidityFeesBonusDistribution AccountType = proto.AccountType_ACCOUNT_TYPE_LIQUIDITY_FEES_BONUS_DISTRIBUTION
AccountTypeVestingRewards AccountType = proto.AccountType_ACCOUNT_TYPE_VESTING_REWARDS
AccountTypeVestedRewards AccountType = proto.AccountType_ACCOUNT_TYPE_VESTED_REWARDS
// Reward account for average position metric.
AccountTypeAveragePositionReward AccountType = proto.AccountType_ACCOUNT_TYPE_REWARD_AVERAGE_POSITION
// Reward account for relative return metric.
AccountTypeRelativeReturnReward AccountType = proto.AccountType_ACCOUNT_TYPE_REWARD_RELATIVE_RETURN
// Reward account for return volatility metric.
AccountTypeReturnVolatilityReward AccountType = proto.AccountType_ACCOUNT_TYPE_REWARD_RETURN_VOLATILITY
// Reward account for validator ranking metric.
AccountTypeValidatorRankingReward AccountType = proto.AccountType_ACCOUNT_TYPE_REWARD_VALIDATOR_RANKING
// Account for pending fee referral rewards.
AccountTypePendingFeeReferralReward AccountType = proto.AccountType_ACCOUNT_TYPE_PENDING_FEE_REFERRAL_REWARD
)