Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final TypeScript changes #337

Merged
merged 5 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"eslint.validate": ["typescript"]
"editor.formatOnPaste": true
}
5 changes: 5 additions & 0 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export default class HTTPClient {
if (typeof port !== 'undefined') {
baseServerURL.set('port', port.toString());
}

if (baseServerURL.protocol.length === 0) {
throw new Error('Invalid base server URL, protocol must be defined.');
}

this.baseURL = baseServerURL;
this.defaultHeaders = defaultHeaders;
this.tokenHeader = tokenHeader;
Expand Down
5 changes: 3 additions & 2 deletions src/client/v2/algod/suggestedParams.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import JSONRequest from '../jsonrequest';
import { SuggestedParams } from '../../../types/transactions/base';

/**
* Returns the common needed parameters for a new transaction, in a format the transaction builder expects
*/
export default class SuggestedParams extends JSONRequest {
export default class SuggestedParamsRequest extends JSONRequest<SuggestedParams> {
/* eslint-disable class-methods-use-this */
path() {
return '/v2/transactions/params';
}

prepare(body: Record<string, any>) {
prepare(body: Record<string, any>): SuggestedParams {
return {
flatFee: false,
fee: body.fee,
Expand Down
4 changes: 2 additions & 2 deletions src/client/v2/jsonrequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import IntDecoding from '../../types/intDecoding';
* Body: The structure of the response's body
*/
export default abstract class JSONRequest<
Data = Record<string, any> | Uint8Array,
Body = Data
Data = Record<string, any>,
Body = Data | Uint8Array
> {
c: HTTPClient;
query: Record<string, any>;
Expand Down
2 changes: 1 addition & 1 deletion src/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function assignGroupID(
from?: string
) {
const gid = computeGroupID(txns);
const result = [];
const result: txnBuilder.Transaction[] = [];
for (const txn of txns) {
const tx = txnBuilder.instantiateTxnIfNeeded(txn);
if (!from || address.encodeAddress(tx.from.publicKey) === from) {
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export * from './types/transactions';
export { default as Algodv2 } from './client/v2/algod/algod';
export { default as Kmd } from './client/kmd';
export { default as IntDecoding } from './types/intDecoding';
export { default as Account } from './types/account';
export { default as Indexer } from './client/v2/indexer/indexer';
export {
isValidAddress,
Expand Down
86 changes: 81 additions & 5 deletions src/makeTxn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export function makePaymentTxnWithSuggestedParamsFromObject(
* @param nonParticipation - configure whether the address wants to stop participating. If true,
* voteKey, selectionKey, voteFirst, voteLast, and voteKeyDilution must be undefined.
*/
/* eslint-disable no-unused-vars,no-redeclare */
export function makeKeyRegistrationTxnWithSuggestedParams(
from: KeyRegistrationTxn['from'],
note: KeyRegistrationTxn['note'],
Expand All @@ -168,8 +169,33 @@ export function makeKeyRegistrationTxnWithSuggestedParams(
voteKeyDilution: KeyRegistrationTxn['voteKeyDilution'],
suggestedParams: MustHaveSuggestedParams<KeyRegistrationTxn>['suggestedParams'],
rekeyTo?: KeyRegistrationTxn['reKeyTo'],
nonParticipation: KeyRegistrationTxn['nonParticipation'] = false
nonParticipation?: false
): txnBuilder.Transaction;
export function makeKeyRegistrationTxnWithSuggestedParams(
from: KeyRegistrationTxn['from'],
note: KeyRegistrationTxn['note'],
voteKey: undefined,
selectionKey: undefined,
voteFirst: undefined,
voteLast: undefined,
voteKeyDilution: undefined,
suggestedParams: MustHaveSuggestedParams<KeyRegistrationTxn>['suggestedParams'],
rekeyTo?: KeyRegistrationTxn['reKeyTo'],
nonParticipation?: true
): txnBuilder.Transaction;
export function makeKeyRegistrationTxnWithSuggestedParams(
from: any,
note: any,
voteKey: any,
selectionKey: any,
voteFirst: any,
voteLast: any,
voteKeyDilution: any,
suggestedParams: any,
rekeyTo?: any,
nonParticipation = false
) {
/* eslint-enable no-unused-vars,no-redeclare */
const o: KeyRegistrationTxn = {
from,
note,
Expand Down Expand Up @@ -208,6 +234,7 @@ export function makeKeyRegistrationTxnWithSuggestedParams(
* voteKey, selectionKey, voteFirst, voteLast, and voteKeyDilution must be undefined.
* @Deprecated in version 2.0 this will change to use the "WithSuggestedParams" signature.
*/
/* eslint-disable no-unused-vars,no-redeclare */
export function makeKeyRegistrationTxn(
from: KeyRegistrationTxn['from'],
fee: MustHaveSuggestedParamsInline<KeyRegistrationTxn>['fee'],
Expand All @@ -222,8 +249,41 @@ export function makeKeyRegistrationTxn(
voteLast: KeyRegistrationTxn['voteLast'],
voteKeyDilution: KeyRegistrationTxn['voteKeyDilution'],
rekeyTo?: KeyRegistrationTxn['reKeyTo'],
nonParticipation: KeyRegistrationTxn['nonParticipation'] = false
nonParticipation?: false
): txnBuilder.Transaction;
export function makeKeyRegistrationTxn(
from: KeyRegistrationTxn['from'],
fee: MustHaveSuggestedParamsInline<KeyRegistrationTxn>['fee'],
firstRound: MustHaveSuggestedParamsInline<KeyRegistrationTxn>['firstRound'],
lastRound: MustHaveSuggestedParamsInline<KeyRegistrationTxn>['lastRound'],
note: KeyRegistrationTxn['note'],
genesisHash: MustHaveSuggestedParamsInline<KeyRegistrationTxn>['genesisHash'],
genesisID: MustHaveSuggestedParamsInline<KeyRegistrationTxn>['genesisID'],
voteKey: undefined,
selectionKey: undefined,
voteFirst: undefined,
voteLast: undefined,
voteKeyDilution: undefined,
rekeyTo?: KeyRegistrationTxn['reKeyTo'],
nonParticipation?: true
): txnBuilder.Transaction;
export function makeKeyRegistrationTxn(
from: any,
fee: any,
firstRound: any,
lastRound: any,
note: any,
genesisHash: any,
genesisID: any,
voteKey: any,
selectionKey: any,
voteFirst: any,
voteLast: any,
voteKeyDilution: any,
rekeyTo?: any,
nonParticipation: any = false
) {
/* eslint-enable no-unused-vars,no-redeclare */
const suggestedParams: SuggestedParams = {
genesisHash,
genesisID,
Expand All @@ -246,6 +306,7 @@ export function makeKeyRegistrationTxn(
}

// helper for above makeKeyRegistrationTxnWithSuggestedParams, instead accepting an arguments object
/* eslint-disable no-unused-vars,no-redeclare */
export function makeKeyRegistrationTxnWithSuggestedParamsFromObject(
o: Pick<
RenameProperty<
Expand All @@ -262,9 +323,24 @@ export function makeKeyRegistrationTxnWithSuggestedParamsFromObject(
| 'voteKeyDilution'
| 'suggestedParams'
| 'rekeyTo'
| 'nonParticipation'
>
) {
> & {
nonParticipation?: false;
}
): txnBuilder.Transaction;
export function makeKeyRegistrationTxnWithSuggestedParamsFromObject(
o: Pick<
RenameProperty<
MustHaveSuggestedParams<KeyRegistrationTxn>,
'reKeyTo',
'rekeyTo'
>,
'from' | 'note' | 'suggestedParams' | 'rekeyTo'
> & {
nonParticipation?: true;
jdtzmn marked this conversation as resolved.
Show resolved Hide resolved
}
): txnBuilder.Transaction;
export function makeKeyRegistrationTxnWithSuggestedParamsFromObject(o: any) {
/* eslint-enable no-unused-vars,no-redeclare */
return makeKeyRegistrationTxnWithSuggestedParams(
o.from,
o.note,
Expand Down
6 changes: 6 additions & 0 deletions tests/9.Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ describe('client', () => {
}
}
});

it('should throw an error if protocol is the empty', () => {
const baseServer = 'localhost'; // should be http://localhost

assert.throws(() => new HTTPClient({}, baseServer));
});
/* eslint-enable dot-notation */
});
});