Skip to content

Commit fd6e2f9

Browse files
Merge pull request #26 from useLiquidOps/feat/dryrunfifo
Dryrun queue
2 parents 276eab8 + b01484f commit fd6e2f9

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

src/ao/messaging/DryRunFIFO.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { DryRun, DryRunResult, MessageInput } from "@permaweb/aoconnect/dist/lib/dryrun";
2+
import { connect } from "@permaweb/aoconnect";
3+
4+
interface DryRunQueueItem {
5+
msg: MessageInput;
6+
resolve: (result: DryRunResult) => void;
7+
reject: (reason?: any) => void;
8+
}
9+
10+
export class DryRunFIFO {
11+
#queue: DryRunQueueItem[];
12+
#running: boolean;
13+
#availableDryRuns: DryRunList;
14+
15+
constructor(CUs: string[], delay = 500) {
16+
this.#queue = [];
17+
this.#running = false;
18+
this.#availableDryRuns = new DryRunList(CUs.map(
19+
(CU_URL) => connect({ MODE: "legacy", CU_URL }).dryrun
20+
), delay);
21+
}
22+
23+
put(msg: MessageInput) {
24+
return new Promise<DryRunResult>((resolve, reject) => {
25+
this.#queue.push({ msg, resolve, reject });
26+
this.#run();
27+
});
28+
}
29+
30+
async #run() {
31+
if (this.#running) return;
32+
this.#running = true;
33+
34+
while (this.#queue.length > 0) {
35+
const dryrun = await this.#availableDryRuns.waitForOne();
36+
const { msg, resolve, reject } = this.#queue.shift()!;
37+
38+
dryrun(msg)
39+
.then(resolve)
40+
.catch(reject)
41+
.finally(() => this.#availableDryRuns.push(dryrun));
42+
}
43+
44+
this.#running = false;
45+
}
46+
}
47+
48+
class DryRunList {
49+
#list: DryRun[];
50+
#delay: number;
51+
#resolves: Array<(val: DryRun) => void>;
52+
53+
constructor(list: DryRun[] = [], delay: number) {
54+
this.#list = list;
55+
this.#delay = delay;
56+
this.#resolves = [];
57+
}
58+
59+
push(item: DryRun) {
60+
setTimeout(() => {
61+
const nextRequest = this.#resolves.shift();
62+
if (nextRequest) nextRequest(item);
63+
else this.#list.push(item);
64+
}, this.#delay);
65+
}
66+
67+
async waitForOne() {
68+
const next = this.#list.shift();
69+
if (next) return next;
70+
71+
return new Promise<DryRun>((resolve) => {
72+
this.#resolves.push(resolve);
73+
});
74+
}
75+
}

src/ao/messaging/getData.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import { DryRunResult } from "@permaweb/aoconnect/dist/lib/dryrun";
1+
import { DryRun, DryRunResult, MessageInput } from "@permaweb/aoconnect/dist/lib/dryrun";
22
import { connectToAO, Services } from "../utils/connect";
3+
import { dryRunAwait } from "../utils/dryRunAwait";
4+
import { connect } from "@permaweb/aoconnect";
5+
import LiquidOps from "../..";
36

47
interface MessageTags {
58
Target: string;
@@ -35,12 +38,15 @@ export async function getData(
3538

3639
try {
3740
const { dryrun } = connectToAO(config);
38-
const { Messages, Spawns, Output, Error } = await dryrun({
41+
const msg = {
3942
process: targetProcessID,
4043
data: "",
4144
tags: convertedMessageTags,
4245
Owner: messageTags.Owner || "1234",
43-
});
46+
};
47+
const { Messages, Spawns, Output, Error } = LiquidOps.dryRunFifo ?
48+
await LiquidOps.dryRunFifo.put(msg) :
49+
await dryrun(msg);
4450

4551
return {
4652
Messages,

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,14 @@ import {
124124
GetEarnings,
125125
GetEarningsRes,
126126
} from "./functions/lend/getEarnings";
127+
import { DryRunFIFO } from "./ao/messaging/DryRunFIFO";
127128

128129
class LiquidOps {
129130
private signer: any;
130131
private configs: Omit<Configs, "MODE">;
131132

133+
static dryRunFifo?: DryRunFIFO;
134+
132135
constructor(signer: any, configs: Omit<Configs, "MODE"> = {}) {
133136
if (!signer) {
134137
throw new Error("Please specify a ao createDataItemSigner signer");
@@ -352,4 +355,5 @@ export {
352355
tokenInput,
353356
tokenData,
354357
lqdTokenAddress,
358+
DryRunFIFO,
355359
};

0 commit comments

Comments
 (0)