From f7b0cea51a326c39e9b7fb4bb253ac8320da03cf Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Thu, 25 Jan 2024 10:43:21 +0900 Subject: [PATCH 1/3] Add estimate gas Signed-off-by: Dongri Jin --- pkg/client/client.go | 27 ++ pkg/relay/ethereum/client.go | 5 +- pkg/relay/ethereum/config.go | 9 + pkg/relay/ethereum/config.pb.go | 333 ++++++++++++++++-- pkg/relay/ethereum/tx.go | 92 +++-- proto/buf.lock | 4 +- .../chains/ethereum/config/config.proto | 8 + 7 files changed, 406 insertions(+), 72 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 3417605..058034f 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -194,3 +194,30 @@ func searchRevertReason(result *callFrame) (string, error) { } return "", fmt.Errorf("revert reason not found") } + +func (cl *ETHClient) EstimateGasFromTx(ctx context.Context, tx *gethtypes.Transaction) (uint64, error) { + from, err := gethtypes.Sender(gethtypes.NewEIP155Signer(tx.ChainId()), tx) + if err != nil { + return 0, err + } + to := tx.To() + value := tx.Value() + gasTipCap := tx.GasTipCap() + gasFeeCap := tx.GasFeeCap() + gasPrice := tx.GasPrice() + data := tx.Data() + callMsg := ethereum.CallMsg{ + From: from, + To: to, + GasPrice: gasPrice, + GasTipCap: gasTipCap, + GasFeeCap: gasFeeCap, + Value: value, + Data: data, + } + estimatedGas, err := cl.EstimateGas(ctx, callMsg) + if err != nil { + return 0, err + } + return estimatedGas, nil +} diff --git a/pkg/relay/ethereum/client.go b/pkg/relay/ethereum/client.go index ba09b1b..b765984 100644 --- a/pkg/relay/ethereum/client.go +++ b/pkg/relay/ethereum/client.go @@ -20,8 +20,7 @@ func (chain *Chain) CallOpts(ctx context.Context, height int64) *bind.CallOpts { func (chain *Chain) TxOpts(ctx context.Context) *bind.TransactOpts { return &bind.TransactOpts{ - From: chain.signer.Address(), - GasLimit: 6382056, - Signer: chain.signer.Sign, + From: chain.signer.Address(), + Signer: chain.signer.Sign, } } diff --git a/pkg/relay/ethereum/config.go b/pkg/relay/ethereum/config.go index 7f78085..9400124 100644 --- a/pkg/relay/ethereum/config.go +++ b/pkg/relay/ethereum/config.go @@ -41,6 +41,15 @@ func (c ChainConfig) Validate() error { if c.MaxRetryForInclusion == 0 { errs = append(errs, fmt.Errorf("config attribute \"max_retry_for_inclusion\" is zero")) } + if c.GasEstimateRate.Numerator == 0 { + errs = append(errs, fmt.Errorf("config attribute \"gas_estimate_rate.numerator\" is zero")) + } + if c.GasEstimateRate.Denominator == 0 { + errs = append(errs, fmt.Errorf("config attribute \"gas_estimate_rate.denominator\" is zero")) + } + if c.MaxGasLimit == 0 { + errs = append(errs, fmt.Errorf("config attribute \"max_gas_limit\" is zero")) + } if c.Signer == nil { errs = append(errs, fmt.Errorf("config attribute \"signer\" is empty")) } else if err := c.Signer.GetCachedValue().(SignerConfig).Validate(); err != nil { diff --git a/pkg/relay/ethereum/config.pb.go b/pkg/relay/ethereum/config.pb.go index 9af203e..08c3097 100644 --- a/pkg/relay/ethereum/config.pb.go +++ b/pkg/relay/ethereum/config.pb.go @@ -39,6 +39,8 @@ type ChainConfig struct { // if set, the relayer updates a LC contract directly if possible // if null, the relayer updates a LC contract via the handler AllowLcFunctions *AllowLCFunctionsConfig `protobuf:"bytes,11,opt,name=allow_lc_functions,json=allowLcFunctions,proto3" json:"allow_lc_functions,omitempty"` + GasEstimateRate *Fraction `protobuf:"bytes,12,opt,name=gas_estimate_rate,json=gasEstimateRate,proto3" json:"gas_estimate_rate,omitempty"` + MaxGasLimit uint64 `protobuf:"varint,13,opt,name=max_gas_limit,json=maxGasLimit,proto3" json:"max_gas_limit,omitempty"` } func (m *ChainConfig) Reset() { *m = ChainConfig{} } @@ -113,9 +115,48 @@ func (m *AllowLCFunctionsConfig) XXX_DiscardUnknown() { var xxx_messageInfo_AllowLCFunctionsConfig proto.InternalMessageInfo +type Fraction struct { + Numerator uint64 `protobuf:"varint,1,opt,name=numerator,proto3" json:"numerator,omitempty"` + Denominator uint64 `protobuf:"varint,2,opt,name=denominator,proto3" json:"denominator,omitempty"` +} + +func (m *Fraction) Reset() { *m = Fraction{} } +func (m *Fraction) String() string { return proto.CompactTextString(m) } +func (*Fraction) ProtoMessage() {} +func (*Fraction) Descriptor() ([]byte, []int) { + return fileDescriptor_a8a57ab2f9f14837, []int{2} +} +func (m *Fraction) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *Fraction) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_Fraction.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *Fraction) XXX_Merge(src proto.Message) { + xxx_messageInfo_Fraction.Merge(m, src) +} +func (m *Fraction) XXX_Size() int { + return m.Size() +} +func (m *Fraction) XXX_DiscardUnknown() { + xxx_messageInfo_Fraction.DiscardUnknown(m) +} + +var xxx_messageInfo_Fraction proto.InternalMessageInfo + func init() { proto.RegisterType((*ChainConfig)(nil), "relayer.chains.ethereum.config.ChainConfig") proto.RegisterType((*AllowLCFunctionsConfig)(nil), "relayer.chains.ethereum.config.AllowLCFunctionsConfig") + proto.RegisterType((*Fraction)(nil), "relayer.chains.ethereum.config.Fraction") } func init() { @@ -123,42 +164,47 @@ func init() { } var fileDescriptor_a8a57ab2f9f14837 = []byte{ - // 546 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x93, 0xc1, 0x6e, 0xd4, 0x30, - 0x10, 0x86, 0x37, 0xb4, 0xb4, 0xbb, 0x5e, 0x0e, 0x95, 0x55, 0x68, 0x5a, 0x20, 0x44, 0x3d, 0xad, - 0x44, 0x9b, 0x48, 0x20, 0x7a, 0xdf, 0x06, 0x55, 0xaa, 0x54, 0x2e, 0xa1, 0x27, 0x2e, 0x96, 0xe3, - 0xcc, 0x26, 0x56, 0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- 0x1b, 0xfa, 0x87, 0x1a, 0xbd, 0xf8, 0xb7, 0x17, 0xbf, 0x46, 0x48, 0xfc, 0xb9, 0x2b, 0xbf, 0x34, - 0x13, 0x31, 0x5c, 0xd5, 0x4b, 0x34, 0xf1, 0xe3, 0x51, 0x21, 0xdc, 0xce, 0x8c, 0xf3, 0xb1, 0x6b, - 0xcc, 0x85, 0xc0, 0xaf, 0xd0, 0xc4, 0x80, 0x00, 0x66, 0x95, 0x36, 0xe1, 0x46, 0xbc, 0xd1, 0xa1, - 0x43, 0xe3, 0x94, 0xde, 0xfe, 0x8a, 0x46, 0xb7, 0xf7, 0x51, 0x70, 0x77, 0x1f, 0x05, 0x3f, 0xef, - 0xa3, 0xe0, 0xdb, 0x43, 0x34, 0xba, 0x7b, 0x88, 0x46, 0x3f, 0x1e, 0xa2, 0xd1, 0x97, 0xac, 0xe2, - 0xb6, 0x6e, 0x8b, 0x84, 0xa9, 0x26, 0x2d, 0xa9, 0xa5, 0xee, 0x84, 0x82, 0x16, 0xc3, 0x17, 0x74, - 0xcc, 0x0b, 0x76, 0xec, 0xce, 0x7f, 0xec, 0xb4, 0x74, 0x79, 0x55, 0xa5, 0xae, 0x1e, 0x2c, 0xc5, - 0x96, 0xdb, 0xbf, 0xf7, 0xbf, 0x03, 0x00, 0x00, 0xff, 0xff, 0x51, 0x2b, 0x64, 0xaf, 0x86, 0x03, - 0x00, 0x00, + // 637 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x54, 0xcd, 0x6e, 0xdb, 0x38, + 0x10, 0xb6, 0x36, 0x7f, 0x32, 0x9d, 0x60, 0xb3, 0x44, 0x76, 0xa3, 0x64, 0x5b, 0x55, 0xf0, 0xc9, + 0x40, 0x13, 0x09, 0x68, 0xd1, 0xdc, 0x1d, 0xb7, 0x29, 0x52, 0xa4, 0x17, 0x35, 0xa7, 0x5e, 0x08, + 0x8a, 0x1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, 0xa5, 0x6e, 0x84, 0x82, 0x66, 0xdd, 0x77, 0xe0, 0x98, 0x67, 0xec, 0xd8, 0x0d, + 0xf8, 0xd8, 0x71, 0xc9, 0xfc, 0xaa, 0x48, 0xdc, 0xb9, 0x93, 0x64, 0x9b, 0xee, 0x15, 0xbd, 0xfc, + 0x11, 0x00, 0x00, 0xff, 0xff, 0xf2, 0xd5, 0x46, 0xf0, 0x4c, 0x04, 0x00, 0x00, } func (m *ChainConfig) Marshal() (dAtA []byte, err error) { @@ -181,6 +227,23 @@ func (m *ChainConfig) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.MaxGasLimit != 0 { + i = encodeVarintConfig(dAtA, i, uint64(m.MaxGasLimit)) + i-- + dAtA[i] = 0x68 + } + if m.GasEstimateRate != nil { + { + size, err := m.GasEstimateRate.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintConfig(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x62 + } if m.AllowLcFunctions != nil { { size, err := m.AllowLcFunctions.MarshalToSizedBuffer(dAtA[:i]) @@ -313,6 +376,39 @@ func (m *AllowLCFunctionsConfig) MarshalToSizedBuffer(dAtA []byte) (int, error) return len(dAtA) - i, nil } +func (m *Fraction) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *Fraction) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *Fraction) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Denominator != 0 { + i = encodeVarintConfig(dAtA, i, uint64(m.Denominator)) + i-- + dAtA[i] = 0x10 + } + if m.Numerator != 0 { + i = encodeVarintConfig(dAtA, i, uint64(m.Numerator)) + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil +} + func encodeVarintConfig(dAtA []byte, offset int, v uint64) int { offset -= sovConfig(v) base := offset @@ -368,6 +464,13 @@ func (m *ChainConfig) Size() (n int) { l = m.AllowLcFunctions.Size() n += 1 + l + sovConfig(uint64(l)) } + if m.GasEstimateRate != nil { + l = m.GasEstimateRate.Size() + n += 1 + l + sovConfig(uint64(l)) + } + if m.MaxGasLimit != 0 { + n += 1 + sovConfig(uint64(m.MaxGasLimit)) + } return n } @@ -393,6 +496,21 @@ func (m *AllowLCFunctionsConfig) Size() (n int) { return n } +func (m *Fraction) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Numerator != 0 { + n += 1 + sovConfig(uint64(m.Numerator)) + } + if m.Denominator != 0 { + n += 1 + sovConfig(uint64(m.Denominator)) + } + return n +} + func sovConfig(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -711,6 +829,61 @@ func (m *ChainConfig) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 12: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field GasEstimateRate", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowConfig + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthConfig + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthConfig + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.GasEstimateRate == nil { + m.GasEstimateRate = &Fraction{} + } + if err := m.GasEstimateRate.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 13: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field MaxGasLimit", wireType) + } + m.MaxGasLimit = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowConfig + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.MaxGasLimit |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipConfig(dAtA[iNdEx:]) @@ -866,6 +1039,94 @@ func (m *AllowLCFunctionsConfig) Unmarshal(dAtA []byte) error { } return nil } +func (m *Fraction) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowConfig + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: Fraction: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: Fraction: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Numerator", wireType) + } + m.Numerator = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowConfig + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Numerator |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Denominator", wireType) + } + m.Denominator = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowConfig + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Denominator |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + default: + iNdEx = preIndex + skippy, err := skipConfig(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthConfig + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipConfig(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/pkg/relay/ethereum/tx.go b/pkg/relay/ethereum/tx.go index 90b102f..4262a2a 100644 --- a/pkg/relay/ethereum/tx.go +++ b/pkg/relay/ethereum/tx.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + math "math" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/gogoproto/proto" @@ -36,39 +37,28 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) { err error ) opts := c.TxOpts(ctx) - switch msg := msg.(type) { - case *clienttypes.MsgCreateClient: - tx, err = c.TxCreateClient(opts, msg) - case *clienttypes.MsgUpdateClient: - tx, err = c.TxUpdateClient(opts, msg, skipUpdateClientCommitment) - case *conntypes.MsgConnectionOpenInit: - tx, err = c.TxConnectionOpenInit(opts, msg) - case *conntypes.MsgConnectionOpenTry: - tx, err = c.TxConnectionOpenTry(opts, msg) - case *conntypes.MsgConnectionOpenAck: - tx, err = c.TxConnectionOpenAck(opts, msg) - case *conntypes.MsgConnectionOpenConfirm: - tx, err = c.TxConnectionOpenConfirm(opts, msg) - case *chantypes.MsgChannelOpenInit: - tx, err = c.TxChannelOpenInit(opts, msg) - case *chantypes.MsgChannelOpenTry: - tx, err = c.TxChannelOpenTry(opts, msg) - case *chantypes.MsgChannelOpenAck: - tx, err = c.TxChannelOpenAck(opts, msg) - case *chantypes.MsgChannelOpenConfirm: - tx, err = c.TxChannelOpenConfirm(opts, msg) - case *chantypes.MsgRecvPacket: - tx, err = c.TxRecvPacket(opts, msg) - case *chantypes.MsgAcknowledgement: - tx, err = c.TxAcknowledgement(opts, msg) - // case *transfertypes.MsgTransfer: - // err = c.client.transfer(msg) - default: - logger.Error("failed to send msg", errors.New("illegal msg type"), "msg", msg) - panic("illegal msg type") + opts.GasLimit = math.MaxUint64 + opts.NoSend = true + tx, err = c.SendTx(opts, msg, skipUpdateClientCommitment) + if err != nil { + logger.Error("failed to send msg / NoSend: true", err, "msg", msg) + return nil, err + } + estimatedGas, err := c.client.EstimateGasFromTx(ctx, tx) + if err != nil { + logger.Error("failed to estimate gas", err, "msg", msg) + return nil, err } + txGasLimit := estimatedGas * c.Config().GasEstimateRate.Numerator / c.Config().GasEstimateRate.Denominator + if txGasLimit > c.Config().MaxGasLimit { + logger.Warn("estimated gas exceeds max gas limit", "estimated_gas", txGasLimit, "max_gas_limit", c.Config().MaxGasLimit) + txGasLimit = c.Config().MaxGasLimit + } + opts.GasLimit = txGasLimit + opts.NoSend = false + tx, err = c.SendTx(opts, msg, skipUpdateClientCommitment) if err != nil { - logger.Error("failed to send msg", err, "msg", msg) + logger.Error("failed to send msg / NoSend: false", err, "msg", msg) return nil, err } if receipt, revertReason, err := c.client.WaitForReceiptAndGet(ctx, tx.Hash(), c.config.EnableDebugTrace); err != nil { @@ -305,3 +295,43 @@ func (c *Chain) TxAcknowledgement(opts *bind.TransactOpts, msg *chantypes.MsgAck ProofHeight: pbToHandlerHeight(msg.ProofHeight), }) } + +func (c *Chain) SendTx(opts *bind.TransactOpts, msg sdk.Msg, skipUpdateClientCommitment bool) (*gethtypes.Transaction, error) { + logger := c.GetChainLogger() + var ( + tx *gethtypes.Transaction + err error + ) + switch msg := msg.(type) { + case *clienttypes.MsgCreateClient: + tx, err = c.TxCreateClient(opts, msg) + case *clienttypes.MsgUpdateClient: + tx, err = c.TxUpdateClient(opts, msg, skipUpdateClientCommitment) + case *conntypes.MsgConnectionOpenInit: + tx, err = c.TxConnectionOpenInit(opts, msg) + case *conntypes.MsgConnectionOpenTry: + tx, err = c.TxConnectionOpenTry(opts, msg) + case *conntypes.MsgConnectionOpenAck: + tx, err = c.TxConnectionOpenAck(opts, msg) + case *conntypes.MsgConnectionOpenConfirm: + tx, err = c.TxConnectionOpenConfirm(opts, msg) + case *chantypes.MsgChannelOpenInit: + tx, err = c.TxChannelOpenInit(opts, msg) + case *chantypes.MsgChannelOpenTry: + tx, err = c.TxChannelOpenTry(opts, msg) + case *chantypes.MsgChannelOpenAck: + tx, err = c.TxChannelOpenAck(opts, msg) + case *chantypes.MsgChannelOpenConfirm: + tx, err = c.TxChannelOpenConfirm(opts, msg) + case *chantypes.MsgRecvPacket: + tx, err = c.TxRecvPacket(opts, msg) + case *chantypes.MsgAcknowledgement: + tx, err = c.TxAcknowledgement(opts, msg) + // case *transfertypes.MsgTransfer: + // err = c.client.transfer(msg) + default: + logger.Error("failed to send msg", errors.New("illegal msg type"), "msg", msg) + panic("illegal msg type") + } + return tx, err +} diff --git a/proto/buf.lock b/proto/buf.lock index 5ba245e..f3b9be7 100644 --- a/proto/buf.lock +++ b/proto/buf.lock @@ -19,8 +19,8 @@ deps: - remote: buf.build owner: cosmos repository: ibc - commit: cc1d31ac98a0477580379346dab8e87b - digest: shake256:eb94a4dcb8476c0061df09486222f4c1a909334106fc99fdbb9c429e2148f9570bc767da8257b222ca20d9b578b5c80c87953613e746a6b8d7ceb85c3fc03038 + commit: 30c7be3837b0465bb385ac55e8130c85 + digest: shake256:915c16dc31b3acb6ee7c8bfdc09c70db3ae1b58ecd5b2c9eae1fc1dff3957ce46e2147eb7898caf8a8ab3ed789c55a1bf88c4722bd84c50f1ed6dfebaef41fa3 - remote: buf.build owner: cosmos repository: ics23 diff --git a/proto/relayer/chains/ethereum/config/config.proto b/proto/relayer/chains/ethereum/config/config.proto index 3f9401d..5a108fa 100644 --- a/proto/relayer/chains/ethereum/config/config.proto +++ b/proto/relayer/chains/ethereum/config/config.proto @@ -27,6 +27,9 @@ message ChainConfig { // if set, the relayer updates a LC contract directly if possible // if null, the relayer updates a LC contract via the handler AllowLCFunctionsConfig allow_lc_functions = 11; + + Fraction gas_estimate_rate = 12; + uint64 max_gas_limit = 13; } message AllowLCFunctionsConfig { @@ -34,3 +37,8 @@ message AllowLCFunctionsConfig { bool allow_all = 2; repeated string selectors = 3; } + +message Fraction { + uint64 numerator = 1; + uint64 denominator = 2; +} From c3078d3ec57c5291a388b89c293aefea27cea777 Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Thu, 25 Jan 2024 10:49:32 +0900 Subject: [PATCH 2/3] Revert proto buf.lock Signed-off-by: Dongri Jin --- proto/buf.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/proto/buf.lock b/proto/buf.lock index f3b9be7..5ba245e 100644 --- a/proto/buf.lock +++ b/proto/buf.lock @@ -19,8 +19,8 @@ deps: - remote: buf.build owner: cosmos repository: ibc - commit: 30c7be3837b0465bb385ac55e8130c85 - digest: shake256:915c16dc31b3acb6ee7c8bfdc09c70db3ae1b58ecd5b2c9eae1fc1dff3957ce46e2147eb7898caf8a8ab3ed789c55a1bf88c4722bd84c50f1ed6dfebaef41fa3 + commit: cc1d31ac98a0477580379346dab8e87b + digest: shake256:eb94a4dcb8476c0061df09486222f4c1a909334106fc99fdbb9c429e2148f9570bc767da8257b222ca20d9b578b5c80c87953613e746a6b8d7ceb85c3fc03038 - remote: buf.build owner: cosmos repository: ics23 From fac1f106a250cf8c5e324b3b0a32e57d02a6f61b Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Tue, 30 Jan 2024 10:34:36 +0900 Subject: [PATCH 3/3] * Fix from(sender) * Add accessList Signed-off-by: Dongri Jin --- pkg/client/client.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 058034f..a5889cb 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -196,7 +196,7 @@ func searchRevertReason(result *callFrame) (string, error) { } func (cl *ETHClient) EstimateGasFromTx(ctx context.Context, tx *gethtypes.Transaction) (uint64, error) { - from, err := gethtypes.Sender(gethtypes.NewEIP155Signer(tx.ChainId()), tx) + from, err := gethtypes.LatestSignerForChainID(tx.ChainId()).Sender(tx) if err != nil { return 0, err } @@ -206,14 +206,16 @@ func (cl *ETHClient) EstimateGasFromTx(ctx context.Context, tx *gethtypes.Transa gasFeeCap := tx.GasFeeCap() gasPrice := tx.GasPrice() data := tx.Data() + accessList := tx.AccessList() callMsg := ethereum.CallMsg{ - From: from, - To: to, - GasPrice: gasPrice, - GasTipCap: gasTipCap, - GasFeeCap: gasFeeCap, - Value: value, - Data: data, + From: from, + To: to, + GasPrice: gasPrice, + GasTipCap: gasTipCap, + GasFeeCap: gasFeeCap, + Value: value, + Data: data, + AccessList: accessList, } estimatedGas, err := cl.EstimateGas(ctx, callMsg) if err != nil {