Skip to content

Commit

Permalink
Update Bundlr price logic (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva authored Dec 15, 2022
1 parent c1506ea commit cad98ec
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-gifts-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/js': patch
---

Update Bundlr price logic
16 changes: 15 additions & 1 deletion packages/js/src/plugins/bundlrStorage/BundlrStorageDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ export type BundlrWalletAdapter = {
) => Promise<TransactionSignature>;
};

/// Size of Bundlr transaction header
const HEADER_SIZE = 2_000;

/// Minimum file size for cost calculation
const MINIMUM_SIZE = 80_000;

export class BundlrStorageDriver implements StorageDriver {
protected _metaplex: Metaplex;
protected _bundlr: WebBundlr | NodeBundlr | null = null;
Expand All @@ -93,10 +99,18 @@ export class BundlrStorageDriver implements StorageDriver {
const price = await bundlr.getPrice(bytes);

return bigNumberToAmount(
price.multipliedBy(this._options.priceMultiplier ?? 1.5)
price.multipliedBy(this._options.priceMultiplier ?? 1.1)
);
}

async getUploadPriceForFiles(files: MetaplexFile[]): Promise<Amount> {
const bytes: number = files.reduce((sum, file) => {
return sum + HEADER_SIZE + Math.max(MINIMUM_SIZE, file.buffer.byteLength);
}, 0);

return this.getUploadPrice(bytes);
}

async upload(file: MetaplexFile): Promise<string> {
const [uri] = await this.uploadAll([file]);

Expand Down
6 changes: 5 additions & 1 deletion packages/js/src/plugins/storageModule/StorageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export class StorageClient implements HasDriver<StorageDriver> {
}

getUploadPriceForFiles(files: MetaplexFile[]): Promise<Amount> {
return this.getUploadPriceForBytes(getBytesFromMetaplexFiles(...files));
const driver = this.driver();

return driver.getUploadPriceForFiles
? driver.getUploadPriceForFiles(files)
: this.getUploadPriceForBytes(getBytesFromMetaplexFiles(...files));
}

upload(file: MetaplexFile): Promise<string> {
Expand Down
1 change: 1 addition & 0 deletions packages/js/src/plugins/storageModule/StorageDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type StorageDriver = {
uri: string,
options?: StorageDownloadOptions
) => Promise<MetaplexFile>;
getUploadPriceForFiles?: (files: MetaplexFile[]) => Promise<Amount>;
};

export type StorageDownloadOptions = Omit<RequestInit, 'signal'> & {
Expand Down

0 comments on commit cad98ec

Please sign in to comment.