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

feat: Add tx subsidization #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/ten-apes-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@protocol.land/pages": minor
---

feat: Add tx subsidization
78 changes: 63 additions & 15 deletions lib/arweaveHelper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Arweave = require('arweave');
const {ArweaveSigner, createData} = require('arbundles');
const {ArweaveSigner, bundleAndSignData, createData} = require('arbundles');
const common = require('./common.js');

async function getAddress(wallet) {
Expand All @@ -18,11 +18,16 @@ function initArweave() {

async function uploadFile(file, tags) {
try {
const bundlrTxId = await bundlrUpload(file, tags);
return bundlrTxId;
} catch (error) {
const arweaveTxId = await arweaveUpload(file, tags);
return arweaveTxId;
const uploadedTx = await subsidizedUpload(file, tags);
return uploadedTx.data.repoTxId;
} catch (e) {
try {
const turboTxId = await turboUpload(file, tags);
return turboTxId;
} catch (error) {
const arweaveTxId = await arweaveUpload(file, tags);
return arweaveTxId;
}
}
}

Expand All @@ -40,14 +45,18 @@ async function arweaveUpload(file, tags) {
}

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

if (response.status !== 200) {
const uploader = await arweave.transactions.getUploader(tx);
while (!uploader.isComplete) {
await uploader.uploadChunk();
}

if (uploader.lastResponseStatus !== 200) {
// throw error if arweave tx wasn't posted
throw `[ arweave ] Posting file to arweave failed.\n\tError: '${
response.status
throw `[ arweave ] Uploading file to arweave failed.\n\tError: '${
uploader.lastResponseStatus
}' - '${
response.statusText
uploader.lastResponseError
}'\n\tCheck if you have plenty $AR to upload ~${Math.ceil(
dataSize / 1024
)} KB of data.`;
Expand All @@ -56,13 +65,13 @@ async function arweaveUpload(file, tags) {
return tx.id;
}

async function bundlrUpload(file, tags) {
async function turboUpload(file, tags) {
const jwk = common.getWallet();
if (!jwk) {
throw '[ bundlr ] No jwk wallet supplied';
throw '[ turbo ] No jwk wallet supplied';
}

const node = 'https://node2.bundlr.network';
const node = 'https://turbo.ardrive.io';
const uint8ArrayFile = new Uint8Array(file);
const signer = new ArweaveSigner(jwk);

Expand All @@ -80,12 +89,51 @@ async function bundlrUpload(file, tags) {

if (res.status >= 400) {
throw new Error(
`[ bundlr ] Posting file with bundlr failed. Error: ${res.status} - ${res.statusText}`
`[ turbo ] Uploading file with turbo failed. Error: ${res.status} - ${res.statusText}`
);
}

return dataItem.id;
}

async function subsidizedUpload(zipBuffer, tags) {
const jwk = common.getWallet();
if (!jwk) {
throw 'No jwk wallet supplied';
}

const node = 'https://subsidize.saikranthi.dev/api/v1/postrepo';
const uint8ArrayZip = new Uint8Array(zipBuffer);
const signer = new ArweaveSigner(jwk);
const address = await getAddress();

const dataItem = createData(uint8ArrayZip, signer, {tags});
await dataItem.sign(signer);

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

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,
}),
});
const upload = await res.json();

if (!upload || !upload.success) {
throw new Error(
`[ PL SUBSIDIZE ] Uploading failed. Error: ${res.status} - ${res.statusText}`
);
}

return upload;
}

exports.getAddress = getAddress;
exports.uploadFile = uploadFile;