Skip to content

Commit

Permalink
chore(readme): update old package name; tests: add coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Nov 5, 2024
1 parent c2a22fe commit 381758f
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test:| go_version_check #@ Run the tests
@go test -cover $(PACKAGES)

test-coverage:| go_version_check #@ Run the tests with coverage
@go test -race -coverprofile=coverage.out -covermode=atomic $(PACKAGES)
@go test -coverpkg=./... -race -coverprofile=coverage.out -covermode=atomic $(PACKAGES)
@go tool cover -html=coverage.out

lint_command_check:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ go get github.com/darrenvechain/thorgo
contracts. It also supports simulating, building, and sending transactions, as well as interacting with smart
contracts for reading and transacting.

### api
### thorest

- `github.com/darrenvechain/thorgo/api`
- The `api` package provides raw API access to the VechainThor blockchain. It allows developers to query the
- `github.com/darrenvechain/thorgo/thorest`
- The `thorest` package provides raw API access to the VechainThor blockchain. It allows developers to query the
blockchain directly without the need for higher-level abstractions provided by `thorgo`.

### txmanager
Expand Down
35 changes: 29 additions & 6 deletions accounts/accounts_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package accounts_test

import (
"math/big"
"testing"

"github.com/darrenvechain/thorgo"
Expand Down Expand Up @@ -35,7 +36,7 @@ func TestMain(m *testing.M) {
}

// TestGetAccount fetches a thor solo account and checks if the balance and energy are greater than 0
func TestGetAccount(t *testing.T) {
func TestVisitor_Get(t *testing.T) {
acc, err := accounts.New(thorClient, account1.Address()).Get()

assert.NoError(t, err, "Account.httpGet should not return an error")
Expand All @@ -48,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) {
func TestVisitor_Get_At(t *testing.T) {
acc, err := accounts.New(thorClient, account1.Address()).Revision(thorest.RevisionID(solo.GenesisID())).Get()

assert.NoError(t, err, "Account.httpGet should not return an error")
Expand All @@ -60,7 +61,7 @@ func TestGetAccountForRevision(t *testing.T) {
}

// TestGetCode fetches the code of the VTHO contract and checks if the code length is greater than 2 (0x)
func TestGetCode(t *testing.T) {
func TestVisitor_Code(t *testing.T) {
vtho, err := accounts.New(thorClient, vthoContract.Address()).Code()

assert.NoError(t, err, "Account.Code should not return an error")
Expand All @@ -69,7 +70,7 @@ func TestGetCode(t *testing.T) {
}

// TestGetCodeForRevision fetches the code of the VTHO contract for the genesis block
func TestGetCodeForRevision(t *testing.T) {
func TestVisitor_Code_At(t *testing.T) {
vtho, err := accounts.New(thorClient, vthoContract.Address()).
Revision(thorest.RevisionID(solo.GenesisID())).
Code()
Expand All @@ -80,7 +81,7 @@ func TestGetCodeForRevision(t *testing.T) {
}

// TestGetStorage fetches a storage position of the VTHO contract and checks if the value is empty
func TestGetStorage(t *testing.T) {
func TestVisitor_Storage(t *testing.T) {
storage, err := accounts.New(thorClient, vthoContract.Address()).Storage(common.Hash{})

assert.NoError(t, err, "Account.Storage should not return an error")
Expand All @@ -89,7 +90,7 @@ func TestGetStorage(t *testing.T) {
}

// TestGetStorageForRevision fetches a storage position of the VTHO contract for the genesis block
func TestGetStorageForRevision(t *testing.T) {
func TestVisitor_Storage_At(t *testing.T) {
storage, err := accounts.New(thorClient, vthoContract.Address()).
Revision(thorest.RevisionID(solo.GenesisID())).
Storage(common.Hash{})
Expand All @@ -98,3 +99,25 @@ func TestGetStorageForRevision(t *testing.T) {
assert.NotNil(t, storage, "Account.Storage should return a storage")
assert.Equal(t, common.Hash{}, storage.Value)
}

func TestVisitor_Call(t *testing.T) {
clause, err := vthoRaw.AsClause("balanceOf", account1.Address())
assert.NoError(t, err)

res, err := accounts.New(thorClient, vthoContract.Address()).Call(clause.Data())
assert.NoError(t, err)
balance := new(big.Int).SetBytes(res.Data)
assert.Greater(t, balance.Uint64(), big.NewInt(0).Uint64())
}

func TestVisitor_Call_At(t *testing.T) {
clause, err := vthoRaw.AsClause("balanceOf", account1.Address())
assert.NoError(t, err)

res, err := accounts.New(thorClient, vthoContract.Address()).
Revision(thorest.RevisionID(solo.GenesisID())).
Call(clause.Data())
assert.NoError(t, err)
balance := new(big.Int).SetBytes(res.Data)
assert.Greater(t, balance.Uint64(), big.NewInt(0).Uint64())
}
33 changes: 33 additions & 0 deletions accounts/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,36 @@ func TestContract_EventCriteria(t *testing.T) {
assert.IsType(t, common.Address{}, ev.Args["_to"])
assert.IsType(t, &big.Int{}, ev.Args["_value"])
}

func TestContract_UnpackLog(t *testing.T) {
receiver, err := txmanager.GeneratePK(thor)
assert.NoError(t, err)

tx, err := vthoRaw.Send(account1, "transfer", receiver.Address(), big.NewInt(1000))
assert.NoError(t, err)

receipt, _ := tx.Wait()
assert.False(t, receipt.Reverted)

// event criteria - match the newly created receiver
criteria, err := vthoRaw.EventCriteria("Transfer", nil, receiver.Address())
assert.NoError(t, err)

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

// unpack log
type outStruct struct {
From common.Address
To common.Address
Value *big.Int
}
res := outStruct{}
err = vthoRaw.UnpackLog(&res, "Transfer", transfers[0])
assert.NoError(t, err)
assert.Equal(t, account1.Address(), res.From)
assert.Equal(t, receiver.Address().Hex(), res.To.Hex())
assert.Equal(t, big.NewInt(1000), res.Value)
}
3 changes: 3 additions & 0 deletions blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func TestMain(m *testing.M) {

// TestGetBestBlock fetches the best block from the network
func TestBlocks_Best(t *testing.T) {
_, err := blocks.Ticker()
assert.NoError(t, err)

block, err := blocks.Best()
assert.NoError(t, err)
assert.NotNil(t, block)
Expand Down
2 changes: 1 addition & 1 deletion builtins/authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestAuthority_FilterCandidate(t *testing.T) {
func TestAuthority(t *testing.T) {
authority, err := NewAuthority(thor)
assert.NoError(t, err)

Expand Down
72 changes: 71 additions & 1 deletion builtins/energy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,86 @@ import (
"testing"

"github.com/darrenvechain/thorgo"
"github.com/darrenvechain/thorgo/thorest"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)

var thor = thorgo.New("https://mainnet.vechain.org")

func TestEnergy(t *testing.T) {
func TestEnergy_Name(t *testing.T) {
energy, err := NewEnergy(thor)
assert.NoError(t, err)

name, err := energy.Name()
assert.NoError(t, err)
assert.Equal(t, "VeThor", name)
}

func TestEnergy_Symbol(t *testing.T) {
energy, err := NewEnergy(thor)
assert.NoError(t, err)

symbol, err := energy.Symbol()
assert.NoError(t, err)
assert.Equal(t, "VTHO", symbol)
}

func TestEnergy_Decimals(t *testing.T) {
energy, err := NewEnergy(thor)
assert.NoError(t, err)

decimals, err := energy.Decimals()
assert.NoError(t, err)
assert.Equal(t, uint8(18), decimals)
}

func TestEnergy_TotalSupply(t *testing.T) {
energy, err := NewEnergy(thor)
assert.NoError(t, err)

totalSupply, err := energy.TotalSupply()
assert.NoError(t, err)
assert.NotZero(t, totalSupply)
}

func TestEnergy_BalanceOf(t *testing.T) {
energy, err := NewEnergy(thor)
assert.NoError(t, err)

energyBal, err := energy.BalanceOf(energy.Address())
assert.NoError(t, err)
assert.NotZero(t, energyBal)
}

func TestEnergy_FilterTransfer(t *testing.T) {
energy, err := NewEnergy(thor)
assert.NoError(t, err)

maxEvents := int64(10)
events, err := energy.FilterTransfer(make([]EnergyTransferCriteria, 0), &thorest.FilterOptions{Limit: &maxEvents}, nil)
assert.NoError(t, err)
assert.Equal(t, maxEvents, int64(len(events)))
}

func TestEnergy_FilterApproval(t *testing.T) {
energy, err := NewEnergy(thor)
assert.NoError(t, err)

maxEvents := int64(10)
events, err := energy.FilterApproval(make([]EnergyApprovalCriteria, 0), &thorest.FilterOptions{Limit: &maxEvents}, nil)
assert.NoError(t, err)
assert.Equal(t, maxEvents, int64(len(events)))
}

func TestEnergy_FilterTransfer_WithCriteria(t *testing.T) {
energy, err := NewEnergy(thor)
assert.NoError(t, err)

to := common.HexToAddress("0xbF89016e670595AAa225eEfa0a84B7FB17b8dAC8")

maxEvents := int64(10)
events, err := energy.FilterTransfer([]EnergyTransferCriteria{{To: &to}}, &thorest.FilterOptions{Limit: &maxEvents}, nil)
assert.NoError(t, err)
assert.NotZero(t, len(events))
}

0 comments on commit 381758f

Please sign in to comment.