Skip to content

Commit

Permalink
Merge pull request #254 from arda-org/retry-policy
Browse files Browse the repository at this point in the history
Replace pauseAfterSend by retry policy
  • Loading branch information
lcswillems authored Feb 22, 2025
2 parents 7e8f142 + 558bb5e commit 39b215c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 47 deletions.
6 changes: 0 additions & 6 deletions xsuite/src/proxy/fsproxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import { hexToBase64 } from "../data/utils";
import { getValuesInOrder, Proxy } from "./proxy";

export class FSProxy extends Proxy {
constructor(...[params]: ConstructorParameters<typeof Proxy>) {
params = typeof params === "string" ? { proxyUrl: params } : params;
params.pauseAfterSend ??= 0;
super(params);
}

async getInitialAddresses() {
const res = await this.fetch("/simulator/initial-wallets");
return {
Expand Down
6 changes: 0 additions & 6 deletions xsuite/src/proxy/lsproxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ import { EncodableAccount, eAccountUnfiltered } from "../data/encoding";
import { getSerializableAccount, Proxy } from "./proxy";

export class LSProxy extends Proxy {
constructor(...[params]: ConstructorParameters<typeof Proxy>) {
params = typeof params === "string" ? { proxyUrl: params } : params;
params.pauseAfterSend ??= 0;
super(params);
}

async getAllSerializableAccounts() {
const res = await this.fetch("/admin/get-all-accounts");
return (res.accounts as any[])
Expand Down
68 changes: 33 additions & 35 deletions xsuite/src/proxy/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class Proxy {
headers: HeadersInit;
fetcher?: Fetcher;
blockNonce?: number;
pauseAfterSend?: number; // TODO-MvX: remove this when blockchain fixed

constructor(params: ProxyNewParamsExtended) {
params = typeof params === "string" ? { proxyUrl: params } : params;
Expand All @@ -38,7 +37,6 @@ export class Proxy {
this.headers = params.headers ?? {};
this.fetcher = params.fetcher;
this.blockNonce = params.blockNonce;
this.pauseAfterSend = params.pauseAfterSend ?? 1_000;
}

static new(params: ProxyNewParamsExtended) {
Expand Down Expand Up @@ -103,19 +101,11 @@ export class Proxy {
`Only ${txsHashesSent.length} of ${rawTxs.length} transactions were sent. The other ones were invalid.`,
);
}
// TODO-MvX: remove this when blockchain fixed
if (this.pauseAfterSend) {
await new Promise((r) => setTimeout(r, this.pauseAfterSend));
}
return txsHashesSent;
}

async sendTx(tx: BroadTx) {
const res = await this.fetch("/transaction/send", await broadTxToRawTx(tx));
// TODO-MvX: remove this when blockchain fixed
if (this.pauseAfterSend) {
await new Promise((r) => setTimeout(r, this.pauseAfterSend));
}
return res.txHash as string;
}

Expand Down Expand Up @@ -156,31 +146,40 @@ export class Proxy {
// eslint-disable-next-line no-constant-condition
while (true) {
const txDataStartTime = Date.now();
let txData = await this.getTxData(txHash);
const hash: string = txData.hash;
const explorerUrl = `${this.explorerUrl}/transactions/${hash}`;
txData = { explorerUrl, hash, ...txData };
const error = findErrorInTxData(txData);
if (error) {
return {
type: "fail",
errorCode: error.code,
errorMessage: error.message,
tx: txData,
};
let txData: TxData | undefined;
try {
txData = await this.getTxData(txHash);
} catch (e) {
if (elapsedBlocks >= 1) {
throw e;
}
}
const success = findSuccessInTxData(txData, elapsedBlocks);
if (success) {
const gasUsed: number = txData.gasUsed;
const fee: bigint = BigInt(txData.fee);
return {
type: "success",
explorerUrl,
hash,
gasUsed,
fee,
tx: txData,
};
if (txData) {
const hash: string = txData.hash;
const explorerUrl = `${this.explorerUrl}/transactions/${hash}`;
txData = { explorerUrl, hash, ...txData };
const error = findErrorInTxData(txData);
if (error) {
return {
type: "fail",
errorCode: error.code,
errorMessage: error.message,
tx: txData,
};
}
const success = findSuccessInTxData(txData, elapsedBlocks);
if (success) {
const gasUsed: number = txData.gasUsed;
const fee: bigint = BigInt(txData.fee);
return {
type: "success",
explorerUrl,
hash,
gasUsed,
fee,
tx: txData,
};
}
}
elapsedBlocks += await this.beforeNextTxData(txDataStartTime);
}
Expand Down Expand Up @@ -801,7 +800,6 @@ export type ProxyNewParams = {
headers?: HeadersInit;
fetcher?: Fetcher;
blockNonce?: number;
pauseAfterSend?: number; // TODO-MvX: remove this when blockchain fixed
};

export type ProxyNewRealnetParams = Prettify<
Expand Down

0 comments on commit 39b215c

Please sign in to comment.