Skip to content

Commit

Permalink
fix(procedure): allow running procedures without an initial pkey
Browse files Browse the repository at this point in the history
  • Loading branch information
monitz87 committed Jun 25, 2021
1 parent f7575d5 commit 2d6ed14
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
36 changes: 24 additions & 12 deletions src/base/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export class Context {

private _middlewareApi: ApolloClient<NormalizedCacheObject> | null;

private _polymeshApi: ApiPromise;

/**
* @hidden
*/
Expand All @@ -146,10 +148,26 @@ export class Context {
polymeshApi.on('error', callback);

this._middlewareApi = middlewareApi;
this._polymeshApi = polymeshApi;
this.polymeshApi = Context.createPolymeshApiProxy(this);
this.keyring = keyring;
this.ss58Format = ss58Format;

const currentPair = keyring.getPairs()[0];

if (currentPair) {
assertFormatValid(currentPair.address, ss58Format);
this.currentPair = currentPair;
}
}

this.polymeshApi = new Proxy(polymeshApi, {
/**
* @hidden
*/
static createPolymeshApiProxy(ctx: Context): ApiPromise {
return new Proxy(ctx._polymeshApi, {
get: (target, prop: keyof ApiPromise): ApiPromise[keyof ApiPromise] => {
if (prop === 'tx' && !this.currentPair) {
if (prop === 'tx' && !ctx.currentPair) {
throw new PolymeshError({
code: ErrorCode.FatalError,
message: 'Cannot perform transactions without an active account',
Expand All @@ -159,15 +177,6 @@ export class Context {
return target[prop];
},
});
this.keyring = keyring;
this.ss58Format = ss58Format;

const currentPair = keyring.getPairs()[0];

if (currentPair) {
assertFormatValid(currentPair.address, ss58Format);
this.currentPair = currentPair;
}
}

/**
Expand Down Expand Up @@ -1068,6 +1077,9 @@ export class Context {
* Context to Procedures with different signers
*/
public clone(): Context {
return clone(this);
const cloned = clone(this);
cloned.polymeshApi = Context.createPolymeshApiProxy(cloned);

return cloned;
}
}
21 changes: 8 additions & 13 deletions src/base/Procedure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ export class Procedure<

/**
* @hidden
* Set the context and storage (if not already set)
* Set the context and storage (if not already set), return the Context
*/
private async setup(args: Args, context: Context, opts: ProcedureOpts = {}): Promise<void> {
private async setup(args: Args, context: Context, opts: ProcedureOpts = {}): Promise<Context> {
if (!this._context) {
const ctx = context.clone();
const { signer } = opts;
Expand All @@ -125,6 +125,8 @@ export class Procedure<
if (!this._storage) {
this._storage = await this.prepareStorage(args);
}

return this._context;
}

/**
Expand All @@ -145,9 +147,7 @@ export class Procedure<
context: Context,
opts?: ProcedureOpts
): Promise<ProcedureAuthorizationStatus> {
await this.setup(args, context, opts);

const { context: ctx } = this;
const ctx = await this.setup(args, context, opts);

const checkAuthorizationResult = await this.getAuthorization(args);

Expand Down Expand Up @@ -216,13 +216,9 @@ export class Procedure<
): Promise<TransactionQueue<ReturnValue, QueueReturnType>> {
try {
const { args: procArgs, transformer } = args;
const ctx = await this.setup(procArgs, context, opts);

await this.setup(procArgs, context, opts);

const { roles, permissions, accountFrozen } = await this._checkAuthorization(
procArgs,
context
);
const { roles, permissions, accountFrozen } = await this._checkAuthorization(procArgs, ctx);

if (accountFrozen) {
throw new PolymeshError({
Expand All @@ -247,10 +243,9 @@ export class Procedure<
}

const procedureResult = await this.prepareTransactions(procArgs);

return new TransactionQueue(
{ transactions: this.transactions, procedureResult, transformer },
context
ctx
);
} finally {
this.cleanup();
Expand Down
6 changes: 5 additions & 1 deletion src/base/__tests__/Procedure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ describe('Procedure class', () => {
sinon.match({ tx: tx2, args: [secondaryKeys] }),
]),
}),
context
{ ...context, currentPair: { address: 'something' } }
);

const func2 = async function (
Expand All @@ -202,8 +202,12 @@ describe('Procedure class', () => {
return this.addProcedure(proc1, args);
};

dsMockUtils.reset();

const proc2 = new Procedure(func2);

context = dsMockUtils.getContextInstance();

queue = await proc2.prepare({ args: procArgs }, context);
expect(queue).toMatchObject({
transactions: [
Expand Down

0 comments on commit 2d6ed14

Please sign in to comment.