feat(x/precisebank): Emit coin_spent and coin_received events (#1978)

This commit is contained in:
drklee3 2024-07-26 13:05:49 -07:00 committed by GitHub
parent f229afce1a
commit 5f802fcfbd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 57 additions and 30 deletions

View File

@ -60,14 +60,15 @@ func (k Keeper) BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) err
} }
} }
fullEmissionCoin := types.SumExtendedCoin(amt) fullEmissionCoins := sdk.NewCoins(types.SumExtendedCoin(amt))
if fullEmissionCoin.IsZero() { if fullEmissionCoins.IsZero() {
return nil return nil
} }
ctx.EventManager().EmitEvent( ctx.EventManager().EmitEvents(sdk.Events{
banktypes.NewCoinBurnEvent(acc.GetAddress(), sdk.NewCoins(fullEmissionCoin)), banktypes.NewCoinBurnEvent(acc.GetAddress(), fullEmissionCoins),
) banktypes.NewCoinSpentEvent(acc.GetAddress(), fullEmissionCoins),
})
return nil return nil
} }

View File

@ -217,20 +217,22 @@ func (suite *burnIntegrationTestSuite) TestBurnCoins() {
fraCoinAmt := tt.burnCoins.AmountOf(types.ExtendedCoinDenom) fraCoinAmt := tt.burnCoins.AmountOf(types.ExtendedCoinDenom)
totalExtCoinAmt := intCoinAmt.Add(fraCoinAmt) totalExtCoinAmt := intCoinAmt.Add(fraCoinAmt)
spentCoins := sdk.NewCoins(sdk.NewCoin(
events := suite.Ctx.EventManager().Events()
extendedEvent := banktypes.NewCoinBurnEvent(
recipientAddr,
sdk.NewCoins(sdk.NewCoin(
types.ExtendedCoinDenom, types.ExtendedCoinDenom,
totalExtCoinAmt, totalExtCoinAmt,
)), ))
)
events := suite.Ctx.EventManager().Events()
expBurnEvent := banktypes.NewCoinBurnEvent(recipientAddr, spentCoins)
expSpendEvent := banktypes.NewCoinSpentEvent(recipientAddr, spentCoins)
if totalExtCoinAmt.IsZero() { if totalExtCoinAmt.IsZero() {
suite.Require().NotContains(events, extendedEvent) suite.Require().NotContains(events, expBurnEvent)
suite.Require().NotContains(events, expSpendEvent)
} else { } else {
suite.Require().Contains(events, extendedEvent) suite.Require().Contains(events, expBurnEvent)
suite.Require().Contains(events, expSpendEvent)
} }
}) })
} }

View File

@ -63,14 +63,15 @@ func (k Keeper) MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) err
} }
} }
fullEmissionCoin := types.SumExtendedCoin(amt) fullEmissionCoins := sdk.NewCoins(types.SumExtendedCoin(amt))
if fullEmissionCoin.IsZero() { if fullEmissionCoins.IsZero() {
return nil return nil
} }
ctx.EventManager().EmitEvent( ctx.EventManager().EmitEvents(sdk.Events{
banktypes.NewCoinMintEvent(acc.GetAddress(), sdk.NewCoins(fullEmissionCoin)), banktypes.NewCoinMintEvent(acc.GetAddress(), fullEmissionCoins),
) banktypes.NewCoinReceivedEvent(acc.GetAddress(), fullEmissionCoins),
})
return nil return nil
} }

View File

@ -342,21 +342,27 @@ func (suite *mintIntegrationTestSuite) TestMintCoins() {
fraCoinAmt := mt.mintAmount.AmountOf(types.ExtendedCoinDenom) fraCoinAmt := mt.mintAmount.AmountOf(types.ExtendedCoinDenom)
totalExtCoinAmt := intCoinAmt.Add(fraCoinAmt) totalExtCoinAmt := intCoinAmt.Add(fraCoinAmt)
extCoins := sdk.NewCoins(sdk.NewCoin(types.ExtendedCoinDenom, totalExtCoinAmt))
// Check for mint event // Check for mint event
events := suite.Ctx.EventManager().Events() events := suite.Ctx.EventManager().Events()
extendedEvent := banktypes.NewCoinMintEvent(
expMintEvent := banktypes.NewCoinMintEvent(
recipientAddr, recipientAddr,
sdk.NewCoins(sdk.NewCoin( extCoins,
types.ExtendedCoinDenom, )
totalExtCoinAmt,
)), expReceivedEvent := banktypes.NewCoinReceivedEvent(
recipientAddr,
extCoins,
) )
if totalExtCoinAmt.IsZero() { if totalExtCoinAmt.IsZero() {
suite.Require().NotContains(events, extendedEvent) suite.Require().NotContains(events, expMintEvent)
suite.Require().NotContains(events, expReceivedEvent)
} else { } else {
suite.Require().Contains(events, extendedEvent) suite.Require().Contains(events, expMintEvent)
suite.Require().Contains(events, expReceivedEvent)
} }
} }
}) })

View File

@ -64,12 +64,12 @@ func (k Keeper) SendCoins(
// Get a full extended coin amount (passthrough integer + fractional) ONLY // Get a full extended coin amount (passthrough integer + fractional) ONLY
// for event attributes. // for event attributes.
fullEmissionCoin := types.SumExtendedCoin(amt) fullEmissionCoins := sdk.NewCoins(types.SumExtendedCoin(amt))
// If no passthrough integer nor fractional coins, then no event emission. // If no passthrough integer nor fractional coins, then no event emission.
// We also want to emit the event with the whole equivalent extended coin // We also want to emit the event with the whole equivalent extended coin
// if only integer coins are sent. // if only integer coins are sent.
if fullEmissionCoin.IsZero() { if fullEmissionCoins.IsZero() {
return nil return nil
} }
@ -79,8 +79,10 @@ func (k Keeper) SendCoins(
banktypes.EventTypeTransfer, banktypes.EventTypeTransfer,
sdk.NewAttribute(banktypes.AttributeKeyRecipient, to.String()), sdk.NewAttribute(banktypes.AttributeKeyRecipient, to.String()),
sdk.NewAttribute(banktypes.AttributeKeySender, from.String()), sdk.NewAttribute(banktypes.AttributeKeySender, from.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, fullEmissionCoin.String()), sdk.NewAttribute(sdk.AttributeKeyAmount, fullEmissionCoins.String()),
), ),
banktypes.NewCoinSpentEvent(from, fullEmissionCoins),
banktypes.NewCoinReceivedEvent(to, fullEmissionCoins),
}) })
return nil return nil

View File

@ -435,6 +435,7 @@ func (suite *sendIntegrationTestSuite) TestSendCoins() {
types.ExtendedCoinDenom, types.ExtendedCoinDenom,
sendAmountFullExtended.AmountOf(types.ExtendedCoinDenom), sendAmountFullExtended.AmountOf(types.ExtendedCoinDenom),
) )
extCoins := sdk.NewCoins(sendExtendedAmount)
// No extra events if not sending akava // No extra events if not sending akava
if sendExtendedAmount.IsZero() { if sendExtendedAmount.IsZero() {
@ -448,7 +449,21 @@ func (suite *sendIntegrationTestSuite) TestSendCoins() {
sdk.NewAttribute(sdk.AttributeKeyAmount, sendExtendedAmount.String()), sdk.NewAttribute(sdk.AttributeKeyAmount, sendExtendedAmount.String()),
) )
suite.Require().Contains(suite.Ctx.EventManager().Events(), extendedEvent) expReceivedEvent := banktypes.NewCoinReceivedEvent(
recipient,
extCoins,
)
expSentEvent := banktypes.NewCoinSpentEvent(
sender,
extCoins,
)
events := suite.Ctx.EventManager().Events()
suite.Require().Contains(events, extendedEvent)
suite.Require().Contains(events, expReceivedEvent)
suite.Require().Contains(events, expSentEvent)
}) })
} }
} }