Skip to content

Commit

Permalink
fix gas price
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouop0 committed Feb 11, 2024
1 parent fb90a50 commit e058f2f
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 17 deletions.
2 changes: 1 addition & 1 deletion internal/svc/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -42,7 +43,6 @@ type BitcoinRPCConfig struct {
var (
config *Config
btcRPCConfig *BitcoinRPCConfig

b2nodeConfig *B2NODEConfig
)

Expand Down
27 changes: 22 additions & 5 deletions pkg/b2node/b2node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -34,6 +35,7 @@ type NodeClient struct {
ChainID string
GrpcConn *grpc.ClientConn
RPCUrl string
Denom string
}

type GasPriceRsp struct {
Expand All @@ -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 {

Check failure on line 48 in pkg/b2node/b2node.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

File is not `gofumpt`-ed (gofumpt)
privatekeyBytes, err := hex.DecodeString(privateKeyHex)
if nil != err {
panic(err)
Expand All @@ -55,6 +58,7 @@ func NewNodeClient(privateKeyHex string, chainID string, address string, grpcCon
ChainID: chainID,
GrpcConn: grpcConn,
RPCUrl: rpcURL,
Denom: denom,
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
}))

Expand Down Expand Up @@ -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)
}
Expand Down
39 changes: 31 additions & 8 deletions pkg/b2node/b2node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions pkg/btcapi/btcapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions pkg/btcapi/mempool/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 8 additions & 0 deletions pkg/btcapi/mempool/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 4 additions & 2 deletions pkg/inscribe/inscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Check failure on line 132 in pkg/inscribe/inscribe.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

ineffectual assignment to err (ineffassign)

return &InscriptionRequest{
CommitTxOutPointList: commitTxOutPointList,
CommitTxPrivateKeyList: commitTxPrivateKeyList,
CommitFeeRate: 18,
FeeRate: 19,
CommitFeeRate: recommendedFees.FastestFee,
FeeRate: recommendedFees.FastestFee + 1,
DataList: dataList,
SingleRevealTxOnly: false,
}, nil
Expand Down

0 comments on commit e058f2f

Please sign in to comment.