ethereum

package
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 19 Imported by: 3

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddressFromPubKey added in v0.2.7

func AddressFromPubKey(pubKey []byte) bytes.Address

AddressFromPubKey returns the Address for the given public key.

func HexToECDSABytes added in v0.2.5

func HexToECDSABytes(hexString string) ([]byte, error)

HexToECDSABytes returns the ECDSA []byte format of the given hex string representation of a Private Key.

Example
package main

import (
	"fmt"

	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

var privateKey []byte

var err error

func main() {
	privateKey, err = ethereum.HexToECDSABytes("91e4a13e5a30ad353cdf5ea7bb909dfdf967122e3b43e331ad947b68a3899b2c")
	if err != nil {
		return
	}

	fmt.Println("success")
}
Output:

success

func ParseSignature added in v0.2.7

func ParseSignature(signature string) ([]byte, error)

ParseSignature parses a hex string signature to bytes

func PublicKeyFromPrivate added in v0.2.5

func PublicKeyFromPrivate(privateKey []byte) ([]byte, error)

PublicKeyFromPrivate returns the uncompressed ECDSA public key from the given privateKey,

func PublicKeyFromSignedMessage added in v0.2.5

func PublicKeyFromSignedMessage(message []byte, signature []byte) ([]byte, error)

PublicKeyFromSignedMessage returns the ECDSA public key used to sign the given message to the given signature.

func SignMessage

func SignMessage(message, privKey []byte) ([]byte, error)

SignMessage returns an ECDSA signed message

Example
privateKey, err = ethereum.HexToECDSABytes("91e4a13e5a30ad353cdf5ea7bb909dfdf967122e3b43e331ad947b68a3899b2c")
if err != nil {
	return
}

publicKey, err = ethereum.PublicKeyFromPrivate(privateKey)
if err != nil {
	return
}

signature, err := ethereum.SignMessage([]byte("hello world"), privateKey)
if err != nil {
	return
}

err = ethereum.VerifySignature([]byte("hello world"), publicKey, signature)
if err != nil {
	return
}

fmt.Println("success")
Output:

success

func ToEthJsMessage added in v0.2.7

func ToEthJsMessage(message string) []byte

ToEthJsMessage returns the signed message in the JS Ethereum message format as bytes.

func VerifySignature

func VerifySignature(message, publicKey, signature []byte) error

VerifySignature checks the signed ECDSA message with the original message to verify if the message was signed by the given public key

Types

type Block

type Block struct {
	// contains filtered or unexported fields
}

Block defines wrappers for a block retrieved by the client.

func (*Block) Number

func (b *Block) Number() (*big.Int, error)

Number returns the *big.Int value of the block number

Example
package main

import (
	"fmt"
	"math/big"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:        4,
		BlockByNumber: 5,
		BlockNumber:   big.NewInt(20),
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	blockNumber, err := block.Number()
	if err != nil {
		return
	}

	fmt.Printf("%d\n", blockNumber)
}
Output:

20

func (*Block) Transaction

func (b *Block) Transaction(hash []byte) (*Transaction, error)

Transaction returns transaction from block with the given transaction hash.

Transaction hash is 32 bytes, if inputted hash is longer than 32 bytes hash will be trimmed.

Example
package main

import (
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

var tx *ethereum.Transaction

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:           4,
		BlockByNumber:    5,
		BlockTransaction: 6,
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	// Mocking transaction hash
	txHash := make([]byte, 32)
	_, err = rand.Read(txHash)
	if err != nil {
		return
	}

	tx, err = block.Transaction(txHash)
	if err != nil {
		return
	}

	fmt.Println("success")
}
Output:

success

func (*Block) Transactions

func (b *Block) Transactions() ([]*Transaction, error)

Transactions returns all transactions from the given block.

Example
package main

import (
	"fmt"
	"math/big"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:            4,
		BlockByNumber:     5,
		BlockTransactions: []uint32{1, 2, 3},
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	txs, err := block.Transactions()
	if err != nil {
		return
	}

	fmt.Println(len(txs))
}
Output:

3

type Client

type Client uint32

Client defines typed wrappers for the Ethereum RPC API.

func New

func New(url string, ops ...rpc.ClientOption) (Client, error)

New connects a client to the given rpc URL.

Example
package main

import (
	"fmt"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client: 5,
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	fmt.Printf("%d\n", client)
}
Output:

5

func (Client) BlockByNumber

func (c Client) BlockByNumber(blockNumber *big.Int) (*Block, error)

BlockByNumber returns a block from the current canonical chain. If number is nil, the latest known block is returned.

Example
package main

import (
	"fmt"
	"math/big"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

var block *ethereum.Block

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:        4,
		BlockByNumber: 5,
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err = client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	fmt.Println("success")
}
Output:

success

func (Client) Close

func (c Client) Close()

Close will close the Ethereum Client

func (Client) CurrentBlockNumber

func (c Client) CurrentBlockNumber() (number uint64, err error)

CurrentBlockNumber returns the most recent block number.

Example
package main

import (
	"fmt"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:             4,
		CurrentBlockNumber: 5,
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	blockNumber, err := client.CurrentBlockNumber()
	if err != nil {
		return
	}

	fmt.Printf("%d\n", blockNumber)
}
Output:

5

func (Client) CurrentChainID added in v0.3.1

func (c Client) CurrentChainID() (*big.Int, error)

CurrentChainID retrieves the current chain ID for transaction replay protection.

Example
package main

import (
	"fmt"
	"math/big"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:         4,
		CurrentChainId: big.NewInt(5),
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	chainId, err := client.CurrentChainID()
	if err != nil {
		return
	}

	fmt.Printf("%d\n", chainId)
}
Output:

5

func (Client) DeployContract

func (c Client) DeployContract(abi, byteCode io.Reader, chainID *big.Int, privateKey []byte) (*Contract, *Transaction, error)

DeployContract method deploys the contract on given chain,returns low level contract interface through which calls and transactions may be made through.

Example
// Mocking the calls to the vm for usage in tests and playground
mockData := ethereumSym.MockData{
	Client:          4,
	ContractAddress: "address",
	Contract: map[string]ethereumSym.MockContractMethod{
		"fakeMethod": {
			Inputs:  []interface{}{big.NewInt(5)},
			Outputs: []interface{}{big.NewInt(6)},
		},
	},
	ContractTransactionId: 2,
	ContractId:            3,
}
mockData.Mock()

// Creates new client from given RPC url, this is not a real rpc url.
client, err := ethereum.New("https://testRPC.url")
if err != nil {
	return
}

// Mocking abi data
abiRawData := make([]byte, 1024)
rand.Read(abiRawData)

// Mocking byte code
byteCodeData := make([]byte, 1024)
rand.Read(byteCodeData)

chainId := big.NewInt(5)

contract, tx, err = client.DeployContract(bytes.NewReader(abiRawData), bytes.NewReader(byteCodeData), chainId, []byte("private key"))
if err != nil {
	return
}

fmt.Println("success")
Output:

success

func (Client) NewBoundContract

func (c Client) NewBoundContract(abi io.Reader, address string) (*Contract, error)

NewBoundContract method creates a low level contract interface through which calls and transactions may be made through.

Example
package main

import (
	"bytes"
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

var contract *ethereum.Contract

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:          4,
		ContractAddress: "address",
		Contract: map[string]ethereumSym.MockContractMethod{
			"fakeMethod": {
				Inputs:  []interface{}{big.NewInt(5)},
				Outputs: []interface{}{big.NewInt(6)},
			},
		},
		ContractTransactionId: 2,
		ContractId:            3,
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	// Mocking abi data
	abiRawData := make([]byte, 1024)
	rand.Read(abiRawData)

	contract, err = client.NewBoundContract(bytes.NewReader(abiRawData), "address")
	if err != nil {
		return
	}

	fmt.Println("success")
}
Output:

success

type Contract

type Contract struct {
	// contains filtered or unexported fields
}

Contract defines typed wrappers for a contract with given abi.

func (*Contract) Address

func (c *Contract) Address() string

Address returns the wallet address of the Contract

func (*Contract) Event added in v0.3.8

func (c *Contract) Event(name string) (*Event, error)

func (*Contract) Method

func (c *Contract) Method(name string) (*ContractMethod, error)

Method returns the contract method with the corresponding inputted name.

Example
package main

import (
	"bytes"
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:          4,
		ContractAddress: "address",
		Contract: map[string]ethereumSym.MockContractMethod{
			"fakeMethod": {
				Inputs:  []interface{}{big.NewInt(5)},
				Outputs: []interface{}{big.NewInt(6)},
			},
		},
		ContractTransactionId: 2,
		ContractId:            3,
	}
	mockData.Mock()

	// Creates new client from given RPC url, this is not a real rpc url.
	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	// Mocking abi data
	abiRawData := make([]byte, 1024)
	rand.Read(abiRawData)

	contract, err := client.NewBoundContract(bytes.NewReader(abiRawData), "address")
	if err != nil {
		return
	}

	fakeMethod, err := contract.Method("fakeMethod")
	if err != nil {
		return
	}

	fmt.Println(fakeMethod.Name())
}
Output:

fakeMethod

func (*Contract) Methods

func (c *Contract) Methods() []*ContractMethod

Methods lists the available methods for within the given contract

Example
package main

import (
	"bytes"
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:          4,
		ContractAddress: "address",
		Contract: map[string]ethereumSym.MockContractMethod{
			"fakeMethod": {
				Inputs:  []interface{}{big.NewInt(5)},
				Outputs: []interface{}{big.NewInt(6)},
			},
		},
		ContractTransactionId: 2,
		ContractId:            3,
	}
	mockData.Mock()

	// Creates new client from given RPC url, this is not a real rpc url.
	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	// Mocking abi data
	abiRawData := make([]byte, 1024)
	rand.Read(abiRawData)

	contract, err := client.NewBoundContract(bytes.NewReader(abiRawData), "address")
	if err != nil {
		return
	}

	fmt.Println(len(contract.Methods()))
}
Output:

1

type ContractMethod

type ContractMethod struct {
	// contains filtered or unexported fields
}

ContractMethod defines the contract method and wraps the methods for the contract method.

func (*ContractMethod) Call

func (c *ContractMethod) Call(inputParameters ...interface{}) ([]interface{}, error)

Call invokes the (constant) contract method with params as input values

Example
package main

import (
	"bytes"
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:          4,
		ContractAddress: "address",
		Contract: map[string]ethereumSym.MockContractMethod{
			"fakeMethod": {
				Inputs:  []interface{}{big.NewInt(5)},
				Outputs: []interface{}{big.NewInt(6)},
			},
		},
		ContractTransactionId: 2,
		ContractId:            3,
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	// Mocking abi data
	abiRawData := make([]byte, 1024)
	rand.Read(abiRawData)

	contract, err := client.NewBoundContract(bytes.NewReader(abiRawData), "address")
	if err != nil {
		return
	}

	fakeMethod, err := contract.Method("fakeMethod")
	if err != nil {
		return
	}

	outputs, err := fakeMethod.Call(big.NewInt(5))
	if err != nil || len(outputs) != 1 {
		return
	}

	fmt.Println(outputs[0])
}
Output:

6

func (*ContractMethod) CallJSON added in v0.3.8

func (c *ContractMethod) CallJSON(json []byte) ([]interface{}, error)

func (*ContractMethod) Name

func (c *ContractMethod) Name() string

Name returns the name of the method.

func (*ContractMethod) Transact

func (c *ContractMethod) Transact(chainID *big.Int, privateKey []byte, inputParameters ...interface{}) (*Transaction, error)

Transact invokes the (paid) contract method with params as input values. If chain id is nil, then current chain Id is used.

Example
// Mocking the calls to the vm for usage in tests and playground
mockData := ethereumSym.MockData{
	Client:          4,
	ContractAddress: "address",
	Contract: map[string]ethereumSym.MockContractMethod{
		"fakeMethod": {
			Inputs:  []interface{}{big.NewInt(5)},
			Outputs: []interface{}{big.NewInt(6)},
		},
	},
	ContractTransactionId: 2,
	ContractId:            3,
}
mockData.Mock()

// Creates new client from given RPC url, this is not a real rpc url.
client, err := ethereum.New("https://testRPC.url")
if err != nil {
	return
}

// Mocking abi data
abiRawData := make([]byte, 1024)
rand.Read(abiRawData)

contract, err := client.NewBoundContract(bytes.NewReader(abiRawData), "address")
if err != nil {
	return
}

fakeMethod, err := contract.Method("fakeMethod")
if err != nil {
	return
}

chainId := big.NewInt(10)

tx, err = fakeMethod.Transact(chainId, []byte("private_key"), big.NewInt(5))
if err != nil {
	return
}

fmt.Println("success")
Output:

success

func (*ContractMethod) TransactJSON added in v0.3.8

func (c *ContractMethod) TransactJSON(chainID *big.Int, privateKey []byte, json []byte) (*Transaction, error)

type Event added in v0.3.8

type Event struct {
	// contains filtered or unexported fields
}

Event defines the Ethereum event and wraps the methods for the event.

func (Event) Subscribe added in v0.3.8

func (e Event) Subscribe(channel string, ttl time.Duration) error

type Transaction

type Transaction struct {
	// contains filtered or unexported fields
}

Transaction defines wrappers for a transaction retrieved by the client.

func (*Transaction) Chain

func (t *Transaction) Chain() (*big.Int, error)

Chain returns the EIP155 chain ID of the transaction. The return value will always be non-nil. For legacy transactions which are not replay-protected, the return value is zero.

Example
package main

import (
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:           4,
		BlockByNumber:    5,
		BlockTransaction: 6,
		TransactionBytes: big.NewInt(7).Bytes(),
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	// Mocking transaction hash
	txHash := make([]byte, 32)
	_, err = rand.Read(txHash)
	if err != nil {
		return
	}

	tx, err := block.Transaction(txHash)
	if err != nil {
		return
	}

	chain, err := tx.Chain()
	if err != nil {
		return
	}

	fmt.Println(chain)
}
Output:

7

func (*Transaction) Data

func (t *Transaction) Data() ([]byte, error)

Data returns the input data of the transaction.

Example
package main

import (
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:           4,
		BlockByNumber:    5,
		BlockTransaction: 6,
		TransactionBytes: []byte("Hello World"),
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	// Mocking transaction hash
	txHash := make([]byte, 32)
	_, err = rand.Read(txHash)
	if err != nil {
		return
	}

	tx, err := block.Transaction(txHash)
	if err != nil {
		return
	}

	data, err := tx.Data()
	if err != nil {
		return
	}

	fmt.Println(string(data))
}
Output:

Hello World

func (*Transaction) Gas

func (t *Transaction) Gas() (uint64, error)

Gas returns the gas limit of the transaction.

Example
package main

import (
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:           4,
		BlockByNumber:    5,
		BlockTransaction: 6,
		TransactionU64:   7,
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	// Mocking transaction hash
	txHash := make([]byte, 32)
	_, err = rand.Read(txHash)
	if err != nil {
		return
	}

	tx, err := block.Transaction(txHash)
	if err != nil {
		return
	}

	gas, err := tx.Gas()
	if err != nil {
		return
	}

	fmt.Println(gas)
}
Output:

7

func (*Transaction) GasFeeCap

func (t *Transaction) GasFeeCap() (*big.Int, error)

GasFeeCap returns the fee cap per gas of the transaction.

Example
package main

import (
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:           4,
		BlockByNumber:    5,
		BlockTransaction: 6,
		TransactionBytes: big.NewInt(7).Bytes(),
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	// Mocking transaction hash
	txHash := make([]byte, 32)
	_, err = rand.Read(txHash)
	if err != nil {
		return
	}

	tx, err := block.Transaction(txHash)
	if err != nil {
		return
	}

	gasFeeCap, err := tx.GasFeeCap()
	if err != nil {
		return
	}

	fmt.Println(gasFeeCap)
}
Output:

7

func (*Transaction) GasPrice

func (t *Transaction) GasPrice() (*big.Int, error)

GasPrice returns the gas price of the transaction

Example
package main

import (
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:           4,
		BlockByNumber:    5,
		BlockTransaction: 6,
		TransactionBytes: big.NewInt(7).Bytes(),
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	// Mocking transaction hash
	txHash := make([]byte, 32)
	_, err = rand.Read(txHash)
	if err != nil {
		return
	}

	tx, err := block.Transaction(txHash)
	if err != nil {
		return
	}

	gasPrice, err := tx.GasPrice()
	if err != nil {
		return
	}

	fmt.Println(gasPrice)
}
Output:

7

func (*Transaction) GasTipCap

func (t *Transaction) GasTipCap() (*big.Int, error)

GasTipCap returns the gasTipCap per gas of the transaction.

Example
package main

import (
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:           4,
		BlockByNumber:    5,
		BlockTransaction: 6,
		TransactionBytes: big.NewInt(7).Bytes(),
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	// Mocking transaction hash
	txHash := make([]byte, 32)
	_, err = rand.Read(txHash)
	if err != nil {
		return
	}

	tx, err := block.Transaction(txHash)
	if err != nil {
		return
	}

	gasTipCap, err := tx.GasTipCap()
	if err != nil {
		return
	}

	fmt.Println(gasTipCap)
}
Output:

7

func (*Transaction) Hash

func (t *Transaction) Hash() ([]byte, error)

Hash returns the transaction hash.

Example
package main

import (
	"bytes"
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking transaction hash
	txHash := make([]byte, 32)
	_, err := rand.Read(txHash)
	if err != nil {
		return
	}

	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:           4,
		BlockByNumber:    5,
		BlockTransaction: 6,
		TransactionBytes: txHash,
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	tx, err := block.Transaction(txHash)
	if err != nil {
		return
	}

	hash, err := tx.Hash()
	if err != nil || !bytes.Equal(hash, txHash) {
		return
	}

	fmt.Println("success")
}
Output:

success

func (*Transaction) Nonce

func (t *Transaction) Nonce() (uint64, error)

Nonce returns the sender account nonce of the transaction.

Example
package main

import (
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:           4,
		BlockByNumber:    5,
		BlockTransaction: 6,
		TransactionU64:   7,
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	// Mocking transaction hash
	txHash := make([]byte, 32)
	_, err = rand.Read(txHash)
	if err != nil {
		return
	}

	tx, err := block.Transaction(txHash)
	if err != nil {
		return
	}

	nonce, err := tx.Nonce()
	if err != nil {
		return
	}

	fmt.Println(nonce)
}
Output:

7

func (*Transaction) RawSignatures

func (t *Transaction) RawSignatures() (rawSignatures, error)

RawSignatures returns the V, R, S signature values of the transaction. The return values should not be modified by the caller.

Example
package main

import (
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:           4,
		BlockByNumber:    5,
		BlockTransaction: 6,
		VSig:             big.NewInt(7),
		RSig:             big.NewInt(8),
		SSig:             big.NewInt(9),
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	// Mocking transaction hash
	txHash := make([]byte, 32)
	_, err = rand.Read(txHash)
	if err != nil {
		return
	}

	tx, err := block.Transaction(txHash)
	if err != nil {
		return
	}

	rawSignatures, err := tx.RawSignatures()
	if err != nil {
		return
	}

	fmt.Println(rawSignatures.VSig, rawSignatures.RSig, rawSignatures.SSig)
}
Output:

7 8 9

func (*Transaction) Send

func (t *Transaction) Send() (err error)

Send injects a signed transaction into the pending pool for execution.

Example
package main

import (
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:           4,
		BlockByNumber:    5,
		BlockTransaction: 6,
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	// Mocking transaction hash
	txHash := make([]byte, 32)
	_, err = rand.Read(txHash)
	if err != nil {
		return
	}

	tx, err := block.Transaction(txHash)
	if err != nil {
		return
	}

	err = tx.Send()
	if err != nil {
		return
	}

	fmt.Println("success")
}
Output:

success

func (*Transaction) ToAddress

func (t *Transaction) ToAddress() ([]byte, error)

ToAddress returns the recipient address of the transaction.

func (*Transaction) Value

func (t *Transaction) Value() (*big.Int, error)

Value returns the ether amount of the transaction.

Example
package main

import (
	"fmt"
	"math/big"
	"math/rand"

	ethereumSym "github.com/taubyte/go-sdk-symbols/ethereum/client"
	ethereum "github.com/taubyte/go-sdk/ethereum/client"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	mockData := ethereumSym.MockData{
		Client:           4,
		BlockByNumber:    5,
		BlockTransaction: 6,
		TransactionBytes: big.NewInt(7).Bytes(),
	}
	mockData.Mock()

	client, err := ethereum.New("https://testRPC.url")
	if err != nil {
		return
	}

	block, err := client.BlockByNumber(big.NewInt(20))
	if err != nil {
		return
	}

	// Mocking transaction hash
	txHash := make([]byte, 32)
	_, err = rand.Read(txHash)
	if err != nil {
		return
	}

	tx, err := block.Transaction(txHash)
	if err != nil {
		return
	}

	value, err := tx.Value()
	if err != nil {
		return
	}

	fmt.Println(value)
}
Output:

7

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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