Skip to content

Commit

Permalink
feat(price commands): init CLI price command PE-6728
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Sep 12, 2024
1 parent 23bd999 commit d737b8e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { version } from '../version.js';
import {
cryptoFund,
getBalance,
getPrice,
topUp,
uploadFile,
uploadFolder,
Expand Down Expand Up @@ -81,6 +82,17 @@ applyOptions(
await runCommand(command, uploadFile);
});

applyOptions(
program
.command('price')
.description(
'Get the current Credits estimate for byte, crypto, or fiat value',
),
[optionMap.value, optionMap.type],
).action(async (_commandOptions, command: Command) => {
await runCommand(command, getPrice);
});

if (
process.argv[1].includes('bin/turbo') || // Running from global .bin
process.argv[1].includes('cli/cli') // Running from source
Expand Down
47 changes: 47 additions & 0 deletions src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import {
currencyMap,
fiatCurrencyTypes,
isCurrency,
isTokenType,
tokenToBaseMap,
} from '../node/index.js';
import { sleep } from '../utils/common.js';
import { version } from '../version.js';
import {
AddressOptions,
CryptoFundOptions,
PriceOptions,
TopUpOptions,
UploadFileOptions,
UploadFolderOptions,
Expand Down Expand Up @@ -259,3 +261,48 @@ export async function uploadFile(options: UploadFileOptions): Promise<void> {

console.log('Uploaded file:', JSON.stringify(result, null, 2));
}

export async function getPrice(options: PriceOptions) {
const value = options.value;
console.log('value', value);
if (value === undefined || !Number.isInteger(+value) || +value <= 0) {
throw new Error('Must provide a positive number --value to get price');
}

const type = options.type ?? 'bytes';

const winc = await (async () => {
if (isTokenType(type)) {
const turbo = TurboFactory.unauthenticated({
...configFromOptions(options),
token: type,
});
return (
await turbo.getWincForToken({
tokenAmount: tokenToBaseMap[type](value),
})
).winc;
}

const turbo = TurboFactory.unauthenticated(configFromOptions(options));
if (type === 'bytes') {
return (await turbo.getUploadCosts({ bytes: [+value] }))[0].winc;
}

if (isCurrency(type)) {
return (
await turbo.getWincForFiat({
amount: currencyMap[type](+value),
})
).winc;
}

throw new Error('Invalid price type!');
})();

console.log(
`Current price estimate for ${value} ${type} is ~${(
+winc / 1_000_000_000_000
).toFixed(12)} Credits`,
);
}
6 changes: 6 additions & 0 deletions src/cli/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export const optionMap = {
description: 'Fiat currency type to use for the action',
default: 'usd',
},
type: {
alias: '--type <priceType>',
description:
'Price type for the action. Can be a fiat currency or crypto token or bytes',
default: 'bytes',
},
txId: {
alias: '-i, --tx-id <txId>',
description: 'Transaction ID or hash to use for action',
Expand Down
5 changes: 5 additions & 0 deletions src/cli/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export type UploadFileOptions = WalletOptions & {
filePath: string | undefined;
};

export type PriceOptions = GlobalOptions & {
value: string | undefined;
type: string | undefined;
};

export type CryptoFundOptions = WalletOptions & {
value: string | undefined;
txId: string | undefined;
Expand Down

0 comments on commit d737b8e

Please sign in to comment.