-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleans up the build, adds examples, other cleaning (#3610)
* Lot's of smoke, no fire. Removes unused enabledForCmds and EnableCommand which were all true anyway. * Moves all build scripts into the ./scripts folder to make them easier to control * Adds and/or cleans up some of the example code.
- Loading branch information
Showing
89 changed files
with
866 additions
and
1,137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# trueblocks-examples | ||
Examples repo for TrueBlocks core | ||
# Examples | ||
|
||
Examples for TrueBlocks core sdk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.png | ||
*.svg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Example - Balance Chart | ||
|
||
This example uses the SDK to first find the block numbers of the chain (Eth Mainnet) for specific month ends from the start of the chain until the present. It then fetches the balance of a specific address at each of these block numbers. The balance is then plotted on a chart. | ||
|
||
## Running the example | ||
|
||
The example code is built using the regular build instructions, but you can build and run it locally using | ||
|
||
```bash | ||
go build -o ./balanceChart ./... | ||
``` | ||
|
||
which will produce a binary called `balanceChart`. You can then run it with | ||
|
||
```bash | ||
./balanceChart | ||
``` | ||
|
||
## Historical chart of balances for prefund accounts | ||
|
||
Here's a bar chart: | ||
|
||
![alt text](./chart.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package charts | ||
|
||
import ( | ||
"fmt" | ||
"image/color" | ||
"log" | ||
"math" | ||
|
||
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base" | ||
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types" | ||
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils" | ||
"gonum.org/v1/plot" | ||
"gonum.org/v1/plot/plotter" | ||
"gonum.org/v1/plot/vg" | ||
"gonum.org/v1/plot/vg/draw" | ||
) | ||
|
||
// GroupedBarChart creates a grouped bar chart from an array of State objects | ||
func GroupedBarChart(data []types.State, names []types.Name, title string, filename string) { | ||
var err error | ||
var p *plot.Plot | ||
if p, err = plot.New(); err != nil { | ||
log.Panic(err) | ||
} | ||
|
||
p.Title.Text = title | ||
p.Title.Font.Size = vg.Points(18) | ||
p.X.Label.Text = "Block Number" | ||
p.Y.Label.Text = "Balance" | ||
p.BackgroundColor = bgColor | ||
// p.Legend.Top = true | ||
// p.Legend.Left = true | ||
// p.Legend.Font.Size = vg.Points(11) | ||
// p.Legend.Font.SetName("Mono") | ||
p.Title.Padding = vg.Points(10) | ||
p.X.Padding = vg.Points(10) | ||
p.Y.Padding = vg.Points(10) | ||
p.X.Tick.Label.Rotation = math.Pi / 2.8 | ||
p.X.Tick.Label.XAlign = draw.XRight | ||
p.X.Tick.Label.YAlign = draw.YBottom | ||
p.X.Tick.Label.Color = color.Black | ||
p.X.Tick.Color = color.Black | ||
p.X.LineStyle.Color = color.Black | ||
p.Y.Tick.Label.Color = color.Black | ||
p.Y.Tick.Color = color.Black | ||
p.Y.LineStyle.Color = color.Black | ||
|
||
namesMap := make(map[string]types.Name, len(names)) | ||
for _, n := range names { | ||
namesMap[small(n.Address)] = n | ||
} | ||
|
||
addrMap := make(map[string]int) | ||
cnt := 1 | ||
for _, d := range data { | ||
if addrMap[small(d.Address)] == 0 { | ||
addrMap[small(d.Address)] = cnt | ||
cnt++ | ||
} | ||
} | ||
|
||
addrs := []string{} | ||
for key := range addrMap { | ||
addrs = append(addrs, key) | ||
} | ||
nAddrs := len(addrs) | ||
|
||
wid := 4. | ||
|
||
offsets := []float64{} | ||
for i := 0; i < nAddrs; i++ { | ||
f := float64(-4 + (i * 4)) | ||
fmt.Println(i, i+1, -4+(i*4), f) | ||
offsets = append(offsets, f) // []float64{-4, -2, 2, 4} | ||
} | ||
// offsetx := []float64{ 0, 2, 6, 8} | ||
|
||
balances := []plotter.Values{} | ||
for i := 0; i < nAddrs; i++ { | ||
balances = append(balances, plotter.Values{}) | ||
} | ||
|
||
for _, d := range data { | ||
idx := addrMap[small(d.Address)] - 1 | ||
ff := base.ToEther(&d.Balance).Float64() | ||
balances[idx] = append(balances[idx], ff) | ||
} | ||
for i := 0; i < nAddrs; i++ { | ||
balances[i] = append(balances[i], 0) | ||
} | ||
|
||
if len(balances) != nAddrs { | ||
log.Panic("Invalid balance length. Have ", len(balances), " want ", nAddrs) | ||
} | ||
if len(offsets) < nAddrs { | ||
log.Panic("Invalid offsets length. Have ", len(offsets), " want ", nAddrs) | ||
} | ||
|
||
minOffset := 0 | ||
maxOffset := 0 | ||
for i := 0; i < nAddrs; i++ { | ||
bar, err := plotter.NewBarChart(balances[i], vg.Points(wid)) | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
bar.LineStyle.Width = vg.Length(1) | ||
bar.LineStyle.Color = colors[(i+6)%len(colors)] | ||
bar.Color = colors[i%len(colors)] | ||
bar.Offset = vg.Points(offsets[i]) | ||
maxOffset += nAddrs * int(wid) | ||
fmt.Println("Adding", namesMap[addrs[i]].Name, "at", offsets[i], "for", addrs[i]) | ||
// p.Legend.Add(namesMap[addrs[i]].Name, bar) | ||
p.Add(bar) | ||
} | ||
|
||
nTicks := 6 | ||
lWid := (maxOffset - minOffset) / nTicks | ||
ticks := []plot.Tick{} | ||
for i := 0; i < nTicks; i++ { | ||
v := minOffset + i*lWid | ||
ticks = append(ticks, plot.Tick{Value: float64(v), Label: fmt.Sprintf("%d", v)}) | ||
} | ||
p.X.Tick.Marker = plot.ConstantTicks(ticks) | ||
|
||
if err := p.Save(16*vg.Inch, 8*vg.Inch, filename); err != nil { | ||
log.Panic(err) | ||
} | ||
|
||
utils.System("open " + filename) | ||
} | ||
|
||
var bgColor = color.RGBA{R: 255, G: 255, B: 255, A: 255} | ||
var colors = []color.Color{ | ||
color.RGBA{0x00, 0x00, 0xff, 0xff}, | ||
color.RGBA{0x00, 0xff, 0x00, 0xff}, | ||
color.RGBA{0xff, 0x00, 0x00, 0xff}, | ||
color.RGBA{0x00, 0xff, 0xff, 0xff}, | ||
color.RGBA{0xff, 0xff, 0x00, 0xff}, | ||
color.RGBA{0xff, 0x00, 0xff, 0xff}, | ||
|
||
color.RGBA{0x00, 0x00, 0x88, 0xff}, | ||
color.RGBA{0x00, 0x88, 0x00, 0xff}, | ||
color.RGBA{0x88, 0x00, 0x00, 0xff}, | ||
color.RGBA{0x00, 0x88, 0x88, 0xff}, | ||
color.RGBA{0x88, 0x88, 0x00, 0xff}, | ||
color.RGBA{0x88, 0x00, 0x88, 0xff}, | ||
} | ||
|
||
func small(a base.Address) string { | ||
return a.Hex()[:6] + "..." + a.Hex()[len(a.Hex())-4:] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package charts | ||
|
||
import ( | ||
"image/color" | ||
"log" | ||
|
||
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types" | ||
"gonum.org/v1/plot" | ||
"gonum.org/v1/plot/plotter" | ||
"gonum.org/v1/plot/vg" | ||
) | ||
|
||
func LineChart(data []types.State, title string, filename string) { | ||
// Manually define a color palette | ||
colorPalette := []color.Color{ | ||
color.RGBA{R: 255, G: 0, B: 0, A: 255}, // Red | ||
color.RGBA{R: 0, G: 255, B: 0, A: 255}, // Green | ||
color.RGBA{R: 0, G: 0, B: 255, A: 255}, // Blue | ||
color.RGBA{R: 255, G: 255, B: 0, A: 255}, // Yellow | ||
// Add more colors as needed | ||
} | ||
|
||
// Group data by address | ||
groupedByAddress := make(map[string][]types.State) | ||
for _, d := range data { | ||
groupedByAddress[d.Address.Hex()] = append(groupedByAddress[d.Address.Hex()], d) | ||
} | ||
|
||
// Create a new plot | ||
p, err := plot.New() | ||
if err != nil { | ||
log.Fatalf("Error creating plot: %v", err) | ||
} | ||
|
||
applyCustomStyling(p, title) | ||
|
||
colorIndex := 0 | ||
|
||
// Iterate over each group and create a line plot | ||
for address, group := range groupedByAddress { | ||
points := make(plotter.XYs, len(group)) | ||
for i, d := range group { | ||
points[i].X = float64(d.BlockNumber) | ||
points[i].Y = d.Balance.Float64() | ||
} | ||
|
||
line, err := plotter.NewLine(points) | ||
if err != nil { | ||
log.Fatalf("Error creating line plot for address %s: %v", address, err) | ||
} | ||
|
||
// Set the color for the line | ||
line.Color = colorPalette[colorIndex%len(colorPalette)] | ||
line.Width = vg.Points(2) | ||
|
||
// Add the line plot to the plot | ||
p.Add(line) | ||
|
||
// Add a legend entry for this line | ||
p.Legend.Add(address, line) | ||
|
||
// Move to the next color for the next address | ||
colorIndex++ | ||
} | ||
|
||
// Save the plot to an SVG file | ||
if err := p.Save(16*vg.Inch, 8*vg.Inch, filename); err != nil { | ||
log.Fatalf("Error saving plot: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package charts | ||
|
||
import ( | ||
"image/color" | ||
|
||
"gonum.org/v1/plot" | ||
"gonum.org/v1/plot/vg" | ||
) | ||
|
||
func applyCustomStyling(p *plot.Plot, title string) { | ||
p.Title.Text = title | ||
p.Title.Font.Size = vg.Points(25) | ||
p.Title.Color = color.RGBA{R: 255, G: 255, B: 255, A: 255} | ||
p.X.Label.Color = color.RGBA{R: 255, G: 255, B: 255, A: 255} | ||
p.Y.Label.Color = color.RGBA{R: 255, G: 255, B: 255, A: 255} | ||
p.X.Color = color.RGBA{R: 255, G: 255, B: 255, A: 255} | ||
p.Y.Color = color.RGBA{R: 255, G: 255, B: 255, A: 255} | ||
p.X.Label.Text = "Timestamp" | ||
p.Y.Label.Text = "Balance" | ||
p.BackgroundColor = color.RGBA{R: 128, G: 128, B: 128, A: 255} | ||
p.Legend.Color = color.RGBA{R: 255, G: 255, B: 255, A: 255} | ||
p.Legend.Font.SetName("Courier New") | ||
p.Legend.Left = true | ||
p.Legend.Top = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/TrueBlocks/trueblocks-core/examples/balanceChart | ||
|
||
// Go Version | ||
go 1.22 | ||
|
||
replace github.com/TrueBlocks/trueblocks-core/sdk => ../../sdk | ||
|
||
require github.com/TrueBlocks/trueblocks-core v2.5.8-release.0.20240422010715-cf442e547b61+incompatible // indirect |
Oops, something went wrong.