Skip to content

Commit

Permalink
add precompiles
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-tron committed May 24, 2024
1 parent e813ec7 commit 76c7397
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tvm/precompiled/jettons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package precompiled

import (
"math/big"

"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
)

// todo: need to cover test corner cases about int size near int64 max value
// DO NOT USE UNTIL FIX TODO!!!
var jettonV1getWalletData = func(data *boc.Cell, args tlb.VmStack) (tlb.VmStack, error) {
var body struct {
Amount tlb.VarUInteger16
Owner tlb.MsgAddress
Master tlb.MsgAddress
Code boc.Cell `tlb:"^"`
}
err := tlb.Unmarshal(data, &body)
if err != nil {
return nil, err
}
var result = make([]tlb.VmStackValue, 4)
var b = big.Int(body.Amount)
if b.IsInt64() {
result[0] = tlb.VmStackValue{
SumType: "VmStkTinyInt",
VmStkTinyInt: b.Int64(),
}
} else {
result[0] = tlb.VmStackValue{
SumType: "VmStkInt",
VmStkInt: tlb.Int257(b),
}
}
ownerCell := boc.NewCell()
masterCell := boc.NewCell()
err = tlb.Marshal(ownerCell, body.Owner)
if err != nil {
return nil, err
}
err = tlb.Marshal(masterCell, body.Master)
if err != nil {
return nil, err
}
result[1], err = tlb.CellToVmCellSlice(ownerCell)
if err != nil {
return nil, err
}
result[2], err = tlb.CellToVmCellSlice(masterCell)
if err != nil {
return nil, err
}
result[3] = tlb.VmStackValue{
SumType: "VmStkCell",
VmStkCell: tlb.Ref[boc.Cell]{Value: body.Code},
}
return result, nil
}
41 changes: 41 additions & 0 deletions tvm/precompiled/mining.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package precompiled

import (
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
)

var getPowParamsGram = func(data *boc.Cell, args tlb.VmStack) (tlb.VmStack, error) {
var body struct {
Skip1 uint64
Skip2 tlb.Bits256
Value1 tlb.Uint128
Value2 tlb.Uint256
Field1 struct {
Value3 tlb.Grams
Value4 uint32
} `tlb:"^"`
}
err := tlb.Unmarshal(data, &body)
if err != nil {
return nil, err
}
return tlb.VmStack{
{
SumType: "VmStkInt",
VmStkInt: tlb.Int257(body.Value1),
},
{
SumType: "VmStkInt",
VmStkInt: tlb.Int257(body.Value2),
},
{
SumType: "VmStkTinyInt",
VmStkTinyInt: int64(body.Field1.Value3),
},
{
SumType: "VmStkTinyInt",
VmStkTinyInt: int64(body.Field1.Value4),
},
}, nil
}
64 changes: 64 additions & 0 deletions tvm/precompiled/wallets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package precompiled

import (
"math/big"

"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
)

var walletv4r2seqno = func(data *boc.Cell, args tlb.VmStack) (tlb.VmStack, error) {
var dataV4 struct {
Seqno uint32
SubWalletId uint32
PublicKey tlb.Bits256
}
err := tlb.Unmarshal(data, &dataV4)
if err != nil {
return nil, err
}
return tlb.VmStack{
{
SumType: "VmStkTinyInt",
VmStkTinyInt: int64(dataV4.Seqno),
},
}, nil
}

var walletv4r2SubwalletID = func(data *boc.Cell, args tlb.VmStack) (tlb.VmStack, error) {
var dataV4 struct {
Seqno uint32
SubWalletId uint32
PublicKey tlb.Bits256
}
err := tlb.Unmarshal(data, &dataV4)
if err != nil {
return nil, err
}
return tlb.VmStack{
{
SumType: "VmStkTinyInt",
VmStkTinyInt: int64(dataV4.SubWalletId),
},
}, nil
}

var walletv4r2publicKey = func(data *boc.Cell, args tlb.VmStack) (tlb.VmStack, error) {
var dataV4 struct {
Seqno uint32
SubWalletId uint32
PublicKey tlb.Bits256
}
err := tlb.Unmarshal(data, &dataV4)
if err != nil {
return nil, err
}
var b big.Int
b.SetBytes(dataV4.PublicKey[:])
return tlb.VmStack{
{
SumType: "VmStkInt",
VmStkInt: tlb.Int257(b),
},
}, nil
}

0 comments on commit 76c7397

Please sign in to comment.