Skip to content

Commit

Permalink
get owned token from enumerable contract
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Dec 13, 2024
1 parent 20a7e91 commit aea7c03
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 57 deletions.
43 changes: 0 additions & 43 deletions frontend/src/ExplorerApiClient.ts

This file was deleted.

81 changes: 67 additions & 14 deletions frontend/src/utils/write-mns-sc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import {
Client,
EOperationStatus,
ICallData,
IDatastoreEntryInput,
MAX_GAS_CALL,
bytesToStr,
bytesToU256,
bytesToU64,
strToBytes,
toMAS,
} from '@massalabs/massa-web3';
import { ToastContent, toast } from '@massalabs/react-ui-kit';
Expand All @@ -18,8 +21,8 @@ import {
} from '../const/sc';
import { OperationToast } from '../lib/connectMassaWallets/components/OperationToast';
import { logSmartContractEvents } from '../lib/connectMassaWallets/utils';
import { ExplorerApiClient } from '../ExplorerApiClient';
import { useAccountStore } from '../lib/connectMassaWallets/store';
import { isEqual } from 'lodash';

interface ToasterMessage {
pending: string;
Expand Down Expand Up @@ -92,7 +95,6 @@ export function useWriteMNS(client?: Client) {
const [opId, setOpId] = useState<string | undefined>(undefined);
const [list, setList] = useState<DnsUserEntryListResult[]>([]);
const [listSpinning, setListSpinning] = useState(false);
const explorerApi = new ExplorerApiClient();
const { chainId } = useAccountStore();

async function getAllocCost(
Expand Down Expand Up @@ -352,21 +354,72 @@ export function useWriteMNS(client?: Client) {

let list: DnsUserEntryListResult[] = [];

let domains = await explorerApi.getDomainOwnedByAddress(params.address);
if (domains && domains.length > 0) {
let dnsInfos = await explorerApi.getDomainsInfo(domains);
const addressInfo = await client?.publicApi().getAddresses([SC_ADDRESS]);
const filter = [
...strToBytes('ownedTokens'),
...strToBytes(params.address),
];
const ownedKeys = addressInfo?.[0].candidate_datastore_keys.filter(
(key) => {
return isEqual(key.slice(0, filter.length), filter);
},
);

for (const domain in dnsInfos) {
if (Object.prototype.hasOwnProperty.call(dnsInfos, domain)) {
list.push({
domain: domain,
targetAddress: dnsInfos[domain].target_address,
tokenId: dnsInfos[domain].tokenId,
});
}
if (ownedKeys) {
const tokenIdsBytes = ownedKeys.map((key) => key.slice(filter.length));
const tokenIds = tokenIdsBytes.map((val) =>
bytesToU256(Uint8Array.from(val)),
);

const DOMAIN_SEPARATOR_KEY = [0x42];
const DOMAIN_KEY_PREFIX = [0x3];
const TARGET_KEY_PREFIX = [0x02];

const domainsDataStoreEntries: IDatastoreEntryInput[] = tokenIdsBytes.map(
(tokenIdBytes) => ({
address: SC_ADDRESS,
key: Uint8Array.from([
...DOMAIN_SEPARATOR_KEY,
...DOMAIN_KEY_PREFIX,
...tokenIdBytes,
]),
}),
);

const domainsRes = await client!
.publicApi()
.getDatastoreEntries(domainsDataStoreEntries);
const domains = domainsRes.map((val) => bytesToStr(val.candidate_value!));

const targetDataStoreEntries: IDatastoreEntryInput[] = domains.map(
(domain) => ({
address: SC_ADDRESS,
key: Uint8Array.from([
...DOMAIN_SEPARATOR_KEY,
...TARGET_KEY_PREFIX,
...strToBytes(domain),
]),
}),
);
const targetsRes = await client!
.publicApi()
.getDatastoreEntries(targetDataStoreEntries);
const targets = targetsRes.map((val) => bytesToStr(val.candidate_value!));

if (
targets.length !== domains.length ||
domains.length !== tokenIds.length ||
tokenIds.length !== targets.length
) {
console.error('Inconsistent data for owned MNS');
}
}

list = domains.map((domain, index) => ({
domain,
targetAddress: targets[index],
tokenId: tokenIds[index],
}));
}
setList(list);
setListSpinning(false);
return list;
Expand Down

0 comments on commit aea7c03

Please sign in to comment.