Skip to content

Commit

Permalink
feat(MPC-1756): add confirmations
Browse files Browse the repository at this point in the history
  • Loading branch information
MnrGreg committed Sep 27, 2024
1 parent 596c32d commit 888ba3e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
29 changes: 12 additions & 17 deletions 4-broadcast-signed-transaction/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package main

import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"os"

"github.com/ethereum/go-ethereum/core/types"
"time"
)

var protocol = "ethereum"
Expand All @@ -32,14 +30,6 @@ func main() {
panic(fmt.Errorf("env variable 'ACCESS_TOKEN' must be set"))
}

// Deserialize the rawUnsignedTransaction
unsignedTx := &types.Transaction{}
unsignedTxBytes, _ := hex.DecodeString(rawUnsignedTransaction)
err := unsignedTx.UnmarshalBinary(unsignedTxBytes)
if err != nil {
panic(err)
}

// * Compile the unsigned tx with the signatureand broadcast to the blockchain https://docs.blockdaemon.com/reference/txcompileandsend-txapi
txRequest := struct {
Unsigned_tx string `json:"unsigned_tx"`
Expand All @@ -57,17 +47,11 @@ func main() {
}

res, err := http.Post(url+"/tx/v1/"+protocol+"-"+network+"/compile_and_send?apiKey="+apiKey, "application/json", bytes.NewReader(reqBody))
resbodyBytes, _ := io.ReadAll(res.Body)
if err != nil {
panic(err)
}
defer res.Body.Close()

// Check HTTP status code
if res.StatusCode != http.StatusOK {
panic(fmt.Errorf("invalid status code:%d %s %s", res.StatusCode, http.StatusText(res.StatusCode), resbodyBytes))
}

// Parse broadcasted transaction response
var response struct {
ID string `json:"id"`
Expand All @@ -77,4 +61,15 @@ func main() {
}
fmt.Printf("Broadcasted transaction hash: https://"+network+".etherscan.io/tx/%s\n", response.ID)

// * Get the number of confirmations for the transaction https://docs.blockdaemon.com/reference/gettxconfirmations
fmt.Printf("Sleeping for 60s before checking confirmations...\n")
time.Sleep(80 * time.Second)
res, err = http.Get(url + "/universal/v1/" + protocol + "/" + network + "/tx/" + response.ID + "/confirmations?apiKey=" + apiKey)
resbodyBytes, _ := io.ReadAll(res.Body)
if err != nil {
panic(err)
}
defer res.Body.Close()
fmt.Println(string(resbodyBytes))

}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ sequenceDiagram
client ->> Blockchain: compile unsigned tx with signature and broadcast<br>(unsigned tx, signature, public key)
```

### Watch the video
[![Watch the video](https://img.youtube.com/vi/irgfKhJVq6Q/3.jpg)](https://youtu.be/irgfKhJVq6Q)

### Prerequisites
- Go https://go.dev/doc/install or use [code-spaces](https://codespaces.new/Blockdaemon/demo-buildervault-wallettransact?quickstart=1)
- Register for a demo Builder Vault tenant: https://www.blockdaemon.com/get-started/builder-vault-sandbox-registration
Expand Down

0 comments on commit 888ba3e

Please sign in to comment.