-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (48 loc) · 1.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"github.com/aviate-labs/agent-go"
"github.com/aviate-labs/agent-go-example/ledger"
"github.com/aviate-labs/agent-go/ic"
"github.com/aviate-labs/agent-go/principal"
"log"
)
func main() {
ledgerAgent, err := ledger.NewAgent(ic.LEDGER_PRINCIPAL, agent.DefaultConfig)
if err != nil {
log.Fatal(err)
}
// We do not know yet what the last block is, so we query the block height first.
blockHeight, err := ledgerAgent.QueryBlocks(ledger.GetBlocksArgs{})
if err != nil {
log.Fatal(err)
}
// oldestBlock = blockHeight.FirstBlockIndex // The first block that we can query the ledger.
lastBlock := blockHeight.ChainLength // The last block that we can query the ledger.
// Query the last 10 blocks.
response, err := ledgerAgent.QueryBlocks(ledger.GetBlocksArgs{
Start: lastBlock - 10,
Length: 10,
})
if err != nil {
log.Fatal(err)
}
for i, block := range response.Blocks {
operation := block.Transaction.Operation
if transfer := operation.Transfer; transfer != nil {
var from principal.AccountIdentifier
copy(from[:], transfer.From)
var to principal.AccountIdentifier
copy(to[:], transfer.To)
fmt.Printf("Block %d: %s -> %s: %.2f ICP.\n", int(lastBlock)+i, from, to, float64(transfer.Amount.E8s)/1e8)
} else if burn := operation.Burn; burn != nil {
var from principal.AccountIdentifier
copy(from[:], burn.From)
fmt.Printf("Block %d: %s: %.2f ICP burned.\n", int(lastBlock)+i, from, float64(burn.Amount.E8s)/1e8)
} else if mint := operation.Mint; mint != nil {
var to principal.AccountIdentifier
copy(to[:], mint.To)
fmt.Printf("Block %d: %s: %.2f ICP minted.\n", int(lastBlock)+i, to, float64(mint.Amount.E8s)/1e8)
}
}
}