-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmain.go
51 lines (44 loc) · 1.45 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
package main
import (
"context"
"fmt"
"github.com/rrrkren/topshot-sales/topshot"
"github.com/onflow/flow-go-sdk/client"
"google.golang.org/grpc"
)
func handleErr(err error) {
if err != nil {
panic(err)
}
}
func main() {
// connect to flow
flowClient, err := client.New("access.mainnet.nodes.onflow.org:9000", grpc.WithInsecure())
handleErr(err)
err = flowClient.Ping(context.Background())
handleErr(err)
// fetch latest block
latestBlock, err := flowClient.GetLatestBlock(context.Background(), false)
handleErr(err)
fmt.Println("current height: ", latestBlock.Height)
// fetch block events of topshot Market.MomentPurchased events for the past 1000 blocks
blockEvents, err := flowClient.GetEventsForHeightRange(context.Background(), client.EventRangeQuery{
Type: "A.c1e4f4f4c4257510.Market.MomentPurchased",
StartHeight: latestBlock.Height - 500,
EndHeight: latestBlock.Height,
})
handleErr(err)
for _, blockEvent := range blockEvents {
for _, purchaseEvent := range blockEvent.Events {
// loop through the Market.MomentPurchased events in this blockEvent
e := topshot.MomentPurchasedEvent(purchaseEvent.Value)
fmt.Println(e)
saleMoment, err := topshot.GetSaleMomentFromOwnerAtBlock(flowClient, blockEvent.Height-1, *e.Seller(), e.Id())
handleErr(err)
fmt.Println(saleMoment)
fmt.Printf("transactionID: %s, block height: %d\n",
purchaseEvent.TransactionID.String(), blockEvent.Height)
fmt.Println()
}
}
}