Skip to content

Commit

Permalink
Merge pull request #9771 from vegaprotocol/genacc
Browse files Browse the repository at this point in the history
fix: allow creation of general account on demand for transferring fees
  • Loading branch information
jeremyletang authored Oct 13, 2023
2 parents 264e660 + 26c4776 commit 8e31309
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions core/collateral/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ func (e *Engine) EnableAsset(ctx context.Context, asset types.Asset) error {
// this is useful when doing a migration so we can create all asset
// account for already enabled assets.
func (e *Engine) ensureAllAssetAccounts(ctx context.Context, asset types.Asset) {
e.log.Debug("ensureAllAssetAccounts started")
// then creat a new infrastructure fee account for the asset
// these are fee related account only
infraFeeID := e.accountID(noMarket, systemOwner, asset.ID, types.AccountTypeFeesInfrastructure)
Expand Down Expand Up @@ -418,6 +419,7 @@ func (e *Engine) ensureAllAssetAccounts(ctx context.Context, asset types.Asset)
// see https://github.com/vegaprotocol/vega/pull/2745 for more information
e.addAccountToHashableSlice(externalAcc)
e.broker.Send(events.NewAccountEvent(ctx, *externalAcc))
e.log.Info("event emitted for new account", logging.String("accID", externalID))
}

// when an asset is enabled a staking reward account is created for it
Expand All @@ -436,6 +438,7 @@ func (e *Engine) ensureAllAssetAccounts(ctx context.Context, asset types.Asset)
e.accs[rewardID] = rewardAcc
e.addAccountToHashableSlice(rewardAcc)
e.broker.Send(events.NewAccountEvent(ctx, *rewardAcc))
e.log.Info("event emitted for new account", logging.String("accID", rewardID))
}
}

Expand All @@ -453,6 +456,7 @@ func (e *Engine) ensureAllAssetAccounts(ctx context.Context, asset types.Asset)
e.accs[netTreasury] = ntAcc
e.addAccountToHashableSlice(ntAcc)
e.broker.Send(events.NewAccountEvent(ctx, *ntAcc))
e.log.Info("event emitted for new account", logging.String("accID", netTreasury))
}

// global insurance for the asset
Expand All @@ -469,6 +473,7 @@ func (e *Engine) ensureAllAssetAccounts(ctx context.Context, asset types.Asset)
e.accs[globalInsurance] = giAcc
e.addAccountToHashableSlice(giAcc)
e.broker.Send(events.NewAccountEvent(ctx, *giAcc))
e.log.Info("event emitted for new account", logging.String("accID", globalInsurance))
}

// pending transfers account
Expand All @@ -486,6 +491,7 @@ func (e *Engine) ensureAllAssetAccounts(ctx context.Context, asset types.Asset)
e.accs[pendingTransfersID] = pendingTransfersAcc
e.addAccountToHashableSlice(pendingTransfersAcc)
e.broker.Send(events.NewAccountEvent(ctx, *pendingTransfersAcc))
e.log.Info("event emitted for new account", logging.String("accID", pendingTransfersID))
}

pendingFeeReferrerRewardID := e.accountID(noMarket, systemOwner, asset.ID, types.AccountTypePendingFeeReferralReward)
Expand All @@ -502,6 +508,7 @@ func (e *Engine) ensureAllAssetAccounts(ctx context.Context, asset types.Asset)
e.accs[pendingFeeReferrerRewardID] = pendingFeeReferrerRewardAcc
e.addAccountToHashableSlice(pendingFeeReferrerRewardAcc)
e.broker.Send(events.NewAccountEvent(ctx, *pendingFeeReferrerRewardAcc))
e.log.Info("event emitted for new account", logging.String("accID", pendingFeeReferrerRewardID))
}
}

Expand Down Expand Up @@ -2006,7 +2013,7 @@ func (e *Engine) getFeeTransferRequest(
makerFee, infraFee, liquiFee *types.Account,
marketID, assetID string,
) (*types.TransferRequest, error) {
getAccount := func(marketID, owner string, accountType vega.AccountType) (*types.Account, error) {
getAccountLogError := func(marketID, owner string, accountType vega.AccountType) (*types.Account, error) {
acc, err := e.GetAccountByID(e.accountID(marketID, owner, assetID, accountType))
if err != nil {
e.log.Error(
Expand All @@ -2030,16 +2037,23 @@ func (e *Engine) getFeeTransferRequest(
}

marginAccount := func() (*types.Account, error) {
return getAccount(marketID, t.Owner, types.AccountTypeMargin)
return getAccountLogError(marketID, t.Owner, types.AccountTypeMargin)
}

referralPendingRewardAccount := func() (*types.Account, error) {
return getAccount(noMarket, systemOwner, types.AccountTypePendingFeeReferralReward)
return getAccountLogError(noMarket, systemOwner, types.AccountTypePendingFeeReferralReward)
}

general, err := getAccount(noMarket, t.Owner, types.AccountTypeGeneral)
general, err := e.GetAccountByID(e.accountID(noMarket, t.Owner, assetID, types.AccountTypeGeneral))
if err != nil {
return nil, err
generalID, err := e.CreatePartyGeneralAccount(ctx, t.Owner, assetID)
if err != nil {
return nil, err
}
general, err = e.GetAccountByID(generalID)
if err != nil {
return nil, err
}
}

treq := &types.TransferRequest{
Expand Down

0 comments on commit 8e31309

Please sign in to comment.