Skip to content

Commit

Permalink
calculate jetton wallet address
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksej-paschenko committed Sep 13, 2024
1 parent fcf2d4d commit fc2929a
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions pkg/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/avast/retry-go"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/abi"
boc "github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/contract/jetton"
Expand Down Expand Up @@ -82,13 +83,18 @@ func (h *Handler) convertToWalletInfo(ctx context.Context, airdrop prover.Wallet
if err != nil {
return nil, err
}
jettonWallet, err := h.getJettonWallet(ctx, airdrop.AccountID)
if err != nil {
return nil, err
}
compressedInfo := oas.WalletInfoCompressedInfo{
Amount: strconv.FormatUint(uint64(airdrop.Data.Amount), 10),
StartFrom: strconv.FormatUint(uint64(airdrop.Data.StartFrom), 10),
ExpiredAt: strconv.FormatUint(uint64(airdrop.Data.ExpireAt), 10),
}
return &oas.WalletInfo{
Owner: airdrop.AccountID.ToRaw(),
JettonWallet: jettonWallet.ToRaw(),
CustomPayload: customPayload,
StateInit: oas.NewOptString(stateInit),
CompressedInfo: oas.NewOptWalletInfoCompressedInfo(compressedInfo),
Expand Down Expand Up @@ -141,14 +147,6 @@ func createCustomPayload(proof []byte) (string, error) {
return customPayload.ToBocBase64()
}

type JettonData struct {
Status tlb.Uint4
Balance tlb.Grams
OwnerAddress tlb.MsgAddress
JettonMasterAddress tlb.MsgAddress
MerkleRoot tlb.Bits256
}

func (h *Handler) GetWallets(ctx context.Context, params oas.GetWalletsParams) (*oas.WalletList, error) {
next, err := ton.ParseAccountID(params.NextFrom)
if err != nil {
Expand Down Expand Up @@ -256,3 +254,26 @@ func DecodeGetWalletStateInitAndSaltResult(stack tlb.VmStack) (resultType string
err = stack.Unmarshal(&result)
return "GetWalletStateInitAndSaltResult", result, err
}

func (h *Handler) getJettonWallet(ctx context.Context, owner ton.AccountID) (ton.AccountID, error) {
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()

// TODO: add cache
_, result, err := abi.GetWalletAddress(ctx, h.cli, h.jettonMaster, owner.ToMsgAddress())
if err != nil {
return ton.AccountID{}, err
}
walletAddress, ok := result.(abi.GetWalletAddressResult)
if !ok {
return ton.AccountID{}, fmt.Errorf("failed get wallet address")
}
jettonWalletAccountID, err := tongo.AccountIDFromTlb(walletAddress.JettonWalletAddress)
if err != nil {
return ton.AccountID{}, err
}
if jettonWalletAccountID == nil {
return ton.AccountID{}, fmt.Errorf("failed to resolve jetton wallet")
}
return *jettonWalletAccountID, nil
}

0 comments on commit fc2929a

Please sign in to comment.