Skip to content

Commit

Permalink
refactor(client): rename to thorest
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Nov 5, 2024
1 parent dd23af8 commit c2a22fe
Show file tree
Hide file tree
Showing 39 changed files with 314 additions and 314 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.59.1
version: v1.60.2
args: --timeout=30m --config=.golangci.yml

- name: Generate builtins
Expand Down
22 changes: 11 additions & 11 deletions accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@ package accounts
import (
"math/big"

"github.com/darrenvechain/thorgo/api"
"github.com/darrenvechain/thorgo/crypto/tx"
"github.com/darrenvechain/thorgo/thorest"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)

type Visitor struct {
client *api.Client
client *thorest.Client
account common.Address
revision *api.Revision
revision *thorest.Revision
}

func New(c *api.Client, account common.Address) *Visitor {
func New(c *thorest.Client, account common.Address) *Visitor {
return &Visitor{client: c, account: account}
}

// Revision sets the optional revision for the API calls.
func (a *Visitor) Revision(revision api.Revision) *Visitor {
func (a *Visitor) Revision(revision thorest.Revision) *Visitor {
a.revision = &revision
return a
}

// Get fetches the account information for the given address.
func (a *Visitor) Get() (*api.Account, error) {
func (a *Visitor) Get() (*thorest.Account, error) {
if a.revision == nil {
return a.client.Account(a.account)
}
return a.client.AccountAt(a.account, *a.revision)
}

// Code fetches the byte code of the contract at the given address.
func (a *Visitor) Code() (*api.AccountCode, error) {
func (a *Visitor) Code() (*thorest.AccountCode, error) {
if a.revision == nil {
return a.client.AccountCode(a.account)
}
Expand All @@ -43,7 +43,7 @@ func (a *Visitor) Code() (*api.AccountCode, error) {
}

// Storage fetches the storage value for the given key.
func (a *Visitor) Storage(key common.Hash) (*api.AccountStorage, error) {
func (a *Visitor) Storage(key common.Hash) (*thorest.AccountStorage, error) {
if a.revision == nil {
return a.client.AccountStorage(a.account, key)
}
Expand All @@ -52,14 +52,14 @@ func (a *Visitor) Storage(key common.Hash) (*api.AccountStorage, error) {
}

// Call executes a read-only contract call.
func (a *Visitor) Call(calldata []byte) (*api.InspectResponse, error) {
func (a *Visitor) Call(calldata []byte) (*thorest.InspectResponse, error) {
clause := tx.NewClause(&a.account).WithData(calldata).WithValue(big.NewInt(0))

request := api.InspectRequest{
request := thorest.InspectRequest{
Clauses: []*tx.Clause{clause},
}
var (
inspection []api.InspectResponse
inspection []thorest.InspectResponse
err error
)

Expand Down
10 changes: 5 additions & 5 deletions accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import (

"github.com/darrenvechain/thorgo"
"github.com/darrenvechain/thorgo/accounts"
"github.com/darrenvechain/thorgo/api"
"github.com/darrenvechain/thorgo/builtins"
"github.com/darrenvechain/thorgo/internal/testcontainer"
"github.com/darrenvechain/thorgo/solo"
"github.com/darrenvechain/thorgo/thorest"
"github.com/darrenvechain/thorgo/txmanager"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)

var (
thorClient *api.Client
thorClient *thorest.Client
thor *thorgo.Thor
vthoContract *builtins.Energy
vthoRaw *accounts.Contract
Expand Down Expand Up @@ -49,7 +49,7 @@ func TestGetAccount(t *testing.T) {
// TestGetAccountForRevision fetches a thor solo account for the genesis block
// and checks if the balance and energy are greater than 0
func TestGetAccountForRevision(t *testing.T) {
acc, err := accounts.New(thorClient, account1.Address()).Revision(api.RevisionID(solo.GenesisID())).Get()
acc, err := accounts.New(thorClient, account1.Address()).Revision(thorest.RevisionID(solo.GenesisID())).Get()

assert.NoError(t, err, "Account.httpGet should not return an error")
assert.NotNil(t, acc, "Account.httpGet should return an account")
Expand All @@ -71,7 +71,7 @@ func TestGetCode(t *testing.T) {
// TestGetCodeForRevision fetches the code of the VTHO contract for the genesis block
func TestGetCodeForRevision(t *testing.T) {
vtho, err := accounts.New(thorClient, vthoContract.Address()).
Revision(api.RevisionID(solo.GenesisID())).
Revision(thorest.RevisionID(solo.GenesisID())).
Code()

assert.NoError(t, err, "Account.Code should not return an error")
Expand All @@ -91,7 +91,7 @@ func TestGetStorage(t *testing.T) {
// TestGetStorageForRevision fetches a storage position of the VTHO contract for the genesis block
func TestGetStorageForRevision(t *testing.T) {
storage, err := accounts.New(thorClient, vthoContract.Address()).
Revision(api.RevisionID(solo.GenesisID())).
Revision(thorest.RevisionID(solo.GenesisID())).
Storage(common.Hash{})

assert.NoError(t, err, "Account.Storage should not return an error")
Expand Down
28 changes: 14 additions & 14 deletions accounts/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
"fmt"
"math/big"

"github.com/darrenvechain/thorgo/api"
"github.com/darrenvechain/thorgo/crypto/tx"
"github.com/darrenvechain/thorgo/thorest"
"github.com/darrenvechain/thorgo/transactions"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)

// Contract is a generic representation of a smart contract.
type Contract struct {
client *api.Client
client *thorest.Client
ABI *abi.ABI
Address common.Address
}

// NewContract creates a new contract instance.
func NewContract(
client *api.Client,
client *thorest.Client,
address common.Address,
abi *abi.ABI,
) *Contract {
Expand All @@ -31,11 +31,11 @@ func NewContract(

// Call executes a read-only contract call.
func (c *Contract) Call(method string, results *[]interface{}, args ...interface{}) error {
return c.CallAt(api.RevisionBest(), method, results, args...)
return c.CallAt(thorest.RevisionBest(), method, results, args...)
}

// CallAt executes a read-only contract call at a specific revision.
func (c *Contract) CallAt(revision api.Revision, method string, results *[]interface{}, args ...interface{}) error {
func (c *Contract) CallAt(revision thorest.Revision, method string, results *[]interface{}, args ...interface{}) error {
if results == nil {
results = new([]interface{})
}
Expand All @@ -44,7 +44,7 @@ func (c *Contract) CallAt(revision api.Revision, method string, results *[]inter
return fmt.Errorf("failed to pack method %s: %w", method, err)
}
clause := tx.NewClause(&c.Address).WithData(packed).WithValue(big.NewInt(0))
request := api.InspectRequest{
request := thorest.InspectRequest{
Clauses: []*tx.Clause{clause},
}
response, err := c.client.InspectAt(request, revision)
Expand Down Expand Up @@ -145,12 +145,12 @@ func (c *Contract) SendWithVET(manager TxManager, vet *big.Int, method string, a
// criteria, err := contract.EventCriteria("Transfer", nil, &to)
//
// Returns an EventCriteria object and any error encountered.
func (c *Contract) EventCriteria(name string, matchers ...interface{}) (api.EventCriteria, error) {
func (c *Contract) EventCriteria(name string, matchers ...interface{}) (thorest.EventCriteria, error) {
ev, ok := c.ABI.Events[name]
if !ok {
return api.EventCriteria{}, fmt.Errorf("event %s not found", name)
return thorest.EventCriteria{}, fmt.Errorf("event %s not found", name)
}
criteria := api.EventCriteria{
criteria := thorest.EventCriteria{
Address: &c.Address,
Topic0: &ev.ID,
}
Expand All @@ -163,13 +163,13 @@ func (c *Contract) EventCriteria(name string, matchers ...interface{}) (api.Even
continue
}
if !ev.Inputs[i].Indexed {
return api.EventCriteria{}, errors.New("can't match non-indexed event inputs")
return thorest.EventCriteria{}, errors.New("can't match non-indexed event inputs")
}
topics, err := abi.MakeTopics(
[]interface{}{matchers[i]},
)
if err != nil {
return api.EventCriteria{}, err
return thorest.EventCriteria{}, err
}

switch i + 1 {
Expand All @@ -190,7 +190,7 @@ func (c *Contract) EventCriteria(name string, matchers ...interface{}) (api.Even
type Event struct {
Name string
Args map[string]interface{}
Log api.EventLog
Log thorest.EventLog
}

// DecodeEvents parses logs into a slice of decoded events.
Expand All @@ -213,7 +213,7 @@ type Event struct {
// }
//
// This function returns a slice of decoded event objects and any error encountered.
func (c *Contract) DecodeEvents(logs []api.EventLog) ([]Event, error) {
func (c *Contract) DecodeEvents(logs []thorest.EventLog) ([]Event, error) {
var decoded []Event
for _, log := range logs {
if len(log.Topics) < 2 {
Expand Down Expand Up @@ -253,7 +253,7 @@ func (c *Contract) DecodeEvents(logs []api.EventLog) ([]Event, error) {
}

// UnpackLog unpacks a retrieved log into the provided output structure.
func (c *Contract) UnpackLog(out interface{}, event string, log api.EventLog) error {
func (c *Contract) UnpackLog(out interface{}, event string, log thorest.EventLog) error {
if len(log.Topics) == 0 {
return errors.New("anonymous events are not supported")
}
Expand Down
4 changes: 2 additions & 2 deletions accounts/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"math/big"
"testing"

"github.com/darrenvechain/thorgo/api"
"github.com/darrenvechain/thorgo/events"
"github.com/darrenvechain/thorgo/thorest"
"github.com/darrenvechain/thorgo/txmanager"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestContract_EventCriteria(t *testing.T) {
assert.NoError(t, err)

// fetch events
transfers, err := events.New(thorClient, []api.EventCriteria{criteria}).Apply(0, 100)
transfers, err := events.New(thorClient, []thorest.EventCriteria{criteria}).Apply(0, 100)
assert.NoError(t, err)

// decode events
Expand Down
6 changes: 3 additions & 3 deletions accounts/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import (
"fmt"
"math/big"

"github.com/darrenvechain/thorgo/api"
"github.com/darrenvechain/thorgo/crypto/tx"
"github.com/darrenvechain/thorgo/thorest"
"github.com/darrenvechain/thorgo/transactions"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)

type Deployer struct {
client *api.Client
client *thorest.Client
bytecode []byte
abi *abi.ABI
value *big.Int
}

func NewDeployer(client *api.Client, bytecode []byte, abi *abi.ABI) *Deployer {
func NewDeployer(client *thorest.Client, bytecode []byte, abi *abi.ABI) *Deployer {
return &Deployer{client: client, bytecode: bytecode, abi: abi, value: big.NewInt(0)}
}

Expand Down
30 changes: 15 additions & 15 deletions blocks/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ import (
"sync/atomic"
"time"

"github.com/darrenvechain/thorgo/api"
"github.com/darrenvechain/thorgo/thorest"
"github.com/ethereum/go-ethereum/common"
"github.com/google/uuid"
)

type subscriber struct {
sub chan *api.ExpandedBlock
sub chan *thorest.ExpandedBlock
ctx context.Context
}

type Blocks struct {
client *api.Client
client *thorest.Client
best atomic.Value
subscribers sync.Map // Using sync.Map for concurrent access
}

func New(c *api.Client) *Blocks {
func New(c *thorest.Client) *Blocks {
b := &Blocks{client: c}
go b.poll()
return b
}

// poll sends the expanded block to all active subscribers.
func (b *Blocks) poll() {
var previous *api.ExpandedBlock
var previous *thorest.ExpandedBlock
var err error
backoff := 5 * time.Second

Expand Down Expand Up @@ -79,23 +79,23 @@ func (b *Blocks) poll() {
// Subscribe adds a new subscriber to the block stream.
// The subscriber will receive the latest block produced.
// The subscriber will be removed when the context is done.
func (b *Blocks) Subscribe(ctx context.Context) <-chan *api.ExpandedBlock {
sub := make(chan *api.ExpandedBlock)
func (b *Blocks) Subscribe(ctx context.Context) <-chan *thorest.ExpandedBlock {
sub := make(chan *thorest.ExpandedBlock)
id := uuid.New().String()
s := subscriber{sub: sub, ctx: ctx}
b.subscribers.Store(id, s)
return sub
}

// ByID returns the block by the given ID.
func (b *Blocks) ByID(id common.Hash) (*api.Block, error) {
func (b *Blocks) ByID(id common.Hash) (*thorest.Block, error) {
return b.client.Block(id.Hex())
}

// Best returns the latest block on chain.
func (b *Blocks) Best() (block *api.Block, err error) {
func (b *Blocks) Best() (block *thorest.Block, err error) {
// Load the best block from the cache.
if best, ok := b.best.Load().(*api.Block); ok {
if best, ok := b.best.Load().(*thorest.Block); ok {
// Convert the timestamp to UTC time.
bestTime := time.Unix(best.Timestamp, 0).UTC()
if time.Since(bestTime) < 10*time.Second {
Expand All @@ -113,29 +113,29 @@ func (b *Blocks) Best() (block *api.Block, err error) {
}

// Finalized returns the finalized block.
func (b *Blocks) Finalized() (*api.Block, error) {
func (b *Blocks) Finalized() (*thorest.Block, error) {
return b.client.Block("finalized")
}

// Justified returns the justified block.
func (b *Blocks) Justified() (*api.Block, error) {
func (b *Blocks) Justified() (*thorest.Block, error) {
return b.client.Block("justified")
}

// ByNumber returns the block by the given number.
func (b *Blocks) ByNumber(number uint64) (*api.Block, error) {
func (b *Blocks) ByNumber(number uint64) (*thorest.Block, error) {
return b.client.Block(fmt.Sprintf("%d", number))
}

// Expanded returns the expanded block information.
// This includes the transactions and receipts.
func (b *Blocks) Expanded(revision string) (*api.ExpandedBlock, error) {
func (b *Blocks) Expanded(revision string) (*thorest.ExpandedBlock, error) {
return b.client.ExpandedBlock(revision)
}

// Ticker waits for the next block to be produced
// Returns the next block
func (b *Blocks) Ticker() (*api.ExpandedBlock, error) {
func (b *Blocks) Ticker() (*thorest.ExpandedBlock, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sub := b.Subscribe(ctx)
Expand Down
4 changes: 2 additions & 2 deletions blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"testing"
"time"

"github.com/darrenvechain/thorgo/api"
"github.com/darrenvechain/thorgo/internal/testcontainer"
"github.com/darrenvechain/thorgo/solo"
"github.com/darrenvechain/thorgo/thorest"
"github.com/stretchr/testify/assert"
)

var (
thorClient *api.Client
thorClient *thorest.Client
blocks *Blocks
)

Expand Down
Loading

0 comments on commit c2a22fe

Please sign in to comment.