mirror of
https://github.com/alexgo-io/clarity-codegen.git
synced 2026-01-12 14:34:34 +08:00
feat: auto retry ABI fetch
This commit is contained in:
@@ -18,6 +18,7 @@ import {
|
||||
isClarityAbiTuple,
|
||||
} from "./contractAbi";
|
||||
import { assertNever, mapValues } from "../utils/helpers";
|
||||
import { asyncAutoRetry } from "../utils/asyncAutoRetry";
|
||||
import axios from "axios";
|
||||
|
||||
type TranscoderDefArgument = TranscoderDef | Record<string, TranscoderDef>;
|
||||
@@ -32,7 +33,7 @@ const toTranscoderDef = ({
|
||||
}): { def: TranscoderDef } => {
|
||||
if (isClarityAbiPrimitive(type)) {
|
||||
if (type === "uint128") {
|
||||
return {def: ["uintT"]};
|
||||
return { def: ["uintT"] };
|
||||
} else if (type === "int128") {
|
||||
return { def: ["intT"] };
|
||||
} else if (type === "bool") {
|
||||
@@ -258,10 +259,12 @@ export const generateContractFromAbi = async ({
|
||||
apiHost: string;
|
||||
output: string;
|
||||
packageName: string;
|
||||
contractOverwrites: {[from: string]: string}
|
||||
contractOverwrites: { [from: string]: string };
|
||||
}): Promise<void> => {
|
||||
const url = `${apiHost}/v2/contracts/interface/${principal}/${contractOverwrites[contractName] ?? contractName}`;
|
||||
const response = await axios.get(url);
|
||||
const url = `${apiHost}/v2/contracts/interface/${principal}/${
|
||||
contractOverwrites[contractName] ?? contractName
|
||||
}`;
|
||||
const response = await asyncAutoRetry(() => axios.get(url));
|
||||
const interfaceData: ClarityAbi = response.data;
|
||||
const defs = {} as Record<string, ContractEntryDescriptorDef>;
|
||||
for (const func of interfaceData.functions) {
|
||||
|
||||
@@ -11,9 +11,11 @@ export async function generateContracts(
|
||||
output: string,
|
||||
name: string,
|
||||
packageName: string = "clarity-codegen",
|
||||
contractOverwrites: {[from: string]: string} = {}
|
||||
contractOverwrites: {[from: string]: string} = {},
|
||||
options: {concurrency?: number} = {}
|
||||
) {
|
||||
const batch = new YBatch({ concurrency: 16 });
|
||||
const concurrency = options.concurrency?? 16
|
||||
const batch = new YBatch({ concurrency });
|
||||
for (const cname of contracts) {
|
||||
await batch.add(async () => {
|
||||
console.log(`Generating contract ${typeof principal === 'string' ? principal : principal(cname)}.${cname}`);
|
||||
|
||||
26
src/utils/asyncAutoRetry.ts
Normal file
26
src/utils/asyncAutoRetry.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
export async function asyncAutoRetry<T>(
|
||||
action: () => Promise<T>,
|
||||
options: {
|
||||
count?: number;
|
||||
isNeedRetry?: (error: any) => boolean;
|
||||
} = {}
|
||||
): Promise<T> {
|
||||
const count = options.count ?? 20;
|
||||
const isNeedRetry = options.isNeedRetry ?? (() => true);
|
||||
|
||||
let remainingRetryCount = count;
|
||||
while (true) {
|
||||
try {
|
||||
return await action();
|
||||
} catch (error) {
|
||||
remainingRetryCount -= 1;
|
||||
|
||||
if (remainingRetryCount >= 0 && isNeedRetry(error)) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
continue;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user