Skip to content

Commit

Permalink
fix: include new lock-for-staking account as an allowable account in …
Browse files Browse the repository at this point in the history
…collateral TransferFunds
  • Loading branch information
wwestgarth committed Oct 22, 2024
1 parent ef17f31 commit 8e4733b
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions core/collateral/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2832,6 +2832,17 @@ func (e *Engine) getTransferFundsTransferRequest(ctx context.Context, t *types.T
// we always pay onto the pending transfers accounts
toAcc = e.GetPendingTransfersAccount(t.Amount.Asset)

case types.AccountTypeLockedForStaking:
fromAcc, err = e.GetPartyLockedForStaking(t.Owner, t.Amount.Asset)
if err != nil {
return nil, fmt.Errorf("account does not exists: %v, %v, %v",
accountType, t.Owner, t.Amount.Asset,
)
}

// we always pay onto the pending transfers accounts
toAcc = e.GetPendingTransfersAccount(t.Amount.Asset)

case types.AccountTypeVestedRewards:
fromAcc = e.GetOrCreatePartyVestedRewardAccount(ctx, t.Owner, t.Amount.Asset)
// we always pay onto the pending transfers accounts
Expand Down Expand Up @@ -2861,6 +2872,21 @@ func (e *Engine) getTransferFundsTransferRequest(ctx context.Context, t *types.T
}
}

case types.AccountTypeLockedForStaking:
toAcc, err = e.GetPartyLockedForStaking(t.Owner, t.Amount.Asset)
if err != nil {
// account does not exists, let's just create it
id, err := e.CreatePartyLockedForStakingAccount(ctx, t.Owner, t.Amount.Asset)
if err != nil {
return nil, err
}
toAcc, err = e.GetAccountByID(id)
if err != nil {
// shouldn't happen, we just created it...
return nil, err
}
}

// this could not exists as well, let's just create in this case
case types.AccountTypeGlobalReward, types.AccountTypeLPFeeReward, types.AccountTypeMakerReceivedFeeReward, types.AccountTypeNetworkTreasury,
types.AccountTypeMakerPaidFeeReward, types.AccountTypeMarketProposerReward, types.AccountTypeAverageNotionalReward,
Expand Down Expand Up @@ -2918,6 +2944,13 @@ func (e *Engine) getTransferFundsFeesTransferRequest(ctx context.Context, t *typ
accountType, t.Owner, t.Amount.Asset,
)
}
case types.AccountTypeLockedForStaking:
fromAcc, err = e.GetPartyLockedForStaking(t.Owner, t.Amount.Asset)
if err != nil {
return nil, fmt.Errorf("account does not exists: %v, %v, %v",
accountType, t.Owner, t.Amount.Asset,
)
}
case types.AccountTypeVestedRewards:
fromAcc = e.GetOrCreatePartyVestedRewardAccount(ctx, t.Owner, t.Amount.Asset)

Expand Down Expand Up @@ -4137,6 +4170,32 @@ func (e *Engine) CreatePartyGeneralAccount(ctx context.Context, partyID, asset s
return generalID, nil
}

// CreatePartyLockedForStakingAccount create the general account for a party.
func (e *Engine) CreatePartyLockedForStakingAccount(ctx context.Context, partyID, asset string) (string, error) {
if !e.AssetExists(asset) {
return "", ErrInvalidAssetID
}

lockedForStakingID := e.accountID(noMarket, partyID, asset, types.AccountTypeLockedForStaking)
if _, ok := e.accs[lockedForStakingID]; !ok {
acc := types.Account{
ID: lockedForStakingID,
Asset: asset,
MarketID: noMarket,
Balance: num.UintZero(),
Owner: partyID,
Type: types.AccountTypeLockedForStaking,
}
e.accs[lockedForStakingID] = &acc
e.addPartyAccount(partyID, lockedForStakingID, &acc)
e.addAccountToHashableSlice(&acc)
e.broker.Send(events.NewPartyEvent(ctx, types.Party{Id: partyID}))
e.broker.Send(events.NewAccountEvent(ctx, acc))
}

return lockedForStakingID, nil
}

// GetOrCreatePartyVestingRewardAccount create the general account for a party.
func (e *Engine) GetOrCreatePartyVestingRewardAccount(ctx context.Context, partyID, asset string) *types.Account {
if !e.AssetExists(asset) {
Expand Down

0 comments on commit 8e4733b

Please sign in to comment.