Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [sc-71135] Add purchase SDK method #455

Merged
merged 18 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/@magic-sdk/provider/src/core/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { createURL } from '../util/url';
import { Extension } from '../modules/base-extension';
import { isEmpty } from '../util/type-guards';
import { SDKEnvironment, sdkNameToEnvName } from './sdk-environment';
import { NFTModule } from '../modules/nft';

/**
* Checks if the given `ext` is compatible with the platform & version of Magic
Expand Down Expand Up @@ -120,6 +121,11 @@ export class SDKBase {
*/
public readonly wallet: WalletModule;

/**
* Contains methods for interacting with NFTs, including purchase.
*/
public readonly nft: NFTModule;

/**
* Contains a Web3-compliant provider. Pass this module to your Web3/Ethers
* instance for automatic compatibility with Ethereum methods.
Expand All @@ -144,6 +150,7 @@ export class SDKBase {
this.auth = new AuthModule(this);
this.user = new UserModule(this);
this.wallet = new WalletModule(this);
this.nft = new NFTModule(this);
this.rpcProvider = new RPCProviderModule(this) as any;

// Prepare extensions
Expand Down
11 changes: 11 additions & 0 deletions packages/@magic-sdk/provider/src/modules/nft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MagicPayloadMethod, NFTPurchaseRequest, NFTPurchaseResponse } from '@magic-sdk/types';
import { BaseModule } from './base-module';
import { createJsonRpcRequestPayload } from '../core/json-rpc';

export class NFTModule extends BaseModule {
/* Start an NFT Purchase flow */
public purchase(options: NFTPurchaseRequest) {
const requestPayload = createJsonRpcRequestPayload(MagicPayloadMethod.NFTPurchase, [options]);
return this.request<NFTPurchaseResponse>(requestPayload);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import browserEnv from '@ikscodes/browser-env';
import { createMagicSDK } from '../../../factories';
import { isPromiEvent } from '../../../../src/util';

beforeEach(() => {
browserEnv.restore();
});

test('Generate JSON RPC request payload with method `magic_nft_purchase`', async () => {
const magic = createMagicSDK();
magic.nft.request = jest.fn();

magic.nft.purchase();

const requestPayload = magic.nft.request.mock.calls[0][0];
expect(requestPayload.method).toBe('magic_nft_purchase');
expect(requestPayload.params).toEqual([]);
});

test('method should return a PromiEvent', () => {
const magic = createMagicSDK();
expect(isPromiEvent(magic.nft.purchase())).toBeTruthy();
});
33 changes: 33 additions & 0 deletions packages/@magic-sdk/types/src/core/json-rpc-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@ export interface RequestUserInfoScope {
};
}

export interface NFTPurchaseRequest {
nft: {
name: string;
price: number;
currencyCode: string;
contractAddress: string;
collection?: string;
imageUrl?: string;
};
identityPrefill: {
firstName: string;
lastName: string;
dateOfBirth: string; // YYYY-MM-DD
emailAddress: string;
phone: string;
address: {
street1: string;
street2: string;
city: string;
regionCode: string;
postalCode: string;
countryCode: string;
};
};
}

export type NFTPurchaseStatus = 'processed' | 'declined' | 'expired';

export interface NFTPurchaseResponse {
status: NFTPurchaseStatus;
}

// --- Payload methods

/**
Expand Down Expand Up @@ -81,6 +113,7 @@ export enum MagicPayloadMethod {
RequestAccounts = 'eth_requestAccounts',
GetInfo = 'mc_get_wallet_info',
ShowUI = 'mc_wallet',
NFTPurchase = 'magic_nft_purchase',
RequestUserInfoWithUI = 'mc_request_user_info',
Disconnect = 'mc_disconnect',
UpdatePhoneNumber = 'magic_auth_update_phone_number',
Expand Down
Loading