Skip to content

Commit

Permalink
feat: add getContext method to AeSdk
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Dec 6, 2023
1 parent 6bebe0c commit 04508d0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
12 changes: 9 additions & 3 deletions src/AeSdkBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ export default class AeSdkBase extends AeSdkMethods {
.signOracleQueryDelegationToContract(contractAddress, oracleQueryId, options);
}

override _getOptions(callOptions: AeSdkMethodsOptions = {}): {
/**
* The same as AeSdkMethods:getContext, but it would resolve ak_-prefixed address in
* `mergeWith.onAccount` to AccountBase.
*/
override getContext(mergeWith: AeSdkMethodsOptions = {}): AeSdkMethodsOptions & {
onNode: Node;
onAccount: AccountBase;
onCompiler: CompilerBase;
Expand All @@ -221,8 +225,10 @@ export default class AeSdkBase extends AeSdkMethods {
...this._options,
onNode: getValueOrErrorProxy(() => this.api),
onCompiler: getValueOrErrorProxy(() => this.compilerApi),
...callOptions,
onAccount: getValueOrErrorProxy(() => this._resolveAccount(callOptions.onAccount)),
...mergeWith,
onAccount: mergeWith.onAccount != null
? this._resolveAccount(mergeWith.onAccount)
: getValueOrErrorProxy(() => this._resolveAccount()),
};
}
}
18 changes: 12 additions & 6 deletions src/AeSdkMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,32 @@ class AeSdkMethods {
Object.assign(this._options, options);
}

_getOptions(
callOptions: AeSdkMethodsOptions = {},
/**
* Returns sdk instance options with references to current account, node, compiler.
* Used to create an instance (Contract, Oracle) bound to AeSdk state.
* @param mergeWith - Merge context with these extra options
* @returns Context object
*/
getContext(
mergeWith: AeSdkMethodsOptions = {},
): AeSdkMethodsOptions & { onAccount: AccountBase; onCompiler: CompilerBase; onNode: Node } {
return {
...this._options,
onAccount: getValueOrErrorProxy(() => this._options.onAccount),
onNode: getValueOrErrorProxy(() => this._options.onNode),
onCompiler: getValueOrErrorProxy(() => this._options.onCompiler),
...callOptions,
...mergeWith,
};
}

async buildTx(options: TxParamsAsync): Promise<Encoded.Transaction> {
return buildTxAsync({ ...this._getOptions(), ...options });
return buildTxAsync({ ...this.getContext(), ...options });
}

async initializeContract<Methods extends ContractMethodsBase>(
options?: Omit<Parameters<typeof Contract.initialize>[0], 'onNode'> & { onNode?: Node },
): Promise<Contract<Methods>> {
return Contract.initialize<Methods>(this._getOptions(options as AeSdkMethodsOptions));
return Contract.initialize<Methods>(this.getContext(options as AeSdkMethodsOptions));
}
}

Expand Down Expand Up @@ -133,7 +139,7 @@ Object.assign(AeSdkMethods.prototype, mapObject<Function, Function>(
function methodWrapper(this: AeSdkMethods, ...args: any[]) {
args.length = handler.length;
const options = args[args.length - 1];
args[args.length - 1] = this._getOptions(options);
args[args.length - 1] = this.getContext(options);
return handler(...args);
},
],
Expand Down
4 changes: 2 additions & 2 deletions test/integration/AeSdkMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ describe('AeSdkMethods', () => {
expect(contract.$options.onAccount?.address).to.be.eql(accounts[1].address);
});

it('converts options to JSON', () => {
const options = aeSdkMethods._getOptions();
it('converts context to JSON', () => {
const options = aeSdkMethods.getContext();
const data = JSON.parse(JSON.stringify(options));
data.onNode._httpClient = '<removed>';
data.onCompiler.api._httpClient = '<removed>';
Expand Down
12 changes: 6 additions & 6 deletions test/integration/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,17 @@ describe('Accounts', () => {
tx.senderId.should.be.equal(onAccount);
});

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

it('Fail on non exist account', async () => {
await expect(
aeSdk.spend(1, aeSdk.address, { onAccount: 'ak_q2HatMwDnwCBpdNtN9oXf5gpD9pGSgFxaa8i2Evcam6gjiggk' }),
).to.be.rejectedWith(
it('Fail on non exist account', () => {
expect(
() => { aeSdk.spend(1, aeSdk.address, { onAccount: 'ak_q2HatMwDnwCBpdNtN9oXf5gpD9pGSgFxaa8i2Evcam6gjiggk' }); },
).to.throw(
UnavailableAccountError,
'Account for ak_q2HatMwDnwCBpdNtN9oXf5gpD9pGSgFxaa8i2Evcam6gjiggk not available',
);
Expand Down
4 changes: 2 additions & 2 deletions test/integration/contract-aci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,14 @@ describe('Contract instance', () => {
}
}

const contract1 = new TestContract(aeSdk._getOptions());
const contract1 = new TestContract(aeSdk.getContext());
expect(contract1._aci).to.be.an('array');
expect(contract1.$options).to.be.an('object');
await contract1.$deploy([]);
expect((await contract1.getArg(42)).decodedResult).to.be.equal(42n);

const contract2 = await TestContract.initialize({
...aeSdk._getOptions(),
...aeSdk.getContext(),
address: contract1.$options.address,
});
expect(contract2._aci).to.be.an('array');
Expand Down
6 changes: 3 additions & 3 deletions test/integration/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ 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

0 comments on commit 04508d0

Please sign in to comment.