From d88b82c8d53aec86d3cb1b7c2e5b9be033489565 Mon Sep 17 00:00:00 2001 From: Sai Kranthi Date: Tue, 12 Mar 2024 11:54:46 +0530 Subject: [PATCH 1/5] feat: add tx subsidization --- src/lib/arweaveHelper.ts | 63 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/src/lib/arweaveHelper.ts b/src/lib/arweaveHelper.ts index ce1418b..8235858 100644 --- a/src/lib/arweaveHelper.ts +++ b/src/lib/arweaveHelper.ts @@ -1,6 +1,6 @@ import { getWallet, initArweave } from './common'; import { Tag } from 'arweave/node/lib/transaction'; -import { ArweaveSigner, createData } from 'arbundles'; +import { ArweaveSigner, bundleAndSignData, createData } from 'arbundles'; import { arseedingUpload } from './arseeding'; const jwk = getWallet(); @@ -15,6 +15,25 @@ export function getActivePublicKey() { } export async function uploadRepo(zipBuffer: Buffer, tags: Tag[]) { + //Subsidized Upload + try { + const uploadedTx = await subsidizedUpload(zipBuffer, tags); + const serviceUsed = uploadedTx.bundled ? 'Turbo' : 'Arweave'; + + console.log( + `[ PL SUBSIDIZE ] Posted Tx to ${serviceUsed}: ${uploadedTx.data.repoTxId}` + ); + + return uploadedTx.data.repoTxId; + } catch (error) { + const userWantsToPay = process.env.HANDLE_SUBSIDY_ERROR === 'true'; + + if (!userWantsToPay) { + throw '[ PL SUBSIDIZE ] Failed to subsidize this transaction.'; + } + //continue + } + const isArSeedingStrategy = process.env.STRATEGY === 'ARSEEDING'; if (isArSeedingStrategy) { const arweaveTxId = await arseedingUpload(zipBuffer, tags); @@ -91,3 +110,45 @@ export async function turboUpload(zipBuffer: Buffer, tags: Tag[]) { return dataItem.id; } + +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 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()) as SubsidizedUploadJsonResponse; + + if (!upload || !upload.success) + throw new Error( + `[ turbo ] Posting repo with turbo failed. Error: ${res.status} - ${res.statusText}` + ); + + return upload; +} + +export type SubsidizedUploadJsonResponse = { + success: boolean; + bundled: boolean; + data: { repoTxId: string }; + error?: string; +}; From 796a09acdc8bb96912b3729e41d3feff5efd5574 Mon Sep 17 00:00:00 2001 From: Sai Kranthi Date: Tue, 12 Mar 2024 17:44:09 +0530 Subject: [PATCH 2/5] chore: update readme --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 3cd3675..db36d89 100644 --- a/README.md +++ b/README.md @@ -63,9 +63,16 @@ jobs: # Optional Environment variables for ArSeeding strategy STRATEGY: "ARSEEDING" ARSEEDING_TOKEN_SYMBOL: "AR" + # Use personal funds in case of transaction subsidization failure + HANDLE_SUBSIDY_ERROR: "true" ``` + +> [!NOTE] +> Currently all Sync transactions are subsidized by Protocol Land. In case of subsidizing service failure, Sync program will exit unless `HANDLE_SUBSIDY_ERROR` env is set which lets you pay for the transaction from your wallet and continue. + + > [!NOTE] > Only include `STRATEGY` and `ARSEEDING_TOKEN_SYMBOL` environment variables for using [ArSeeding](https://web3infra.dev/docs/arseeding/introduction/lightNode/) to sync your repositories to Protocol Land. > Supported Arweave tokens for ArSeeding strategy are: `['XYZ', 'ARDRIVE', 'PIA', 'VRT', 'U', 'STAMP', 'AR']`. From ac63074a96ae127b5aa304eaf974d0a821b12e8e Mon Sep 17 00:00:00 2001 From: Sai Kranthi Date: Tue, 12 Mar 2024 20:10:15 +0530 Subject: [PATCH 3/5] chore: update readme optional content --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index db36d89..03a7c0e 100644 --- a/README.md +++ b/README.md @@ -112,10 +112,17 @@ Follow `1.` and `2.` from the previous section to set up a GitHub Secret. STRATEGY='ARSEEDING' ARSEEDING_TOKEN_SYMBOL='AR' + + # Use personal funds in case of transaction subsidization failure + + HANDLE_SUBSIDY_ERROR: "true" ``` - Replace `'YOUR_WALLET_JWK_HERE'` with your Arweave wallet's JWK (JSON Web Key). + Replace `'YOUR_WALLET_JWK_HERE'` with your Arweave wallet's JWK (JSON Web Key). + + Currently Sync transaction costs are subsidized by Protocol Land and in case of subsidization failure, Sync program will exit unless `HANDLE_SUBSIDY_ERROR` env is set which lets you pay for the transaction from your wallet and continue. If the compressed size of your repository exceeds 100kb, ensure your wallet has enough $AR to cover the transaction fees. + These environment variables (`WALLET`, `REPO_TITLE`, and `REPO_DESCRIPTION`) are crucial for setting up your repository and providing a meaningful description. `STRATEGY` and `ARSEEDING_TOKEN_SYMBOL` are needed for using [ArSeeding](https://web3infra.dev/docs/arseeding/introduction/lightNode/) to sync repositories to Protocol Land. Supported Arweave tokens for ArSeeding strategy are: `['XYZ', 'ARDRIVE', 'PIA', 'VRT', 'U', 'STAMP', 'AR']`. From c51dcd67153eb1437a4a46226a07d45a65cf3fb1 Mon Sep 17 00:00:00 2001 From: Sai Kranthi Date: Tue, 12 Mar 2024 20:13:22 +0530 Subject: [PATCH 4/5] chore: update readme fixing typo --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 03a7c0e..db9caf9 100644 --- a/README.md +++ b/README.md @@ -115,14 +115,14 @@ Follow `1.` and `2.` from the previous section to set up a GitHub Secret. # Use personal funds in case of transaction subsidization failure - HANDLE_SUBSIDY_ERROR: "true" + HANDLE_SUBSIDY_ERROR='true' ``` Replace `'YOUR_WALLET_JWK_HERE'` with your Arweave wallet's JWK (JSON Web Key). Currently Sync transaction costs are subsidized by Protocol Land and in case of subsidization failure, Sync program will exit unless `HANDLE_SUBSIDY_ERROR` env is set which lets you pay for the transaction from your wallet and continue. If the compressed size of your repository exceeds 100kb, ensure your wallet has enough $AR to cover the transaction fees. - + These environment variables (`WALLET`, `REPO_TITLE`, and `REPO_DESCRIPTION`) are crucial for setting up your repository and providing a meaningful description. `STRATEGY` and `ARSEEDING_TOKEN_SYMBOL` are needed for using [ArSeeding](https://web3infra.dev/docs/arseeding/introduction/lightNode/) to sync repositories to Protocol Land. Supported Arweave tokens for ArSeeding strategy are: `['XYZ', 'ARDRIVE', 'PIA', 'VRT', 'U', 'STAMP', 'AR']`. From eaf6750da34bd20780a196eb02b4fbf12c4bf1a8 Mon Sep 17 00:00:00 2001 From: Sai Kranthi Date: Wed, 13 Mar 2024 15:24:20 +0530 Subject: [PATCH 5/5] chore: add changeset --- .changeset/real-grapes-play.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/real-grapes-play.md diff --git a/.changeset/real-grapes-play.md b/.changeset/real-grapes-play.md new file mode 100644 index 0000000..34f93e0 --- /dev/null +++ b/.changeset/real-grapes-play.md @@ -0,0 +1,5 @@ +--- +'@protocol.land/sync': minor +--- + +Subsidize sync transaction cost