Skip to content

Commit

Permalink
Update contract routing
Browse files Browse the repository at this point in the history
  • Loading branch information
KuznetsovNikita committed May 15, 2024
1 parent dd4b8c2 commit 7e2bb39
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
18 changes: 11 additions & 7 deletions packages/uikit/src/desktop-pages/notcoin/NotcoinPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { Address, BitString, SendMode, beginCell, internal, toNano } from '@ton/core';
import { Address, SendMode, beginCell, internal, toNano } from '@ton/core';
import { APIConfig } from '@tonkeeper/core/dist/entries/apis';
import { CellSigner, Signer } from '@tonkeeper/core/dist/entries/signer';
import { WalletState } from '@tonkeeper/core/dist/entries/wallet';
Expand Down Expand Up @@ -34,6 +34,7 @@ import { useAppSdk, useToast } from '../../hooks/appSdk';
import { useDateTimeFormat } from '../../hooks/useDateTimeFormat';
import { getSigner } from '../../state/mnemonic';
import { useCheckTouchId } from '../../state/password';
import { chooseAddress } from './address';

const useVouchers = () => {
const wallet = useWalletContext();
Expand Down Expand Up @@ -85,16 +86,19 @@ export const confirmWalletSeqNo = async (
};

const getNotcoinBurnAddress = (nftAddress: string, config: TonendpointConfig) => {
const nftAddressBits = new BitString(Address.parse(nftAddress).hash, 0, 4);

const burnAddresses = config.notcoin_burn_addresses ?? [];
const burnAddressesBits = burnAddresses.map(
(address: string) => new BitString(Address.parse(address).hash, 0, 4)

const { match } = chooseAddress(
Address.parse(nftAddress),
burnAddresses.map(item => Address.parse(item))
);

const index = burnAddressesBits.findIndex(item => nftAddressBits.equals(item));
if (match) {
return match;
}

return Address.parse(burnAddresses[index]);
var item = burnAddresses[Math.floor(Math.random() * burnAddresses.length)];
return Address.parse(item);
};

const checkBurnDate = async (api: APIConfig, config: TonendpointConfig) => {
Expand Down
70 changes: 70 additions & 0 deletions packages/uikit/src/desktop-pages/notcoin/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Address } from '@ton/core';

function findMatchingBits(a: number, b: number, start_from: number) {
let bitPos = start_from;
let keepGoing = true;
do {
const bitCount = bitPos + 1;
const mask = (1 << bitCount) - 1;
const shift = 8 - bitCount;
if (((a >> shift) & mask) == ((b >> shift) & mask)) {
bitPos++;
} else {
keepGoing = false;
}
} while (keepGoing && bitPos < 7);

return bitPos;
}

export function chooseAddress(user: Address, contracts: Address[]) {
const maxBytes = 32;
let byteIdx = 0;
let bitIdx = 0;
let bestMatch: Address | undefined;

if (user.workChain !== 0) {
throw new TypeError(`Only basechain user address allowed:${user}`);
}
for (let testContract of contracts) {
if (testContract.workChain !== 0) {
throw new TypeError(`Only basechain deposit address allowed:${testContract}`);
}
if (byteIdx >= maxBytes) {
break;
}
if (
byteIdx == 0 ||
testContract.hash.subarray(0, byteIdx).equals(user.hash.subarray(0, byteIdx))
) {
let keepGoing = true;
do {
if (keepGoing && testContract.hash[byteIdx] == user.hash[byteIdx]) {
bestMatch = testContract;
byteIdx++;
bitIdx = 0;
if (byteIdx == maxBytes) {
break;
}
} else {
keepGoing = false;
if (bitIdx < 7) {
const resIdx = findMatchingBits(
user.hash[byteIdx],
testContract.hash[byteIdx],
bitIdx
);
if (resIdx > bitIdx) {
bitIdx = resIdx;
bestMatch = testContract;
}
}
}
} while (keepGoing);
}
}
return {
match: bestMatch,
prefixLength: byteIdx * 8 + bitIdx
};
}

0 comments on commit 7e2bb39

Please sign in to comment.