Skip to content

Commit

Permalink
improve(arweave): handle out of gas error
Browse files Browse the repository at this point in the history
  • Loading branch information
james-a-morris committed Feb 15, 2024
1 parent 132e6ef commit fa3f545
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/caching/Arweave/ArweaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,24 @@ export class ArweaveClient {
await this.client.transactions.sign(transaction, this.arweaveJWT);
// Send the transaction
const result = await this.client.transactions.post(transaction);
this.logger.debug({
at: "ArweaveClient:set",
message: `Arweave transaction posted with ${transaction.id}`,
});

// Ensure that the result is successful
if (result.status !== 200) {
const message = result?.data?.error?.msg ?? "Unknown error";
this.logger.error({
at: "ArweaveClient:set",
message: `Arweave transaction failed with ${transaction.id}`,
message,
result,
txn: transaction.id,
address: await this.getAddress(),
balance: (await this.getBalance()).toString(),
});
throw new Error("Server failed to receive arweave transaction");
throw new Error(message);
} else {
this.logger.debug({
at: "ArweaveClient:set",
message: `Arweave transaction posted with ${transaction.id}`,
});
}
return transaction.id;
}
Expand Down
20 changes: 20 additions & 0 deletions test/arweaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ArweaveClient } from "../src/caching";
import { parseWinston, toBN } from "../src/utils";
import { object, string } from "superstruct";
import { ARWEAVE_TAG_APP_NAME } from "../src/constants";
import { assertPromiseError } from "./utils";

const INITIAL_FUNDING_AMNT = "5000000000";
const LOCAL_ARWEAVE_NODE = {
Expand Down Expand Up @@ -154,4 +155,23 @@ describe("ArweaveClient", () => {
topic: topicTag,
});
});

it.only("should gracefully handle out of funds errors", async () => {
const jwk = await Arweave.init({}).wallets.generate();
// Create a new Arweave client
const client = new ArweaveClient(
jwk,
// Define default winston logger
winston.createLogger({
level: "info",
format: winston.format.json(),
defaultMeta: { service: "arweave-client" },
transports: [new winston.transports.Console()],
}),
LOCAL_ARWEAVE_NODE.host,
LOCAL_ARWEAVE_NODE.protocol,
LOCAL_ARWEAVE_NODE.port
);
await assertPromiseError(client.set({ test: "value" }), "You don't have enough tokens");
});
});

0 comments on commit fa3f545

Please sign in to comment.