Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions abi-tolk/contracts_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package abitolk

// Code autogenerated. DO NOT EDIT.

import ()

var contractErrors = map[ContractInterface]map[int32]string{
TonCocoonClient: {1000: "ERROR_OLD_MESSAGE",
1001: "ERROR_LOW_SMC_BALANCE",
1003: "ERROR_LOW_MSG_VALUE",
1005: "ERROR_SIGNED_MSG_FORMAT_MISMATCH",
1006: "ERROR_CLOSED",
1007: "ERROR_BAD_SIGNATURE",
1009: "ERROR_EXPECTED_OWNER",
1010: "ERROR_EXPECTED_MESSAGE_FROM_OWNER",
1011: "ERROR_NOT_UNLOCKED_YET",
1015: "ERROR_EXPECTED_MY_ADDRESS",
},
TonCocoonProxy: {1003: "ERROR_LOW_MSG_VALUE",
1005: "ERROR_SIGNED_MSG_FORMAT_MISMATCH",
1006: "ERROR_CLOSED",
1007: "ERROR_BAD_SIGNATURE",
1010: "ERROR_EXPECTED_MESSAGE_FROM_OWNER",
1011: "ERROR_NOT_UNLOCKED_YET",
1013: "ERROR_UNKNOWN_TEXT_OP",
1014: "ERROR_CONTRACT_ADDRESS_MISMATCH",
1015: "ERROR_EXPECTED_MY_ADDRESS",
},
TonCocoonRoot: {1004: "ERROR_MSG_FORMAT_MISMATCH",
1005: "ERROR_SIGNED_MSG_FORMAT_MISMATCH",
1007: "ERROR_BAD_SIGNATURE",
1010: "ERROR_EXPECTED_MESSAGE_FROM_OWNER",
2000: "ERROR_UNKNOWN_PROXY_TYPE",
},
TonCocoonWallet: {1005: "ERROR_SIGNED_MSG_FORMAT_MISMATCH",
1007: "ERROR_BAD_SIGNATURE",
},
TonCocoonWorker: {1000: "ERROR_OLD_MESSAGE",
1003: "ERROR_LOW_MSG_VALUE",
1005: "ERROR_SIGNED_MSG_FORMAT_MISMATCH",
1006: "ERROR_CLOSED",
1007: "ERROR_BAD_SIGNATURE",
1010: "ERROR_EXPECTED_MESSAGE_FROM_OWNER",
1015: "ERROR_EXPECTED_MY_ADDRESS",
},
TonTep74JettonMinter: {73: "ERR_NOT_FROM_ADMIN",
74: "ERR_UNAUTHORIZED_BURN",
75: "ERR_NOT_ENOUGH_AMOUNT_TO_RESPOND",
},
TonTep74JettonWallet: {333: "ERR_WRONG_WORKCHAIN",
705: "ERR_NOT_FROM_OWNER",
706: "ERR_NOT_ENOUGH_BALANCE",
707: "ERR_INVALID_WALLET",
708: "ERR_INVALID_PAYLOAD",
709: "ERR_NOT_ENOUGH_TON",
},
}

var defaultExitCodes = map[int32]string{
0: "Ok",
1: "Ok",
2: "Stack underflow",
3: "Stack overflow",
4: "Integer overflow or division by zero",
5: "Integer out of expected range",
6: "Invalid opcode",
7: "Type check error",
8: "Cell overflow",
9: "Cell underflow",
10: "Dictionary error",
11: "Unknown error",
12: "Impossible situation error",
13: "Out of gas error",
-14: "Out of gas error",
}

func GetContractError(interfaces []ContractInterface, code int32) *string {
for _, i := range interfaces {
if errors, ok := contractErrors[i]; ok {
if msg, ok := errors[code]; ok {
return &msg
}
}
}
if msg, ok := defaultExitCodes[code]; ok {
return &msg
}
return nil
}
131 changes: 131 additions & 0 deletions abi-tolk/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//go:build ignore

package main

import (
"fmt"
"go/format"
"io/fs"
"os"
"path/filepath"
"strings"

parser "github.com/tonkeeper/tongo/abi-tolk/parser"
"github.com/tonkeeper/tongo/tolk"
)

const HEADER = `package abitolk
// Code autogenerated. DO NOT EDIT.

import (
%v
)

`
const SCHEMAS_PATH = "schemas/"

func mergeMethods(abis []tolk.ABI) (map[string][]parser.GetMethodWithAbi, error) {
methodsMap := map[string][]parser.GetMethodWithAbi{}
for _, abi := range abis {
for _, method := range abi.GetMethods {
current, ok := methodsMap[method.Name]
if !ok {
methodsMap[method.Name] = append(methodsMap[method.Name], parser.GetMethodWithAbi{
ABI: abi,
GetMethod: method,
})
continue
}
if len(current[0].GetMethod.Parameters) != len(method.Parameters) {
return nil, fmt.Errorf("method '%s' has a version with input params, it has to be defined with golang_name to avoid collision", method.Name)
}
current = append(current, parser.GetMethodWithAbi{
ABI: abi,
GetMethod: method,
})
methodsMap[method.Name] = current
}
}

return methodsMap, nil
}

func main() {
var abi []tolk.ABI
filepath.Walk(SCHEMAS_PATH, func(path string, info fs.FileInfo, err error) error {
if !strings.HasSuffix(info.Name(), ".json") {
return nil
}
scheme, err := os.ReadFile(path)
if err != nil {
panic(err)
}
a, err := parser.ParseABI(scheme)
if err != nil {
panic(err)
}
abi = append(abi, a)
return nil
})

uniqueGetMethods, err := mergeMethods(abi)
if err != nil {
panic(err)
}

gen := parser.NewGenerator(abi, uniqueGetMethods)
types := gen.CollectedTypes()
msgDecoder := gen.GenerateMsgDecoder()

getMethods, simpleMethods, err := gen.GetMethods()
if err != nil {
panic(err)
}
invocationOrder, err := gen.RenderInvocationOrderList(simpleMethods)
if err != nil {
panic(err)
}
messagesMD, err := gen.RenderMessagesMD()
if err != nil {
panic(err)
}
payloads, err := gen.RenderPayload()
if err != nil {
panic(err)
}
contractErrors, err := gen.RenderContractErrors()
if err != nil {
panic(err)
}

for _, f := range [][]string{
{types, "types.go", `"github.com/tonkeeper/tongo/tlb"`, `"fmt"`, `"encoding/json"`},
{msgDecoder, "messages_generated.go", `"github.com/tonkeeper/tongo/tlb"`},
{getMethods, "get_methods.go", `"context"`, `"fmt"`, `"github.com/tonkeeper/tongo/ton"`, `"github.com/tonkeeper/tongo/tlb"`},
{invocationOrder, "interfaces.go", `"github.com/tonkeeper/tongo/ton"`},
{payloads, "payload_msg_types.go", `"github.com/tonkeeper/tongo/boc"`, `"github.com/tonkeeper/tongo/tlb"`},
{contractErrors, "contracts_errors.go"},
} {
file, err := os.Create(f[1])
if err != nil {
panic(err)
}
code := []byte(fmt.Sprintf(HEADER, strings.Join(f[2:], "\n")) + f[0])
formatedCode, err := format.Source(code)
if err != nil {
formatedCode = code
//panic(err)
}
_, err = file.Write(formatedCode)
if err != nil {
panic(err)
}
err = file.Close()
if err != nil {
panic(err)
}
}
if err := os.WriteFile("messages.md", []byte(messagesMD), 0644); err != nil {
panic(err)
}
}
Loading
Loading