Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KLC-1265 update klever go sdk to be full vm compatible #25

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions builders/baseBuilder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package builders

import (
"encoding/hex"
"fmt"
"math/big"

"github.com/klever-io/klever-go-sdk/core/address"
"github.com/klever-io/klever-go/tools/check"
)

type baseBuilder struct {
args []string
err error
}

func (builder *baseBuilder) addBytes(bytes []byte) {
if len(bytes) == 0 {
bytes = []byte{0}
}

builder.args = append(builder.args, hex.EncodeToString(bytes))
}

func (builder *baseBuilder) checkAddress(address address.Address) error {
if check.IfNil(address) {
return fmt.Errorf("%w in builder.checkAddress", ErrNilAddress)
}
if !address.IsValid() {
return fmt.Errorf("%w in builder.checkAddress", ErrInvalidAddress)
}

return nil
}

func (builder *baseBuilder) addArgHexString(hexed string) {
if builder.err != nil {
return
}

_, err := hex.DecodeString(hexed)
if err != nil {
builder.err = fmt.Errorf("%w in builder.ArgHexString for string %s", err, hexed)
return
}

builder.args = append(builder.args, hexed)
}

func (builder *baseBuilder) addArgAddress(address address.Address) {
if builder.err != nil {
return
}

err := builder.checkAddress(address)
if err != nil {
builder.err = err
return
}

builder.addBytes(address.Bytes())
}

func (builder *baseBuilder) addArgBigInt(value *big.Int) {
if builder.err != nil {
return
}

if value == nil {
builder.err = fmt.Errorf("%w in builder.ArgBigInt", ErrNilValue)
return
}

builder.addBytes(value.Bytes())
}

func (builder *baseBuilder) addArgInt64(value int64) {
if builder.err != nil {
return
}

b := big.NewInt(value)

builder.addBytes(b.Bytes())
}

func (builder *baseBuilder) addArgBytes(bytes []byte) {
if builder.err != nil {
return
}

if len(bytes) == 0 {
builder.err = fmt.Errorf("%w in builder.ArgBytes", ErrInvalidValue)
}

builder.addBytes(bytes)
}
26 changes: 26 additions & 0 deletions builders/baseBuilder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package builders

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBaseBuilder_AnErrorWillPreventProcessingOfTheArguments(t *testing.T) {
t.Parallel()

expectedErr := errors.New("expected error")
b := &baseBuilder{
err: expectedErr,
}

b.addArgBytes(nil)
b.addArgAddress(nil)
b.addArgBigInt(nil)
b.addArgHexString("not hexed")
b.addArgInt64(0)

assert.Equal(t, expectedErr, b.err)
assert.Equal(t, 0, len(b.args))
}
15 changes: 15 additions & 0 deletions builders/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package builders

import "errors"

// ErrInvalidValue signals that an invalid value was provided
var ErrInvalidValue = errors.New("invalid value")

// ErrNilValue signals that a nil value was provided
var ErrNilValue = errors.New("nil value")

// ErrNilAddress signals that a nil address was provided
var ErrNilAddress = errors.New("nil address handler")

// ErrInvalidAddress signals that an invalid address was provided
var ErrInvalidAddress = errors.New("invalid address handler")
42 changes: 42 additions & 0 deletions builders/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package builders

import (
"math/big"

"github.com/klever-io/klever-go-sdk/core/address"
"github.com/klever-io/klever-go-sdk/provider"
)

// TxDataBuilder defines the behavior of a transaction data builder
type TxDataBuilder interface {
Function(function string) TxDataBuilder

ArgHexString(hexed string) TxDataBuilder
ArgAddress(address address.Address) TxDataBuilder
ArgBigInt(value *big.Int) TxDataBuilder
ArgInt64(value int64) TxDataBuilder
ArgBytes(bytes []byte) TxDataBuilder
ArgBytesList(list [][]byte) TxDataBuilder

ToDataString() (string, error)
ToDataBytes() ([]byte, error)

IsInterfaceNil() bool
}

// VMQueryBuilder defines the behavior of a vm query builder
type VMQueryBuilder interface {
Function(function string) VMQueryBuilder
CallerAddress(address address.Address) VMQueryBuilder
Address(address address.Address) VMQueryBuilder

ArgHexString(hexed string) VMQueryBuilder
ArgAddress(address address.Address) VMQueryBuilder
ArgBigInt(value *big.Int) VMQueryBuilder
ArgInt64(value int64) VMQueryBuilder
ArgBytes(bytes []byte) VMQueryBuilder

ToVmValueRequest() (*provider.VmValueRequest, error)

IsInterfaceNil() bool
}
106 changes: 106 additions & 0 deletions builders/txDataBuilder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package builders

import (
"math/big"
"strings"

"github.com/klever-io/klever-go-sdk/core/address"
)

const dataSeparator = "@"

// txDataBuilder can be used to easy construct a transaction's data field for a smart contract call
// can also be used to construct a VmValueRequest instance ready to be used on a VM query
type txDataBuilder struct {
*baseBuilder
function string
}

// NewTxDataBuilder creates a new transaction data builder
func NewTxDataBuilder() *txDataBuilder {
return &txDataBuilder{
baseBuilder: &baseBuilder{},
}
}

// Function sets the function to be called
func (builder *txDataBuilder) Function(function string) TxDataBuilder {
builder.function = function

return builder
}

// ArgHexString adds the provided hex string to the arguments list
func (builder *txDataBuilder) ArgHexString(hexed string) TxDataBuilder {
builder.addArgHexString(hexed)

return builder
}

// ArgAddress adds the provided address to the arguments list
func (builder *txDataBuilder) ArgAddress(address address.Address) TxDataBuilder {
builder.addArgAddress(address)

return builder
}

// ArgBigInt adds the provided value to the arguments list
func (builder *txDataBuilder) ArgBigInt(value *big.Int) TxDataBuilder {
builder.addArgBigInt(value)

return builder
}

// ArgInt64 adds the provided value to the arguments list
func (builder *txDataBuilder) ArgInt64(value int64) TxDataBuilder {
builder.addArgInt64(value)

return builder
}

// ArgBytes adds the provided bytes to the arguments list. The parameter should contain at least one byte
func (builder *txDataBuilder) ArgBytes(bytes []byte) TxDataBuilder {
builder.addArgBytes(bytes)

return builder
}

// ArgBytesList adds the provided list of bytes. Each argument from the list should contain at least one byte
func (builder *txDataBuilder) ArgBytesList(list [][]byte) TxDataBuilder {
for _, arg := range list {
builder.addArgBytes(arg)
}

return builder
}

// ToDataString returns the formatted data string ready to be used in a transaction call
func (builder *txDataBuilder) ToDataString() (string, error) {
if builder.err != nil {
return "", builder.err
}

var parts []string
if len(builder.function) > 0 {
parts = append(parts, builder.function)
}

parts = append(parts, builder.args...)

return strings.Join(parts, dataSeparator), nil
}

// ToDataBytes returns the formatted data string ready to be used in a transaction call as bytes
func (builder *txDataBuilder) ToDataBytes() ([]byte, error) {
dataField, err := builder.ToDataString()
if err != nil {
return nil, err
}

return []byte(dataField), err
}

// IsInterfaceNil returns true if there is no value under the interface
func (builder *txDataBuilder) IsInterfaceNil() bool {
return builder == nil
}
Loading