Skip to content

Commit

Permalink
Merge pull request #26 from labscommunity/kranthi/subsidize
Browse files Browse the repository at this point in the history
feat: add tx subsidization
  • Loading branch information
kranthicodes authored Mar 13, 2024
2 parents d456f17 + eaf6750 commit b78056d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-grapes-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@protocol.land/sync': minor
---

Subsidize sync transaction cost
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']`.
Expand Down Expand Up @@ -105,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']`.
Expand Down
63 changes: 62 additions & 1 deletion src/lib/arweaveHelper.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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;
};

0 comments on commit b78056d

Please sign in to comment.