feat: auto retry ABI fetch

This commit is contained in:
c4605
2024-03-20 12:59:14 +00:00
parent 7a658b4651
commit 9345e639a0
3 changed files with 37 additions and 6 deletions

View File

@@ -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) {

View File

@@ -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}`);

View 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;
}
}
}