Skip to content

Commit

Permalink
Merge branch 'develop' into jacob/kmd-httpclient-to-typescript
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/client/v2/algod/accountInformation.ts
#	src/client/v2/algod/algod.ts
#	src/client/v2/algod/block.ts
#	src/client/v2/algod/compile.ts
#	src/client/v2/algod/dryrun.ts
#	src/client/v2/algod/getApplicationByID.ts
#	src/client/v2/algod/getAssetByID.ts
#	src/client/v2/algod/pendingTransactionInformation.ts
#	src/client/v2/algod/pendingTransactions.ts
#	src/client/v2/algod/pendingTransactionsByAddress.ts
#	src/client/v2/algod/proof.ts
#	src/client/v2/algod/sendRawTransaction.ts
#	src/client/v2/algod/statusAfterBlock.ts
#	src/client/v2/algod/supply.ts
#	src/client/v2/indexer/indexer.ts
#	src/client/v2/indexer/lookupAccountByID.ts
#	src/client/v2/indexer/lookupAccountTransactions.ts
#	src/client/v2/indexer/lookupApplications.ts
#	src/client/v2/indexer/lookupAssetBalances.ts
#	src/client/v2/indexer/lookupAssetByID.ts
#	src/client/v2/indexer/lookupAssetTransactions.ts
#	src/client/v2/indexer/lookupBlock.ts
#	src/client/v2/indexer/lookupTransactionByID.ts
#	src/client/v2/indexer/searchForTransactions.ts
#	src/client/v2/jsonrequest.ts
#	src/main.ts
#	src/utils/utils.ts
  • Loading branch information
jdtzmn committed Mar 25, 2021
2 parents 0461136 + df036ec commit 52f40a2
Show file tree
Hide file tree
Showing 27 changed files with 213 additions and 104 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ For detailed information about the different API calls in `client`, visit https:
Running examples requires access to a running node. Follow the instructions in Algorand's [developer resources](https://developer.algorand.org/docs/run-a-node/setup/install/) to install a node on your computer.
**As portions of the codebase are written in TypeScript, examples cannot be run directly using `node`**. Please refer to the instructions described in the [examples/README.md](examples/README.md) file for more information regarding running the examples.
**As portions of the codebase are written in TypeScript, example files cannot be run directly using `node`**. Please refer to the instructions described in the [examples/README.md](examples/README.md) file for more information regarding running the examples.
## SDK Development
Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Portions of the codebase are written in TypeScript, so running examples requires
$ npm run example examples/generate_sender_receiver.js
```

> **Disclaimer:** `ts-node` is **not** required when importing this module from NPM. Code imported from the `algosdk` NPM module will be pre-compiled to plain JavaScript.
## Configuration

Many of the examples in this folder require configuration information such as:
Expand Down
2 changes: 1 addition & 1 deletion src/client/v2/algod/accountInformation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { IntDecoding } from '../../../types/intDecoding';
import IntDecoding from '../../../types/intDecoding';

export default class AccountInformation extends JSONRequest {
constructor(
Expand Down
2 changes: 1 addition & 1 deletion src/client/v2/algod/getApplicationByID.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { IntDecoding } from '../../../types/intDecoding';
import IntDecoding from '../../../types/intDecoding';

export default class GetApplicationByID extends JSONRequest {
constructor(c: HTTPClient, intDecoding: IntDecoding, private index: number) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/v2/algod/getAssetByID.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { IntDecoding } from '../../../types/intDecoding';
import IntDecoding from '../../../types/intDecoding';

export default class GetAssetByID extends JSONRequest {
constructor(c: HTTPClient, intDecoding: IntDecoding, private index: number) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/v2/algod/proof.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { IntDecoding } from '../../../types/intDecoding';
import IntDecoding from '../../../types/intDecoding';

export default class Proof extends JSONRequest {
constructor(
Expand Down
15 changes: 7 additions & 8 deletions src/client/v2/algod/sendRawTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { HTTPClient } from '../../client';
import { concatArrays } from '../../../utils/utils';

/**
* Sets the default header (if not previously set) for sending a raw
* transaction.
* @param headers
* @returns {*}
*/
export function setSendTransactionHeaders(headers = {}) {
let hdrs = headers;
Expand All @@ -16,7 +16,7 @@ export function setSendTransactionHeaders(headers = {}) {
return hdrs;
}

function isByteArray(array: Uint8Array) {
function isByteArray(array: any): array is Uint8Array {
return array && array.byteLength !== undefined;
}

Expand All @@ -34,13 +34,12 @@ export default class SendRawTransaction extends JSONRequest {
if (!stxOrStxs.every(isByteArray)) {
throw new TypeError('Array elements must be byte arrays');
}
forPosting = Array.prototype.concat(
...stxOrStxs.map((arr) => Array.from(arr))
);
} else if (!isByteArray(forPosting as Uint8Array)) {
// Flatten into a single Uint8Array
forPosting = concatArrays(...stxOrStxs);
} else if (!isByteArray(forPosting)) {
throw new TypeError('Argument must be byte array');
}
this.txnBytesToPost = forPosting as Uint8Array;
this.txnBytesToPost = forPosting;
}

// eslint-disable-next-line class-methods-use-this
Expand Down
2 changes: 1 addition & 1 deletion src/client/v2/algod/statusAfterBlock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { IntDecoding } from '../../../types/intDecoding';
import IntDecoding from '../../../types/intDecoding';

export default class StatusAfterBlock extends JSONRequest {
constructor(c: HTTPClient, intDecoding: IntDecoding, private round: number) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/v2/algod/supply.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import JSONRequest from '../jsonrequest';

export default class Supply extends JSONRequest {
// eslint-disable-next-line no-underscore-dangle,class-methods-use-this
// eslint-disable-next-line class-methods-use-this
path() {
return '/v2/ledger/supply';
}
Expand Down
2 changes: 1 addition & 1 deletion src/client/v2/indexer/lookupAccountByID.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { IntDecoding } from '../../../types/intDecoding';
import IntDecoding from '../../../types/intDecoding';

export default class LookupAccountByID extends JSONRequest {
constructor(
Expand Down
23 changes: 19 additions & 4 deletions src/client/v2/indexer/lookupAccountTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { IntDecoding } from '../../../types/intDecoding';
import IntDecoding from '../../../types/intDecoding';

/**
* Accept base64 string or Uint8Array and output base64 string
* @param data Base64 string or Uint8Array
* @returns The inputted base64 string, or a base64 string representation of the Uint8Array
*/
export function base64StringFunnel(data: Uint8Array | string) {
if (typeof data === 'string') {
return data;
}
return Buffer.from(data).toString('base64');
}

export default class LookupAccountTransactions extends JSONRequest {
constructor(
Expand All @@ -16,9 +28,12 @@ export default class LookupAccountTransactions extends JSONRequest {
return `/v2/accounts/${this.account}/transactions`;
}

// notePrefix to filter with, as uint8array
notePrefix(prefix: string) {
this.query['note-prefix'] = prefix;
/**
* notePrefix to filter with
* @param prefix base64 string or uint8array
*/
notePrefix(prefix: Uint8Array | string) {
this.query['note-prefix'] = base64StringFunnel(prefix);
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/v2/indexer/lookupApplications.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { IntDecoding } from '../../../types/intDecoding';
import IntDecoding from '../../../types/intDecoding';

export default class LookupApplications extends JSONRequest {
constructor(c: HTTPClient, intDecoding: IntDecoding, private index: number) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/v2/indexer/lookupAssetBalances.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { IntDecoding } from '../../../types/intDecoding';
import IntDecoding from '../../../types/intDecoding';

export default class LookupAssetBalances extends JSONRequest {
constructor(c: HTTPClient, intDecoding: IntDecoding, private index: number) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/v2/indexer/lookupAssetByID.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { IntDecoding } from '../../../types/intDecoding';
import IntDecoding from '../../../types/intDecoding';

export default class LookupAssetByID extends JSONRequest {
constructor(c: HTTPClient, intDecoding: IntDecoding, private index: number) {
Expand Down
14 changes: 9 additions & 5 deletions src/client/v2/indexer/lookupAssetTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { IntDecoding } from '../../../types/intDecoding';
import IntDecoding from '../../../types/intDecoding';
import { base64StringFunnel } from './lookupAccountTransactions';

export default class LookupAssetTransactions extends JSONRequest {
constructor(c: HTTPClient, intDecoding: IntDecoding, private index: number) {
Expand All @@ -12,9 +13,12 @@ export default class LookupAssetTransactions extends JSONRequest {
return `/v2/assets/${this.index}/transactions`;
}

// notePrefix to filter with, as uint8array
notePrefix(prefix: string) {
this.query['note-prefix'] = prefix;
/**
* notePrefix to filter with
* @param prefix base64 string or uint8array
*/
notePrefix(prefix: Uint8Array | string) {
this.query['note-prefix'] = base64StringFunnel(prefix);
return this;
}

Expand All @@ -37,7 +41,7 @@ export default class LookupAssetTransactions extends JSONRequest {
}

// round to filter with, as int
round(round) {
round(round: number) {
this.query.round = round;
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/client/v2/indexer/lookupBlock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { IntDecoding } from '../../../types/intDecoding';
import IntDecoding from '../../../types/intDecoding';

export default class LookupBlock extends JSONRequest {
constructor(c: HTTPClient, intDecoding: IntDecoding, private round: number) {
Expand Down
4 changes: 2 additions & 2 deletions src/client/v2/indexer/lookupTransactionByID.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import { IntDecoding } from '../../../types/intDecoding';
import IntDecoding from '../../../types/intDecoding';

export default class LookupTransactionByID extends JSONRequest {
constructor(c: HTTPClient, intDecoding: IntDecoding, private txID) {
constructor(c: HTTPClient, intDecoding: IntDecoding, private txID: string) {
super(c, intDecoding);
this.txID = txID;
}
Expand Down
10 changes: 7 additions & 3 deletions src/client/v2/indexer/searchForTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import JSONRequest from '../jsonrequest';
import { base64StringFunnel } from './lookupAccountTransactions';

export default class SearchForTransactions extends JSONRequest {
// eslint-disable-next-line class-methods-use-this
path() {
return '/v2/transactions';
}

// notePrefix to filter with, as uint8array
notePrefix(prefix: Uint8Array) {
this.query['note-prefix'] = prefix;
/**
* notePrefix to filter with
* @param prefix base64 string or uint8array
*/
notePrefix(prefix: Uint8Array | string) {
this.query['note-prefix'] = base64StringFunnel(prefix);
return this;
}

Expand Down
18 changes: 7 additions & 11 deletions src/client/v2/jsonrequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import HTTPClient from '../client';
import { IntDecoding } from '../../types/intDecoding';
import IntDecoding from '../../types/intDecoding';

/**
* Base abstract class for JSON requests.
Expand All @@ -13,17 +13,19 @@ export default abstract class JSONRequest<
Body = Data
> {
c: HTTPClient;
query: Record<string, any> = {};
query: Record<string, any>;
intDecoding: IntDecoding;

/**
* @param {HttpClient} client HTTPClient object.
* @param {"default" | "safe" | "mixed" | "bigint" | undefined} intDecoding The method to use
* for decoding integers from this request's response. See the setIntDecoding method for more
* details.
*/
constructor(client: HTTPClient, public intDecoding?: IntDecoding) {
constructor(client: HTTPClient, intDecoding?: IntDecoding) {
this.c = client;
this.intDecoding = intDecoding || 'default';
this.query = {};
this.intDecoding = intDecoding || IntDecoding.DEFAULT;
}

/**
Expand Down Expand Up @@ -53,13 +55,7 @@ export default abstract class JSONRequest<
if (this.intDecoding !== 'default') {
jsonOptions.intDecoding = this.intDecoding;
}
const res = await this.c.get(
// eslint-disable-next-line no-underscore-dangle
this.path(),
this.query,
headers,
jsonOptions
);
const res = await this.c.get(this.path(), this.query, headers, jsonOptions);
return this.prepare(res.body);
}

Expand Down
Loading

0 comments on commit 52f40a2

Please sign in to comment.