Skip to content

Instantly share code, notes, and snippets.

@a2468834
Last active September 23, 2024 02:06
Show Gist options
  • Save a2468834/66b0631b971c16baa3cc1f9a4e307241 to your computer and use it in GitHub Desktop.
Save a2468834/66b0631b971c16baa3cc1f9a4e307241 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"encoding/base64"
"fmt"
"io"
"net/http"
"runtime/debug"
"strings"
"time"
"github.com/xssnick/tonutils-go/liteclient"
"github.com/xssnick/tonutils-go/tl"
"github.com/xssnick/tonutils-go/ton"
"github.com/xssnick/tonutils-go/tvm/cell"
)
const URL string = "https://gist.github.com/a2468834/ae7f47b96f446a536c430bfa01bfb6e8/raw/abfb41196e25f4278aafd2a27aa01d1b66062873/example.boc.txt"
const INDEX = 0
func NewAPIClient(globalConfigURL string) ton.APIClientWrapped {
connectionPool := liteclient.NewConnectionPool()
globalConfig, _ := liteclient.GetConfigFromUrl(
context.Background(),
globalConfigURL,
)
connectionPool.AddConnectionsFromConfig(
context.Background(),
globalConfig,
)
client := ton.NewAPIClient(
connectionPool,
ton.ProofCheckPolicyFast,
).WithRetry()
client.SetTrustedBlockFromConfig(globalConfig)
return client
}
func main() {
defer handlePanic()
client := NewAPIClient("https://ton.org/testnet-global.config.json")
bocArray := func() []string {
res, _ := http.Get(URL)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
return strings.Split(string(body), "\n")
}()
boc := func() *cell.Cell {
a, err := base64.URLEncoding.DecodeString(bocArray[INDEX])
if err != nil {
panic(err.Error())
}
b, err := cell.FromBOC(a)
if err != nil {
panic(err.Error())
}
return b
}()
// Execute "sendBoc" operation /////////////////////////////////////////////
result := func() tl.Serializable {
var res tl.Serializable
err := client.Client().QueryLiteserver(
context.Background(),
ton.SendMessage{Body: boc.ToBOC()},
&res,
)
if err != nil {
panic(err.Error())
}
return res
}()
// Do actions along with the different result type
switch t := result.(type) {
case ton.SendMessageStatus:
if t.Status != 1 {
// Broadcasting failure
fmt.Printf(
"\nINSERT INTO boc_status (hash, status, created_at, remark)\nVALUES ('%s', 'error', %d, 'SendBoc status code: %d');\n",
base64.URLEncoding.EncodeToString((boc.Hash())),
time.Now().UnixNano(),
t.Status,
)
panic("SendBoc with error")
} else {
// Successfully broadcasting
fmt.Printf(
"\nINSERT INTO boc_status (hash, status, created_at, remark)\nVALUES ('%s', 'broadcasted', %d, '%s');\n",
base64.URLEncoding.EncodeToString(boc.Hash()),
time.Now().UnixNano(),
"NULL",
)
}
case ton.LSError:
// LiteServer error
fmt.Printf(
"\nINSERT INTO boc_status (hash, status, created_at, remark)\nVALUES ('%s', 'error', %d, '%s');\n",
base64.URLEncoding.EncodeToString(boc.Hash()),
time.Now().UnixNano(),
fmt.Sprintf("%+v", result),
)
panic("SendBoc with error")
}
// Execute infinite loop for querying L2 DB `messages` table until find one
fmt.Printf("\nWait for updating L2 DB `messages` table\n")
time.Sleep(10 * time.Second)
// Find the given message hash appear in the L2 DB `messages`
fmt.Printf(
"\nUPDATE boc_status\nSET status='confirmed', created_at=%d\nWHERE hash='%s';\n",
time.Now().UnixNano(),
base64.URLEncoding.EncodeToString(boc.Hash()),
)
////////////////////////////////////////////////////////////////////////////
}
func handlePanic() {
if r := recover(); r != nil {
fmt.Printf("[PanicMsg] %+v\n\n", r)
fmt.Println(string(debug.Stack()))
}
}
@a2468834
Copy link
Author

When BoC is invalid

INSERT INTO boc_status (hash, status, created_at, remark)
VALUES ('_HczAvddDyPjDsEX6NolZVTMM0ZW4RsrxE9naEbeg8Q=', 'error', 1726911529760997528, 'lite server error, code 0: cannot apply external message to current state : External message was not accepted
Cannot run message on account: inbound external message rejected by transaction A0510E1D932CFF36FF1E5139D1F2000945B86D79B856B651BE96D97CA00DE765:
exitcode=33, steps=23, gas_used=0
VM Log (truncated):
...te NOW
execute LEQ
execute THROWIF 36
execute PUSH c4
execute CTOS
execute LDU 32
execute LDU 32
execute LDU 256
execute LDDICT
execute ENDS
execute XCPU s4,s3
execute EQUAL
execute THROWIFNOT 33
default exception handler, terminating vm with exit code 33
');

@a2468834
Copy link
Author

When BoC is valid

INSERT INTO boc_status (hash, status, created_at, remark)
VALUES ('_HczAvddDyPjDsEX6NolZVTMM0ZW4RsrxE9naEbeg8Q=', 'broadcasted', 1726911529760997528, 'NULL');

Wait for updating L2 DB `messages` table

UPDATE boc_status
SET status='confirmed', created_at=1726911753760997528
WHERE hash='_HczAvddDyPjDsEX6NolZVTMM0ZW4RsrxE9naEbeg8Q=';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment