-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dev-tools): add support for DR bundle methods
- Loading branch information
1 parent
5528406
commit ef467bc
Showing
9 changed files
with
210 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
libs/dev-tools/src/lib/services/dr/await-data-result-bundle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
libs/dev-tools/src/lib/services/dr/get-data-request-bundle-status.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}, | ||
); | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
libs/dev-tools/src/lib/services/dr/get-data-result-bundle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}, | ||
); | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
libs/dev-tools/src/lib/services/dr/post-and-await-data-request-bundle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
68
libs/dev-tools/src/lib/services/dr/post-data-request-bundle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |