-
Notifications
You must be signed in to change notification settings - Fork 609
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add protorev backrun_event emission * add empty backrun event to pass in as param to make rebalance tests pass * Add basic validity test for protorev_backrun event * Add pool id to event, add documentation * Add tx_hash to backrun event * change pool point var naming * Consolidate create and emit backrun event functions into one * Update logic to not require an additional kvstore read to get data for event - Instead of reading from store to create the event (previously read to determine remaining block-level pool points remaining), use a previously read variable and passes it through the necessary functions to be able to emit it at the end * Rename pool points fn to be more general * Add test to verify proper event emission * add tx_hash to event documentation * Remove unused error return * Update godoc and naming for more clarity * add changelog entry (cherry picked from commit 62968cc) Co-authored-by: Jeremy Liu <31809888+NotJeremyLiu@users.noreply.github.com>
- Loading branch information
1 parent
773082c
commit bd809cf
Showing
10 changed files
with
209 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package keeper | ||
|
||
import ( | ||
"encoding/hex" | ||
"strconv" | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/tendermint/tendermint/crypto/tmhash" | ||
|
||
"github.com/osmosis-labs/osmosis/v15/x/protorev/types" | ||
) | ||
|
||
// EmitBackrunEvent updates and emits a backrunEvent | ||
func EmitBackrunEvent(ctx sdk.Context, pool SwapToBackrun, inputCoin sdk.Coin, profit, tokenOutAmount sdk.Int, remainingTxPoolPoints, remainingBlockPoolPoints uint64) { | ||
// Get tx hash | ||
txHash := strings.ToUpper(hex.EncodeToString(tmhash.Sum(ctx.TxBytes()))) | ||
// Update the backrun event and add it to the context | ||
backrunEvent := sdk.NewEvent( | ||
types.TypeEvtBackrun, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
sdk.NewAttribute(types.AttributeKeyTxHash, txHash), | ||
sdk.NewAttribute(types.AttributeKeyUserPoolId, strconv.FormatUint(pool.PoolId, 10)), | ||
sdk.NewAttribute(types.AttributeKeyUserDenomIn, pool.TokenInDenom), | ||
sdk.NewAttribute(types.AttributeKeyUserDenomOut, pool.TokenOutDenom), | ||
sdk.NewAttribute(types.AttributeKeyTxPoolPointsRemaining, strconv.FormatUint(remainingTxPoolPoints, 10)), | ||
sdk.NewAttribute(types.AttributeKeyBlockPoolPointsRemaining, strconv.FormatUint(remainingBlockPoolPoints, 10)), | ||
sdk.NewAttribute(types.AttributeKeyProtorevProfit, profit.String()), | ||
sdk.NewAttribute(types.AttributeKeyProtorevAmountIn, inputCoin.Amount.String()), | ||
sdk.NewAttribute(types.AttributeKeyProtorevAmountOut, tokenOutAmount.String()), | ||
sdk.NewAttribute(types.AttributeKeyProtorevArbDenom, inputCoin.Denom), | ||
) | ||
ctx.EventManager().EmitEvent(backrunEvent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"encoding/hex" | ||
"strconv" | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/tendermint/tendermint/crypto/tmhash" | ||
|
||
"github.com/osmosis-labs/osmosis/v15/x/protorev/keeper" | ||
"github.com/osmosis-labs/osmosis/v15/x/protorev/types" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestBackRunEvent() { | ||
testcases := map[string]struct { | ||
pool keeper.SwapToBackrun | ||
remainingTxPoolPoints uint64 | ||
remainingBlockPoolPoints uint64 | ||
profit sdk.Int | ||
tokenOutAmount sdk.Int | ||
inputCoin sdk.Coin | ||
}{ | ||
"basic valid": { | ||
pool: keeper.SwapToBackrun{ | ||
PoolId: 1, | ||
TokenInDenom: "uosmo", | ||
TokenOutDenom: "uatom", | ||
}, | ||
remainingTxPoolPoints: 100, | ||
remainingBlockPoolPoints: 100, | ||
profit: sdk.NewInt(100), | ||
tokenOutAmount: sdk.NewInt(100), | ||
inputCoin: sdk.NewCoin("uosmo", sdk.NewInt(100)), | ||
}, | ||
} | ||
|
||
for name, tc := range testcases { | ||
suite.Run(name, func() { | ||
expectedEvent := sdk.NewEvent( | ||
types.TypeEvtBackrun, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
sdk.NewAttribute(types.AttributeKeyTxHash, strings.ToUpper(hex.EncodeToString(tmhash.Sum(suite.Ctx.TxBytes())))), | ||
sdk.NewAttribute(types.AttributeKeyUserPoolId, strconv.FormatUint(tc.pool.PoolId, 10)), | ||
sdk.NewAttribute(types.AttributeKeyUserDenomIn, tc.pool.TokenInDenom), | ||
sdk.NewAttribute(types.AttributeKeyUserDenomOut, tc.pool.TokenOutDenom), | ||
sdk.NewAttribute(types.AttributeKeyTxPoolPointsRemaining, strconv.FormatUint(tc.remainingTxPoolPoints, 10)), | ||
sdk.NewAttribute(types.AttributeKeyBlockPoolPointsRemaining, strconv.FormatUint(tc.remainingBlockPoolPoints, 10)), | ||
sdk.NewAttribute(types.AttributeKeyProtorevProfit, tc.profit.String()), | ||
sdk.NewAttribute(types.AttributeKeyProtorevAmountIn, tc.inputCoin.Amount.String()), | ||
sdk.NewAttribute(types.AttributeKeyProtorevAmountOut, tc.tokenOutAmount.String()), | ||
sdk.NewAttribute(types.AttributeKeyProtorevArbDenom, tc.inputCoin.Denom), | ||
) | ||
|
||
keeper.EmitBackrunEvent(suite.Ctx, tc.pool, tc.inputCoin, tc.profit, tc.tokenOutAmount, tc.remainingTxPoolPoints, tc.remainingBlockPoolPoints) | ||
|
||
// Get last event emitted and ensure it is the expected event | ||
actualEvent := suite.Ctx.EventManager().Events()[len(suite.Ctx.EventManager().Events())-1] | ||
suite.Equal(expectedEvent, actualEvent) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package types | ||
|
||
const ( | ||
TypeEvtBackrun = "protorev_backrun" | ||
|
||
AttributeValueCategory = ModuleName | ||
AttributeKeyTxHash = "tx_hash" | ||
AttributeKeyUserPoolId = "user_pool_id" | ||
AttributeKeyUserDenomIn = "user_denom_in" | ||
AttributeKeyUserDenomOut = "user_denom_out" | ||
AttributeKeyBlockPoolPointsRemaining = "block_pool_points_remaining" | ||
AttributeKeyTxPoolPointsRemaining = "tx_pool_points_remaining" | ||
AttributeKeyProtorevProfit = "profit" | ||
AttributeKeyProtorevAmountIn = "amount_in" | ||
AttributeKeyProtorevAmountOut = "amount_out" | ||
AttributeKeyProtorevArbDenom = "arb_denom" | ||
) |