Skip to content

Commit

Permalink
feat: string manifest builder
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurvinci committed Oct 21, 2024
1 parent fa81b6f commit 35da17e
Show file tree
Hide file tree
Showing 9 changed files with 950 additions and 232 deletions.
38 changes: 18 additions & 20 deletions src/GatewayProcessor/GatewayProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ import {
} from "@radixdlt/radix-engine-toolkit";
import { divideInBatches, parseNonFungibleData, withMaxLoops } from "./Utils";
import {
ComponentAddress,
FungibleResource,
NonFungibleItem,
NonFungibleResource,
ResourceAddress,
ResourceInformation,
} from "../Types/RadixTypes";
import pLimit from "p-limit";
Expand Down Expand Up @@ -273,10 +271,10 @@ export class GatewayProcessor {
* @returns A promise resolving to a map containing resource addresses as keys and Resource objects as values.
*/
async getResourcesInformation(
resource_addresses: ResourceAddress[],
resource_addresses: string[],
additional_metadata?: string[],
): Promise<Map<ResourceAddress, ResourceInformation>> {
let resource_map = new Map<ResourceAddress, ResourceInformation>();
): Promise<Map<string, ResourceInformation>> {
let resource_map = new Map<string, ResourceInformation>();
const batches = divideInBatches(resource_addresses, 20);
const limit = pLimit(this._concurrencyLimit);
await Promise.all(
Expand All @@ -297,14 +295,14 @@ export class GatewayProcessor {
* @param entity Address of the component.
*/
async getFungibleResourcesHeldBy(
entity: ComponentAddress,
entity: string,
): Promise<FungibleResource[]> {
const resp = await this.entityDetails([entity]);
const entityState = resp.items[0];
let held_resources: FungibleResource[] = [];

let amount_map = new Map<ResourceAddress, number>();
let resources: ResourceAddress[] = [];
let amount_map = new Map<string, number>();
let resources: string[] = [];

if (entityState.fungible_resources) {
entityState.fungible_resources.items.forEach((resource) => {
Expand Down Expand Up @@ -343,7 +341,7 @@ export class GatewayProcessor {
* @param entity Address of the entity.
*/
async getNonFungibleResourcesHeldBy(
entity: ComponentAddress,
entity: string,
): Promise<NonFungibleResource[]> {
const ledger_state = await this.ledgerState();

Expand Down Expand Up @@ -421,8 +419,8 @@ export class GatewayProcessor {
* @param non_fungible_resource Address of the non-fungible resource.
*/
async getNonFungibleIdsHeldBy(
entity: ComponentAddress,
non_fungible_resource: ResourceAddress,
entity: string,
non_fungible_resource: string,
): Promise<string[]> {
const ledger_state = await this.ledgerState();

Expand Down Expand Up @@ -478,7 +476,7 @@ export class GatewayProcessor {
* @param at_ledger_state Optional ledger state when to make the query.
*/
async getAllNonFungibleIds(
resource_address: ResourceAddress,
resource_address: string,
at_ledger_state?: number,
): Promise<string[]> {
const state_version = at_ledger_state
Expand Down Expand Up @@ -507,7 +505,7 @@ export class GatewayProcessor {
* @param at_ledger_state State against which to make the query.
*/
async getNonFungibleItemsFromIds(
resource_address: ResourceAddress,
resource_address: string,
ids: string[],
at_ledger_state?: number,
): Promise<NonFungibleItem[]> {
Expand Down Expand Up @@ -585,7 +583,7 @@ export class GatewayProcessor {
* @param resource_address Address of the non-fungible items.
* @param ids Ids of the non-fungible items.
*/
async getNftOwners(resource_address: ResourceAddress, ids: string[]) {
async getNftOwners(resource_address: string, ids: string[]) {
const nft_batches = divideInBatches(ids, 100);
const limit = pLimit(this._concurrencyLimit);
let return_map = new Map<string, string>();
Expand All @@ -611,10 +609,10 @@ export class GatewayProcessor {
* Takes up to 20 resource addresses as input
*/
private async limitedResourcesInformation(
resource_addresses: ResourceAddress[],
resource_addresses: string[],
additional_metadata?: string[],
): Promise<Map<ResourceAddress, ResourceInformation>> {
let resource_map = new Map<ResourceAddress, ResourceInformation>();
): Promise<Map<string, ResourceInformation>> {
let resource_map = new Map<string, ResourceInformation>();

let resp = await this.entityDetails(resource_addresses);
resp.items.forEach((item) => {
Expand Down Expand Up @@ -802,7 +800,7 @@ export class GatewayProcessor {
);
}
private async nonFungibleIds(
resource_address: ResourceAddress,
resource_address: string,
at_ledger_state: number,
cursor?: string,
): Promise<StateNonFungibleIdsResponse> {
Expand Down Expand Up @@ -874,7 +872,7 @@ export class GatewayProcessor {
}

private async getNonFungibleData(
address: ResourceAddress,
address: string,
ids: string[],
at_ledger_state?: number,
): Promise<StateNonFungibleDetailsResponseItem[]> {
Expand All @@ -896,7 +894,7 @@ export class GatewayProcessor {
}

private async getEntityLocation(
address: ResourceAddress,
address: string,
ids: string[],
): Promise<StateNonFungibleLocationResponseItem[]> {
return withMaxLoops(
Expand Down
156 changes: 156 additions & 0 deletions src/ManifestBuilder/ManifestTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { Decimal } from "../Types/RadixTypes";
import { NFT } from "../Types/NFT";

/**
* Returns a manifest string representation of an address.
* @param address - The address to represent.
* @returns Manifest representation of the address.
*/
export function manifestAddress(address: string): string {
return `Address("${address}")`;
}

/**
* Returns a manifest string representation of an address reservation.
* @param reservationName - The reservation name.
* @returns Manifest representation of the address reservation.
*/
export function manifestAddressReservation(reservationName: string): string {
return `AddressReservation("${reservationName}")`;
}

/**
* Returns a manifest representation of an array.
* @param typeName - The type of the items in the array.
* @param content - The array content.
* @returns Manifest representation of the array.
*/
export function manifestArray(typeName: string, content: string[]): string {
return `Array<${typeName}>(${arrayToString(content)})`;
}

/**
* Returns a manifest string representation of a bucket.
* @param bucketName - The bucket name.
* @returns Manifest representation of the bucket.
*/
export function manifestBucket(bucketName: string): string {
return `Bucket("${bucketName}")`;
}

/**
* Returns a manifest string representation of a decimal.
* @param {Decimal} decimal - The decimal object.
* @returns Manifest representation of the decimal.
*/
export function manifestDecimal(decimal: Decimal): string {
return `Decimal("${decimal}")`;
}

/**
* Returns a manifest string representation of an enum.
* @param {number} variantId - The variant ID.
* @param fields - The fields of the enum.
* @returns Manifest representation of the enum.
*/
export function manifestEnum(variantId: number, fields: string[]): string {
return `Enum<${variantId}u8>(${arrayToString(fields)})`;
}

/**
* Returns a manifest string representation of a non-fungible global ID.
* @param globalId - The global ID.
* @returns Manifest representation of the non-fungible global ID.
*/
export function manifestGlobalId(globalId: string): string {
return `NonFungibleGlobalId("${globalId}")`;
}

/**
* Returns a manifest string representation of a non-fungible local ID.
* @param id - The local ID.
* @returns Manifest representation of the non-fungible local ID.
*/
export function manifestLocalId(id: string): string {
return `NonFungibleLocalId("${id}")`;
}

/**
* Returns a manifest string representation of an array of non-fungible local IDs.
* @param ids - Array of local IDs.
* @returns Manifest representation of the local ID array.
*/
export function manifestLocalIdArray(ids: string[]): string {
return manifestArray(
"NonFungibleLocalId",
ids.map((id) => manifestLocalId(id)),
);
}

/**
* Returns a manifest string representation of a map.
* @param keyType - The type of the keys.
* @param valueType - The type of the values.
* @param {Map<string, string>} map - The map to represent.
* @returns Manifest representation of the map.
*/
export function manifestMap(
keyType: string,
valueType: string,
map: Map<string, string>,
): string {
const content = [];
for (const [key, value] of map.entries()) {
content.push(`${key} => ${value}`);
}
return `Map<${keyType}, ${valueType}>(${arrayToString(content)})`;
}

/**
* Returns a manifest string representation of a named address.
* @param addressName - The name of the address.
* @returns Manifest representation of the named address.
*/
export function manifestNamedAddress(addressName: string): string {
return `NamedAddress("${addressName}")`;
}

/**
* Returns a manifest string representation of an NFT.
* @param nft - The NFT object.
* @returns Manifest representation of the NFT.
*/
export function manifestNFT(nft: NFT): string {
return manifestGlobalId(nft.globalId());
}

/**
* Returns a manifest string representation of a proof.
* @param proofName - The name of the proof.
* @returns Manifest representation of the proof.
*/
export function manifestProof(proofName: string): string {
return `Proof("${proofName}")`;
}

/**
* Returns a manifest string representation of a string.
* @param str - The string to represent.
* @returns Manifest representation of the string.
*/
export function manifestString(str: string): string {
return `"${str}"`;
}

/**
* Returns a manifest string representation of a tuple.
* @param elements - The elements of the tuple.
* @returns Manifest representation of the tuple.
*/
export function manifestTuple(elements: string[]): string {
return `Tuple(${arrayToString(elements)})`;
}

function arrayToString(elements: string[]): string {
return elements.join(", ");
}
Loading

0 comments on commit 35da17e

Please sign in to comment.