Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 10/18 #12

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading