Skip to content

Commit

Permalink
Merge branch 'main' into thlorenz/feat/upload
Browse files Browse the repository at this point in the history
* main:
  Refactor Storage Module (#117)
  Remove wallet adapter dependency (#116)
  • Loading branch information
thlorenz committed May 28, 2022
2 parents b3bb3e6 + e5ae16a commit aa4a05e
Show file tree
Hide file tree
Showing 41 changed files with 768 additions and 837 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ You may access the current storage driver using `metaplex.storage()` which will

```ts
class StorageDriver {
getPrice(...files: MetaplexFile[]): Promise<SolAmount>;
getPrice(...files: MetaplexFile[]): Promise<Amount>;
upload(file: MetaplexFile): Promise<string>;
uploadAll(files: MetaplexFile[]): Promise<string[]>;
uploadJson<T extends object>(json: T): Promise<string>;
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"@metaplex-foundation/mpl-candy-machine": "^4.2.0",
"@metaplex-foundation/mpl-token-metadata": "^2.1.2",
"@solana/spl-token": "^0.2.0",
"@solana/wallet-adapter-base": "^0.9.3",
"@solana/web3.js": "^1.37.0",
"abort-controller": "^3.0.0",
"bignumber.js": "^9.0.2",
Expand Down
16 changes: 0 additions & 16 deletions src/Metaplex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import {
Cluster,
resolveClusterFromConnection,
IdentityDriver,
StorageDriver,
RpcDriver,
ProgramDriver,
OperationDriver,
} from '@/types';
import { GuestIdentityDriver } from '@/plugins/guestIdentity';
import { BundlrStorageDriver } from '@/plugins/bundlrStorage';
import { CoreRpcDriver } from '@/plugins/coreRpcDriver';
import { CoreProgramDriver } from '@/plugins/coreProgramDriver';
import { CoreOperationDriver } from '@/plugins/coreOperationDriver';
Expand All @@ -30,9 +28,6 @@ export class Metaplex {
/** Encapsulates the identity of the users interacting with the SDK. */
protected identityDriver: IdentityDriver;

/** Encapsulates where assets should be uploaded. */
protected storageDriver: StorageDriver;

/** Encapsulates how to read and write on-chain. */
protected rpcDriver: RpcDriver;

Expand All @@ -46,7 +41,6 @@ export class Metaplex {
this.connection = connection;
this.cluster = options.cluster ?? resolveClusterFromConnection(connection);
this.identityDriver = new GuestIdentityDriver(this);
this.storageDriver = new BundlrStorageDriver(this);
this.rpcDriver = new CoreRpcDriver(this);
this.programDriver = new CoreProgramDriver(this);
this.operationDriver = new CoreOperationDriver(this);
Expand All @@ -73,16 +67,6 @@ export class Metaplex {
return this;
}

storage() {
return this.storageDriver;
}

setStorageDriver(storage: StorageDriver) {
this.storageDriver = storage;

return this;
}

rpc() {
return this.rpcDriver;
}
Expand Down
42 changes: 41 additions & 1 deletion src/errors/SdkError.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PublicKey } from '@solana/web3.js';
import { Cluster, Program } from '@/types';
import { Cluster, Program, Currency } from '@/types';
import {
MetaplexError,
MetaplexErrorInputWithoutSource,
Expand Down Expand Up @@ -29,6 +29,46 @@ export class OperationHandlerMissingError extends SdkError {
}
}

export class DriverNotProvidedError extends SdkError {
constructor(driver: string, cause?: Error) {
super({
cause,
key: 'driver_not_provided',
title: 'Driver Not Provided',
problem: `The SDK tried to access the driver [${driver}] but was not provided.`,
solution:
'Make sure the driver is registered by using the "setDriver(myDriver)" method.',
});
}
}

export class CurrencyMismatchError extends SdkError {
left: Currency;
right: Currency;
operation?: string;
constructor(
left: Currency,
right: Currency,
operation?: string,
cause?: Error
) {
const wrappedOperation = operation ? ` [${operation}]` : '';
super({
cause,
key: 'currency_mismatch',
title: 'Currency Mismatch',
problem:
`The SDK tried to execute an operation${wrappedOperation} on two different currencies: ` +
`${left.symbol} and ${right.symbol}.`,
solution:
'Provide both amounts in the same currency to perform this operation.',
});
this.left = left;
this.right = right;
this.operation = operation;
}
}

export class InvalidJsonVariableError extends SdkError {
constructor(cause?: Error) {
super({
Expand Down
20 changes: 9 additions & 11 deletions src/plugins/awsStorage/AwsStorageDriver.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import { Metaplex } from '@/Metaplex';
import { StorageDriver, MetaplexFile } from '@/types';
import { SolAmount } from '@/utils';
import { lamports, Amount } from '@/types';
import { MetaplexFile, StorageDriver } from '../storageModule';

export class AwsStorageDriver extends StorageDriver {
export class AwsStorageDriver implements StorageDriver {
protected client: S3Client;
protected bucketName: string;

constructor(metaplex: Metaplex, client: S3Client, bucketName: string) {
super(metaplex);
constructor(client: S3Client, bucketName: string) {
this.client = client;
this.bucketName = bucketName;
}

public async getPrice(..._files: MetaplexFile[]): Promise<SolAmount> {
return SolAmount.zero();
async getUploadPrice(_bytes: number): Promise<Amount> {
return lamports(0);
}

public async upload(file: MetaplexFile): Promise<string> {
async upload(file: MetaplexFile): Promise<string> {
const command = new PutObjectCommand({
Bucket: this.bucketName,
Key: file.uniqueName,
Body: file.toBuffer(),
Body: file.buffer,
});

try {
Expand All @@ -34,7 +32,7 @@ export class AwsStorageDriver extends StorageDriver {
}
}

protected async getUrl(key: string) {
async getUrl(key: string) {
const region = await this.client.config.region();
const encodedKey = encodeURIComponent(key);

Expand Down
4 changes: 1 addition & 3 deletions src/plugins/awsStorage/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export const awsStorage = (
bucketName: string
): MetaplexPlugin => ({
install(metaplex: Metaplex) {
metaplex.setStorageDriver(
new AwsStorageDriver(metaplex, client, bucketName)
);
metaplex.storage().setDriver(new AwsStorageDriver(client, bucketName));
},
});
Loading

0 comments on commit aa4a05e

Please sign in to comment.