Skip to content

Commit

Permalink
feature (soulname renewal): added a function to load all soulnames ev…
Browse files Browse the repository at this point in the history
…en when they are expired
  • Loading branch information
H34D committed Jan 18, 2024
1 parent 6b86fa6 commit 53fefb0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/modules/soul-name/load.ts
Expand Up @@ -84,6 +84,12 @@ export const loadSoulNamesByNames = async (
) as SoulNameDetails[];
};

/**
* loads active soul names
*
* @param masa
* @param identityIdOrAddress
*/
export const loadSoulNames = async (
masa: MasaInterface,
identityIdOrAddress: BigNumber | string,
Expand All @@ -106,6 +112,55 @@ export const loadSoulNames = async (
return soulNames;
};

/**
* loads all soul names even expired ones
*
* @param masa
* @param identityIdOrAddress
*/
export const loadSoulNamesWithExpired = async (
masa: MasaInterface,
identityIdOrAddress: BigNumber | string,
): Promise<string[]> => {
let soulNames: string[] = [];

try {
const { "ownerOf(uint256)": ownerOfIdentity } =
masa.contracts.instances.SoulboundIdentityContract;

const address = isBigNumber(identityIdOrAddress)
? await ownerOfIdentity(identityIdOrAddress)
: identityIdOrAddress;

const { balanceOf, tokenOfOwnerByIndex, tokenData } =
masa.contracts.instances.SoulNameContract;

const [loadedSoulnames, balance] = await Promise.all([
loadSoulNames(masa, address),
balanceOf(address),
]);

soulNames = loadedSoulnames;

// getSoulNames does not return expired soul names, this means if we encounter such a name
// we might load it in a different way
if (balance.toNumber() !== soulNames.length) {
for (let i = 0; i < balance.toNumber(); i++) {
const soulnameTokenId = await tokenOfOwnerByIndex(address, i);
const { name } = await tokenData(soulnameTokenId);

if (soulNames.indexOf(name) === -1) {
soulNames = [...soulNames, name];
}
}
}
} catch {
// ignore
}

return soulNames;
};

export const loadSoulNameDetailsByAddress = async (
masa: MasaInterface,
address: string,
Expand Down
8 changes: 8 additions & 0 deletions src/modules/soul-name/soul-name.ts
Expand Up @@ -10,6 +10,7 @@ import {
loadSoulNameByName,
loadSoulNameByTokenId,
loadSoulNames,
loadSoulNamesWithExpired,
} from "./load";
import { renewSoulName } from "./renew";
import { resolveSoulName } from "./resolve";
Expand Down Expand Up @@ -44,6 +45,13 @@ export class MasaSoulName extends MasaBase {
loadSoulNames = (identityIdOrAddress: BigNumber | string) =>
loadSoulNames(this.masa, identityIdOrAddress);

/**
*
* @param identityIdOrAddress
*/
loadSoulNamesWithExpired = (identityIdOrAddress: BigNumber | string) =>
loadSoulNamesWithExpired(this.masa, identityIdOrAddress);

/**
*
* @param soulName
Expand Down

0 comments on commit 53fefb0

Please sign in to comment.