Skip to content

Commit

Permalink
Merge pull request #1921 from jorgemmsilva/feat/decode-gas-policy
Browse files Browse the repository at this point in the history
feat(wasp-cli): encode gas policy
  • Loading branch information
jorgemmsilva authored Feb 16, 2023
2 parents 2bdc38c + 4060bf4 commit c4b1cf7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/util/ratio.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ func (r Ratio32) String() string {
return fmt.Sprintf("%d:%d", r.A, r.B)
}

func Ratio32FromString(s string) (Ratio32, error) {
parts := strings.Split(s, ":")
if len(parts) != 2 {
return Ratio32{}, fmt.Errorf("invalid string")
}
a, err := strconv.ParseUint(parts[0], 10, 32)
if err != nil {
return Ratio32{}, err
}
b, err := strconv.ParseUint(parts[1], 10, 32)
if err != nil {
return Ratio32{}, err
}
return Ratio32{A: uint32(a), B: uint32(b)}, nil
}

func (r Ratio32) Bytes() []byte {
var b [8]byte
copy(b[:4], Uint32To4Bytes(r.A))
Expand Down
50 changes: 50 additions & 0 deletions tools/wasp-cli/decode/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
iotago "github.com/iotaledger/iota.go/v3"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/kv"
wasp_util "github.com/iotaledger/wasp/packages/util"
"github.com/iotaledger/wasp/packages/vm/gas"
"github.com/iotaledger/wasp/tools/wasp-cli/log"
"github.com/iotaledger/wasp/tools/wasp-cli/util"
Expand All @@ -18,6 +19,7 @@ func Init(rootCmd *cobra.Command) {
rootCmd.AddCommand(initDecodeCmd())
rootCmd.AddCommand(initDecodeMetadataCmd())
rootCmd.AddCommand(initDecodeGasFeePolicy())
rootCmd.AddCommand(initEncodeGasFeePolicy())
}

func initDecodeCmd() *cobra.Command {
Expand Down Expand Up @@ -89,3 +91,51 @@ func initDecodeGasFeePolicy() *cobra.Command {
},
}
}

func initEncodeGasFeePolicy() *cobra.Command {
var (
tokenID string
gasPerToken uint64
evmGasRatio string
validatorFeeShare uint8
)

cmd := &cobra.Command{
Use: "encode-gaspolicy",
Short: "Translates metadata from Hex to a humanly-readable format",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
gasPolicy := gas.DefaultGasFeePolicy()

if tokenID != "" {
tokenIDBytes := util.TokenIDFromString(tokenID)
nativeTokenID, err := isc.NativeTokenIDFromBytes(tokenIDBytes)
log.Check(err)
gasPolicy.GasFeeTokenID = nativeTokenID
}

if gasPerToken != 0 {
gasPolicy.GasPerToken = gasPerToken
}

if evmGasRatio != "" {
ratio, err := wasp_util.Ratio32FromString(evmGasRatio)
log.Check(err)
gasPolicy.EVMGasRatio = ratio
}

if validatorFeeShare <= 100 {
gasPolicy.ValidatorFeeShare = validatorFeeShare
}

log.Printf(iotago.EncodeHex(gasPolicy.Bytes()))
},
}

cmd.Flags().StringVar(&tokenID, "tokenID", "", "TokenID for the gas fee")
cmd.Flags().Uint64Var(&gasPerToken, "gasPerToken", 0, "gas per token")
cmd.Flags().StringVar(&evmGasRatio, "evmGasRatio", "", "evm gas ratio (format: a:b)")
cmd.Flags().Uint8Var(&validatorFeeShare, "validatorFeeShare", 101, "validator fee share (between 0 and 100)")

return cmd
}

0 comments on commit c4b1cf7

Please sign in to comment.