Skip to content

Commit

Permalink
feat(token-price): init token-price CLI command PE-7171
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Nov 22, 2024
1 parent 3586f57 commit 5325173
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
import { listShares } from './commands/listShares.js';
import { revokeCredits } from './commands/revokeCredits.js';
import { shareCredits } from './commands/shareCredits.js';
import { tokenPrice } from './commands/tokenPrice.js';
import {
globalOptions,
listSharesOptions,
Expand Down Expand Up @@ -99,6 +100,15 @@ applyOptions(
await runCommand(command, price);
});

applyOptions(
program
.command('token-price')
.description('Get the current token price for provided byte value'),
[optionMap.value],
).action(async (_commandOptions, command: Command) => {
await runCommand(command, tokenPrice);
});

applyOptions(
program
.command('share-credits')
Expand Down
51 changes: 51 additions & 0 deletions src/cli/commands/tokenPrice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { isTokenType } from '../../common/index.js';
import { TurboFactory } from '../../node/factory.js';
import { tokenTypes } from '../../types.js';
import { TokenPriceOptions } from '../types.js';
import { configFromOptions } from '../utils.js';

export async function tokenPrice(options: TokenPriceOptions) {
const value = options.value !== undefined ? +options.value : undefined;
if (
value === undefined ||
value <= 0 ||
isNaN(value) ||
!Number.isInteger(value)
) {
throw new Error(
'Must provide a positive number for byte to get price.\nFor example, to get the SOL price for 100 MiB use the following:\nturbo token-price --token solana --value 1048576000',
);
}
const token = options.token;

if (!isTokenType(token)) {
throw new Error(
`Invalid token type ${token}. Must be one of ${tokenTypes.join(', ')}`,
);
}

const turbo = TurboFactory.unauthenticated(configFromOptions(options));
const { tokenPrice } = await turbo.getTokenPriceForBytes({ bytes: +value });

const output = {
tokenPrice,
bytes: value,
message: `The current price estimate for ${value} bytes is ${tokenPrice} ${token}`,
};
console.log(JSON.stringify(output, null, 2));
}
5 changes: 4 additions & 1 deletion src/cli/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ export type UploadFileOptions = UploadOptions & {
filePath: string | undefined;
};

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

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

Expand Down

0 comments on commit 5325173

Please sign in to comment.