Skip to content

Commit

Permalink
Merge pull request #29 from labscommunity/kranthi/improve-subsidizing…
Browse files Browse the repository at this point in the history
…-handler

fix: use form data with subsidized uploads
  • Loading branch information
kranthicodes authored Apr 2, 2024
2 parents bd15b79 + 127996d commit 42efbab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-planes-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@protocol.land/sync': patch
---

Use form data to upload data with subsidizing service
31 changes: 19 additions & 12 deletions src/lib/arweaveHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,22 @@ async function arweaveUpload(zipBuffer: Buffer, tags: Tag[]) {
for (const tag of tags) tx.addTag(tag.name, tag.value);

await arweave.transactions.sign(tx, jwk);
const response = await arweave.transactions.post(tx);

console.log(`${response.status} - ${response.statusText}`);
let uploader = await arweave.transactions.getUploader(tx);

if (response.status !== 200) {
while (!uploader.isComplete) {
await uploader.uploadChunk();
console.log(
`${uploader.pctComplete}% complete, ${uploader.uploadedChunks}/${uploader.totalChunks}`
);
}

if (!uploader.isComplete) {
// throw error if arweave tx wasn't posted
throw `[ arweave ] Posting repo to arweave failed.\n\tError: '${
response.status
uploader.lastResponseStatus
}' - '${
response.statusText
uploader.lastResponseError
}'\n\tCheck if you have plenty $AR to upload ~${Math.ceil(
dataSize / 1024
)} KB of data.`;
Expand Down Expand Up @@ -114,7 +120,7 @@ export async function turboUpload(zipBuffer: Buffer, tags: Tag[]) {
export async function subsidizedUpload(zipBuffer: Buffer, tags: Tag[]) {
if (!jwk) throw '[ turbo ] No jwk wallet supplied';

const node = 'https://subsidize.saikranthi.dev/api/v1/postrepo';
const node = 'https://subsidize.saikranthi.dev/api/v1/postrepobuffer';
const uint8ArrayZip = new Uint8Array(zipBuffer);
const signer = new ArweaveSigner(jwk);
const address = await getAddress();
Expand All @@ -123,18 +129,19 @@ export async function subsidizedUpload(zipBuffer: Buffer, tags: Tag[]) {
await dataItem.sign(signer);

const bundle = await bundleAndSignData([dataItem], signer);
const bundleBuffer = bundle.getRaw();

const formData = new FormData();
formData.append('txBundle', new Blob([bundleBuffer]));
formData.append('platform', 'CLI');
formData.append('owner', address);

const res = await fetch(`${node}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify({
txBundle: bundle.getRaw(),
platform: 'CLI',
owner: address,
}),
body: formData,
});
const upload = (await res.json()) as SubsidizedUploadJsonResponse;

Expand Down

0 comments on commit 42efbab

Please sign in to comment.