klayfee

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2019 License: MIT Imports: 12 Imported by: 1

README

klayfee-go GoDoc Go Report Card Build Status

This package is an official Go client for a fee delegation service for Klaytn network, Klayfee. Please note that this is the first public preview, API might change in future.

Other packages

Installation
$ go get github.com/kompose-app/klayfee-go
Example
package main

import (
    "context"
    "log"

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

    "github.com/kompose-app/klayfw"
    klayfee "github.com/kompose-app/klayfee-go"
)

func main() {
    var rawTxBytes []byte
    // prepare and sign your klaytn Tx, e.g. TxTypeFeeDelegatedValueTransfer
    rawTxBytes = makeRawTx(context.Background())

    baobabClient, _ := klayfee.NewClient(klayfee.BaobabEndpoint, "")
    receipt, err := baobabClient.SendRawTx(context.Background(), rawTxBytes)

    if err != nil {
        log.Fatalln(err)
    }
    log.Printf("status: %d, txhash: %s", receipt.Status, receipt.TxHash.Hex())
}

func makeRawTx(ctx context.Context) []byte {
    klayClient, _ := client.Dial("https://api.baobab.klaytn.net:8651")
    senderPrivKey, _ := crypto.HexToECDSA("5f6b9db3020b7e090d6488e78f9c8ca1640c5b66fc1b003f5df328e721362ca2")
    senderAddress := crypto.PubkeyToAddress(senderPrivKey.PublicKey)
    nonce, _ := klayClient.PendingNonceAt(ctx, senderAddress)

    rawTx, _ := types.NewTransactionWithMap(
        types.TxTypeFeeDelegatedValueTransfer,
        map[types.TxValueKeyType]interface{}{
            types.TxValueKeyNonce:    nonce,
            types.TxValueKeyTo:       common.HexToAddress("0x993cc8f5b6023ac3ea04d2022fa1ef46a0ebf4ef"),
            types.TxValueKeyAmount:   klayfw.ToPeb(0.0001).ToInt(),
            types.TxValueKeyGasLimit: uint64(31000),
            types.TxValueKeyGasPrice: klayfw.Ston(25).ToInt(),
            types.TxValueKeyFrom:     senderAddress,
            types.TxValueKeyFeePayer: senderAddress, // workaround
        },
    )

    chainID, _ := klayClient.ChainID(ctx)
    signer := types.NewEIP155Signer(chainID)
    _ = rawTx.Sign(signer, senderPrivKey)
    _ = rawTx.SetFeePayerSignatures(types.TxSignatures{{}}) // workaround
    rawTxBytes, _ := rlp.EncodeToBytes(rawTx)

    return rawTxBytes
}
License

MIT

Documentation

Index

Constants

View Source
const (
	// BaobabEndpoint is used to send transactions to Baobab network, that is Klaytn's testnet.
	// The API key is not required and it can be used for free to test your application before prod.
	BaobabEndpoint = "https://baobab.klayfee.com"
	// CypressEndpoint is used to send transactions to Cypress network, that is Klaytn's mainnet.
	// The API key is required for that endpoint and the transaction fee costs will be paid off your balance.
	CypressEndpoint = "https://cypress.klayfee.com"
)

Variables

View Source
var (
	// ErrUnauthorized is returned when client has detected an authorization error.
	ErrUnauthorized = errors.New("unauthorized error: wrong API key or Endpoint")
	// ErrForbidden is retuned when server declined the request, probably based on low balance.
	ErrForbidden = errors.New("forbidden error: please check you API key and spendable gas balance")
	// ErrBadRequest is returned when server indicated that the request is missing important fields or is forged.
	ErrBadRequest = errors.New("bad request error: client sent a malformed request")
)

Functions

This section is empty.

Types

type FeeDelegationClient

type FeeDelegationClient interface {
	// SendRawTx sends signed raw transaction bytes to the fee delegation server.
	//
	// If succeed, that transaction will be submitted into blockchain, but gas fees
	// are charged regardless that. Refer to klaytn economics to decide how to work with gas.
	SendRawTx(ctx context.Context, rawTx []byte) (*Receipt, error)

	// GasBalance returns maximum spendable gas allocation of your account.
	//
	// When gas spendings are counted towards that allocation, we use GasPrice * GasUsed formula,
	// but when transaction is being submitted, it is evaluated as GasPrice * GasLimit, so
	// ensure that with your variables in signed transaction you have enough allocation beforehand.
	GasBalance(ctx context.Context) (*big.Int, error)
	// SetHTTPClient allows to set a custom HTTP client.
	SetHTTPClient(cli *http.Client)
}

FeeDelegationClient is an interface of basic client of a fee delegation service. This is our first release, the API might change in the future.

func NewClient

func NewClient(klayfeeEndpoint string, klayfeeAPIKey string) (FeeDelegationClient, error)

NewClient constructs a new FeeDelegationClient for the given endpoint, see endpoint description to choose the correct one. Or specify your own URL.

type Receipt

type Receipt struct {
	Status       int         `json:"status"`
	GasUsed      int         `json:"gasUsed"`
	GasPrice     *big.Int    `json:"gasPrice"`
	GasTotalCost *big.Int    `json:"gasTotalCost"`
	TxHash       common.Hash `json:"txHash"`
	TxHashSender common.Hash `json:"txHashSender"`
}

Receipt describes transaction results and the total cost in Pebs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL