From e058f2fc407c2c12dc566cbfd371fbedab0cc32d Mon Sep 17 00:00:00 2001 From: zhouop0 <11733741+zhouop0@users.noreply.github.com> Date: Sun, 11 Feb 2024 19:43:04 +0800 Subject: [PATCH] fix gas price --- internal/svc/svc.go | 2 +- internal/types/config.go | 2 +- pkg/b2node/b2node.go | 27 +++++++++++++++++---- pkg/b2node/b2node_test.go | 39 ++++++++++++++++++++++++------- pkg/btcapi/btcapi.go | 9 +++++++ pkg/btcapi/mempool/client.go | 13 +++++++++++ pkg/btcapi/mempool/client_test.go | 8 +++++++ pkg/inscribe/inscribe.go | 6 +++-- 8 files changed, 89 insertions(+), 17 deletions(-) diff --git a/internal/svc/svc.go b/internal/svc/svc.go index 348a293..531dbf2 100644 --- a/internal/svc/svc.go +++ b/internal/svc/svc.go @@ -61,7 +61,7 @@ func NewServiceContext(cfg *types.Config, bitcoinCfg *types.BitcoinRPCConfig, b2 if err != nil { log.Panicf("[svc] init b2node grpc panic: %s\n", err) } - nodeClient := b2node.NewNodeClient(privateKeHex, chainID, address, grpcConn, b2nodeConfig.RPCUrl) + nodeClient := b2node.NewNodeClient(privateKeHex, chainID, address, grpcConn, b2nodeConfig.RPCUrl, b2nodeConfig.CoinDenom) svc = &ServiceContext{ BTCConfig: bitcoinCfg, diff --git a/internal/types/config.go b/internal/types/config.go index 443f3c4..71e2d7b 100644 --- a/internal/types/config.go +++ b/internal/types/config.go @@ -30,6 +30,7 @@ type B2NODEConfig struct { GRPCHost string `env:"B2NODE_GRPC_HOST" envDefault:"127.0.0.1"` GRPCPort uint32 `env:"B2NODE_GRPC_PORT" envDefault:"9090"` RPCUrl string `env:"B2NODE_RPC_URL" envDefault:"http://localhost:8545"` + CoinDenom string `env:"B2NODE_COIN_DENOM" envDefault:"aphoton" ` } type BitcoinRPCConfig struct { @@ -42,7 +43,6 @@ type BitcoinRPCConfig struct { var ( config *Config btcRPCConfig *BitcoinRPCConfig - b2nodeConfig *B2NODEConfig ) diff --git a/pkg/b2node/b2node.go b/pkg/b2node/b2node.go index 69bc3ef..b6cfbac 100644 --- a/pkg/b2node/b2node.go +++ b/pkg/b2node/b2node.go @@ -21,6 +21,7 @@ import ( "github.com/evmos/ethermint/crypto/ethsecp256k1" eTypes "github.com/evmos/ethermint/types" committerTypes "github.com/evmos/ethermint/x/committer/types" + feeTypes "github.com/evmos/ethermint/x/feemarket/types" "google.golang.org/grpc" ) @@ -34,6 +35,7 @@ type NodeClient struct { ChainID string GrpcConn *grpc.ClientConn RPCUrl string + Denom string } type GasPriceRsp struct { @@ -42,7 +44,8 @@ type GasPriceRsp struct { Result string `json:"result"` } -func NewNodeClient(privateKeyHex string, chainID string, address string, grpcConn *grpc.ClientConn, rpcURL string) *NodeClient { +func NewNodeClient(privateKeyHex string, chainID string, address string, grpcConn *grpc.ClientConn, + rpcURL string, denom string) *NodeClient { privatekeyBytes, err := hex.DecodeString(privateKeyHex) if nil != err { panic(err) @@ -55,6 +58,7 @@ func NewNodeClient(privateKeyHex string, chainID string, address string, grpcCon ChainID: chainID, GrpcConn: grpcConn, RPCUrl: rpcURL, + Denom: denom, } } @@ -179,10 +183,19 @@ func (n NodeClient) GetEthGasPrice() (uint64, error) { return parseUint, nil } +func (n NodeClient) GetGasPrice() (uint64, error) { + queryClient := feeTypes.NewQueryClient(n.GrpcConn) + res, err := queryClient.Params(context.Background(), &feeTypes.QueryParamsRequest{}) + if err != nil { + return 0, fmt.Errorf("[GetGasPrice] err: %s", err) + } + return res.Params.BaseFee.Uint64(), nil +} + func (n NodeClient) broadcastTx(msgs ...sdk.Msg) (*tx.BroadcastTxResponse, error) { - gasPrice, err := n.GetEthGasPrice() + gasPrice, err := n.GetGasPrice() if err != nil { - return nil, fmt.Errorf("[broadcastTx][GetEthGasPrice] err: %s", err) + return nil, fmt.Errorf("[broadcastTx][GetGasPrice] err: %s", err) } txBytes, err := n.buildSimTx(gasPrice, msgs...) if err != nil { @@ -223,7 +236,7 @@ func (n NodeClient) buildSimTx(gasPrice uint64, msgs ...sdk.Msg) ([]byte, error) } txBuilder.SetGasLimit(DefaultBaseGasPrice) txBuilder.SetFeeAmount(sdk.NewCoins(sdk.Coin{ - Denom: "aphoton", + Denom: n.Denom, Amount: sdkmath.NewIntFromUint64(gasPrice * DefaultBaseGasPrice), })) @@ -270,7 +283,11 @@ func (n NodeClient) QueryProposalByID(id uint64) (*committerTypes.Proposal, erro } func (n NodeClient) CommitterBitcoinTx(msg *committerTypes.MsgBitcoinTx) (*tx.BroadcastTxResponse, error) { - txBytes, err := n.buildSimTx(7, msg) + gasPrice, err := n.GetGasPrice() + if err != nil { + return nil, fmt.Errorf("[CommitterBitcoinTx][GetGasPrice] err: %s", err) + } + txBytes, err := n.buildSimTx(gasPrice, msg) if err != nil { return nil, fmt.Errorf("[SubmitProof] err: %s", err) } diff --git a/pkg/b2node/b2node_test.go b/pkg/b2node/b2node_test.go index 5c34c89..4ed4487 100644 --- a/pkg/b2node/b2node_test.go +++ b/pkg/b2node/b2node_test.go @@ -17,11 +17,12 @@ func TestGetAccountInfo(t *testing.T) { chainID := "ethermint_9000-1" address := "ethm1jvqt5echmshc8gjsqdzk9unclt8qkx4knxcjdj" rpcUrl := "http://localhost:8545" + denom := "aphoton" grpcConn, err := types.GetClientConnection("127.0.0.1", types.WithClientPortOption(9090)) if err != nil { panic(err) } - nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl) + nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl, denom) addInfo, err := nodeClient.GetAccountInfo(address) // fmt.Println(addInfo.CodeHash) @@ -34,11 +35,12 @@ func TestSubmitProof(t *testing.T) { chainID := "ethermint_9000-1" address := "ethm1jvqt5echmshc8gjsqdzk9unclt8qkx4knxcjdj" rpcUrl := "http://localhost:8545" + denom := "aphoton" grpcConn, err := types.GetClientConnection("127.0.0.1", types.WithClientPortOption(9090)) if err != nil { panic(err) } - nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl) + nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl, denom) proposalID, err := nodeClient.SubmitProof(0, address, "proof7", "stateRoot", 1, 70) require.NoError(t, err) fmt.Println(proposalID) @@ -60,11 +62,12 @@ func TestQueryLastProposalID(t *testing.T) { chainID := "ethermint_9000-1" address := "ethm17ezey9h6zw0yzaxq00w3gmt0rdet063v3vfmee" rpcUrl := "http://localhost:8545" + denom := "aphoton" grpcConn, err := types.GetClientConnection("127.0.0.1", types.WithClientPortOption(9090)) if err != nil { panic(err) } - nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl) + nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl, denom) lastID, endIndex, err := nodeClient.QueryLastProposalID() if err != nil { panic(err) @@ -78,11 +81,12 @@ func TestQueryProposalByID(t *testing.T) { chainID := "ethermint_9000-1" address := "ethm17ezey9h6zw0yzaxq00w3gmt0rdet063v3vfmee" rpcUrl := "http://localhost:8545" + denom := "aphoton" grpcConn, err := types.GetClientConnection("127.0.0.1", types.WithClientPortOption(9090)) if err != nil { panic(err) } - nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl) + nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl, denom) proposal, err := nodeClient.QueryProposalByID(6) fmt.Println("id:", proposal.Id) fmt.Println("proposer:", proposal.Proposer) @@ -100,41 +104,60 @@ func TestCommitterBitcoinTx(t *testing.T) { chainID := "ethermint_9000-1" address := "ethm17ezey9h6zw0yzaxq00w3gmt0rdet063v3vfmee" rpcUrl := "http://localhost:8545" + denom := "aphoton" grpcConn, err := types.GetClientConnection("127.0.0.1", types.WithClientPortOption(9090)) if err != nil { panic(err) } - nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl) + nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl, denom) res, err := nodeClient.CommitterBitcoinTx(&xcommitterTypes.MsgBitcoinTx{Id: 1, From: "ethm10ky5utnz5ddlmus5t2mm5ftxal3u0u6rsnx5nl", BitcoinTxHash: "1234567890"}) require.NoError(t, err) fmt.Println(res) } -func TestGetGasPrice(t *testing.T) { +func TestGetETHGasPrice(t *testing.T) { privateKeHex := "0c993419ff40521f20370c45721c92626c2f1fd35267258fb3d093ed0826b611" chainID := "ethermint_9000-1" address := "ethm1mffw0yzmusgm9fwd40jaal3vwustuhhx8rh03q" rpcUrl := "http://localhost:8545" + denom := "aphoton" grpcConn, err := types.GetClientConnection("127.0.0.1", types.WithClientPortOption(9090)) if err != nil { panic(err) } - nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl) + nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl, denom) gasprice, err := nodeClient.GetEthGasPrice() require.NoError(t, err) fmt.Println(gasprice) } +func TestGetGasPrice(t *testing.T) { + privateKeHex := "37927fcde10259a7114a58487cb6303d04c33291ba29bbb8e488eef150e6a59a" + chainID := "ethermint_9000-1" + address := "ethm1nexknt73vdv6cm3h6ep6u7pe9vg8kr6kqwyl0a" + rpcUrl := "http://localhost:8545" + denom := "aphoton" + grpcConn, err := types.GetClientConnection("127.0.0.1", types.WithClientPortOption(9090)) + if err != nil { + panic(err) + } + nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl, denom) + gasPrice, err := nodeClient.GetGasPrice() + require.NoError(t, err) + fmt.Println(gasPrice) +} + func TestAddCommitter(t *testing.T) { privateKeHex := "37927fcde10259a7114a58487cb6303d04c33291ba29bbb8e488eef150e6a59a" chainID := "ethermint_9000-1" address := "ethm1nexknt73vdv6cm3h6ep6u7pe9vg8kr6kqwyl0a" rpcUrl := "http://localhost:8545" + denom := "aphoton" grpcConn, err := types.GetClientConnection("127.0.0.1", types.WithClientPortOption(9090)) if err != nil { panic(err) } - nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl) + nodeClient := NewNodeClient(privateKeHex, chainID, address, grpcConn, rpcUrl, denom) add, err := nodeClient.AddCommitter("ethm1c3csplac80qt22p5qwx3l5telv6ge9ycmzwe3w") require.NoError(t, err) fmt.Println(add) diff --git a/pkg/btcapi/btcapi.go b/pkg/btcapi/btcapi.go index 4e62243..f752e6d 100644 --- a/pkg/btcapi/btcapi.go +++ b/pkg/btcapi/btcapi.go @@ -33,12 +33,21 @@ type Transaction struct { } `json:"status"` } +type RecommendedFees struct { + FastestFee int64 `json:"fastestFee"` + HalfHourFee int64 `json:"halfHourFee"` + HourFee int64 `json:"hourFee"` + EconomyFee int64 `json:"economyFee"` + MinimumFee int64 `json:"minimumFee"` +} + type Client interface { GetRawTransaction(txHash *chainhash.Hash) (*wire.MsgTx, error) BroadcastTx(tx *wire.MsgTx) (*chainhash.Hash, error) ListUnspent(address btcutil.Address) ([]*UnspentOutput, error) GetTransactionByID(ID string) (*Transaction, error) GetCurrentBlockHash() (int64, error) + GetRecommendedFees() (*RecommendedFees, error) } func Request(method, baseURL, subPath string, requestBody io.Reader) ([]byte, error) { diff --git a/pkg/btcapi/mempool/client.go b/pkg/btcapi/mempool/client.go index ae179d9..cd79e0a 100644 --- a/pkg/btcapi/mempool/client.go +++ b/pkg/btcapi/mempool/client.go @@ -99,4 +99,17 @@ func (c *Client) GetCurrentBlockHash() (int64, error) { return blockHeight, nil } +func (c *Client) GetRecommendedFees() (*btcapi.RecommendedFees, error) { + res, err := c.request(http.MethodGet, "/v1/fees/recommended", nil) + if err != nil { + return nil, err + } + var recommendedFees btcapi.RecommendedFees + err = json.Unmarshal(res, &recommendedFees) + if err != nil { + return nil, err + } + return &recommendedFees, nil +} + var _ btcapi.Client = (*Client)(nil) diff --git a/pkg/btcapi/mempool/client_test.go b/pkg/btcapi/mempool/client_test.go index c2765b8..fbd70c3 100644 --- a/pkg/btcapi/mempool/client_test.go +++ b/pkg/btcapi/mempool/client_test.go @@ -36,3 +36,11 @@ func TestGetCurrentBlockHash(t *testing.T) { require.NoError(t, err) fmt.Print(hash) } + +func TestGetRecommendedFees(t *testing.T) { + client := NewClient(&chaincfg.MainNetParams) + recommendedFees, err := client.GetRecommendedFees() + require.NoError(t, err) + fmt.Println(recommendedFees.FastestFee) + fmt.Println(recommendedFees.HalfHourFee) +} diff --git a/pkg/inscribe/inscribe.go b/pkg/inscribe/inscribe.go index 27a5a84..40f3722 100644 --- a/pkg/inscribe/inscribe.go +++ b/pkg/inscribe/inscribe.go @@ -129,11 +129,13 @@ func NewInscriptionRequest(btcAPIClient btcapi.Client, address btcutil.Address, commitTxOutPointList = append(commitTxOutPointList, unspentList[i].Outpoint) commitTxPrivateKeyList = append(commitTxPrivateKeyList, privateKey) } + recommendedFees, err := btcAPIClient.GetRecommendedFees() + return &InscriptionRequest{ CommitTxOutPointList: commitTxOutPointList, CommitTxPrivateKeyList: commitTxPrivateKeyList, - CommitFeeRate: 18, - FeeRate: 19, + CommitFeeRate: recommendedFees.FastestFee, + FeeRate: recommendedFees.FastestFee + 1, DataList: dataList, SingleRevealTxOnly: false, }, nil