Skip to content

Commit 35da17e

Browse files
committed
feat: string manifest builder
1 parent fa81b6f commit 35da17e

9 files changed

Lines changed: 950 additions & 232 deletions

File tree

src/GatewayProcessor/GatewayProcessor.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ import {
2929
} from "@radixdlt/radix-engine-toolkit";
3030
import { divideInBatches, parseNonFungibleData, withMaxLoops } from "./Utils";
3131
import {
32-
ComponentAddress,
3332
FungibleResource,
3433
NonFungibleItem,
3534
NonFungibleResource,
36-
ResourceAddress,
3735
ResourceInformation,
3836
} from "../Types/RadixTypes";
3937
import pLimit from "p-limit";
@@ -273,10 +271,10 @@ export class GatewayProcessor {
273271
* @returns A promise resolving to a map containing resource addresses as keys and Resource objects as values.
274272
*/
275273
async getResourcesInformation(
276-
resource_addresses: ResourceAddress[],
274+
resource_addresses: string[],
277275
additional_metadata?: string[],
278-
): Promise<Map<ResourceAddress, ResourceInformation>> {
279-
let resource_map = new Map<ResourceAddress, ResourceInformation>();
276+
): Promise<Map<string, ResourceInformation>> {
277+
let resource_map = new Map<string, ResourceInformation>();
280278
const batches = divideInBatches(resource_addresses, 20);
281279
const limit = pLimit(this._concurrencyLimit);
282280
await Promise.all(
@@ -297,14 +295,14 @@ export class GatewayProcessor {
297295
* @param entity Address of the component.
298296
*/
299297
async getFungibleResourcesHeldBy(
300-
entity: ComponentAddress,
298+
entity: string,
301299
): Promise<FungibleResource[]> {
302300
const resp = await this.entityDetails([entity]);
303301
const entityState = resp.items[0];
304302
let held_resources: FungibleResource[] = [];
305303

306-
let amount_map = new Map<ResourceAddress, number>();
307-
let resources: ResourceAddress[] = [];
304+
let amount_map = new Map<string, number>();
305+
let resources: string[] = [];
308306

309307
if (entityState.fungible_resources) {
310308
entityState.fungible_resources.items.forEach((resource) => {
@@ -343,7 +341,7 @@ export class GatewayProcessor {
343341
* @param entity Address of the entity.
344342
*/
345343
async getNonFungibleResourcesHeldBy(
346-
entity: ComponentAddress,
344+
entity: string,
347345
): Promise<NonFungibleResource[]> {
348346
const ledger_state = await this.ledgerState();
349347

@@ -421,8 +419,8 @@ export class GatewayProcessor {
421419
* @param non_fungible_resource Address of the non-fungible resource.
422420
*/
423421
async getNonFungibleIdsHeldBy(
424-
entity: ComponentAddress,
425-
non_fungible_resource: ResourceAddress,
422+
entity: string,
423+
non_fungible_resource: string,
426424
): Promise<string[]> {
427425
const ledger_state = await this.ledgerState();
428426

@@ -478,7 +476,7 @@ export class GatewayProcessor {
478476
* @param at_ledger_state Optional ledger state when to make the query.
479477
*/
480478
async getAllNonFungibleIds(
481-
resource_address: ResourceAddress,
479+
resource_address: string,
482480
at_ledger_state?: number,
483481
): Promise<string[]> {
484482
const state_version = at_ledger_state
@@ -507,7 +505,7 @@ export class GatewayProcessor {
507505
* @param at_ledger_state State against which to make the query.
508506
*/
509507
async getNonFungibleItemsFromIds(
510-
resource_address: ResourceAddress,
508+
resource_address: string,
511509
ids: string[],
512510
at_ledger_state?: number,
513511
): Promise<NonFungibleItem[]> {
@@ -585,7 +583,7 @@ export class GatewayProcessor {
585583
* @param resource_address Address of the non-fungible items.
586584
* @param ids Ids of the non-fungible items.
587585
*/
588-
async getNftOwners(resource_address: ResourceAddress, ids: string[]) {
586+
async getNftOwners(resource_address: string, ids: string[]) {
589587
const nft_batches = divideInBatches(ids, 100);
590588
const limit = pLimit(this._concurrencyLimit);
591589
let return_map = new Map<string, string>();
@@ -611,10 +609,10 @@ export class GatewayProcessor {
611609
* Takes up to 20 resource addresses as input
612610
*/
613611
private async limitedResourcesInformation(
614-
resource_addresses: ResourceAddress[],
612+
resource_addresses: string[],
615613
additional_metadata?: string[],
616-
): Promise<Map<ResourceAddress, ResourceInformation>> {
617-
let resource_map = new Map<ResourceAddress, ResourceInformation>();
614+
): Promise<Map<string, ResourceInformation>> {
615+
let resource_map = new Map<string, ResourceInformation>();
618616

619617
let resp = await this.entityDetails(resource_addresses);
620618
resp.items.forEach((item) => {
@@ -802,7 +800,7 @@ export class GatewayProcessor {
802800
);
803801
}
804802
private async nonFungibleIds(
805-
resource_address: ResourceAddress,
803+
resource_address: string,
806804
at_ledger_state: number,
807805
cursor?: string,
808806
): Promise<StateNonFungibleIdsResponse> {
@@ -874,7 +872,7 @@ export class GatewayProcessor {
874872
}
875873

876874
private async getNonFungibleData(
877-
address: ResourceAddress,
875+
address: string,
878876
ids: string[],
879877
at_ledger_state?: number,
880878
): Promise<StateNonFungibleDetailsResponseItem[]> {
@@ -896,7 +894,7 @@ export class GatewayProcessor {
896894
}
897895

898896
private async getEntityLocation(
899-
address: ResourceAddress,
897+
address: string,
900898
ids: string[],
901899
): Promise<StateNonFungibleLocationResponseItem[]> {
902900
return withMaxLoops(
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import { Decimal } from "../Types/RadixTypes";
2+
import { NFT } from "../Types/NFT";
3+
4+
/**
5+
* Returns a manifest string representation of an address.
6+
* @param address - The address to represent.
7+
* @returns Manifest representation of the address.
8+
*/
9+
export function manifestAddress(address: string): string {
10+
return `Address("${address}")`;
11+
}
12+
13+
/**
14+
* Returns a manifest string representation of an address reservation.
15+
* @param reservationName - The reservation name.
16+
* @returns Manifest representation of the address reservation.
17+
*/
18+
export function manifestAddressReservation(reservationName: string): string {
19+
return `AddressReservation("${reservationName}")`;
20+
}
21+
22+
/**
23+
* Returns a manifest representation of an array.
24+
* @param typeName - The type of the items in the array.
25+
* @param content - The array content.
26+
* @returns Manifest representation of the array.
27+
*/
28+
export function manifestArray(typeName: string, content: string[]): string {
29+
return `Array<${typeName}>(${arrayToString(content)})`;
30+
}
31+
32+
/**
33+
* Returns a manifest string representation of a bucket.
34+
* @param bucketName - The bucket name.
35+
* @returns Manifest representation of the bucket.
36+
*/
37+
export function manifestBucket(bucketName: string): string {
38+
return `Bucket("${bucketName}")`;
39+
}
40+
41+
/**
42+
* Returns a manifest string representation of a decimal.
43+
* @param {Decimal} decimal - The decimal object.
44+
* @returns Manifest representation of the decimal.
45+
*/
46+
export function manifestDecimal(decimal: Decimal): string {
47+
return `Decimal("${decimal}")`;
48+
}
49+
50+
/**
51+
* Returns a manifest string representation of an enum.
52+
* @param {number} variantId - The variant ID.
53+
* @param fields - The fields of the enum.
54+
* @returns Manifest representation of the enum.
55+
*/
56+
export function manifestEnum(variantId: number, fields: string[]): string {
57+
return `Enum<${variantId}u8>(${arrayToString(fields)})`;
58+
}
59+
60+
/**
61+
* Returns a manifest string representation of a non-fungible global ID.
62+
* @param globalId - The global ID.
63+
* @returns Manifest representation of the non-fungible global ID.
64+
*/
65+
export function manifestGlobalId(globalId: string): string {
66+
return `NonFungibleGlobalId("${globalId}")`;
67+
}
68+
69+
/**
70+
* Returns a manifest string representation of a non-fungible local ID.
71+
* @param id - The local ID.
72+
* @returns Manifest representation of the non-fungible local ID.
73+
*/
74+
export function manifestLocalId(id: string): string {
75+
return `NonFungibleLocalId("${id}")`;
76+
}
77+
78+
/**
79+
* Returns a manifest string representation of an array of non-fungible local IDs.
80+
* @param ids - Array of local IDs.
81+
* @returns Manifest representation of the local ID array.
82+
*/
83+
export function manifestLocalIdArray(ids: string[]): string {
84+
return manifestArray(
85+
"NonFungibleLocalId",
86+
ids.map((id) => manifestLocalId(id)),
87+
);
88+
}
89+
90+
/**
91+
* Returns a manifest string representation of a map.
92+
* @param keyType - The type of the keys.
93+
* @param valueType - The type of the values.
94+
* @param {Map<string, string>} map - The map to represent.
95+
* @returns Manifest representation of the map.
96+
*/
97+
export function manifestMap(
98+
keyType: string,
99+
valueType: string,
100+
map: Map<string, string>,
101+
): string {
102+
const content = [];
103+
for (const [key, value] of map.entries()) {
104+
content.push(`${key} => ${value}`);
105+
}
106+
return `Map<${keyType}, ${valueType}>(${arrayToString(content)})`;
107+
}
108+
109+
/**
110+
* Returns a manifest string representation of a named address.
111+
* @param addressName - The name of the address.
112+
* @returns Manifest representation of the named address.
113+
*/
114+
export function manifestNamedAddress(addressName: string): string {
115+
return `NamedAddress("${addressName}")`;
116+
}
117+
118+
/**
119+
* Returns a manifest string representation of an NFT.
120+
* @param nft - The NFT object.
121+
* @returns Manifest representation of the NFT.
122+
*/
123+
export function manifestNFT(nft: NFT): string {
124+
return manifestGlobalId(nft.globalId());
125+
}
126+
127+
/**
128+
* Returns a manifest string representation of a proof.
129+
* @param proofName - The name of the proof.
130+
* @returns Manifest representation of the proof.
131+
*/
132+
export function manifestProof(proofName: string): string {
133+
return `Proof("${proofName}")`;
134+
}
135+
136+
/**
137+
* Returns a manifest string representation of a string.
138+
* @param str - The string to represent.
139+
* @returns Manifest representation of the string.
140+
*/
141+
export function manifestString(str: string): string {
142+
return `"${str}"`;
143+
}
144+
145+
/**
146+
* Returns a manifest string representation of a tuple.
147+
* @param elements - The elements of the tuple.
148+
* @returns Manifest representation of the tuple.
149+
*/
150+
export function manifestTuple(elements: string[]): string {
151+
return `Tuple(${arrayToString(elements)})`;
152+
}
153+
154+
function arrayToString(elements: string[]): string {
155+
return elements.join(", ");
156+
}

0 commit comments

Comments
 (0)