Skip to content

Commit

Permalink
add errors codes, fix multisig hash
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-tron committed Apr 11, 2024
1 parent b1c716c commit 3747d32
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 11 deletions.
65 changes: 65 additions & 0 deletions abi/contracts_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package abi

// Code autogenerated. DO NOT EDIT.

var contractErrors = map[ContractInterface]map[int32]string{
MultisigOrderV2: {104: "Unauthorized init",
105: "Already inited",
106: "Unauthorized sign",
107: "Already approved",
111: "Expired",
112: "Already executed",
65535: "Unknown op",
},
MultisigV2: {100: "Not enough ton",
101: "Unauthorized execute",
102: "Signers outdated",
103: "Invalid dictionary sequence",
108: "Inconsistent data",
109: "Invalid threshold",
110: "Invalid signers",
111: "Expired",
1007: "Unauthorized proposer",
1008: "Invalid new order",
},
WalletV4R2: {33: "Invalid seqno",
34: "Invalid subwallet ID",
35: "Invalid signature",
36: "Expired message",
39: "Plugin not found",
80: "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 {
errors := defaultExitCodes
for _, i := range interfaces {
e, ok := contractErrors[i]
if !ok {
errors = e
break
}
}
if msg, ok := errors[code]; ok {
return &msg
}
return nil
}
6 changes: 6 additions & 0 deletions abi/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,19 @@ func main() {
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/boc"`, `"github.com/tonkeeper/tongo/tlb"`},
{invocationOrder, "interfaces.go", `"github.com/tonkeeper/tongo/ton"`},
{jettons, "jetton_msg_types.go", `"github.com/tonkeeper/tongo/boc"`, `"github.com/tonkeeper/tongo/tlb"`},
{nfts, "nfts_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 {
Expand Down
16 changes: 8 additions & 8 deletions abi/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,10 +771,6 @@ var knownContracts = map[ton.Bits256]knownContractDescription{
GetNftData,
},
},
ton.MustParseHash("4e84760554543d7fc12cc3882646070e8655ebcf324af77c5c3b3b68e3103997"): {
contractInterfaces: []ContractInterface{MultisigV2},
getMethods: []InvokeFn{},
},
ton.MustParseHash("587cc789eff1c84f46ec3797e45fc809a14ff5ae24f1e0c7a6a99cc9dc9061ff"): {
contractInterfaces: []ContractInterface{WalletV1R3},
getMethods: []InvokeFn{
Expand All @@ -788,10 +784,6 @@ var knownContracts = map[ton.Bits256]knownContractDescription{
Seqno,
},
},
ton.MustParseHash("6305a8061c856c2ccf05dcb0df5815c71475870567cab5f049e340bcf59251f3"): {
contractInterfaces: []ContractInterface{MultisigOrderV2},
getMethods: []InvokeFn{},
},
ton.MustParseHash("64dd54805522c5be8a9db59cea0105ccf0d08786ca79beb8cb79e880a8d7322d"): {
contractInterfaces: []ContractInterface{WalletV4R1},
getMethods: []InvokeFn{
Expand Down Expand Up @@ -828,6 +820,10 @@ var knownContracts = map[ton.Bits256]knownContractDescription{
contractInterfaces: []ContractInterface{WalletHighloadV2},
getMethods: []InvokeFn{},
},
ton.MustParseHash("a01e057fbd4288402b9898d78d67bd4e90254c93c5866879bc2d1d12865436bc"): {
contractInterfaces: []ContractInterface{MultisigOrderV2},
getMethods: []InvokeFn{},
},
ton.MustParseHash("a0cfc2c48aee16a271f2cfc0b7382d81756cecb1017d077faaab3bb602f6868c"): {
contractInterfaces: []ContractInterface{WalletV1R1},
getMethods: []InvokeFn{},
Expand All @@ -838,6 +834,10 @@ var knownContracts = map[ton.Bits256]knownContractDescription{
Seqno,
},
},
ton.MustParseHash("d3d14da9a627f0ec3533341829762af92b9540b21bf03665fac09c2b46eabbac"): {
contractInterfaces: []ContractInterface{MultisigV2},
getMethods: []InvokeFn{},
},
ton.MustParseHash("d4902fcc9fad74698fa8e353220a68da0dcf72e32bcb2eb9ee04217c17d3062c"): {
contractInterfaces: []ContractInterface{WalletV1R2},
getMethods: []InvokeFn{
Expand Down
46 changes: 46 additions & 0 deletions abi/parser/errors.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@


var contractErrors = map[ContractInterface]map[int32]string{
{{range $Interface, $Errors := .Interfaces}}
{{- if ne (len $Errors) 0 -}}
{{- $Interface}}: {
{{- range $Code, $Name := $Errors -}}
{{ $Code}}: "{{$Name}}",
{{ end -}} },
{{ end -}}
{{- end -}}
}


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 {
errors := defaultExitCodes
for _, i := range interfaces{
e, ok := contractErrors[i]
if !ok {
errors = e
break
}
}
if msg, ok := errors[code]; ok {
return &msg
}
return nil
}
26 changes: 26 additions & 0 deletions abi/parser/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ var (
messagesTemplate string
//go:embed payloads.tmpl
payloadTmpl string
//go:embed errors.tmpl
contractErrorsTmpl string
)

type MsgType int
Expand Down Expand Up @@ -748,3 +750,27 @@ func getOrderedKeys[M ~map[tlb.Tag]V, V any](m M) []tlb.Tag {
})
return keys
}

func (g *Generator) RenderContractErrors() (string, error) {
tmpl, err := template.New("contractErrors").Parse(contractErrorsTmpl)
if err != nil {
return "", err
}
var context = struct {
Interfaces map[string]map[int]string
}{
Interfaces: map[string]map[int]string{},
}
for _, iface := range g.abi.Interfaces {
ifaceName := utils.ToCamelCase(iface.Name)
context.Interfaces[ifaceName] = map[int]string{}
for _, e := range iface.Errors {
context.Interfaces[ifaceName][e.Code] = e.Text
}
}
var buf bytes.Buffer

err = tmpl.Execute(&buf, context)
return buf.String(), err

}
4 changes: 4 additions & 0 deletions abi/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type Interface struct {
} `xml:"msg_out"`
CodeHashes []string `xml:"code_hash"`
Inherits string `xml:"inherits,attr"`
Errors []struct {
Text string `xml:",chardata"`
Code int `xml:"code,attr"`
} `xml:"error"`
}

type InterfaceMessage struct {
Expand Down
6 changes: 3 additions & 3 deletions abi/schemas/multisig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


<interface name="multisig_v2">
<code_hash>4e84760554543d7fc12cc3882646070e8655ebcf324af77c5c3b3b68e3103997</code_hash>
<code_hash>d3d14da9a627f0ec3533341829762af92b9540b21bf03665fac09c2b46eabbac</code_hash>
<error code="1007">Unauthorized proposer</error>
<error code="1008">Invalid new order</error>
<error code="100">Not enough ton</error>
Expand All @@ -28,14 +28,14 @@
</interface>

<interface name="multisig_order_v2">
<code_hash>6305a8061c856c2ccf05dcb0df5815c71475870567cab5f049e340bcf59251f3</code_hash>
<code_hash>a01e057fbd4288402b9898d78d67bd4e90254c93c5866879bc2d1d12865436bc</code_hash> <!-- with library -->
<error code="104">Unauthorized init</error>
<error code="105">Already inited</error>
<error code="106">Unauthorized sign</error>
<error code="107">Already approved</error>
<error code="111">Expired</error>
<error code="112">Already executed</error>
<error code="0xffff">Unknown op</error>
<error code="65535">Unknown op</error>
</interface>


Expand Down
7 changes: 7 additions & 0 deletions abi/schemas/wallets.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@
<msg_in>
<ext_in name="wallet_signed_v4"/>
</msg_in>

<error code="33">Invalid seqno</error>
<error code="34">Invalid subwallet ID</error>
<error code="35">Invalid signature</error>
<error code="36">Expired message</error>
<error code="39">Plugin not found</error>
<error code="80">Not enough ton</error>
</interface>
<interface name="wallet_v5r1" inherits="wallet">
<code_hash>f3d7ca53493deedac28b381986a849403cbac3d2c584779af081065af0ac4b93</code_hash> <!-- with library -->
Expand Down

0 comments on commit 3747d32

Please sign in to comment.