Skip to content

Commit

Permalink
Tonkeeper query id (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
KuznetsovNikita authored Feb 14, 2024
1 parent 613e0ac commit fee4062
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
10 changes: 9 additions & 1 deletion packages/core/src/service/transfer/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
storeMessage,
toNano
} from 'ton-core';

import { mnemonicToPrivateKey } from 'ton-crypto';
import { WalletContractV3R1 } from 'ton/dist/wallets/WalletContractV3R1';
import { WalletContractV3R2 } from 'ton/dist/wallets/WalletContractV3R2';
import { WalletContractV4 } from 'ton/dist/wallets/WalletContractV4';
import nacl from 'tweetnacl';
import { APIConfig } from '../../entries/apis';
import { WalletState } from '../../entries/wallet';
import {
Expand Down Expand Up @@ -168,3 +168,11 @@ export async function getKeyPairAndSeqno(options: {
export const getTTL = () => {
return Math.floor(Date.now() / 1e3) + 300; // 5min
};

export const getTonkeeperQueryId = () => {
// cell.WriteUint(0x546de4ef, 32) //crc32("tonkeeper")
// cell.WriteUint(rand.Read(32), 32) //случайные 32 бита

const value = Buffer.concat([Buffer.from('546de4ef', 'hex'), nacl.randomBytes(4)]);
return BigInt(`0x${value.toString('hex')}`);
};
7 changes: 4 additions & 3 deletions packages/core/src/service/transfer/jettonService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
checkWalletBalanceOrDie,
checkWalletPositiveBalanceOrDie,
externalMessage,
getTonkeeperQueryId,
getTTL,
getWalletBalance,
SendMode
Expand All @@ -22,7 +23,7 @@ const jettonTransferAmount = toNano('0.64');
const jettonTransferForwardAmount = BigInt('1');

const jettonTransferBody = (params: {
queryId?: number;
queryId: bigint;
jettonAmount: bigint;
toAddress: Address;
responseAddress: Address;
Expand All @@ -31,7 +32,7 @@ const jettonTransferBody = (params: {
}) => {
return beginCell()
.storeUint(0xf8a7ea5, 32) // request_transfer op
.storeUint(params.queryId || 0, 64)
.storeUint(params.queryId, 64)
.storeCoins(params.jettonAmount)
.storeAddress(params.toAddress)
.storeAddress(params.responseAddress)
Expand All @@ -53,7 +54,7 @@ const createJettonTransfer = (
const jettonAmount = BigInt(amount.stringWeiAmount);

const body = jettonTransferBody({
queryId: Date.now(),
queryId: getTonkeeperQueryId(),
jettonAmount,
toAddress: Address.parse(recipientAddress),
responseAddress: Address.parse(walletState.active.rawAddress),
Expand Down
23 changes: 12 additions & 11 deletions packages/core/src/service/transfer/nftService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ import {
checkWalletPositiveBalanceOrDie,
createTransferMessage,
getKeyPairAndSeqno,
getTonkeeperQueryId,
getWalletBalance
} from './common';

const initNftTransferAmount = toNano('1');
const nftTransferForwardAmount = BigInt('1');

const nftTransferBody = (params: {
queryId?: number;
queryId: bigint;
newOwnerAddress: Address;
responseAddress: Address;
forwardAmount: bigint;
forwardPayload: Cell | null;
}) => {
return beginCell()
.storeUint(0x5fcc3d14, 32) // transfer op
.storeUint(params.queryId || 0, 64)
.storeUint(params.queryId, 64)
.storeAddress(params.newOwnerAddress)
.storeAddress(params.responseAddress)
.storeBit(false) // null custom_payload
Expand All @@ -35,21 +36,21 @@ const nftTransferBody = (params: {
.endCell();
};

const nftRenewBody = (params?: { queryId?: number }) => {
const nftRenewBody = (params: { queryId: bigint }) => {
return beginCell()
.storeUint(0x4eb1f0f9, 32) // op::change_dns_record,
.storeUint(params?.queryId || 0, 64)
.storeUint(params.queryId, 64)
.storeUint(0, 256)
.endCell();
};

const addressToDNSAddressFormat = (address: string) =>
beginCell().storeUint(0x9fd3, 16).storeAddress(Address.parse(address)).storeUint(0, 8);

const nftLinkBody = (params: { queryId?: number; linkToAddress: string }) => {
const nftLinkBody = (params: { queryId: bigint; linkToAddress: string }) => {
let cell = beginCell()
.storeUint(0x4eb1f0f9, 32) // op::change_dns_record,
.storeUint(params?.queryId || 0, 64)
.storeUint(params?.queryId, 64)
.storeUint(
BigInt('0xe8d44050873dba865aa7c170ab4cce64d90839a34dcfd6cf71d14e0205443b1b'),
256
Expand All @@ -72,7 +73,7 @@ const createNftTransfer = (
secretKey: Buffer = Buffer.alloc(64)
) => {
const body = nftTransferBody({
queryId: Date.now(),
queryId: getTonkeeperQueryId(),
newOwnerAddress: Address.parse(recipientAddress),
responseAddress: Address.parse(walletState.active.rawAddress),
forwardAmount: nftTransferForwardAmount,
Expand Down Expand Up @@ -160,7 +161,7 @@ export const sendNftRenew = async (options: {
}) => {
const { seqno, keyPair } = await getKeyPairAndSeqno(options);

const body = nftRenewBody();
const body = nftRenewBody({ queryId: getTonkeeperQueryId() });

const cell = createTransferMessage(
{
Expand All @@ -186,7 +187,7 @@ export const estimateNftRenew = async (options: {
const [wallet, seqno] = await getWalletBalance(options.api, options.walletState);
checkWalletPositiveBalanceOrDie(wallet);

const body = nftRenewBody();
const body = nftRenewBody({ queryId: getTonkeeperQueryId() });

const cell = createTransferMessage(
{
Expand All @@ -211,7 +212,7 @@ export const sendNftLink = async (options: {
}) => {
const { seqno, keyPair } = await getKeyPairAndSeqno(options);

const body = nftLinkBody(options);
const body = nftLinkBody({ ...options, queryId: getTonkeeperQueryId() });

const cell = createTransferMessage(
{
Expand All @@ -238,7 +239,7 @@ export const estimateNftLink = async (options: {
const [wallet, seqno] = await getWalletBalance(options.api, options.walletState);
checkWalletPositiveBalanceOrDie(wallet);

const body = nftLinkBody(options);
const body = nftLinkBody({ ...options, queryId: getTonkeeperQueryId() });

const cell = createTransferMessage(
{
Expand Down

0 comments on commit fee4062

Please sign in to comment.