Skip to content

Commit

Permalink
Merge pull request #199 from tonkeeper/generate-abi-messages-md
Browse files Browse the repository at this point in the history
Generate messages.MD in abi package containing supported msg op names
  • Loading branch information
mr-tron authored Nov 16, 2023
2 parents 1212700 + b8cf1d4 commit 0e2ebdc
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 5 deletions.
7 changes: 7 additions & 0 deletions abi/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func main() {
if err != nil {
panic(err)
}
messagesMD, err := gen.RenderMessagesMD()
if err != nil {
panic(err)
}

jettons, err := gen.RenderJetton()
if err != nil {
Expand Down Expand Up @@ -135,4 +139,7 @@ func main() {
panic(err)
}
}
if err := os.WriteFile("messages.md", []byte(messagesMD), 0644); err != nil {
panic(err)
}
}
127 changes: 127 additions & 0 deletions abi/messages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@

# List of supported message opcodes

The first 4 bytes of a message's body identify the `operation` to be performed, or the `method` of the smart contract to be invoked.

The list below contains the supported message operations, their names and opcodes.

| Name | Message operation code |
|-------------|------------------------|
| AcceptStorageContract| 0x7a361688 |
| AuctionFillUp| 0x370fec51 |
| Bounce| 0xffffffff |
| ChallengeQuarantinedChannelState| 0x088eaa32 |
| ChangeDnsRecord| 0x4eb1f0f9 |
| ChannelClosed| 0xdddc88ba |
| ChannelCooperativeClose| 0x5577587e |
| ChannelCooperativeCommit| 0x79a126ef |
| CloseStorageContract| 0x79f937ea |
| DedustDepositLiquidityAll| 0xb56b9598 |
| DedustPayout| 0x474f86cf |
| DedustPayoutFromPool| 0xad4eb6f5 |
| DedustSwap| 0xea06185d |
| DedustSwapExternal| 0x61ee542d |
| DedustSwapPeer| 0x72aca8aa |
| DeleteDnsRecord| 0x4eb1f0f9 |
| DeployStorageContract| 0xe4748df1 |
| DnsBalanceRelease| 0x4ed14b65 |
| ElectorNewStake| 0x4e73744b |
| ElectorNewStakeConfirmation| 0xf374484c |
| ElectorRecoverStakeRequest| 0x47657424 |
| ElectorRecoverStakeResponse| 0xf96f7324 |
| EncryptedTextComment| 0x2167da4b |
| Excess| 0xd53276db |
| FinishUncooperativeChannelClose| 0x25432a91 |
| GetRoyaltyParams| 0x693d3950 |
| GetStaticData| 0x2fcb26a2 |
| InitPaymentChannel| 0x0e0620c2 |
| JettonBurn| 0x595f07bc |
| JettonBurnNotification| 0x7bdd97de |
| JettonInternalTransfer| 0x178d4519 |
| JettonNotify| 0x7362d09c |
| JettonTransfer| 0x0f8a7ea5 |
| MegatonWtonMint| 0x77a33521 |
| NftOwnershipAssigned| 0x05138d91 |
| NftTransfer| 0x5fcc3d14 |
| OfferStorageContract| 0x107c49ef |
| OutbidNotification| 0x557cea20 |
| OwnershipProof| 0x0524c7ae |
| PaymentRequest| 0x706c7567 |
| PaymentRequestResponse| 0xf06c7567 |
| ProcessGovernanceDecision| 0x44beae41 |
| ProofStorage| 0x419d5d4d |
| ProveOwnership| 0x04ded148 |
| ReportRoyaltyParams| 0xa8cb00ad |
| ReportStaticData| 0x8b771735 |
| SbtDestroy| 0x1f04537a |
| SbtOwnerInfo| 0x0dd607e3 |
| SbtRequestOwner| 0xd0c3bfea |
| SbtRevoke| 0x6f89f5e3 |
| SettleChannelConditionals| 0x66f6f069 |
| StartUncooperativeChannelClose| 0x1f151acf |
| StonfiPaymentRequest| 0xf93bb43f |
| StonfiSwap| 0x25938561 |
| StorageContractConfirmed| 0xd4caedcd |
| StorageContractTerminated| 0xb6236d63 |
| StorageRewardWithdrawal| 0xa91baf56 |
| StorageWithdraw| 0x46ed2e94 |
| SubscriptionPayment| 0x73756273 |
| TegroSwapTon| 0x600c00fd |
| TeleitemCancelAuction| 0x371638ae |
| TeleitemDeploy| 0x299a3e15 |
| TeleitemOk| 0xa37a0983 |
| TeleitemReturnBid| 0xa43227e1 |
| TeleitemStartAuction| 0x487a8e81 |
| TelemintDeploy| 0x4637289a |
| TelemintDeployV2| 0x4637289b |
| TextComment| 0x00000000 |
| TonstakeControllerApprove| 0x7b4b42e6 |
| TonstakeControllerCredit| 0x1690c604 |
| TonstakeControllerDisapprove| 0xe8a0abfe |
| TonstakeControllerPoolHalt| 0x139a1b4e |
| TonstakeControllerPoolSendMessage| 0x270695fb |
| TonstakeControllerPoolSetSudoer| 0x79e7c016 |
| TonstakeControllerPoolUnhalt| 0x7247e7a5 |
| TonstakeControllerPoolUpgrade| 0x96e7f528 |
| TonstakeControllerRecoverStake| 0xeb373a05 |
| TonstakeControllerReturnAvailableFunds| 0x55c26cd5 |
| TonstakeControllerReturnUnusedLoan| 0xed7378a6 |
| TonstakeControllerSendRequestLoan| 0x6335b11a |
| TonstakeControllerTopUp| 0xd372158c |
| TonstakeControllerUpdateValidatorHash| 0xf0fd2250 |
| TonstakeControllerValidatorWithdrawal| 0x30026327 |
| TonstakeControllerWithdrawValidator| 0x8efed779 |
| TonstakeImanagerOperationFee| 0x54d37487 |
| TonstakeImanagerRequestNotification| 0xb1ebae06 |
| TonstakeImanagerSetInterest| 0xc9f04485 |
| TonstakeImanagerStats| 0xc1344900 |
| TonstakeNftBurn| 0xf127fe4e |
| TonstakeNftBurnNotification| 0xed58b0b2 |
| TonstakeNftInit| 0x132f9a45 |
| TonstakeNftPayout| 0xdb3b8abd |
| TonstakePoolDeployController| 0xb27edcad |
| TonstakePoolDeposit| 0x47d54391 |
| TonstakePoolLoanRepayment| 0xdfdca27b |
| TonstakePoolPrepareGovernanceMigration| 0x9971881c |
| TonstakePoolRequestLoan| 0xe642c965 |
| TonstakePoolSetDepositSettings| 0x9bf5561c |
| TonstakePoolSetGovernanceFee| 0x2aaa96a0 |
| TonstakePoolSetRoles| 0x5e517f36 |
| TonstakePoolTouch| 0x4bc7c2df |
| TonstakePoolWithdraw| 0x319b0cdc |
| TonstakePoolWithdrawal| 0x0a77535c |
| TopUpChannelBalance| 0x67c7d281 |
| UpdatePubkey| 0x53f34cd6 |
| UpdateStorageParams| 0x54cbf19b |
| WalletPluginDestruct| 0x64737472 |
| WalletPluginDestructResponse| 0xe4737472 |
| WhalesNominatorsAcceptStake| 0x99a811fb |
| WhalesNominatorsAcceptWithdraws| 0xa19fd934 |
| WhalesNominatorsDeposit| 0x7bcd1fef |
| WhalesNominatorsForceKick| 0x1596920c |
| WhalesNominatorsSendStake| 0xa2065f2c |
| WhalesNominatorsStakeWithdrawCompleted| 0x23d421e1 |
| WhalesNominatorsStakeWithdrawDelayed| 0x74bb3427 |
| WhalesNominatorsWithdraw| 0xda803efd |
| WhalesNominatorsWithdrawUnowned| 0x251d6a98 |
| WhalesNominatorsWithdrawUnownedResponse| 0x1d1715bf |
52 changes: 47 additions & 5 deletions abi/parser/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"text/template"

"github.com/tonkeeper/tongo/abi"
"golang.org/x/exp/slices"

"github.com/tonkeeper/tongo/tlb"
Expand Down Expand Up @@ -48,6 +49,8 @@ var (
msgDecoderReturnErr = "if err != nil {return \"\", nil, err}\n"
returnInvalidStack = "{return \"\", nil, fmt.Errorf(\"invalid stack format\")}\n"
returnStrNilErr = "if err != nil {return \"\", nil, err}\n"
//go:embed messages.md.tmpl
messagesMDTemplate string
//go:embed invocation_order.tmpl
invocationOrderTemplate string
//go:embed get_methods.tmpl
Expand Down Expand Up @@ -178,19 +181,19 @@ func (g *Generator) registerABI() error {
}
}
for _, internal := range g.abi.Internals {
err := registerMstType(g.loadedTlbMsgTypes, "MsgBody", internal.Name, internal.Input, internal.FixedLength)
err := registerMsgType(g.loadedTlbMsgTypes, "MsgBody", internal.Name, internal.Input, internal.FixedLength)
if err != nil {
return err
}
}
for _, jetton := range g.abi.JettonPayloads {
err := registerMstType(g.loadedJettonsMsgTypes, "JettonPayload", jetton.Name, jetton.Input, jetton.FixedLength)
err := registerMsgType(g.loadedJettonsMsgTypes, "JettonPayload", jetton.Name, jetton.Input, jetton.FixedLength)
if err != nil {
return err
}
}
for _, nft := range g.abi.NFTPayloads {
err := registerMstType(g.loadedNFTsMsgTypes, "NFTPayload", nft.Name, nft.Input, nft.FixedLength)
err := registerMsgType(g.loadedNFTsMsgTypes, "NFTPayload", nft.Name, nft.Input, nft.FixedLength)
if err != nil {
return err
}
Expand Down Expand Up @@ -220,7 +223,7 @@ func (g *Generator) registerType(s string) error {
return nil
}

func registerMstType(known map[uint32][]TLBMsgBody, postfix, name, s string, fixedLength bool) error {
func registerMsgType(known map[uint32][]TLBMsgBody, postfix, name, s string, fixedLength bool) error {
parsed, err := tlbParser.Parse(s)
if err != nil {
return fmt.Errorf("can't decode %v error %w", s, err)
Expand Down Expand Up @@ -466,7 +469,7 @@ func (g *Generator) GenerateMsgDecoder() string {

context := messagesContext{g.loadedTlbMsgTypes}

tmpl, err := template.New("mesages").Parse(messagesTemplate)
tmpl, err := template.New("messages").Parse(messagesTemplate)
if err != nil {
panic(err)
return ""
Expand All @@ -479,6 +482,44 @@ func (g *Generator) GenerateMsgDecoder() string {
return buf.String()
}

type messageOperation struct {
Name abi.MsgOpName
OpCode string
}

type messagesMDContext struct {
Operations []messageOperation
}

// RenderMessagesMD renders messages.md file with messages and their names + opcodes.
func (g *Generator) RenderMessagesMD() (string, error) {
context := messagesMDContext{}
for opcode, bodies := range g.loadedTlbMsgTypes {
for _, body := range bodies {
operation := messageOperation{
Name: body.OperationName,
OpCode: fmt.Sprintf("0x%08x", opcode),
}
context.Operations = append(context.Operations, operation)
}
}
sort.Slice(context.Operations, func(i, j int) bool {
if context.Operations[i].Name == context.Operations[j].Name {
return context.Operations[i].OpCode < context.Operations[j].OpCode
}
return context.Operations[i].Name < context.Operations[j].Name
})
tmpl, err := template.New("messagesMD").Parse(messagesMDTemplate)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, context); err != nil {
return "", err
}
return buf.String(), nil
}

type templateContext struct {
Interfaces map[string]string
InvocationOrder []methodDescription
Expand All @@ -491,6 +532,7 @@ type methodDescription struct {
Name string
InvokeFnName string
}

type interfacDescripion struct {
Name string
Results []string
Expand Down
12 changes: 12 additions & 0 deletions abi/parser/messages.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# List of supported message opcodes

The first 4 bytes of a message's body identify the `operation` to be performed, or the `method` of the smart contract to be invoked.

The list below contains the supported message operations, their names and opcodes.

| Name | Message operation code |
|-------------|------------------------|
{{- range $_, $op := .Operations }}
| {{ $op.Name }}| {{ $op.OpCode }} |
{{- end }}

0 comments on commit 0e2ebdc

Please sign in to comment.