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

WIP: klaytn service #59

Draft
wants to merge 2 commits into
base: develop
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
114 changes: 114 additions & 0 deletions account/klaytn/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package klaytn

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

"github.com/klaytn/klaytn/blockchain/types"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/crypto"
"github.com/klaytn/klaytn/ser/rlp"
)

var (
errNoFromAddr = errors.New("from addr must be set")
errNoToAddr = errors.New("to addr must be set")
)

type TransactionBuilder struct {
signer types.Signer
values map[types.TxValueKeyType]interface{}
fromPrvKey []byte
feePayerKey []byte
}

func NewTransactionBuilder(signer types.Signer) *TransactionBuilder {
return &TransactionBuilder{
signer: signer,
}
}

func (tb *TransactionBuilder) SetNonce(n uint64) *TransactionBuilder {
tb.values[types.TxValueKeyNonce] = n
return tb
}

func (tb *TransactionBuilder) SetAmount(amount int64) *TransactionBuilder {
tb.values[types.TxValueKeyAmount] = big.NewInt(amount)
return tb
}
func (tb *TransactionBuilder) SetFrom(addr string, prvKey []byte) *TransactionBuilder {
tb.values[types.TxValueKeyFrom] = common.HexToAddress(addr)
tb.fromPrvKey = prvKey
return tb
}

func (tb *TransactionBuilder) SetTo(addr string) *TransactionBuilder {
tb.values[types.TxValueKeyTo] = common.HexToAddress(addr)
return tb
}

func (tb *TransactionBuilder) SetGasLimit(gasLimit uint64) *TransactionBuilder {
tb.values[types.TxValueKeyGasLimit] = gasLimit
return tb
}

func (tb *TransactionBuilder) SetGasPrice(gasPrice int64) *TransactionBuilder {
tb.values[types.TxValueKeyGasPrice] = big.NewInt(gasPrice)
return tb
}

func (tb *TransactionBuilder) SetFeePayer(addr string, prvKey []byte) *TransactionBuilder {
tb.values[types.TxValueKeyTo] = common.HexToAddress(addr)
tb.feePayerKey = prvKey
return tb
}

func (tb *TransactionBuilder) Build() (string, error) {
if len(tb.fromPrvKey) == 0 {
return "", errNoFromAddr
}

if _, ok := tb.values[types.TxValueKeyTo]; !ok {
return "", errNoToAddr
}

txType := types.TxTypeValueTransfer
if _, ok := tb.values[types.TxValueKeyTo]; ok {
txType = types.TxTypeFeeDelegatedValueTransfer
}

tx, err := types.NewTransactionWithMap(txType, tb.values)
if err != nil {
return "", err
}

fromPrvKey, err := crypto.ToECDSA(tb.fromPrvKey)
if err != nil {
return "", err
}

err = tx.Sign(tb.signer, fromPrvKey)
if err != nil {
return "", err
}

if txType == types.TxTypeFeeDelegatedValueTransfer {
feePayerPrvKey, err := crypto.ToECDSA(tb.feePayerKey)
if err != nil {
return "", err
}
err = tx.SignFeePayer(tb.signer, feePayerPrvKey)
if err != nil {
return "", err
}
}

b, err := rlp.EncodeToBytes(tx)
if err != nil {
return "", err
}

return hex.EncodeToString(b), nil
}
59 changes: 59 additions & 0 deletions account/klaytn/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package klaytn

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

"github.com/klaytn/klaytn/blockchain/types"
"github.com/klaytn/klaytn/client"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/ser/rlp"
)

type Client interface {
BalanceAt(address string) (*big.Int, error)
NonceAt(address string) (uint64, error)
SendTransaction(rawTransaction string) (string, error)
GasPrice() (*big.Int, error)
}

type KenClient struct {
client *client.Client
}

func (k *KenClient) BalanceAt(address string) (*big.Int, error) {
account := common.HexToAddress(address)
balance, err := k.client.BalanceAt(context.Background(), account, nil)
if err != nil {
return &big.Int{}, err
}

return balance, nil
}

func (k *KenClient) NonceAt(address string) (uint64, error) {
account := common.HexToAddress(address)
return k.client.NonceAt(context.Background(), account, nil)
}

func (k *KenClient) SendTransaction(rawTransaction string) (string, error) {
rawTxBytes, err := hex.DecodeString(rawTransaction)

tx := new(types.Transaction)
err = rlp.DecodeBytes(rawTxBytes, &tx)
if err != nil {
return "", err
}

err = k.client.SendTransaction(context.Background(), tx)
if err != nil {
return "", err
}

return tx.Hash().Hex(), nil
}

func (k *KenClient) GasPrice() (*big.Int, error) {
return k.client.SuggestGasPrice(context.Background())
}
32 changes: 32 additions & 0 deletions account/klaytn/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package klaytn

import (
"math/big"

"github.com/DE-labtory/zulu/types"
)

type Params struct {
NodeUrl string
ChainId *big.Int
GasLimit uint64
}

// chainId from https://github.com/klaytn/caver-java/blob/ea087d85ca53f90d627e67a6816c78dc72d887d8/core/src/main/java/com/klaytn/caver/utils/ChainId.java
var (
CypressParams = Params{
NodeUrl: "https://api.cypress.klaytn.net:8651",
ChainId: big.NewInt(8217),
GasLimit: 23000,
}
BaobabParams = Params{
NodeUrl: "https://api.baobab.klaytn.net:8651",
ChainId: big.NewInt(1001),
GasLimit: 23000,
}
)

var Supplier = map[types.Network]Params{
types.Cypress: CypressParams,
types.Baobab: BaobabParams,
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/golang/protobuf v1.4.2 // indirect
github.com/google/go-cmp v0.5.1 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/klaytn/klaytn v1.5.1
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
Expand Down
Loading