Skip to content

Commit

Permalink
Merge pull request #11 from holaplex/espi/arweave-payer
Browse files Browse the repository at this point in the history
Switch to Arweave wallet
  • Loading branch information
kespinola authored Oct 18, 2023
2 parents 398c46a + d10af58 commit 7fac954
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
11 changes: 4 additions & 7 deletions bundlr.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,17 @@ class Uploader {

const response = await this.client.fund(price);

fastify.log.info(response);
console.log(response.message);

return price;
}
}

export function irys(gateway, url, providerUrl, key) {
export function irys(gateway, url, key) {
const irys = new Irys({
url, // URL of the node you want to connect to
token: "solana", // Token used for payment
key, // ETH or SOL private key
config: {
providerUrl,
},
token: "arweave", // Token used for payment
key: JSON.parse(key),
});

return new Uploader(irys, gateway);
Expand Down
34 changes: 16 additions & 18 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const UploadError = createError(
"UPLOAD_ERROR",
"The upload was not successful"
);
const FundError = createError("FUND_ERROR", "The funding was not successful");

// Initialize Fastify with logging enabled
const fastify = Fastify({
Expand All @@ -25,12 +26,9 @@ await fastify.register(fastifyEnv, {
dotenv: true,
schema: {
type: "object",
required: ["SOLANA_KEYPAIR", "SOLANA_RPC_URL", "IRYS_URL", "IRYS_GATEWAY"],
required: ["ARWEAVE_PRIVATE_KEY", "IRYS_URL", "IRYS_GATEWAY"],
properties: {
SOLANA_PPC_URL: {
type: "string",
},
SOLANA_KEYPAIR: {
ARWEAVE_PRIVATE_KEY: {
type: "string",
},
IRYS_URL: {
Expand Down Expand Up @@ -112,8 +110,7 @@ await fastify.register(fastifySwaggerUi, {
const uploader = irys(
fastify.config.IRYS_GATEWAY,
fastify.config.IRYS_URL,
fastify.config.SOLANA_RPC_URL,
fastify.config.SOLANA_KEYPAIR
fastify.config.ARWEAVE_PRIVATE_KEY
);

// Health check endpoint
Expand Down Expand Up @@ -146,26 +143,27 @@ fastify.post(
reply.status(400).send(request.validationError);
return;
}

let upload_status = "FAILED";
const start = Date.now();
let contentType;
let buffer;

try {
// Check if request is multipart
if (request.isMultipart()) {
// Get file from request and convert to buffer
const data = await request.file();
const buffer = await data.toBuffer();
const contentType = data.mimetype;

const results = await uploader.upload(buffer, contentType);

reply.send(results);
buffer = await data.toBuffer();
contentType = data.mimetype;
} else {
const buffer = Buffer.from(JSON.stringify(request.body));
buffer = Buffer.from(JSON.stringify(request.body));
contentType = "application/json";
}

const results = await uploader.upload(buffer, "application/json");
const results = await uploader.upload(buffer, contentType);

reply.send(results);
}
reply.send(results);
upload_status = "COMPLETED";
} catch (err) {
fastify.log.error(err);
Expand All @@ -187,7 +185,7 @@ fastify.post("/fund/:bytes", {}, async function handler(request, reply) {
} catch (err) {
fastify.log.error(err);
// Send upload error as response if any error occurs
reply.send(new UploadError());
reply.send(new FundError());
}
});

Expand Down

0 comments on commit 7fac954

Please sign in to comment.