Skip to content

Commit

Permalink
Merge pull request #43 from InjectiveLabs/f/add_skip_limit_params
Browse files Browse the repository at this point in the history
feat: add INJ gas logs
  • Loading branch information
achilleas-kal authored May 13, 2022
2 parents 0069b2d + dabec28 commit cb2671d
Show file tree
Hide file tree
Showing 27 changed files with 447 additions and 74 deletions.
19 changes: 19 additions & 0 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
log "github.com/xlab/suplog"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"math"
"math/big"
"net/http"
"os"
Expand Down Expand Up @@ -86,6 +87,7 @@ type ChainClient interface {
DerivativeOrder(defaultSubaccountID eth.Hash, network common.Network, d *DerivativeOrderData) *exchangetypes.DerivativeOrder
OrderCancel(defaultSubaccountID eth.Hash, d *OrderCancelData) *exchangetypes.OrderData

GetGasFee() (string, error)
Close()
}

Expand All @@ -104,6 +106,7 @@ type chainClient struct {
accNum uint64
accSeq uint64
gasWanted uint64
gasFee string

sessionCookie string
sessionEnabled bool
Expand Down Expand Up @@ -656,6 +659,22 @@ func (c *chainClient) runBatchBroadcast() {
}
}

func (c *chainClient) GetGasFee() (string, error) {
gasPrices := strings.Trim(c.opts.GasPrices, "inj")

gas, err := strconv.ParseFloat(gasPrices, 64)

if err != nil {
return "", err
}

gasFeeAdjusted := gas * float64(c.gasWanted) / math.Pow(10, 18)
gasFeeFormatted := strconv.FormatFloat(gasFeeAdjusted, 'f', -1, 64)
c.gasFee = gasFeeFormatted

return c.gasFee, err
}

func (c *chainClient) DefaultSubaccount(acc cosmtypes.AccAddress) eth.Hash {
return eth.BytesToHash(eth.RightPadBytes(acc.Bytes(), 32))
}
Expand Down
28 changes: 25 additions & 3 deletions examples/chain/0_LocalOrderHash/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func main() {
// network := common.LoadNetwork("mainnet", "k8s")
network := common.LoadNetwork("testnet", "k8s")
tmRPC, err := rpchttp.New(network.TmEndpoint, "/websocket")

if err != nil {
fmt.Println(err)
}
Expand All @@ -29,31 +30,37 @@ func main() {
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)

if err != nil {
panic(err)
}

// initialize grpc client

clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)

if err != nil {
fmt.Println(err)
}

clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmRPC)

chainClient, err := chainclient.NewChainClient(
clientCtx,
network.ChainGrpcEndpoint,
common.OptionTLSCert(network.ChainTlsCert),
common.OptionGasPrices("500000000inj"),
)

if err != nil {
fmt.Println(err)
}

// build orders
// prepare tx msg
defaultSubaccountID := chainClient.DefaultSubaccount(senderAddress)

spotOrder := chainClient.SpotOrder(defaultSubaccountID, network, &chainclient.SpotOrderData{
Expand Down Expand Up @@ -81,16 +88,31 @@ func main() {
msg1.Sender = senderAddress.String()
msg1.Orders = []exchangetypes.DerivativeOrder{*derivativeOrder, *derivativeOrder}

// compute local order hashes
orderHashes, err := chainClient.ComputeOrderHashes(msg.Orders, msg1.Orders)

if err != nil {
fmt.Println(err)
}
fmt.Println("computed spot order hashes", orderHashes.Spot)
fmt.Println("computed derivative order hashes", orderHashes.Derivative)

fmt.Println("computed spot order hashes: ", orderHashes.Spot)
fmt.Println("computed derivative order hashes: ", orderHashes.Derivative)

//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
err = chainClient.QueueBroadcastMsg(msg, msg1)

if err != nil {
fmt.Println(err)
}

time.Sleep(time.Second * 5)

gasFee, err := chainClient.GetGasFee()

if err != nil {
fmt.Println(err)
return
}

fmt.Println("gas fee:", gasFee, "INJ")
}
17 changes: 14 additions & 3 deletions examples/chain/10_MsgBatchCancelDerivativeOrders/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (
"os"
"time"

cosmtypes "github.com/cosmos/cosmos-sdk/types"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
"github.com/InjectiveLabs/sdk-go/client/common"

exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
cosmtypes "github.com/cosmos/cosmos-sdk/types"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)

func main() {
// network := common.LoadNetwork("mainnet", "k8s")
network := common.LoadNetwork("testnet", "k8s")
tmRPC, err := rpchttp.New(network.TmEndpoint, "/websocket")

if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -73,11 +74,21 @@ func main() {
msg.Data = []exchangetypes.OrderData{*order}
CosMsgs := []cosmtypes.Msg{msg}

// AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
err = chainClient.QueueBroadcastMsg(CosMsgs...)

if err != nil {
fmt.Println(err)
}

time.Sleep(time.Second * 5)

gasFee, err := chainClient.GetGasFee()

if err != nil {
fmt.Println(err)
return
}

fmt.Println("gas fee:", gasFee, "INJ")
}
21 changes: 19 additions & 2 deletions examples/chain/11_MsgBatchCreateSpotLimitOrders/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (
"os"
"time"

"github.com/InjectiveLabs/sdk-go/client/common"
"github.com/shopspring/decimal"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"

exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)

func main() {
// network := common.LoadNetwork("mainnet", "k8s")
network := common.LoadNetwork("testnet", "k8s")
tmRPC, err := rpchttp.New(network.TmEndpoint, "/websocket")

if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -76,20 +77,36 @@ func main() {
msg.Orders = []exchangetypes.SpotOrder{*order}

simRes, err := chainClient.SimulateMsg(clientCtx, msg)

if err != nil {
fmt.Println(err)
}

simResMsgs := common.MsgResponse(simRes.Result.Data)
msgBatchCreateSpotLimitOrdersResponse := exchangetypes.MsgBatchCreateSpotLimitOrdersResponse{}
msgBatchCreateSpotLimitOrdersResponse.Unmarshal(simResMsgs[0].Data)

if err != nil {
fmt.Println(err)
}

fmt.Println("simulated order hashes", msgBatchCreateSpotLimitOrdersResponse.OrderHashes)

//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
err = chainClient.QueueBroadcastMsg(msg)

if err != nil {
fmt.Println(err)
}

time.Sleep(time.Second * 5)

gasFee, err := chainClient.GetGasFee()

if err != nil {
fmt.Println(err)
return
}

fmt.Println("gas fee:", gasFee, "INJ")
}
23 changes: 20 additions & 3 deletions examples/chain/12_MsgBatchCreateDerivativeLimitOrders/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (
"os"
"time"

cosmtypes "github.com/cosmos/cosmos-sdk/types"
"github.com/InjectiveLabs/sdk-go/client/common"
"github.com/shopspring/decimal"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"

exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
cosmtypes "github.com/cosmos/cosmos-sdk/types"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)

func main() {
// network := common.LoadNetwork("mainnet", "k8s")
network := common.LoadNetwork("testnet", "k8s")
tmRPC, err := rpchttp.New(network.TmEndpoint, "/websocket")

if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -81,20 +82,36 @@ func main() {
msg.Orders = []exchangetypes.DerivativeOrder{*order}

simRes, err := chainClient.SimulateMsg(clientCtx, msg)

if err != nil {
fmt.Println(err)
}

simResMsgs := common.MsgResponse(simRes.Result.Data)
msgBatchCreateDerivativeLimitOrdersResponse := exchangetypes.MsgBatchCreateDerivativeLimitOrdersResponse{}
msgBatchCreateDerivativeLimitOrdersResponse.Unmarshal(simResMsgs[0].Data)

if err != nil {
fmt.Println(err)
}

fmt.Println("simulated order hashes", msgBatchCreateDerivativeLimitOrdersResponse.OrderHashes)

//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
err = chainClient.QueueBroadcastMsg(msg)

if err != nil {
fmt.Println(err)
}

time.Sleep(time.Second * 5)

gasFee, err := chainClient.GetGasFee()

if err != nil {
fmt.Println(err)
return
}

fmt.Println("gas fee:", gasFee, "INJ")
}
17 changes: 14 additions & 3 deletions examples/chain/13_MsgIncreasePositionMargin/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (
"os"
"time"

cosmtypes "github.com/cosmos/cosmos-sdk/types"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
"github.com/InjectiveLabs/sdk-go/client/common"

exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
cosmtypes "github.com/cosmos/cosmos-sdk/types"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)

func main() {
// network := common.LoadNetwork("mainnet", "k8s")
network := common.LoadNetwork("testnet", "k8s")
tmRPC, err := rpchttp.New(network.TmEndpoint, "/websocket")

if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -66,11 +67,21 @@ func main() {
fmt.Println(err)
}

//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
err = chainClient.QueueBroadcastMsg(msg)

if err != nil {
fmt.Println(err)
}

time.Sleep(time.Second * 5)

gasFee, err := chainClient.GetGasFee()

if err != nil {
fmt.Println(err)
return
}

fmt.Println("gas fee:", gasFee, "INJ")
}
17 changes: 14 additions & 3 deletions examples/chain/15_MsgWithdraw/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (
"os"
"time"

sdktypes "github.com/cosmos/cosmos-sdk/types"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
"github.com/InjectiveLabs/sdk-go/client/common"

exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
sdktypes "github.com/cosmos/cosmos-sdk/types"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)

func main() {
// network := common.LoadNetwork("mainnet", "k8s")
network := common.LoadNetwork("testnet", "k8s")
tmRPC, err := rpchttp.New(network.TmEndpoint, "/websocket")

if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -66,11 +67,21 @@ func main() {
fmt.Println(err)
}

//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
err = chainClient.QueueBroadcastMsg(msg)

if err != nil {
fmt.Println(err)
}

time.Sleep(time.Second * 5)

gasFee, err := chainClient.GetGasFee()

if err != nil {
fmt.Println(err)
return
}

fmt.Println("gas fee:", gasFee, "INJ")
}
Loading

0 comments on commit cb2671d

Please sign in to comment.