Skip to content

Commit

Permalink
v3.0.21
Browse files Browse the repository at this point in the history
  • Loading branch information
mytonwalletorg committed Sep 25, 2024
1 parent 87c90e3 commit 5f1fae4
Show file tree
Hide file tree
Showing 56 changed files with 667 additions and 173 deletions.
1 change: 1 addition & 0 deletions changelogs/3.0.21.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bug fixes and performance improvements
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mytonwallet",
"version": "3.0.20",
"version": "3.0.21",
"description": "The most feature-rich web wallet and browser extension for TON – with support of multi-accounts, tokens (jettons), NFT, TON DNS, TON Sites, TON Proxy, and TON Magic.",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion public/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.20
3.0.21
1 change: 1 addition & 0 deletions src/api/chains/ton/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export {
getTokenBalances,
getAddressTokenBalances,
fetchToken,
insertMintlessPayload,
} from './tokens';
export {
resolveTokenWalletAddress,
Expand Down
2 changes: 1 addition & 1 deletion src/api/chains/ton/nfts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export async function submitNftTransfers(options: {
};
});

return submitMultiTransfer(accountId, password, messages);
return submitMultiTransfer({ accountId, password, messages });
}

function buildNotcoinVoucherExchange(fromAddress: string, nftAddress: string, nftIndex: number) {
Expand Down
153 changes: 123 additions & 30 deletions src/api/chains/ton/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import type { JettonBalance } from 'tonapi-sdk-js';
import { Address, Cell } from '@ton/core';

import type { ApiNetwork, ApiToken } from '../../types';
import type { AnyPayload, ApiTransactionExtra, JettonMetadata } from './types';
import type {
AnyPayload, ApiTransactionExtra, JettonMetadata, TonTransferParams,
} from './types';

import { TINY_TOKENS } from '../../../config';
import { parseAccountId } from '../../../util/account';
Expand All @@ -13,6 +15,7 @@ import {
fetchJettonMetadata,
fixBase64ImageData,
parseJettonWalletMsgBody,
parsePayloadBase64,
} from './util/metadata';
import { fetchJettonBalances } from './util/tonapiio';
import {
Expand Down Expand Up @@ -118,43 +121,88 @@ export function parseTokenTransaction(
};
}

export async function buildTokenTransfer(
export async function insertMintlessPayload(
network: ApiNetwork,
tokenAddress: string,
fromAddress: string,
toAddress: string,
amount: bigint,
payload?: AnyPayload,
) {
const tokenWalletAddress = await resolveTokenWalletAddress(network, fromAddress, tokenAddress);
const tokenWallet = getTokenWallet(network, tokenWalletAddress);
const token = getTokenByAddress(tokenAddress)!;
tokenAddress: string,
transfer: TonTransferParams,
): Promise<TonTransferParams> {
const { toAddress, payload } = transfer;

let isTokenWalletDeployed = true;
let customPayload: string | undefined;
let stateInit: string | undefined;
const token = getTokenByAddress(tokenAddress);
if (typeof payload !== 'string' || !token?.customPayloadApiUrl) {
return transfer;
}

const isMintlessToken = !!token.customPayloadApiUrl;
let isMintlessClaimed: boolean | undefined;
let mintlessTokenBalance: bigint | undefined;
const parsedPayload = await parsePayloadBase64(network, toAddress, payload);
if (parsedPayload.type !== 'tokens:transfer') {
throw new Error('Invalid payload');
}

if (isMintlessToken) {
isTokenWalletDeployed = !!(await isActiveSmartContract(network, tokenWalletAddress));
isMintlessClaimed = isTokenWalletDeployed && await checkMintlessTokenWalletIsClaimed(network, tokenWalletAddress);
const {
mintlessTokenBalance,
isMintlessClaimed,
stateInit,
customPayload,
} = await getMintlessParams({
network,
token,
fromAddress,
tokenWalletAddress: transfer.toAddress,
});

if (!isMintlessClaimed) {
const data = await fetchMintlessTokenWalletData(token.customPayloadApiUrl!, fromAddress);
if (!mintlessTokenBalance || isMintlessClaimed) {
return transfer;
}

if (data) {
customPayload = data.custom_payload;
mintlessTokenBalance = BigInt(data.compressed_info.amount);
const newPayload = buildTokenTransferBody({
toAddress: parsedPayload.destination,
queryId: parsedPayload.queryId,
tokenAmount: parsedPayload.amount,
forwardAmount: parsedPayload.forwardAmount,
forwardPayload: Cell.fromBase64(parsedPayload.forwardPayload!),
responseAddress: parsedPayload.responseDestination,
customPayload: Cell.fromBase64(customPayload!),
});

if (!isTokenWalletDeployed) {
stateInit = data.state_init;
}
}
}
}
return {
...transfer,
stateInit: stateInit ? Cell.fromBase64(stateInit) : undefined,
payload: newPayload,
isBase64Payload: false,
};
}

export async function buildTokenTransfer(options: {
network: ApiNetwork;
tokenAddress: string;
fromAddress: string;
toAddress: string;
amount: bigint;
payload?: AnyPayload;
}) {
const {
network,
tokenAddress,
fromAddress,
toAddress,
amount,
} = options;
let { payload } = options;

const tokenWalletAddress = await resolveTokenWalletAddress(network, fromAddress, tokenAddress);
const tokenWallet = getTokenWallet(network, tokenWalletAddress);
const token = getTokenByAddress(tokenAddress)!;

const {
isTokenWalletDeployed,
isMintlessClaimed,
mintlessTokenBalance,
customPayload,
stateInit,
} = await getMintlessParams({
network, fromAddress, token, tokenWalletAddress,
});

if (isTokenWalletDeployed) {
const realTokenAddress = await resolveTokenAddress(network, tokenWalletAddress);
Expand Down Expand Up @@ -191,6 +239,51 @@ export async function buildTokenTransfer(
};
}

export async function getMintlessParams(options: {
network: ApiNetwork;
fromAddress: string;
token: ApiToken;
tokenWalletAddress: string;
}) {
const {
network, fromAddress, token, tokenWalletAddress,
} = options;

let isTokenWalletDeployed = true;
let customPayload: string | undefined;
let stateInit: string | undefined;

const isMintlessToken = !!token.customPayloadApiUrl;
let isMintlessClaimed: boolean | undefined;
let mintlessTokenBalance: bigint | undefined;

if (isMintlessToken) {
isTokenWalletDeployed = !!(await isActiveSmartContract(network, tokenWalletAddress));
isMintlessClaimed = isTokenWalletDeployed && await checkMintlessTokenWalletIsClaimed(network, tokenWalletAddress);

if (!isMintlessClaimed) {
const data = await fetchMintlessTokenWalletData(token.customPayloadApiUrl!, fromAddress);

if (data) {
customPayload = data.custom_payload;
mintlessTokenBalance = BigInt(data.compressed_info.amount);

if (!isTokenWalletDeployed) {
stateInit = data.state_init;
}
}
}
}

return {
isTokenWalletDeployed,
isMintlessClaimed,
mintlessTokenBalance,
customPayload,
stateInit,
};
}

export async function checkMintlessTokenWalletIsClaimed(network: ApiNetwork, tokenWalletAddress: string) {
const res = await getTonClient(network)
.runMethod(Address.parse(tokenWalletAddress), 'is_claimed');
Expand Down
Loading

0 comments on commit 5f1fae4

Please sign in to comment.