-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (66 loc) · 2.22 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"github.com/gin-gonic/gin"
"fmt"
//algo package
"github.com/algorand/go-algorand-sdk/future"
"github.com/algorand/go-algorand-sdk/client/v2/algod"
"github.com/algorand/go-algorand-sdk/client/v2/common"
"context"
json "encoding/json"
)
type SignedTransaction struct {
Txn []uint8 `json:"signedTXN"`
TxnID string `json:"txID"`
}
func broadcast (c *gin.Context) {
const algodAddress = "https://testnet-algorand.api.purestake.io/ps2"
const psTokenKey = "X-API-Key"
const psToken = ""
commonClient, err := common.MakeClient(algodAddress, psTokenKey, psToken)
if err != nil {
fmt.Printf("failed to make common client: %s\n", err)
return
}
algodClient := (*algod.Client)(commonClient)
fmt.Println("Status")
nodeStatus, err := algodClient.Status().Do(context.Background())
if err != nil {
fmt.Printf("error getting algod status: %s\n", err)
return
}
fmt.Printf("algod last round: %d\n", nodeStatus.LastRound)
var stxn SignedTransaction
if err := c.BindJSON(&stxn); err != nil {
fmt.Println(stxn.Txn)
c.JSON(400, gin.H{"error": "Transaction error"})
return
}
c.JSON(200, gin.H{"transaction": stxn.Txn})
fmt.Println(stxn.Txn)
sendResponse, err := algodClient.SendRawTransaction(stxn.Txn).Do(context.Background())
if err != nil {
fmt.Printf("failed to send transaction: %s\n", err)
return
}
fmt.Printf("Submitted transaction %s\n", sendResponse)
confirmedTxn, err := future.WaitForConfirmation(algodClient, stxn.TxnID, 4, context.Background())
if err != nil {
fmt.Printf("Error waiting for confirmation on txID: %s\n", stxn.TxnID)
return
}
txnJSON, err := json.MarshalIndent(confirmedTxn.Transaction.Txn, "", "\t")
if err != nil {
fmt.Printf("Can not marshal txn data: %s\n", err)
}
fmt.Printf("Transaction information: %s\n", txnJSON)
fmt.Printf("Decoded note: %s\n", string(confirmedTxn.Transaction.Txn.Note))
fmt.Printf("Amount sent: %d microAlgos\n", confirmedTxn.Transaction.Txn.Amount)
fmt.Printf("Fee: %d microAlgos\n", confirmedTxn.Transaction.Txn.Fee)
fmt.Printf("Decoded note: %s\n", string(confirmedTxn.Transaction.Txn.Note))
}
func main () {
router := gin.Default()
router.POST("/broadcast", broadcast)
router.Run(":500")
}