Skip to content

Commit

Permalink
feat(dev-tools): add support for DR bundle methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomasvdam committed Sep 24, 2024
1 parent 5528406 commit ef467bc
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 3 deletions.
5 changes: 5 additions & 0 deletions libs/dev-tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ export { uploadWasmBinary } from "@dev-tools/services/wasm/upload-wasm-binary";
export { getWasmBinary } from "@dev-tools/services/wasm/get-wasm-binary";

export { postDataRequest } from "@dev-tools/services/dr/post-data-request";
export { postDataRequestBundle } from "@dev-tools/services/dr/post-data-request-bundle";
export { getDataRequestStatus } from "@dev-tools/services/dr/get-data-request-status";
export { getDataRequestBundleStatus } from "@dev-tools/services/dr/get-data-request-bundle-status";
export { awaitDataResult } from "@dev-tools/services/dr/await-data-result";
export { awaitDataResultBundle } from "@dev-tools/services/dr/await-data-result-bundle";
export { getDataResult } from "@dev-tools/services/dr/get-data-result";
export { getDataResultBundle } from "@dev-tools/services/dr/get-data-result-bundle";
export { postAndAwaitDataRequest } from "@dev-tools/services/dr/post-and-await-data-request";
export { postAndAwaitDataRequestBundle } from "@dev-tools/services/dr/post-and-await-data-request-bundle";

export * from "./lib/testing/create-mock-reveal";
export * from "./lib/testing/create-mock-tally-args";
Expand Down
41 changes: 41 additions & 0 deletions libs/dev-tools/src/lib/services/dr/await-data-result-bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { ISigner } from "@dev-tools/services/signer";
import { createSigningClient } from "@dev-tools/services/signing-client";
import { tryAsync } from "@dev-tools/utils/try-async";
import { getDataResultBundle } from "./get-data-result-bundle";

type Opts = {
/** Defaults to 60 seconds. */
timeoutSeconds: number;
/** Defaults to 10 seconds */
pollingIntervalSeconds: number;
};

export async function awaitDataResultBundle(
signer: ISigner,
drIds: string[],
opts: Opts = { timeoutSeconds: 60, pollingIntervalSeconds: 10 },
) {
const sigingClientResult = await createSigningClient(signer);
if (sigingClientResult.isErr) {
throw sigingClientResult.error;
}
const timeoutTime = Date.now() + opts.timeoutSeconds * 1000;

while (Date.now() < timeoutTime) {
const result = await tryAsync(async () =>
getDataResultBundle(signer, drIds),
);
if (!result.isErr && result.value.every((r) => r.result !== null)) {
return result.value;
}
await sleep(opts.pollingIntervalSeconds * 1000);
}

throw new Error(
`Timeout: bundled data requests took longer than ${opts.timeoutSeconds} seconds to execute.`,
);
}

export function sleep(durationMs: number) {
return new Promise((resolve) => setTimeout(resolve, durationMs));
}
2 changes: 1 addition & 1 deletion libs/dev-tools/src/lib/services/dr/await-data-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function awaitDataResult(
await sleep(opts.pollingIntervalSeconds * 1000);
}

return new Error(
throw new Error(
`Timeout: DR "${drId}" took longer than ${opts.timeoutSeconds} seconds to execute.`,
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { tryAsync } from "@dev-tools/utils/try-async";
import type { ISigner } from "../signer";
import {
type DataRequestStatus,
getDataRequestStatus,
} from "./get-data-request-status";

type StatusBatchResponse = {
drId: string;
status: DataRequestStatus | null;
error?: string;
};

export async function getDataRequestBundleStatus(
signer: ISigner,
drIds: string[],
): Promise<StatusBatchResponse[]> {
return Promise.all(
drIds.map(async (drId) => {
const status = await tryAsync(() => getDataRequestStatus(signer, drId));
return status.mapOrElse<StatusBatchResponse>(
(error) => {
return { drId, status: null, error };
},
({ status }) => {
return { drId, status, error: undefined };
},
);
}),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import assert from "node:assert";
import type { ISigner } from "../signer";
import { createSigningClient } from "../signing-client";

type DataRequestStatus = "pending" | "committing" | "revealing" | "resolved";
export type DataRequestStatus =
| "pending"
| "committing"
| "revealing"
| "resolved";

export async function getDataRequestStatus(
signer: ISigner,
Expand Down
28 changes: 28 additions & 0 deletions libs/dev-tools/src/lib/services/dr/get-data-result-bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { tryAsync } from "@dev-tools/utils/try-async";
import type { ISigner } from "../signer";
import { type DataRequestResult, getDataResult } from "./get-data-result";

type ResultBatchResponse = {
drId: string;
result: DataRequestResult | null;
error?: string;
};

export async function getDataResultBundle(
signer: ISigner,
drIds: string[],
): Promise<ResultBatchResponse[]> {
return Promise.all(
drIds.map(async (drId) => {
const status = await tryAsync(() => getDataResult(signer, drId));
return status.mapOrElse<ResultBatchResponse>(
(error) => {
return { drId, result: null, error };
},
(result) => {
return { drId, result, error: undefined };
},
);
}),
);
}
4 changes: 3 additions & 1 deletion libs/dev-tools/src/lib/services/dr/get-data-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ const DataResultSchema = pipe(
}),
);

export type DataRequestResult = InferOutput<typeof DataResultSchema>;

export async function getDataResult(
signer: ISigner,
drId: string,
): Promise<InferOutput<typeof DataResultSchema>> {
): Promise<DataRequestResult> {
const sigingClientResult = await createSigningClient(signer);
if (sigingClientResult.isErr) {
throw sigingClientResult.error;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { GasOptions } from "@dev-tools/services/gas-options";
import type { ISigner } from "@dev-tools/services/signer";
import { awaitDataResultBundle } from "./await-data-result-bundle";
import type { PostDataRequestInput } from "./create-dr-input";
import { postDataRequestBundle } from "./post-data-request-bundle";

type AwaitOptions = Parameters<typeof awaitDataResultBundle>["2"];

export async function postAndAwaitDataRequestBundle(
signer: ISigner,
dataRequestInputs: PostDataRequestInput[],
gasOptions?: GasOptions,
awaitOptions?: AwaitOptions,
) {
const postDrResponse = await postDataRequestBundle(
signer,
dataRequestInputs,
gasOptions,
);

const dataResults = await awaitDataResultBundle(
signer,
postDrResponse.drIds,
awaitOptions,
);

return dataResults;
}
68 changes: 68 additions & 0 deletions libs/dev-tools/src/lib/services/dr/post-data-request-bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { GasOptions } from "../gas-options";
import { signAndSendTx } from "../sign-and-send-tx";
import type { ISigner } from "../signer";
import { createSigningClient } from "../signing-client";
import {
type PostDataRequestInput,
createPostedDataRequest,
} from "./create-dr-input";

export async function postDataRequestBundle(
signer: ISigner,
dataRequestInputs: PostDataRequestInput[],
gasOptions?: GasOptions,
): Promise<{ tx: string; drIds: string[] }> {
const sigingClientResult = await createSigningClient(signer);
if (sigingClientResult.isErr) {
throw sigingClientResult.error;
}

const contract = signer.getCoreContractAddress();

const { client: sigingClient, address } = sigingClientResult.value;

const messages = dataRequestInputs.map((dataRequestInput) => {
return {
typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract",
value: {
sender: address,
contract,
msg: Buffer.from(
JSON.stringify({
post_data_request: {
seda_payload: Buffer.from([]).toString("base64"),
payback_address: Buffer.from("seda_sdk").toString("base64"),
posted_dr: createPostedDataRequest(dataRequestInput),
},
}),
),
},
};
});

const response = await signAndSendTx(
sigingClient,
address,
messages,
gasOptions,
);

if (response.isErr) {
throw response.error;
}

if (response.value.code === 1) {
throw new Error(`TX failed: "${response.value.transactionHash}"`);
}

const drIds = response.value.msgResponses.map((messageResponseRaw) => {
const messageResponse = sigingClient.registry.decode(messageResponseRaw);

return JSON.parse(Buffer.from(messageResponse.data).toString());
});

return {
tx: response.value.transactionHash,
drIds,
};
}

0 comments on commit ef467bc

Please sign in to comment.