Skip to content

Commit 0bca669

Browse files
[NONEVM-2034] Use TLB tags for encoding messages in wrappers (#82)
1 parent cc763fb commit 0bca669

File tree

14 files changed

+160
-378
lines changed

14 files changed

+160
-378
lines changed

integration-tests/tracetracking/async/wrappers/requestreply/item_price.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
test_utils "integration-tests/utils"
77

88
"github.com/xssnick/tonutils-go/tlb"
9-
"github.com/xssnick/tonutils-go/tvm/cell"
109

1110
"github.com/smartcontractkit/chainlink-ton/pkg/ton/tracetracking"
1211
"github.com/smartcontractkit/chainlink-ton/pkg/ton/wrappers"
@@ -25,26 +24,20 @@ func NewItemPriceProvider(apiClient tracetracking.SignedAPIClient) *ItemPricePro
2524
}
2625

2726
type ItemPriceInitData struct {
28-
ID uint32
29-
Price uint64
27+
ID uint32 `tlb:"## 32"`
28+
Price uint64 `tlb:"## 64"`
3029
}
3130

3231
func (p *ItemPriceProvider) Deploy(initData ItemPriceInitData) (ItemPrice, error) {
33-
// Deploy the contract
34-
b := cell.BeginCell()
35-
err := b.StoreUInt(uint64(initData.ID), 32)
32+
initDataCell, err := tlb.ToCell(initData)
3633
if err != nil {
37-
return ItemPrice{}, fmt.Errorf("failed to store ID: %w", err)
38-
}
39-
err = b.StoreUInt(initData.Price, 64)
40-
if err != nil {
41-
return ItemPrice{}, fmt.Errorf("failed to store Price: %w", err)
34+
return ItemPrice{}, fmt.Errorf("failed to serialize init data: %w", err)
4235
}
4336
compiledContract, err := wrappers.ParseCompiledContract(ItemPriceContractPath)
4437
if err != nil {
4538
return ItemPrice{}, fmt.Errorf("failed to compile contract: %w", err)
4639
}
47-
contract, err := wrappers.Deploy(&p.apiClient, compiledContract, b.EndCell(), tlb.MustFromTON("1"))
40+
contract, err := wrappers.Deploy(&p.apiClient, compiledContract, initDataCell, tlb.MustFromTON("1"))
4841
if err != nil {
4942
return ItemPrice{}, err
5043
}

integration-tests/tracetracking/async/wrappers/requestreply/price_registry.go

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/xssnick/tonutils-go/address"
1010
"github.com/xssnick/tonutils-go/tlb"
11-
"github.com/xssnick/tonutils-go/tvm/cell"
1211

1312
"github.com/smartcontractkit/chainlink-ton/pkg/ton/tracetracking"
1413
"github.com/smartcontractkit/chainlink-ton/pkg/ton/wrappers"
@@ -27,24 +26,19 @@ func NewPriceRegistryProvider(apiClient tracetracking.SignedAPIClient) *PriceReg
2726
}
2827

2928
type PriceRegistryInitData struct {
30-
ID uint32
29+
ID uint32 `tlb:"## 32"`
3130
}
3231

3332
func (p *PriceRegistryProvider) Deploy(initData PriceRegistryInitData) (PriceRegistry, error) {
34-
b := cell.BeginCell()
35-
err := b.StoreUInt(0, 1)
33+
initDataCell, err := tlb.ToCell(wrappers.LazyLoadingTactContractInitData(initData))
3634
if err != nil {
37-
return PriceRegistry{}, fmt.Errorf("failed to store init bit: %w", err)
38-
}
39-
err = b.StoreUInt(uint64(initData.ID), 32)
40-
if err != nil {
41-
return PriceRegistry{}, fmt.Errorf("failed to store ID: %w", err)
35+
return PriceRegistry{}, fmt.Errorf("failed to serialize init data: %w", err)
4236
}
4337
compiledContract, err := wrappers.ParseCompiledContract(PriceRegistryContractPath)
4438
if err != nil {
4539
return PriceRegistry{}, fmt.Errorf("failed to compile contract: %w", err)
4640
}
47-
contract, err := wrappers.Deploy(&p.apiClient, compiledContract, b.EndCell(), tlb.MustFromTON("1"))
41+
contract, err := wrappers.Deploy(&p.apiClient, compiledContract, initDataCell, tlb.MustFromTON("1"))
4842
if err != nil {
4943
return PriceRegistry{}, err
5044
}
@@ -59,32 +53,18 @@ type PriceRegistry struct {
5953
}
6054

6155
type AddPriceItemMessage struct {
62-
queryID uint64
63-
Key uint8
64-
Addr *address.Address
65-
}
66-
67-
func (m AddPriceItemMessage) OpCode() uint64 {
68-
return 0x3
69-
}
70-
func (m AddPriceItemMessage) StoreArgs(b *cell.Builder) error {
71-
err := b.StoreUInt(m.queryID, 64)
72-
if err != nil {
73-
return fmt.Errorf("failed to store queryID: %w", err)
74-
}
75-
err = b.StoreUInt(uint64(m.Key), 8)
76-
if err != nil {
77-
return fmt.Errorf("failed to store Key: %w", err)
78-
}
79-
err = b.StoreAddr(m.Addr)
80-
if err != nil {
81-
return fmt.Errorf("failed to store Addr: %w", err)
82-
}
83-
return nil
56+
_ tlb.Magic `tlb:"#00000003"` //nolint:revive // This field should stay uninitialized
57+
QueryID uint64 `tlb:"## 64"`
58+
Key uint8 `tlb:"## 8"`
59+
Addr *address.Address `tlb:"addr"`
8460
}
8561

8662
func (p PriceRegistry) SendAddPriceItem(key uint8, addr *address.Address) (msgReceived *tracetracking.ReceivedMessage, err error) {
8763
queryID := rand.Uint64()
88-
msgReceived, err = p.Contract.CallWaitRecursively(AddPriceItemMessage{queryID, key, addr}, tlb.MustFromTON("0.5"))
64+
msgReceived, err = p.Contract.CallWaitRecursively(AddPriceItemMessage{
65+
QueryID: queryID,
66+
Key: key,
67+
Addr: addr,
68+
}, tlb.MustFromTON("0.5"))
8969
return msgReceived, err
9070
}

integration-tests/tracetracking/async/wrappers/requestreply/storage.go

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/xssnick/tonutils-go/address"
1010
"github.com/xssnick/tonutils-go/tlb"
11-
"github.com/xssnick/tonutils-go/tvm/cell"
1211

1312
"github.com/smartcontractkit/chainlink-ton/pkg/ton/tracetracking"
1413
"github.com/smartcontractkit/chainlink-ton/pkg/ton/wrappers"
@@ -27,25 +26,19 @@ func NewStorageProvider(apiClient tracetracking.SignedAPIClient) *StorageProvide
2726
}
2827

2928
type StorageInitData struct {
30-
ID uint32
29+
ID uint32 `tlb:"## 32"`
3130
}
3231

3332
func (p *StorageProvider) Deploy(initData StorageInitData) (Storage, error) {
34-
// Deploy the contract
35-
b := cell.BeginCell()
36-
err := b.StoreUInt(0, 1)
33+
initDataCell, err := tlb.ToCell(wrappers.LazyLoadingTactContractInitData(initData))
3734
if err != nil {
38-
return Storage{}, fmt.Errorf("failed to store init bit: %w", err)
39-
}
40-
err = b.StoreUInt(uint64(initData.ID), 32)
41-
if err != nil {
42-
return Storage{}, fmt.Errorf("failed to store ID: %w", err)
35+
return Storage{}, fmt.Errorf("failed to serialize init data: %w", err)
4336
}
4437
compiledContract, err := wrappers.ParseCompiledContract(StorageContractPath)
4538
if err != nil {
4639
return Storage{}, fmt.Errorf("failed to compile contract: %w", err)
4740
}
48-
contract, err := wrappers.Deploy(&p.apiClient, compiledContract, b.EndCell(), tlb.MustFromTON("1"))
41+
contract, err := wrappers.Deploy(&p.apiClient, compiledContract, initDataCell, tlb.MustFromTON("1"))
4942
if err != nil {
5043
return Storage{}, err
5144
}
@@ -60,33 +53,19 @@ type Storage struct {
6053
}
6154

6255
type getPriceFromMessage struct {
63-
queryID uint64
64-
PriceRegistry *address.Address
65-
Key uint8
66-
}
67-
68-
func (m getPriceFromMessage) OpCode() uint64 {
69-
return 0x1
70-
}
71-
func (m getPriceFromMessage) StoreArgs(b *cell.Builder) error {
72-
err := b.StoreUInt(m.queryID, 64)
73-
if err != nil {
74-
return fmt.Errorf("failed to store queryID: %w", err)
75-
}
76-
err = b.StoreAddr(m.PriceRegistry)
77-
if err != nil {
78-
return fmt.Errorf("failed to store PriceRegistry address: %w", err)
79-
}
80-
err = b.StoreUInt(uint64(m.Key), 8)
81-
if err != nil {
82-
return fmt.Errorf("failed to store Key: %w", err)
83-
}
84-
return nil
56+
_ tlb.Magic `tlb:"#00000001"` //nolint:revive // This field should stay uninitialized
57+
QueryID uint64 `tlb:"## 64"`
58+
PriceRegistry *address.Address `tlb:"addr"`
59+
Key uint8 `tlb:"## 8"`
8560
}
8661

8762
func (s Storage) SendGetPriceFrom(priceRegistry *address.Address, key uint8) (msgReceived *tracetracking.ReceivedMessage, err error) {
8863
queryID := rand.Uint64()
89-
msgReceived, err = s.Contract.CallWaitRecursively(getPriceFromMessage{queryID, priceRegistry, key}, tlb.MustFromTON("0.5"))
64+
msgReceived, err = s.Contract.CallWaitRecursively(getPriceFromMessage{
65+
QueryID: queryID,
66+
PriceRegistry: priceRegistry,
67+
Key: key,
68+
}, tlb.MustFromTON("0.5"))
9069
return msgReceived, err
9170
}
9271

integration-tests/tracetracking/async/wrappers/requestreplywithtwodependencies/inventory.go

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/xssnick/tonutils-go/address"
1010
"github.com/xssnick/tonutils-go/tlb"
11-
"github.com/xssnick/tonutils-go/tvm/cell"
1211

1312
"github.com/smartcontractkit/chainlink-ton/pkg/ton/tracetracking"
1413
"github.com/smartcontractkit/chainlink-ton/pkg/ton/wrappers"
@@ -27,24 +26,19 @@ func NewInventoryProvider(apiClient tracetracking.SignedAPIClient) *InventoryPro
2726
}
2827

2928
type InventoryInitData struct {
30-
ID uint32
29+
ID uint32 `tlb:"## 32"`
3130
}
3231

3332
func (p *InventoryProvider) Deploy(initData InventoryInitData) (Inventory, error) {
34-
b := cell.BeginCell()
35-
err := b.StoreUInt(0, 1)
33+
initDataCell, err := tlb.ToCell(wrappers.LazyLoadingTactContractInitData(initData))
3634
if err != nil {
37-
return Inventory{}, fmt.Errorf("failed to store init bit: %w", err)
38-
}
39-
err = b.StoreUInt(uint64(initData.ID), 32)
40-
if err != nil {
41-
return Inventory{}, fmt.Errorf("failed to store ID: %w", err)
35+
return Inventory{}, fmt.Errorf("failed to serialize init data: %w", err)
4236
}
4337
compiledContract, err := wrappers.ParseCompiledContract(InventoryContractPath)
4438
if err != nil {
4539
return Inventory{}, fmt.Errorf("failed to compile contract: %w", err)
4640
}
47-
contract, err := wrappers.Deploy(&p.apiClient, compiledContract, b.EndCell(), tlb.MustFromTON("1"))
41+
contract, err := wrappers.Deploy(&p.apiClient, compiledContract, initDataCell, tlb.MustFromTON("1"))
4842
if err != nil {
4943
return Inventory{}, err
5044
}
@@ -59,38 +53,21 @@ type Inventory struct {
5953
}
6054

6155
type AddItemMessage struct {
62-
queryID uint64
63-
Key uint8
64-
PriceAddr *address.Address
65-
CountAddr *address.Address
66-
}
67-
68-
func (m AddItemMessage) OpCode() uint64 {
69-
return 0x2
70-
}
71-
func (m AddItemMessage) StoreArgs(b *cell.Builder) error {
72-
err := b.StoreUInt(m.queryID, 64)
73-
if err != nil {
74-
return fmt.Errorf("failed to store queryID: %w", err)
75-
}
76-
err = b.StoreUInt(uint64(m.Key), 8)
77-
if err != nil {
78-
return fmt.Errorf("failed to store Key: %w", err)
79-
}
80-
err = b.StoreAddr(m.PriceAddr)
81-
if err != nil {
82-
return fmt.Errorf("failed to store PriceAddr: %w", err)
83-
}
84-
err = b.StoreAddr(m.CountAddr)
85-
if err != nil {
86-
return fmt.Errorf("failed to store CountAddr: %w", err)
87-
}
88-
return nil
56+
_ tlb.Magic `tlb:"#00000002"` //nolint:revive // This field should stay uninitialized
57+
QueryID uint64 `tlb:"## 64"`
58+
Key uint8 `tlb:"## 8"`
59+
PriceAddr *address.Address `tlb:"addr"`
60+
CountAddr *address.Address `tlb:"addr"`
8961
}
9062

9163
func (p Inventory) SendAddItem(key uint8, priceAddr *address.Address, countAddr *address.Address) (msgReceived *tracetracking.ReceivedMessage, err error) {
9264
queryID := rand.Uint64()
93-
msgReceived, err = p.Contract.CallWaitRecursively(AddItemMessage{queryID, key, priceAddr, countAddr}, tlb.MustFromTON("0.5"))
65+
msgReceived, err = p.Contract.CallWaitRecursively(AddItemMessage{
66+
QueryID: queryID,
67+
Key: key,
68+
PriceAddr: priceAddr,
69+
CountAddr: countAddr,
70+
}, tlb.MustFromTON("0.5"))
9471
return msgReceived, err
9572
}
9673

integration-tests/tracetracking/async/wrappers/requestreplywithtwodependencies/item_count.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
test_utils "integration-tests/utils"
77

88
"github.com/xssnick/tonutils-go/tlb"
9-
"github.com/xssnick/tonutils-go/tvm/cell"
109

1110
"github.com/smartcontractkit/chainlink-ton/pkg/ton/tracetracking"
1211
"github.com/smartcontractkit/chainlink-ton/pkg/ton/wrappers"
@@ -25,26 +24,20 @@ func NewItemCountProvider(apiClient tracetracking.SignedAPIClient) *ItemCountPro
2524
}
2625

2726
type ItemCountInitData struct {
28-
ID uint32
29-
Count uint64
27+
ID uint32 `tlb:"## 32"`
28+
Count uint64 `tlb:"## 64"`
3029
}
3130

3231
func (p *ItemCountProvider) Deploy(initData ItemCountInitData) (ItemCount, error) {
33-
// Deploy the contract
34-
b := cell.BeginCell()
35-
err := b.StoreUInt(uint64(initData.ID), 32)
32+
initDataCell, err := tlb.ToCell(initData)
3633
if err != nil {
37-
return ItemCount{}, fmt.Errorf("failed to store ID: %w", err)
38-
}
39-
err = b.StoreUInt(initData.Count, 64)
40-
if err != nil {
41-
return ItemCount{}, fmt.Errorf("failed to store Count: %w", err)
34+
return ItemCount{}, fmt.Errorf("failed to serialize init data: %w", err)
4235
}
4336
compiledContract, err := wrappers.ParseCompiledContract(ItemCountContractPath)
4437
if err != nil {
4538
return ItemCount{}, fmt.Errorf("failed to compile contract: %w", err)
4639
}
47-
contract, err := wrappers.Deploy(&p.apiClient, compiledContract, b.EndCell(), tlb.MustFromTON("1"))
40+
contract, err := wrappers.Deploy(&p.apiClient, compiledContract, initDataCell, tlb.MustFromTON("1"))
4841
if err != nil {
4942
return ItemCount{}, err
5043
}

integration-tests/tracetracking/async/wrappers/requestreplywithtwodependencies/item_price.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
test_utils "integration-tests/utils"
77

88
"github.com/xssnick/tonutils-go/tlb"
9-
"github.com/xssnick/tonutils-go/tvm/cell"
109

1110
"github.com/smartcontractkit/chainlink-ton/pkg/ton/tracetracking"
1211
"github.com/smartcontractkit/chainlink-ton/pkg/ton/wrappers"
@@ -25,26 +24,20 @@ func NewItemPriceProvider(apiClient tracetracking.SignedAPIClient) *ItemPricePro
2524
}
2625

2726
type ItemPriceInitData struct {
28-
ID uint32
29-
Price uint64
27+
ID uint32 `tlb:"## 32"`
28+
Price uint64 `tlb:"## 64"`
3029
}
3130

3231
func (p *ItemPriceProvider) Deploy(initData ItemPriceInitData) (ItemPrice, error) {
33-
// Deploy the contract
34-
b := cell.BeginCell()
35-
err := b.StoreUInt(uint64(initData.ID), 32)
32+
initDataCell, err := tlb.ToCell(initData)
3633
if err != nil {
37-
return ItemPrice{}, fmt.Errorf("failed to store ID: %w", err)
38-
}
39-
err = b.StoreUInt(initData.Price, 64)
40-
if err != nil {
41-
return ItemPrice{}, fmt.Errorf("failed to store Price: %w", err)
34+
return ItemPrice{}, fmt.Errorf("failed to serialize init data: %w", err)
4235
}
4336
compiledContract, err := wrappers.ParseCompiledContract(ItemPriceContractPath)
4437
if err != nil {
4538
return ItemPrice{}, fmt.Errorf("failed to compile contract: %w", err)
4639
}
47-
contract, err := wrappers.Deploy(&p.apiClient, compiledContract, b.EndCell(), tlb.MustFromTON("1"))
40+
contract, err := wrappers.Deploy(&p.apiClient, compiledContract, initDataCell, tlb.MustFromTON("1"))
4841
if err != nil {
4942
return ItemPrice{}, err
5043
}

0 commit comments

Comments
 (0)