Skip to content

Commit

Permalink
feat(token-price): init new price method PE-7171
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Nov 22, 2024
1 parent 516bb72 commit c4df2f2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/common/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
} from '../types.js';
import { TurboHTTPService } from './http.js';
import { TurboWinstonLogger } from './logger.js';
import { exponentMap, tokenToBaseMap } from './token/index.js';

export const developmentPaymentServiceURL = 'https://payment.ardrive.dev';
export const defaultPaymentServiceURL = 'https://payment.ardrive.io';
Expand Down Expand Up @@ -287,6 +288,33 @@ export class TurboUnauthenticatedPaymentService
}
return response;
}

public async getTokenPriceForBytes({
bytes,
}: {
bytes: number;
}): Promise<{ tokenPrice: string; bytes: number; token: TokenType }> {
const wincPriceForOneToken = (
await this.getWincForToken({
tokenAmount: tokenToBaseMap[this.token](1),
})
).winc;
const wincPriceForOneGiB = (
await this.getUploadCosts({
bytes: [2 ** 30],
})
)[0].winc;

const tokenPriceForOneGiB = new BigNumber(wincPriceForOneGiB).dividedBy(
wincPriceForOneToken,
);
const tokenPriceForBytes = tokenPriceForOneGiB
.dividedBy(2 ** 30)
.times(bytes)
.toFixed(exponentMap[this.token]);

return { bytes, tokenPrice: tokenPriceForBytes, token: this.token };
}
}
// NOTE: to avoid redundancy, we use inheritance here - but generally prefer composition over inheritance
export class TurboAuthenticatedPaymentService
Expand Down
9 changes: 9 additions & 0 deletions src/common/token/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export const defaultTokenMap: TokenFactory = {
pol: (config: TokenConfig) => new PolygonToken(config),
} as const;

export const exponentMap: Record<TokenType, number> = {
arweave: 12,
solana: 9,
ethereum: 18,
kyve: 6,
matic: 18,
pol: 18,
} as const;

export const tokenToBaseMap: Record<
TokenType,
(a: BigNumber.Value) => BigNumber.Value
Expand Down
12 changes: 12 additions & 0 deletions src/common/turbo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
TurboRevokeCreditsParams,
TurboSignedDataItemFactory,
TurboSubmitFundTxResponse,
TurboTokenPriceForBytesResponse,
TurboUnauthenticatedClientConfiguration,
TurboUnauthenticatedClientInterface,
TurboUnauthenticatedPaymentServiceInterface,
Expand Down Expand Up @@ -169,6 +170,17 @@ export class TurboUnauthenticatedClient
return this.paymentService.getWincForToken(params);
}

/**
* Determines the price in the instantiated token to upload one data item of a specific size in bytes, including all Turbo cost adjustments and fees.
*/
getTokenPriceForBytes({
bytes,
}: {
bytes: number;
}): Promise<TurboTokenPriceForBytesResponse> {
return this.paymentService.getTokenPriceForBytes({ bytes });
}

/**
* Uploads a signed data item to the Turbo Upload Service.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ export type TurboWincForTokenResponse = Omit<
equivalentWincTokenAmount: string;
};

export type TurboTokenPriceForBytesResponse = {
tokenPrice: string;
bytes: number;
token: TokenType;
};

export type TurboWincForFiatParams = {
amount: CurrencyMap;
nativeAddress?: NativeAddress;
Expand Down Expand Up @@ -580,6 +586,11 @@ export interface TurboUnauthenticatedPaymentServiceInterface {
getWincForToken(
params: TurboWincForTokenParams,
): Promise<TurboWincForTokenResponse>;
getTokenPriceForBytes({
bytes,
}: {
bytes: number;
}): Promise<TurboTokenPriceForBytesResponse>;
getUploadCosts({ bytes }: { bytes: number[] }): Promise<TurboPriceResponse[]>;
createCheckoutSession(
params: TurboCheckoutSessionParams,
Expand Down

0 comments on commit c4df2f2

Please sign in to comment.