Skip to content

Commit

Permalink
feat: Show push fees & ask user consent for push
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanpaudel93 committed Feb 7, 2024
1 parent 6dad261 commit 080ee02
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 28 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"arweave": "^1.14.4",
"jszip": "^3.10.1",
"node-machine-id": "^1.1.12",
"redstone-api": "^0.4.11",
"uuid": "^9.0.1",
"warp-contracts": "^1.4.19"
"warp-contracts": "^1.4.25"
}
}
70 changes: 69 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/lib/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const trackRepositoryUpdateEvent = async (
wallet: any,
data: Record<any, any>
) => {
return;
await trackAmplitudeAnalyticsEvent(
'Repository',
'Add files to repo',
Expand All @@ -83,6 +84,7 @@ export const trackRepositoryCloneEvent = async (
wallet: any,
data: Record<any, any>
) => {
return;
await trackAmplitudeAnalyticsEvent(
'Repository',
'Clone a repo',
Expand Down
11 changes: 1 addition & 10 deletions src/lib/arweaveHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Arweave from 'arweave';
import { ArweaveSigner, createData } from 'arbundles';
import { getWallet, log } from './common';
import { getWallet, initArweave, log } from './common';
import type { Tag } from '../types';
import { withAsync } from './withAsync';
import type { JsonWebKey } from 'crypto';
Expand Down Expand Up @@ -35,14 +34,6 @@ export async function uploadRepo(zipBuffer: Buffer, tags: Tag[]) {
}
}

function initArweave() {
return Arweave.init({
host: 'arweave.net',
port: 443,
protocol: 'https',
});
}

export async function arweaveDownload(txId: string) {
const { response, error } = await withAsync(() =>
fetch(`https://arweave.net/${txId}`)
Expand Down
14 changes: 12 additions & 2 deletions src/lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { execSync } from 'child_process';
import { accessSync, constants, readFileSync } from 'fs';
import type { JsonWebKey } from 'crypto';
import path from 'path';
import Arweave from 'arweave';

const ANSI_RESET = '\x1b[0m';
const ANSI_RED = '\x1b[31m';
Expand All @@ -14,16 +15,25 @@ const DIRTY_EXT = '.tmp';
export const PL_TMP_PATH = '.protocol.land';
export const GIT_CONFIG_KEYFILE = 'protocol.land.keyfile';
export const getWarpContractTxId = () =>
'w5ZU15Y2cLzZlu3jewauIlnzbKw-OAxbN9G5TbuuiDQ';
'xw8kD7s33ElIEioN96sPmLTJj9Rs2v4dZ67KWWKqojQ';
// get gitdir (usually '.git')
export const gitdir = process.env.GIT_DIR as string;

export function initArweave() {
return Arweave.init({
host: 'arweave.net',
port: 443,
protocol: 'https',
});
}

export const log = (message: any, options?: { color: 'red' | 'green' }) => {
if (!options) console.error(` [PL] ${message}`);
else {
const { color } = options;
console.error(
`${color === 'red' ? ANSI_RED : ANSI_GREEN
`${
color === 'red' ? ANSI_RED : ANSI_GREEN
} [PL] ${message}${ANSI_RESET}`
);
}
Expand Down
68 changes: 68 additions & 0 deletions src/lib/prices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import redstone from 'redstone-api';
import { initArweave } from './common';

interface CoinGeckoPriceResult {
arweave: {
[key: string]: number;
};
[key: string]: {
[key: string]: number;
};
}

export async function getArPrice() {
try {
const data = (await (
await fetch(
`https://api.coingecko.com/api/v3/simple/price?ids=arweave&vs_currencies=usd`
)
).json()) as CoinGeckoPriceResult;

return data.arweave.usd as number;
} catch (e) {
const response = await redstone.getPrice('AR');

if (!response.value) {
return 0;
}

return (response.source as any).coingecko as number;
}
}

export async function getWinstonPriceForBytes(bytes: number) {
try {
const response = await fetch(`https://arweave.net/price/${bytes}`);
const winston = await response.text();

return +winston;
} catch (error: any) {
throw new Error(error);
}
}

export function formatBytes(bytes: number): string {
if (bytes === 0) return '0 Bytes';

const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const k = 1024;
const i = Math.floor(Math.log(bytes) / Math.log(k));

return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}

export async function calculateEstimate(bytes: number) {
const formattedSize = formatBytes(bytes);

const costInWinston = await getWinstonPriceForBytes(bytes);
const costInAR = +initArweave().ar.winstonToAr(costInWinston.toString());

const costFor1ARInUSD = await getArPrice();
const costInUSD = costInAR * costFor1ARInUSD;

return {
formattedSize,
costInAR: costInAR.toPrecision(5),
costInUSD: costInUSD.toPrecision(5),
};
}
9 changes: 2 additions & 7 deletions src/lib/privateRepo.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import Arweave from 'arweave';
import crypto from 'crypto';
import type { PrivateState } from '../types';
import { getActivePublicKey } from './arweaveHelper';
import { getWallet } from './common';
import { getWallet, initArweave } from './common';

const arweave = new Arweave({
host: 'ar-io.net',
port: 443,
protocol: 'https',
});
const arweave = initArweave();

async function deriveAddress(publicKey: string) {
const pubKeyBuf = arweave.utils.b64UrlToBuffer(publicKey);
Expand Down
Loading

0 comments on commit 080ee02

Please sign in to comment.