Skip to content

Commit

Permalink
add logic for hydrating variables before resolving job activity
Browse files Browse the repository at this point in the history
  • Loading branch information
simke9445 committed Mar 22, 2024
1 parent df61ea1 commit fbd4b2a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/refs.injective.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"testnet": {
"warp-controller": {
"codeId": "7967",
"codeId": "7991",
"address": "inj1u56l96hs08hqznj0c202arftmh9j3c7m9dylpy"
},
"warp-resolver": {
"codeId": "7971",
"codeId": "7990",
"address": "inj1pku5zgartl3xdlr509u3wjfs48k2lqylpscydf"
},
"warp-templates": {
Expand Down
106 changes: 105 additions & 1 deletion src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { cosmosMsgToCreateTxMsg } from './utils';
import { warp_templates } from './types/contracts/warp_templates';
import { Job, parseJob } from './types/job';
import { warp_account_tracker } from './types/contracts';
import { cloneDeep } from 'lodash';

const FEE_ADJUSTMENT_FACTOR = 8;

Expand Down Expand Up @@ -57,7 +58,9 @@ export class WarpSdk {
}

public async isJobActive(jobId: string): Promise<boolean> {
const job = await this.job(jobId);
let job = await this.job(jobId);

job = await this.replaceVariableReferences(job);

for (let execution of job.executions) {
const isCondActive = await this.condition.resolveCond(execution.condition, job);
Expand All @@ -70,6 +73,22 @@ export class WarpSdk {
return false;
}

public async replaceVariableReferences(job: Job): Promise<Job> {
let resolvedVars: warp_resolver.Variable[] = [];

for (const variable of job.vars) {
const replacedVariable = await this.replaceReferencesInVariable(variable, resolvedVars);

const resolvedValue: string = await this.condition.resolveVariable(replacedVariable, (v) => String(v), job);

this.updateResolvedVars(replacedVariable, resolvedValue, resolvedVars);
}

job.vars = resolvedVars;

return job;
}

public async jobs(opts: warp_controller.QueryJobsMsg = {}): Promise<Job[]> {
const { jobs } = await contractQuery<
Extract<warp_controller.QueryMsg, { query_jobs: {} }>,
Expand Down Expand Up @@ -463,4 +482,89 @@ export class WarpSdk {

return this.wallet.tx(txPayload);
}

// private
private async replaceReferencesInVariable(
inputVariable: warp_resolver.Variable,
resolvedVars: warp_resolver.Variable[]
): Promise<warp_resolver.Variable> {
const variable = cloneDeep(inputVariable);

if ('query' in variable && variable.query.init_fn) {
let q = this.replaceReferencesInCustom(variable.query.init_fn.query, resolvedVars);

variable.query.init_fn.query = this.replaceReferencesInObject(q, resolvedVars);
}

return variable;
}

private replaceReferencesInCustom(
q: warp_resolver.QueryRequestFor_String,
resolvedVars: warp_resolver.Variable[]
): warp_resolver.QueryRequestFor_String {
if ('custom' in q) {
let parsed = JSON.parse(q.custom);
let replaced = this.replaceReferencesInObject(parsed, resolvedVars);

return {
custom: JSON.stringify(replaced),
};
}

return q;
}

private replaceReferencesInObject(query: any, resolvedVars: warp_resolver.Variable[]): any {
let jsonString = JSON.stringify(query);

resolvedVars.forEach((variable) => {
const varName = this.extractVariableName(variable);
const varValue = this.extractVariableValue(variable);
const reference = new RegExp(`\\$warp.variable.${varName}`, 'g');

jsonString = jsonString.replace(reference, varValue);
});

return JSON.parse(jsonString);
}

private updateResolvedVars(
variable: warp_resolver.Variable,
resolvedValue: string,
resolvedVars: warp_resolver.Variable[]
): void {
this.updateVariableValue(variable, resolvedValue);
resolvedVars.push(variable);
}

private updateVariableValue(variable: warp_resolver.Variable, value: string): void {
if ('static' in variable) {
variable.static.value = value;
} else if ('external' in variable) {
variable.external.value = value;
} else if ('query' in variable) {
variable.query.value = value;
}
}

private extractVariableName(variable: warp_resolver.Variable): string {
if ('static' in variable) {
return variable.static.name;
} else if ('external' in variable) {
return variable.external.name;
} else if ('query' in variable) {
return variable.query.name;
}
}

private extractVariableValue(variable: warp_resolver.Variable): string {
if ('static' in variable) {
return variable.static.value;
} else if ('external' in variable) {
return variable.external.value;
} else if ('query' in variable) {
return variable.query.value;
}
}
}

0 comments on commit fbd4b2a

Please sign in to comment.