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

feature/more-examples-6 #3615

Merged
merged 17 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Working on usesSDK
  • Loading branch information
tjayrush committed Apr 26, 2024
commit 3fe7b95d66ccc32d77e658436c73d9359bfb488c
13 changes: 4 additions & 9 deletions examples/usesSdk/abis.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package main

import (
"bytes"
"fmt"

"github.com/TrueBlocks/trueblocks-core/sdk"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
)

// DoAbis tests the When sdk function
Expand All @@ -17,11 +14,9 @@ func DoAbis() {
Addrs: []string{"uniswap.eth"},
}

buf := bytes.Buffer{}
if err := opts.AbisBytes(&buf); err != nil {
if functions, _, err := opts.Abis(); err != nil {
logger.Fatal(err)
} else {
SaveToFile[types.Function]("usesSDK/abis.json", functions)
}

file.StringToAsciiFile("usesSDK/abis.json", buf.String())
fmt.Println(buf.String())
}
64 changes: 54 additions & 10 deletions examples/usesSdk/blocks.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,72 @@
package main

import (
"bytes"
"fmt"

"github.com/TrueBlocks/trueblocks-core/sdk"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
)

// DoBlocks tests the Blocks sdk function
func DoBlocks() {
logger.Info("DoMonitors")
logger.Info("DoBlocks")

opts := sdk.BlocksOptions{
BlockIds: testBlocks,
Hashes: true,
Globals: sdk.Globals{
Cache: true,
},
}

if blocks, _, err := opts.Blocks(); err != nil {
logger.Fatal(err)
} else {
SaveToFile[types.Block[types.Transaction]]("usesSDK/blocks.json", blocks)
}

opts.Hashes = true
if blocks, _, err := opts.BlocksHashes(); err != nil {
logger.Fatal(err)
} else {
SaveToFile[types.Block[string]]("usesSDK/blocks-hashes.json", blocks)
}

opts.Hashes = false
opts.Traces = true
if traces, _, err := opts.BlocksTraces(); err != nil {
logger.Fatal(err)
} else {
SaveToFile[types.Trace]("usesSDK/blocks-traces.json", traces)
}

var buf bytes.Buffer
if err := opts.BlocksBytes(&buf); err != nil {
opts.Traces = false
opts.Uniq = true
if apps, _, err := opts.BlocksUniq(); err != nil {
logger.Fatal(err)
} else {
SaveToFile[types.Appearance]("usesSDK/blocks-uniq.json", apps)
}

file.StringToAsciiFile("usesSDK/blocks.json", buf.String())
fmt.Println(buf.String())
opts.Uniq = false
opts.Logs = true
if logs, _, err := opts.BlocksLogs(); err != nil {
logger.Fatal(err)
} else {
SaveToFile[types.Log]("usesSDK/blocks-logs.json", logs)
}

opts.Logs = false
opts.Withdrawals = true
if withdrawals, _, err := opts.BlocksWithdrawals(); err != nil {
logger.Fatal(err)
} else {
SaveToFile[types.Withdrawal]("usesSDK/blocks-withdrawals.json", withdrawals)
}

opts.Withdrawals = false
opts.Count = false
if counts, _, err := opts.BlocksCount(); err != nil {
logger.Fatal(err)
} else {
SaveToFile[types.BlockCount]("usesSDK/blocks-count.json", counts)
}
}
7 changes: 7 additions & 0 deletions examples/usesSdk/chunks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ func DoChunks() {
file.StringToAsciiFile("usesSDK/chunks.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *ChunksOptions) ChunksManifest() ([]types.ChunkRecord, *types.MetaData, error) {
// func (opts *ChunksOptions) ChunksIndex() ([]types.ChunkIndex, *types.MetaData, error) {
// func (opts *ChunksOptions) ChunksBlooms() ([]types.ChunkBloom, *types.MetaData, error) {
// func (opts *ChunksOptions) ChunksPins() ([]types.ChunkPinReport, *types.MetaData, error) {
// func (opts *ChunksOptions) ChunksAddresses() ([]types.ChunkAddress, *types.MetaData, error) {
// // func (opts *ChunksOptions) ChunksAppearances() ([]types.ChunkAppearance, *types.MetaData, error) {
// func (opts *ChunksOptions) ChunkStats() ([]types.ChunkStats, *types.MetaData, error) {
9 changes: 9 additions & 0 deletions examples/usesSdk/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ func DoExport() {
file.StringToAsciiFile("usesSDK/export.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *ExportOptions) Export() ([]types.Transaction, *types.MetaData, error) {
// func (opts *ExportOptions) ExportAppearances() ([]types.Appearance, *types.MetaData, error) {
// func (opts *ExportOptions) ExportReceipts() ([]types.Receipt, *types.MetaData, error) {
// func (opts *ExportOptions) ExportLogs() ([]types.Log, *types.MetaData, error) {
// func (opts *ExportOptions) ExportTraces() ([]types.Trace, *types.MetaData, error) {
// func (opts *ExportOptions) ExportStatements() ([]types.Statement, *types.MetaData, error) {
// func (opts *ExportOptions) ExportBalances() ([]types.State, *types.MetaData, error) {
// func (opts *ExportOptions) ExportWithdrawals() ([]types.Withdrawal, *types.MetaData, error) {
// func (opts *ExportOptions) ExportCount() ([]types.AppearanceCount, *types.MetaData, error) {
1 change: 1 addition & 0 deletions examples/usesSdk/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ func DoInit() {
file.StringToAsciiFile("usesSDK/init.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *InitOptions) InitAll() ([]bool, *types.MetaData, error) {
3 changes: 3 additions & 0 deletions examples/usesSdk/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ func DoList() {
file.StringToAsciiFile("usesSDK/list.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *ListOptions) List() ([]types.Appearance, *types.MetaData, error) {
// func (opts *ListOptions) ListCount() ([]types.AppearanceCount, *types.MetaData, error) {
// func (opts *ListOptions) ListBounds() ([]types.Bounds, *types.MetaData, error) {
1 change: 1 addition & 0 deletions examples/usesSdk/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ func DoLogs() {
file.StringToAsciiFile("usesSDK/logs.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *LogsOptions) Logs() ([]types.Log, *types.MetaData, error) {
39 changes: 19 additions & 20 deletions examples/usesSdk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,30 @@ import "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"

func main() {
// DoSlurp()
DoNames()
// DoNames()
DoBlocks()
DoTransactions()
DoReceipts()
DoLogs()
DoTraces()
DoState()
DoTokens()
// DoTransactions()
// DoReceipts()
// DoLogs()
// DoTraces()
// DoState()
// DoTokens()
DoAbis()
DoWhen()
DoList()
DoExport()
DoMonitors()
DoConfig()
DoStatus()
// DoDaemon()
// DoScrape()
DoChunks()
// DoInit()
DoExplore()
// DoWhen()
// DoList()
// DoExport()
// DoMonitors()
// DoConfig()
// DoStatus()
// // DoDaemon()
// // DoScrape()
// DoChunks()
// // DoInit()
// DoExplore()
}

var testBlocks = []string{
"13800-1000000:monthly",
"14011011",
"5001001",
}

var testAddrs = []string{
Expand Down
3 changes: 3 additions & 0 deletions examples/usesSdk/monitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ func DoMonitors() {
file.StringToAsciiFile("usesSDK/monitors.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *MonitorsOptions) Monitors() ([]bool, *types.MetaData, error) {
// func (opts *MonitorsOptions) MonitorsClean() ([]types.MonitorClean, *types.MetaData, error) {
// func (opts *MonitorsOptions) MonitorsList() ([]types.Monitor, *types.MetaData, error) {
3 changes: 3 additions & 0 deletions examples/usesSdk/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ func DoNames() {
file.StringToAsciiFile("usesSDK/names.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *NamesOptions) Names() ([]types.Name, *types.MetaData, error) {
// func (opts *NamesOptions) NamesAddr() ([]base.Address, *types.MetaData, error) {
// func (opts *NamesOptions) NamesTags() ([]string, *types.MetaData, error) {
1 change: 1 addition & 0 deletions examples/usesSdk/receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ func DoReceipts() {
file.StringToAsciiFile("usesSDK/receipts.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *ReceiptsOptions) Receipts() ([]types.Receipt, *types.MetaData, error) {
41 changes: 41 additions & 0 deletions examples/usesSdk/save.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"bufio"
"fmt"
"os"
)

// SaveToFile writes the content of the slice to a file specified by fn.
func SaveToFile[T fmt.Stringer](fn string, slice []T) error {
file, err := os.Create(fn)
if err != nil {
return err
}
defer file.Close()

writer := bufio.NewWriter(file)
if _, err = writer.WriteString("[\n"); err != nil {
return err
}

for i, item := range slice {
if i > 0 {
if _, err = writer.WriteString(","); err != nil {
return err
}
}
if writer.WriteString(fmt.Sprintf("%s\n", item.String())); err != nil {
return err
}
}
if _, err = writer.WriteString("]\n"); err != nil {
return err
}

if writer.Flush(); err != nil {
return err
}

return nil
}
3 changes: 3 additions & 0 deletions examples/usesSdk/slurp.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ func DoSlurp() {
file.StringToAsciiFile("usesSDK/slurp.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *SlurpOptions) Slurp() ([]types.Slurp, *types.MetaData, error) {
// func (opts *SlurpOptions) SlurpAppearances() ([]types.Appearance, *types.MetaData, error) {
// func (opts *SlurpOptions) SlurpCount() ([]types.SlurpCount, *types.MetaData, error) {
2 changes: 2 additions & 0 deletions examples/usesSdk/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ func DoState() {
file.StringToAsciiFile("usesSDK/state.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *StateOptions) State() ([]types.State, *types.MetaData, error) {
// func (opts *StateOptions) StateCall() ([]types.Result, *types.MetaData, error) {
1 change: 1 addition & 0 deletions examples/usesSdk/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ func DoTokens() {
file.StringToAsciiFile("usesSDK/tokens.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *TokensOptions) Tokens() ([]types.Token, *types.MetaData, error) {
2 changes: 2 additions & 0 deletions examples/usesSdk/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ func DoTraces() {
file.StringToAsciiFile("usesSDK/traces.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *TracesOptions) Traces() ([]types.Trace, *types.MetaData, error) {
// func (opts *TracesOptions) TracesCount() ([]types.TraceCount, *types.MetaData, error) {
4 changes: 4 additions & 0 deletions examples/usesSdk/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ func DoTransactions() {
file.StringToAsciiFile("usesSDK/transactions.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *TransactionsOptions) Transactions() ([]types.Transaction, *types.MetaData, error) {
// func (opts *TransactionsOptions) TransactionsTraces() ([]types.Trace, *types.MetaData, error) {
// func (opts *TransactionsOptions) TransactionsUniq() ([]types.Appearance, *types.MetaData, error) {
// func (opts *TransactionsOptions) TransactionsLogs() ([]types.Log, *types.MetaData, error) {
3 changes: 3 additions & 0 deletions examples/usesSdk/when.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ func DoWhen() {
file.StringToAsciiFile("usesSDK/when.json", buf.String())
fmt.Println(buf.String())
}
// func (opts *WhenOptions) When() ([]types.NamedBlock, *types.MetaData, error) {
// func (opts *WhenOptions) WhenTimestamps() ([]types.Timestamp, *types.MetaData, error) {
// func (opts *WhenOptions) WhenCount() ([]types.TimestampCount, *types.MetaData, error) {
7 changes: 6 additions & 1 deletion scripts/build-examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ cd "$SCRIPT_DIR/../examples" || exit 1 # Exit if changing directory fails

# Find directories containing a 'go.mod' file, search only direct children
find . -maxdepth 2 -type f -name 'go.mod' -exec dirname {} \; | while read -r dir; do
build_target "$(basename "$dir")"
# Check for the presence of a '.skip' file in the directory
if [[ ! -f "$dir/.skip" ]]; then
build_target "$(basename "$dir")"
else
echo "Skipping $dir due to .skip file"
fi
done
2 changes: 1 addition & 1 deletion src/apps/chifra/pkg/types/types_appearance.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Appearance struct {
// EXISTING_CODE
}

func (s *Appearance) String() string {
func (s Appearance) String() string {
bytes, _ := json.Marshal(s)
return string(bytes)
}
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/pkg/types/types_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type Block[Tx string | Transaction] struct {
// EXISTING_CODE
}

func (s *Block[Tx]) String() string {
func (s Block[Tx]) String() string {
bytes, _ := json.Marshal(s)
return string(bytes)
}
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/pkg/types/types_blockcount.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type BlockCount struct {
// EXISTING_CODE
}

func (s *BlockCount) String() string {
func (s BlockCount) String() string {
bytes, _ := json.Marshal(s)
return string(bytes)
}
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/pkg/types/types_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Function struct {
// EXISTING_CODE
}

func (s *Function) String() string {
func (s Function) String() string {
bytes, _ := json.Marshal(s)
return string(bytes)
}
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/pkg/types/types_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Log struct {
// EXISTING_CODE
}

func (s *Log) String() string {
func (s Log) String() string {
bytes, _ := json.Marshal(s)
return string(bytes)
}
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/pkg/types/types_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Trace struct {
// EXISTING_CODE
}

func (s *Trace) String() string {
func (s Trace) String() string {
bytes, _ := json.Marshal(s)
return string(bytes)
}
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/pkg/types/types_withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Withdrawal struct {
// EXISTING_CODE
}

func (s *Withdrawal) String() string {
func (s Withdrawal) String() string {
bytes, _ := json.Marshal(s)
return string(bytes)
}
Expand Down