Skip to content

Commit

Permalink
fix: ability to use sdk without nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jul 15, 2022
1 parent a435e9d commit 8fa29e4
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 31 deletions.
15 changes: 6 additions & 9 deletions src/AeSdkBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,16 @@ class AeSdkBase {
}
}

async _getOptions(): Promise<{
_getOptions(): {
onNode: Node;
onAccount: AccountBase;
onCompiler: Compiler;
networkId: string;
}> {
} {
return {
...this._options,
onNode: getValueOrErrorProxy(() => this.api),
onAccount: getValueOrErrorProxy(() => this._resolveAccount()),
onCompiler: getValueOrErrorProxy(() => this.compilerApi),
// TODO: remove networkId
networkId: (await this.api.getStatus()).networkId,
};
}

Expand All @@ -266,7 +263,7 @@ class AeSdkBase {
): Promise<EncodedData<'tx'>> {
// @ts-expect-error TODO: need to figure out what's wrong here
return _buildTx<TxType>(txType, {
...await this._getOptions(),
...this._getOptions(),
...options,
});
}
Expand Down Expand Up @@ -315,8 +312,8 @@ Object.assign(AeSdkBase.prototype, mapObject<Function, Function>(
methods,
([name, handler]) => [
name,
async function methodWrapper(...args: any[]) {
const instanceOptions = await this._getOptions();
function methodWrapper(...args: any[]) {
const instanceOptions = this._getOptions();
const lastArg = args[args.length - 1];
if (lastArg != null && typeof lastArg === 'object' && lastArg.constructor === Object) {
args[args.length - 1] = {
Expand All @@ -330,5 +327,5 @@ Object.assign(AeSdkBase.prototype, mapObject<Function, Function>(
],
));

export default AeSdkBase as new (options: ConstructorParameters<typeof AeSdkBase>[0]) =>
export default AeSdkBase as new (options?: ConstructorParameters<typeof AeSdkBase>[0]) =>
AeSdkBase & AeSdkBaseMethods;
6 changes: 5 additions & 1 deletion src/spend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ export async function send(tx: EncodedData<'tx'>, options: SendOptions): Promise
? undefined
: (await getAccount(await options.onAccount.address(options), options)).authFun;

const signed = await options.onAccount.signTransaction(tx, { ...options, authFun });
const signed = await options.onAccount.signTransaction(tx, {
...options,
authFun,
networkId: (await options.onNode.getStatus()).networkId,
});

return options.innerTx === true
? { hash: buildTxHash(signed), rawTx: signed }
Expand Down
43 changes: 26 additions & 17 deletions test/integration/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
UnexpectedTsError,
} from '../../src/utils/errors';
import { AeSdk } from '../../src';
import { EncodedData } from '../../src/utils/encoder';

describe('Accounts', () => {
let aeSdk: AeSdk;
Expand Down Expand Up @@ -159,36 +160,42 @@ describe('Accounts', () => {
});

describe('Make operation on specific account without changing of current account', () => {
let address: EncodedData<'ak'>;

before(async () => {
address = await aeSdk.address();
});

it('Can make spend on specific account', async () => {
const current = await aeSdk.address();
const accounts = aeSdk.addresses();
const onAccount = accounts.find((acc) => acc !== current);
const onAccount = accounts.find((acc) => acc !== address);

const { tx } = await aeSdk.spend(1, await aeSdk.address(), { onAccount });
const { tx } = await aeSdk.spend(1, address, { onAccount });
if (tx?.senderId == null) throw new UnexpectedTsError();
tx.senderId.should.be.equal(onAccount);
current.should.be.equal(current);
});

it('Fail on invalid account', async () => {
await expect(aeSdk.spend(1, await aeSdk.address(), { onAccount: 1 as any }))
.to.be.rejectedWith(
TypeError,
'Account should be an address (ak-prefixed string), keypair, or instance of AccountBase, got 1 instead',
);
it('Fail on invalid account', () => {
expect(() => {
aeSdk.spend(1, address, { onAccount: 1 as any });
}).to.throw(
TypeError,
'Account should be an address (ak-prefixed string), keypair, or instance of AccountBase, got 1 instead',
);
});

it('Fail on non exist account', async () => {
await expect(aeSdk.spend(1, await aeSdk.address(), { onAccount: 'ak_q2HatMwDnwCBpdNtN9oXf5gpD9pGSgFxaa8i2Evcam6gjiggk' }))
.to.be.rejectedWith(
UnavailableAccountError,
'Account for ak_q2HatMwDnwCBpdNtN9oXf5gpD9pGSgFxaa8i2Evcam6gjiggk not available',
);
it('Fail on non exist account', () => {
expect(() => {
aeSdk.spend(1, address, { onAccount: 'ak_q2HatMwDnwCBpdNtN9oXf5gpD9pGSgFxaa8i2Evcam6gjiggk' });
}).to.throw(
UnavailableAccountError,
'Account for ak_q2HatMwDnwCBpdNtN9oXf5gpD9pGSgFxaa8i2Evcam6gjiggk not available',
);
});

it('Fail on no accounts', async () => {
const aeSdkWithoutAccount = await getSdk(0);
await expect(aeSdkWithoutAccount.spend(1, await aeSdk.address()))
await expect(aeSdkWithoutAccount.spend(1, address))
.to.be.rejectedWith(
TypeError,
'Account should be an address (ak-prefixed string), keypair, or instance of AccountBase, got undefined instead',
Expand All @@ -202,6 +209,7 @@ describe('Accounts', () => {
'Account should be an address (ak-prefixed string), keypair, or instance of AccountBase, got 123 instead',
);
});

it('Make operation on account using keyPair/MemoryAccount', async () => {
const keypair = generateKeyPair();
const memoryAccount = new MemoryAccount({ keypair });
Expand All @@ -217,6 +225,7 @@ describe('Accounts', () => {
addressFromKeypair.should.be.equal(keypair.publicKey);
addressFrommemoryAccount.should.be.equal(keypair.publicKey);
});

it('Make operation on account using keyPair: Invalid keypair', async () => {
const keypair = generateKeyPair();
keypair.publicKey = 'ak_bev1aPMdAeJTuUiCJ7mHbdQiAizrkRGgoV9FfxHYb6pAxo5WY';
Expand Down
2 changes: 1 addition & 1 deletion test/integration/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Node Chain', () => {
new Array(5).fill(undefined).map(async () => aeSdk.height()),
);
expect(heights).to.eql(heights.map(() => heights[0]));
expect(httpSpy.callCount).to.be.equal(2);
expect(httpSpy.callCount).to.be.equal(1);
httpSpy.restore();
});

Expand Down
7 changes: 4 additions & 3 deletions test/integration/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,11 @@ describe('Aepp<->Wallet', function aeppWallet() {
Object.keys(subscriptionResponse.address.connected).length.should.be.equal(1);
});

it('Try to use `onAccount` for not existent account', async () => {
it('Try to use `onAccount` for not existent account', () => {
const { publicKey } = generateKeyPair();
await expect(aepp.spend(100, publicKey, { onAccount: publicKey }))
.to.be.rejectedWith(UnAuthorizedAccountError, `You do not have access to account ${publicKey}`);
expect(() => {
aepp.spend(100, publicKey, { onAccount: publicKey });
}).to.throw(UnAuthorizedAccountError, `You do not have access to account ${publicKey}`);
});

it('aepp accepts key pairs in onAccount', async () => {
Expand Down
11 changes: 11 additions & 0 deletions test/unit/ae-sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';
import '../index';
import { AeSdk } from '../../src';

describe('AeSdk', () => {
it('executes methods without node, compiler, accounts', () => {
const aeSdk = new AeSdk();
expect(aeSdk._getPollInterval('block')).to.be.a('number');
});
});

0 comments on commit 8fa29e4

Please sign in to comment.