-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,235 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { EncodedBoxReference } from './types'; | ||
import { BoxReference } from './types/transactions/base'; | ||
|
||
function translateBoxReference( | ||
reference: BoxReference, | ||
foreignApps: number[], | ||
appIndex: number | ||
): EncodedBoxReference { | ||
const referenceId = reference.appIndex; | ||
const referenceName = reference.name; | ||
const isOwnReference = referenceId === 0 || referenceId === appIndex; | ||
let index = 0; | ||
|
||
if (foreignApps != null) { | ||
// Foreign apps start from index 1; index 0 is its own app ID. | ||
index = foreignApps.indexOf(referenceId) + 1; | ||
} | ||
// Check if the app referenced is itself after checking the foreign apps array. | ||
// If index is zero, then the app ID was not found in the foreign apps array | ||
// or the foreign apps array was null. | ||
if (index === 0 && !isOwnReference) { | ||
// Error if the app is trying to reference a foreign app that was not in | ||
// its own foreign apps array. | ||
throw new Error(`Box ref with appId ${referenceId} not in foreign-apps`); | ||
} | ||
return { i: index, n: referenceName }; | ||
} | ||
|
||
/** | ||
* translateBoxReferences translates an array of BoxReferences with app IDs | ||
* into an array of EncodedBoxReferences with foreign indices. | ||
*/ | ||
export function translateBoxReferences( | ||
references: BoxReference[] | undefined, | ||
foreignApps: number[], | ||
appIndex: number | ||
): EncodedBoxReference[] { | ||
if (references == null) return []; | ||
return references.map((bx) => | ||
translateBoxReference(bx, foreignApps, appIndex) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import JSONRequest from '../jsonrequest'; | ||
import HTTPClient from '../../client'; | ||
import IntDecoding from '../../../types/intDecoding'; | ||
import { Box } from './models/types'; | ||
|
||
/** | ||
* Given an application ID and the box name (key), return the value stored in the box. | ||
* | ||
* #### Example | ||
* ```typescript | ||
* const index = 60553466; | ||
* const boxName = Buffer.from("foo"); | ||
* const boxResponse = await algodClient.getApplicationBoxByName(index, boxName).do(); | ||
* const boxValue = boxResponse.value; | ||
* ``` | ||
* | ||
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2applicationsapplication-idbox) | ||
* @param index - The application ID to look up. | ||
* @category GET | ||
*/ | ||
export default class GetApplicationBoxByName extends JSONRequest< | ||
Box, | ||
Record<string, any> | ||
> { | ||
constructor( | ||
c: HTTPClient, | ||
intDecoding: IntDecoding, | ||
private index: number, | ||
name: Uint8Array | ||
) { | ||
super(c, intDecoding); | ||
this.index = index; | ||
// Encode name in base64 format and append the encoding prefix. | ||
const encodedName = Buffer.from(name).toString('base64'); | ||
this.query.name = encodeURI(`b64:${encodedName}`); | ||
} | ||
|
||
/** | ||
* @returns `/v2/applications/${index}/box` | ||
*/ | ||
path() { | ||
return `/v2/applications/${this.index}/box`; | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
prepare(body: Record<string, any>): Box { | ||
return Box.from_obj_for_encoding(body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import JSONRequest from '../jsonrequest'; | ||
import HTTPClient from '../../client'; | ||
import IntDecoding from '../../../types/intDecoding'; | ||
import { BoxesResponse } from './models/types'; | ||
|
||
/** | ||
* Given an application ID, return all the box names associated with the app. | ||
* | ||
* #### Example | ||
* ```typescript | ||
* const index = 60553466; | ||
* const boxesResponse = await algodClient.getApplicationBoxes(index).max(3).do(); | ||
* const boxNames = boxesResponse.boxes.map(box => box.name); | ||
* ``` | ||
* | ||
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2applicationsapplication-idboxes) | ||
* @param index - The application ID to look up. | ||
* @category GET | ||
*/ | ||
export default class GetApplicationBoxes extends JSONRequest< | ||
BoxesResponse, | ||
Record<string, any> | ||
> { | ||
constructor(c: HTTPClient, intDecoding: IntDecoding, private index: number) { | ||
super(c, intDecoding); | ||
this.index = index; | ||
this.query.max = 0; | ||
} | ||
|
||
/** | ||
* @returns `/v2/applications/${index}/boxes` | ||
*/ | ||
path() { | ||
return `/v2/applications/${this.index}/boxes`; | ||
} | ||
|
||
/** | ||
* Limit results for pagination. | ||
* | ||
* #### Example | ||
* ```typescript | ||
* const maxResults = 20; | ||
* const boxesResult = await algodClient | ||
* .GetApplicationBoxes(1234) | ||
* .limit(maxResults) | ||
* .do(); | ||
* ``` | ||
* | ||
* @param limit - maximum number of results to return. | ||
* @category query | ||
*/ | ||
max(max: number) { | ||
this.query.max = max; | ||
return this; | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
prepare(body: Record<string, any>): BoxesResponse { | ||
return BoxesResponse.from_obj_for_encoding(body); | ||
} | ||
} |
Oops, something went wrong.