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

Implement view-in-wallet #548

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/@magic-sdk/provider/src/modules/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,23 @@ export class NFTModule extends BaseModule {
}

/* Start an NFT Checkout flow with Paypal */
public checkout(options: NFTCheckoutRequest) {
public async checkout(options: NFTCheckoutRequest) {
const requestPayload = createJsonRpcRequestPayload(MagicPayloadMethod.NFTCheckout, [options]);
return this.request<NFTCheckoutResponse>(requestPayload);
const response = await this.request<NFTCheckoutResponse>(requestPayload);

if (response?.viewInWallet) {
const requestViewInWalletPayload = createJsonRpcRequestPayload(MagicPayloadMethod.ShowUI, [
{
deeplink: 'collectible-details',
params: {
contractAddress: options.contractAddress,
tokenId: options.tokenId,
},
},
]);
await this.request<NFTCheckoutResponse>(requestViewInWalletPayload);
}

return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import browserEnv from '@ikscodes/browser-env';
import { createMagicSDK } from '../../../factories';
import { isPromiEvent } from '../../../../src/util';

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

test('checkout method should return a PromiEvent', () => {
const magic = createMagicSDK();

magic.nft.request = jest.fn().mockReturnValue({
status: 'complete',
viewInWallet: true,
});

magic.nft.checkout({
contractId: '1cd4cfe8-b997-466e-8b0d-ff1177222ba4',
contractAddress: '0x375625833431b22623ce222e55b1cd15a459ba49',
tokenId: '1',
name: 'NFT Checkout Test',
imageUrl: 'https://nft-cdn.alchemy.com/matic-mumbai/382ceb42f28fb4df0d12897d5d433084',
quantity: 1,
});

const requestPayload = magic.nft.request.mock.calls[0][0];
console.log(requestPayload.params);
expect(requestPayload.method).toBe('magic_nft_checkout');
expect(requestPayload.params).toMatchObject([
{
contractId: '1cd4cfe8-b997-466e-8b0d-ff1177222ba4',
contractAddress: '0x375625833431b22623ce222e55b1cd15a459ba49',
tokenId: '1',
name: 'NFT Checkout Test',
imageUrl: 'https://nft-cdn.alchemy.com/matic-mumbai/382ceb42f28fb4df0d12897d5d433084',
quantity: 1,
},
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,3 @@ test('purchase method should return a PromiEvent', () => {
const magic = createMagicSDK();
expect(isPromiEvent(magic.nft.purchase())).toBeTruthy();
});

test('method should return a PromiEvent', () => {
const magic = createMagicSDK();
expect(isPromiEvent(magic.nft.checkout())).toBeTruthy();
});
10 changes: 6 additions & 4 deletions packages/@magic-sdk/types/src/core/json-rpc-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,21 @@ export interface NFTPurchaseResponse {
export interface NFTCheckoutRequest {
// given by magic / found in the developer dashboard in future
contractId: string;
contractAddress: string;
// in contract, if ERC1155… for ERC721, use token ID = 0
tokenId: string;
// Checkout UI compares against session wallet, if == then show “Magic Wallet”
walletAddress: string;
quantity: number;
imageUrl: string;
name: string;
imageUrl: string;
quantity: number;
// Checkout UI compares against session wallet, if == then show “Magic Wallet”
walletAddress?: string;
}

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

export interface NFTCheckoutResponse {
status: NFTCheckoutStatus;
viewInWallet?: boolean;
}

export enum Wallets {
Expand Down