Skip to content

Commit

Permalink
New deploy contract method
Browse files Browse the repository at this point in the history
  • Loading branch information
xssnick committed Aug 15, 2023
1 parent 9aa1c93 commit 7554a49
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
1 change: 0 additions & 1 deletion liteclient/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ func (c *ConnectionPool) QueryADNL(ctx context.Context, request tl.Serializable,
return err
}
}
println("QWITH", host)

_, hasDeadline := ctx.Deadline()
if !hasDeadline {
Expand Down
7 changes: 1 addition & 6 deletions ton/payments/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ func NewPaymentChannelClient(api TonApi) *Client {
}
}

func (c *Client) GetAsyncChannel(ctx context.Context, addr *address.Address, verify bool) (*AsyncChannel, error) {
block, err := c.api.CurrentMasterchainInfo(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get block: %w", err)
}

func (c *Client) GetAsyncChannel(ctx context.Context, block *ton.BlockIDExt, addr *address.Address, verify bool) (*AsyncChannel, error) {
acc, err := c.api.GetAccount(ctx, block, addr)
if err != nil {
return nil, fmt.Errorf("failed to get account: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions ton/payments/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ func TestClient_DeployAsyncChannel(t *testing.T) {
t.Fatal(fmt.Errorf("failed to build deploy channel params: %w", err))
}

channelAddr, err := w.DeployContract(context.Background(), tlb.MustFromTON("0.02"), body, code, data, true)
channelAddr, _, block, err := w.DeployContractWaitTransaction(context.Background(), tlb.MustFromTON("0.02"), body, code, data)
if err != nil {
t.Fatal(fmt.Errorf("failed to deploy channel: %w", err))
}

ch, err := client.GetAsyncChannel(context.Background(), channelAddr, true)
ch, err := client.GetAsyncChannel(context.Background(), block, channelAddr, true)
if err != nil {
t.Fatal(fmt.Errorf("failed to get channel: %w", err))
}
Expand Down
10 changes: 4 additions & 6 deletions ton/retrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package ton

import (
"context"
"encoding/json"
"fmt"
"github.com/xssnick/tonutils-go/tl"
"os"
"strings"
)

Expand Down Expand Up @@ -36,10 +34,10 @@ func (w *retryClient) QueryLiteserver(ctx context.Context, payload tl.Serializab
}

if tmp, ok := result.(*tl.Serializable); ok && tmp != nil {
if lsErr, ok := (*tmp).(LSError); ok && (lsErr.Code == 651 || lsErr.Code == 652 || lsErr.Code == -400) {
println("RETRY", tries, lsErr.Code)
json.NewEncoder(os.Stdout).Encode(payload)

if lsErr, ok := (*tmp).(LSError); ok && (lsErr.Code == 651 ||
lsErr.Code == 652 ||
lsErr.Code == -400 ||
(lsErr.Code == 0 && strings.Contains(lsErr.Text, "Failed to get account state"))) {
if ctx, err = w.original.StickyContextNextNode(ctx); err != nil { // try next node
// no more nodes left, return as it is
return nil
Expand Down
8 changes: 1 addition & 7 deletions ton/wallet/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,12 @@ func TestWallet_DeployContract(t *testing.T) {
codeBytes, _ := hex.DecodeString("b5ee9c72410104010020000114ff00f4a413f4bcf2c80b010203844003020009a1b63c43510007a0000061d2421bb1")
code, _ := cell.FromBOC(codeBytes)

addr, err := w.DeployContract(ctx, tlb.MustFromTON("0.005"), cell.BeginCell().EndCell(), code, cell.BeginCell().MustStoreUInt(rand.Uint64(), 64).EndCell(), true)
addr, _, block, err := w.DeployContractWaitTransaction(ctx, tlb.MustFromTON("0.005"), cell.BeginCell().EndCell(), code, cell.BeginCell().MustStoreUInt(rand.Uint64(), 64).EndCell())
if err != nil {
t.Fatal("deploy err:", err)
}
t.Logf("contract address: %s", addr.String())

block, err := api.CurrentMasterchainInfo(ctx)
if err != nil {
t.Fatal("CurrentMasterchainInfo err:", err.Error())
return
}

res, err := api.RunGetMethod(ctx, block, addr, "dappka", 5, 10)
if err != nil {
t.Fatal("run err:", err)
Expand Down
31 changes: 31 additions & 0 deletions ton/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,37 @@ func (w *Wallet) transfer(ctx context.Context, to *address.Address, amount tlb.C
return w.Send(ctx, transfer, waitConfirmation...)
}

func (w *Wallet) DeployContractWaitTransaction(ctx context.Context, amount tlb.Coins, msgBody, contractCode, contractData *cell.Cell) (*address.Address, *tlb.Transaction, *ton.BlockIDExt, error) {
state := &tlb.StateInit{
Data: contractData,
Code: contractCode,
}

stateCell, err := tlb.ToCell(state)
if err != nil {
return nil, nil, nil, err
}

addr := address.NewAddress(0, 0, stateCell.Hash())

tx, block, err := w.SendWaitTransaction(ctx, &Message{
Mode: 1 + 2,
InternalMessage: &tlb.InternalMessage{
IHRDisabled: true,
Bounce: false,
DstAddr: addr,
Amount: amount,
Body: msgBody,
StateInit: state,
},
})
if err != nil {
return nil, nil, nil, err
}
return addr, tx, block, nil
}

// Deprecated: use DeployContractWaitTransaction
func (w *Wallet) DeployContract(ctx context.Context, amount tlb.Coins, msgBody, contractCode, contractData *cell.Cell, waitConfirmation ...bool) (*address.Address, error) {
state := &tlb.StateInit{
Data: contractData,
Expand Down

0 comments on commit 7554a49

Please sign in to comment.