Skip to content

Commit

Permalink
REST API: Allow bigints for client args (#893)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpaulos authored Aug 23, 2024
1 parent 7fa10ab commit a0366b9
Show file tree
Hide file tree
Showing 46 changed files with 205 additions and 202 deletions.
5 changes: 4 additions & 1 deletion examples/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ async function main() {
3
);
// Grab app id from confirmed transaction result
const appId = Number(result.applicationIndex);
const appId = result.applicationIndex;
if (!appId) {
throw new Error('App not created');
}
console.log(`Created app with index: ${appId}`);
// example: APP_CREATE

Expand Down
5 changes: 4 additions & 1 deletion examples/asa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ async function main() {
await algodClient.sendRawTransaction(signedTxn).do();
const result = await algosdk.waitForConfirmation(algodClient, txn.txID(), 3);

const assetIndex = Number(result.assetIndex);
const { assetIndex } = result;
if (!assetIndex) {
throw new Error('Asset not created');
}
console.log(`Asset ID created: ${assetIndex}`);
// example: ASSET_CREATE

Expand Down
5 changes: 4 additions & 1 deletion examples/atc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ async function main() {
createTxn.txID(),
3
);
const appIndex = Number(response.applicationIndex);
const appIndex = response.applicationIndex;
if (!appIndex) {
throw new Error('Application not created');
}

// example: ATC_CREATE
const atc = new algosdk.AtomicTransactionComposer();
Expand Down
47 changes: 16 additions & 31 deletions examples/block_fetcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,42 @@ const address = 'http://127.0.0.1';
const port = 8080;
const client = new algosdk.Algodv2(token, address, port);

// Recursively remove all null values from object
function removeNulls(obj) {
for (const key in obj) {
if (obj[key] === null) {
// eslint-disable-next-line no-param-reassign
delete obj[key];
} else if (typeof obj[key] === 'object') {
removeNulls(obj[key]);
}
}
}

(async () => {
// Retrieve current status
let status = await client.status().do();

while (true) {
// Get latest round number
let lastRound = Number(status.lastRound);
let { lastRound } = status;
console.log(`Round: ${lastRound}`);

// Fetch block
const round = await client.block(lastRound).do();
const { block } = round;
const { txns } = block;
const txns = block.payset;

// For all transactions in the block reconstruct them
// into Transaction objects and calculate their TxID
for (const t in txns) {
const tx = txns[t];
const { txn } = txns[t];
for (const stxnInBlock of txns) {
const { txn } = stxnInBlock.signedTxn.signedTxn;

// Skip StateProofs
if (txn.type === 'stpf') continue;

// Remove nulls (mainly where an appl txn contains a null app arg)
removeNulls(txn);
if (txn.type === algosdk.TransactionType.stpf) continue;

// Use Genesis Hash and Genesis ID from the block
const { gh } = block;
let { gen } = block;
let gh: Uint8Array | undefined = block.header.genesisHash;
let gen: string | undefined = block.header.genesisID;

// Unset gen if `hgi` isn't set
if (!tx.hgi) gen = null;
// Unset gh if hasGenesisID isn't set
if (!stxnInBlock.hasGenesisID) gh = undefined;
// Unset gen if hasGenesisID isn't set
if (!stxnInBlock.hasGenesisID) gen = undefined;

// Construct Transaction
const transaction = algosdk.Transaction.from_obj_for_encoding({
...txn,
gh,
gen,
});
const encodingData = txn.toEncodingData();
encodingData.set('gh', gh);
encodingData.set('gen', gen);
const transaction = algosdk.Transaction.fromEncodingData(encodingData);
const txid = transaction.txID();

// If set to true, validate the TxID exists against the node
Expand All @@ -80,6 +65,6 @@ function removeNulls(obj) {

// Wait for next round
status = await client.statusAfterBlock(lastRound).do();
lastRound = status['last-round'];
lastRound = status.lastRound;
}
})();
7 changes: 5 additions & 2 deletions examples/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export async function getLocalAccounts(): Promise<SandboxAccount[]> {
export async function deployCalculatorApp(
algodClient: algosdk.Algodv2,
creator: SandboxAccount
): Promise<number> {
): Promise<bigint> {
const approvalProgram = fs.readFileSync(
path.join(__dirname, '/calculator/approval.teal'),
'utf8'
Expand Down Expand Up @@ -164,6 +164,9 @@ export async function deployCalculatorApp(
appCreateTxn.txID(),
3
);
const appId = Number(result.applicationIndex);
const appId = result.applicationIndex;
if (!appId) {
throw new Error('Application not created');
}
return appId;
}
2 changes: 1 addition & 1 deletion src/client/urlTokenBaseHTTPClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class URLTokenBaseHTTPClient implements BaseHTTPClient {
const address = new URL(fixedRelativePath, this.baseURL);
if (query) {
for (const [key, value] of Object.entries(query)) {
address.searchParams.set(key, value);
address.searchParams.set(key, value.toString());
}
}
return address.toString();
Expand Down
4 changes: 3 additions & 1 deletion src/client/v2/algod/accountApplicationInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import { Address } from '../../../encoding/address.js';

export default class AccountApplicationInformation extends JSONRequest<AccountApplicationResponse> {
private account: string;
private applicationID: bigint;

constructor(
c: HTTPClient,
account: string | Address,
private applicationID: number
applicationID: number | bigint
) {
super(c);
this.account = account.toString();
this.applicationID = BigInt(applicationID);
}

path() {
Expand Down
4 changes: 3 additions & 1 deletion src/client/v2/algod/accountAssetInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import { Address } from '../../../encoding/address.js';

export default class AccountAssetInformation extends JSONRequest<AccountAssetResponse> {
private account: string;
private assetID: bigint;

constructor(
c: HTTPClient,
account: string | Address,
private assetID: number
assetID: number | bigint
) {
super(c);
this.account = account.toString();
this.assetID = BigInt(assetID);
}

path() {
Expand Down
37 changes: 20 additions & 17 deletions src/client/v2/algod/algod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class AlgodClient extends ServiceClient {
*
* #### Example
* ```typescript
* const health = await algodClient.healthCheck().do();
* await algodClient.healthCheck().do();
* ```
*
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/#get-health)
Expand Down Expand Up @@ -126,7 +126,7 @@ export class AlgodClient extends ServiceClient {
*
* #### Example
* ```typescript
* const { txId } = await algodClient.sendRawTransaction(signedTxns).do();
* const { txid } = await algodClient.sendRawTransaction(signedTxns).do();
* const result = await waitForConfirmation(algodClient, txid, 3);
* ```
*
Expand Down Expand Up @@ -173,7 +173,7 @@ export class AlgodClient extends ServiceClient {
* @param index - The asset ID to look up.
* @category GET
*/
accountAssetInformation(account: string | Address, index: number) {
accountAssetInformation(account: string | Address, index: number | bigint) {
return new AccountAssetInformation(this.c, account, index);
}

Expand All @@ -192,7 +192,10 @@ export class AlgodClient extends ServiceClient {
* @param index - The application ID to look up.
* @category GET
*/
accountApplicationInformation(account: string | Address, index: number) {
accountApplicationInformation(
account: string | Address,
index: number | bigint
) {
return new AccountApplicationInformation(this.c, account, index);
}

Expand All @@ -209,7 +212,7 @@ export class AlgodClient extends ServiceClient {
* @param roundNumber - The round number of the block to get.
* @category GET
*/
block(roundNumber: number) {
block(roundNumber: number | bigint) {
return new Block(this.c, roundNumber);
}

Expand All @@ -226,7 +229,7 @@ export class AlgodClient extends ServiceClient {
* @param roundNumber - The round number of the block to get.
* @category GET
*/
getBlockHash(roundNumber: number) {
getBlockHash(roundNumber: number | bigint) {
return new GetBlockHash(this.c, roundNumber);
}

Expand All @@ -243,7 +246,7 @@ export class AlgodClient extends ServiceClient {
* @param roundNumber - The round number of the block to get.
* @category GET
*/
getBlockTxids(roundNumber: number) {
getBlockTxids(roundNumber: number | bigint) {
return new GetBlockTxids(this.c, roundNumber);
}

Expand Down Expand Up @@ -355,7 +358,7 @@ export class AlgodClient extends ServiceClient {
* @param round - The number of the round to wait for.
* @category GET
*/
statusAfterBlock(round: number) {
statusAfterBlock(round: number | bigint) {
return new StatusAfterBlock(this.c, round);
}

Expand Down Expand Up @@ -504,7 +507,7 @@ export class AlgodClient extends ServiceClient {
* @param index - The application ID to look up.
* @category GET
*/
getApplicationBoxByName(index: number, boxName: Uint8Array) {
getApplicationBoxByName(index: number | bigint, boxName: Uint8Array) {
return new GetApplicationBoxByName(this.c, index, boxName);
}

Expand All @@ -522,7 +525,7 @@ export class AlgodClient extends ServiceClient {
* @param index - The application ID to look up.
* @category GET
*/
getApplicationBoxes(index: number) {
getApplicationBoxes(index: number | bigint) {
return new GetApplicationBoxes(this.c, index);
}

Expand Down Expand Up @@ -556,7 +559,7 @@ export class AlgodClient extends ServiceClient {
* @param txID - The transaction ID for which to generate a proof.
* @category GET
*/
getTransactionProof(round: number, txID: string) {
getTransactionProof(round: number | bigint, txID: string) {
return new GetTransactionProof(this.c, round, txID);
}

Expand All @@ -572,7 +575,7 @@ export class AlgodClient extends ServiceClient {
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2#get-v2blocksroundlightheaderproof)
* @param round
*/
getLightBlockHeaderProof(round: number) {
getLightBlockHeaderProof(round: number | bigint) {
return new LightBlockHeaderProof(this.c, round);
}

Expand All @@ -588,7 +591,7 @@ export class AlgodClient extends ServiceClient {
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2#get-v2stateproofsround)
* @param round
*/
getStateProof(round: number) {
getStateProof(round: number | bigint) {
return new StateProof(this.c, round);
}

Expand Down Expand Up @@ -678,7 +681,7 @@ export class AlgodClient extends ServiceClient {
* @param offset
* @category POST
*/
setBlockOffsetTimestamp(offset: number) {
setBlockOffsetTimestamp(offset: number | bigint) {
return new SetBlockOffsetTimestamp(this.c, offset);
}

Expand Down Expand Up @@ -710,7 +713,7 @@ export class AlgodClient extends ServiceClient {
* @param round
* @category POST
*/
setSyncRound(round: number) {
setSyncRound(round: number | bigint) {
return new SetSyncRound(this.c, round);
}

Expand Down Expand Up @@ -789,7 +792,7 @@ export class AlgodClient extends ServiceClient {
* @param round the round number to be searched for
* @category GET
*/
getLedgerStateDelta(round: number) {
getLedgerStateDelta(round: number | bigint) {
return new GetLedgerStateDelta(this.c, round);
}

Expand All @@ -806,7 +809,7 @@ export class AlgodClient extends ServiceClient {
* @param round the round number to be searched for
* @category GET
*/
getTransactionGroupLedgerStateDeltasForRound(round: number) {
getTransactionGroupLedgerStateDeltasForRound(round: number | bigint) {
return new GetTransactionGroupLedgerStateDeltasForRound(this.c, round);
}
}
6 changes: 3 additions & 3 deletions src/client/v2/algod/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { BlockResponse } from './models/types.js';
* block gets the block info for the given round. this call may block
*/
export default class Block extends JSONRequest<BlockResponse> {
private round: number;
private round: bigint;

constructor(c: HTTPClient, roundNumber: number) {
constructor(c: HTTPClient, roundNumber: number | bigint) {
super(c);
this.round = roundNumber;
this.round = BigInt(roundNumber);
this.query = { format: 'msgpack' };
}

Expand Down
9 changes: 4 additions & 5 deletions src/client/v2/algod/getApplicationBoxByName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ import { Box } from './models/types.js';
* @category GET
*/
export default class GetApplicationBoxByName extends JSONRequest<Box> {
constructor(
c: HTTPClient,
private index: number,
name: Uint8Array
) {
private index: bigint;

constructor(c: HTTPClient, index: number | bigint, name: Uint8Array) {
super(c);
this.index = BigInt(index);
// Encode name in base64 format and append the encoding prefix.
const encodedName = bytesToBase64(name);
this.query.name = encodeURI(`b64:${encodedName}`);
Expand Down
8 changes: 4 additions & 4 deletions src/client/v2/algod/getApplicationBoxes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import { BoxesResponse } from './models/types.js';
* @category GET
*/
export default class GetApplicationBoxes extends JSONRequest<BoxesResponse> {
constructor(
c: HTTPClient,
private index: number
) {
private index: bigint;

constructor(c: HTTPClient, index: number | bigint) {
super(c);
this.index = BigInt(index);
this.query.max = 0;
}

Expand Down
Loading

0 comments on commit a0366b9

Please sign in to comment.