Skip to content

Commit

Permalink
chore: add fees to Upload User Smart Contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
maharifu committed Aug 19, 2024
1 parent e2bafbd commit 4f639af
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
13 changes: 8 additions & 5 deletions x/consensus/keeper/estimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ func (k Keeper) checkAndProcessEstimatedMessage(ctx context.Context,
return fmt.Errorf("failed to set elected gas estimate: %w", err)
}

if err := k.checkAndProcessEstimatedSubmitLogicCall(ctx, msg, q, estimate); err != nil {
if err := k.checkAndProcessEstimatedFeePayer(ctx, msg, q, estimate); err != nil {
return fmt.Errorf("failed to process estimated submit logic call: %w", err)
}

return nil
}

func (k Keeper) checkAndProcessEstimatedSubmitLogicCall(
func (k Keeper) checkAndProcessEstimatedFeePayer(
ctx context.Context,
msg types.QueuedSignedMessageI,
q consensus.Queuer,
Expand All @@ -117,21 +117,24 @@ func (k Keeper) checkAndProcessEstimatedSubmitLogicCall(
if err != nil {
return fmt.Errorf("failed to convert message to evm message: %w", err)
}
action, ok := m.Action.(*evmtypes.Message_SubmitLogicCall)
action, ok := m.Action.(evmtypes.FeePayer)
if !ok {
// Skip messages that are not SubmitLogicCall
// Skip messages that do not contain fees
return nil
}

valAddr, err := sdk.ValAddressFromBech32(m.GetAssignee())
if err != nil {
return fmt.Errorf("failed to parse validator address: %w", err)
}

fees, err := k.calculateFeesForEstimate(ctx, valAddr, m.GetChainReferenceID(), estimate)
if err != nil {
return fmt.Errorf("failed to calculate fees for estimate: %w", err)
}
action.SubmitLogicCall.Fees = fees

action.SetFees(fees)

_, err = q.Put(ctx, m, &consensus.PutOptions{
MsgIDToReplace: msg.GetId(),
})
Expand Down
26 changes: 26 additions & 0 deletions x/consensus/keeper/estimate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ func Test_CheckAndProcessEstimatedMessages(t *testing.T) {
return true
},
},
{
name: "Upload user smart contract message",
msg: &evmtypes.Message{
TurnstoneID: "abc",
ChainReferenceID: chainReferenceID,
Assignee: validators[0].Address.String(),
Action: &evmtypes.Message_UploadUserSmartContract{
UploadUserSmartContract: &evmtypes.UploadUserSmartContract{},
},
},
slcCheck: func(m *evmtypes.Message, r *require.Assertions, expected bool) bool {
usc := m.GetUploadUserSmartContract()
if usc == nil {
return expected
}
if !expected {
return usc.Fees == nil
}

r.NotNil(usc.Fees)
r.Equal(uint64(31500), usc.Fees.RelayerFee, "relayer fee: got %d", usc.Fees.RelayerFee)
r.Equal(uint64(9450), usc.Fees.CommunityFee, "community fee: got %d", usc.Fees.CommunityFee)
r.Equal(uint64(315), usc.Fees.SecurityFee, "security fee: got %d", usc.Fees.SecurityFee)
return true
},
},
}

for _, tc := range tt {
Expand Down
17 changes: 17 additions & 0 deletions x/evm/types/turnstone_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,20 @@ type SmartContractUploader interface {
GetAbi() string
GetBytecode() []byte
}

type FeePayer interface {
SetFees(fees *Fees)
}

var (
_ FeePayer = &Message_SubmitLogicCall{}
_ FeePayer = &Message_UploadUserSmartContract{}
)

func (m *Message_SubmitLogicCall) SetFees(fees *Fees) {
m.SubmitLogicCall.Fees = fees
}

func (m *Message_UploadUserSmartContract) SetFees(fees *Fees) {
m.UploadUserSmartContract.Fees = fees
}

0 comments on commit 4f639af

Please sign in to comment.