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

add web3modal extension #742

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4df5a5a
add web3modal extension
hcote Apr 22, 2024
0b7d9c7
remove unused vars
hcote Apr 22, 2024
a5864bb
rename
hcote Apr 22, 2024
55cc255
move override methods to thirdpartywallet module
hcote Apr 23, 2024
a746b06
remove provider dependency
hcote Apr 23, 2024
ffbb08d
typing updates
hcote Apr 24, 2024
a92e507
fix logout on disconnect
hcote Apr 24, 2024
a29c027
add returns to fns routing to tpw methods
hcote Apr 24, 2024
c3b302e
Merge branch 'master' into hcote-web3modal
hcote Apr 24, 2024
26604e3
move all logic to thirdpartywallet module
hcote Apr 25, 2024
42a4280
dont route nftCheckout to 3pw
hcote Apr 25, 2024
5a56d1a
update iframe.allow for google login
hcote Apr 30, 2024
3732179
fix capitalization
hcote May 4, 2024
56bb93c
add connectWIthUI options param
hcote May 7, 2024
f262429
make param optional
hcote May 12, 2024
b9b9caa
add event 1193 event listeners after connecting
hcote May 13, 2024
56fde71
update isConnected initialization
hcote May 14, 2024
3390b6f
remove old tests
hcote May 15, 2024
ad6d291
update tests
hcote May 18, 2024
dac3493
fix deepsource lint errors
hcote May 19, 2024
7953979
Merge branch 'master' into hcote-web3modal
hcote May 19, 2024
3a83159
update ls key names
hcote May 20, 2024
d328947
temp: remove reference to ThirdPartyWallets
hcote May 21, 2024
b44497f
update import
hcote May 21, 2024
53124ba
pluralize thirdpartywallets & update to web3modal canary version for …
hcote May 29, 2024
afa46ff
fix tests
hcote May 29, 2024
0386f43
implement nft.checkout with 3rd-party wallets (#744)
octave08 May 31, 2024
e8f9583
nft checkout update
hcote May 31, 2024
d862d23
add intermediary events for nft.checkout
hcote Jun 6, 2024
baa4f36
Merge branch 'master' into hcote-web3modal
hcote Jun 6, 2024
7ebccbf
Merge branch 'master' into hcote-web3modal
hcote Jun 7, 2024
218e104
yarn.lock
hcote Jun 7, 2024
c714eea
update web3modal package
hcote Jun 12, 2024
c05cd91
Merge branch 'master' into hcote-web3modal
hcote Jun 12, 2024
884053f
yarn.lock
hcote Jun 12, 2024
39d6e46
update web3modal package
hcote Jun 14, 2024
ff13a5f
yarn.lock
hcote Jun 14, 2024
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
Prev Previous commit
Next Next commit
add intermediary events for nft.checkout
  • Loading branch information
hcote committed Jun 6, 2024
commit d862d237706c86e0125bf3d4f33ebd8e1e9602f9
23 changes: 11 additions & 12 deletions packages/@magic-sdk/provider/src/modules/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import {
MagicPayloadMethod,
NFTCheckoutRequest,
NFTCheckoutResponse,
NFTCheckoutEvents,
NFTPurchaseRequest,
NFTPurchaseResponse,
NFTTransferRequest,
NFTTransferResponse,
NftCheckoutEventEmit,
NftCheckoutEventOnReceived,
NftCheckoutIntermediaryEvents,
} from '@magic-sdk/types';
import { BaseModule } from './base-module';
import { createJsonRpcRequestPayload } from '../core/json-rpc';
Expand All @@ -29,27 +29,26 @@ export class NFTModule extends BaseModule {
walletProvider: isThirdPartyWalletConnected ? 'web3modal' : 'magic',
},
]);
const req = this.request<NFTCheckoutResponse>(requestPayload);
const promiEvent = this.request<NFTCheckoutResponse, NFTCheckoutEvents>(requestPayload);

// Add intermediary event listener if user is purchasing with a third-party wallet
if (isThirdPartyWalletConnected) {
req.on(NftCheckoutEventOnReceived.Initiated as any, async (rawTransaction) => {
promiEvent.on(NftCheckoutIntermediaryEvents.Initiated, async (rawTransaction) => {
try {
// prompt third party wallet with transaction details
const hash = await this.request({
method: 'eth_sendTransaction',
params: [rawTransaction],
});

console.log('hash from sdk', hash);
this.createIntermediaryEvent(NftCheckoutEventEmit.Success, requestPayload.id as string)(hash);
this.createIntermediaryEvent(NftCheckoutIntermediaryEvents.Success, requestPayload.id as string)(hash);
} catch (error) {
console.error('error from sdk', error);
this.createIntermediaryEvent(NftCheckoutEventEmit.Failure, requestPayload.id as string)();
this.createIntermediaryEvent(NftCheckoutIntermediaryEvents.Failure, requestPayload.id as string)();
}
});
promiEvent.on(NftCheckoutIntermediaryEvents.Disconnect, () => {
this.sdk.thirdPartyWallets.resetThirdPartyWalletState();
promiEvent.emit('disconnect');
});
}
return req;
return promiEvent;
}

/* Start an NFT Transfer flow */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-disable global-require, @typescript-eslint/no-var-requires */

import browserEnv from '@ikscodes/browser-env';
import { MagicPayloadMethod } from '@magic-sdk/types';

import { isPromiEvent } from '../../../../src/util';
import { createMagicSDK, createMagicSDKTestMode } from '../../../factories';
import { createMagicSDK } from '../../../factories';

beforeEach(() => {
browserEnv.restore();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
import browserEnv from '@ikscodes/browser-env';
import { BaseModule } from '../../../../src/modules/base-module';
import { createMagicSDK, createViewController } from '../../../factories';
import { createMagicSDK } from '../../../factories';
import { createPromiEvent } from '../../../../src/util';
import { NftCheckoutEventHandler, NftCheckoutEventOnReceived } from '../../../../../types/src/modules/nft-types';

function createBaseModule(postStub: jest.Mock) {
const sdk = createMagicSDK();
const viewController = createViewController('');

viewController.post = postStub;
Object.defineProperty(sdk, 'overlay', {
get: () => viewController,
});

const baseModule: any = new BaseModule(sdk);

return { baseModule };
}
import { NftCheckoutIntermediaryEvents, NftCheckoutEventHandler } from '../../../../../types/src/modules/nft-types';

beforeEach(() => {
browserEnv.restore();
Expand Down Expand Up @@ -114,7 +99,7 @@ test('Third party wallet is connected', async () => {
isCryptoCheckoutEnabled: true,
});

mockPromiEvent.emit(NftCheckoutEventOnReceived.Initiated, '0x1234');
mockPromiEvent.emit(NftCheckoutIntermediaryEvents.Initiated, '0x1234');

expect(response.status).toBe('processed');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('third party wallet getInfo', () => {
});

describe('format web3modal getinfo response', () => {
it('should format the response correctly', async () => {
it('should format the response correctly', () => {
const magic = createMagicSDK();

magic.web3modal = {
Expand All @@ -51,7 +51,7 @@ describe('format web3modal getinfo response', () => {
expect(response).toEqual({
publicAddress: '0x1234567890',
email: null,
issuer: `$did:ethr:0x1234567890`,
issuer: '$did:ethr:0x1234567890',
phoneNumber: null,
isMfaEnabled: false,
recoveryFactors: [],
Expand Down
5 changes: 2 additions & 3 deletions packages/@magic-sdk/types/src/modules/intermediary-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
UpdateEmailEventEmit,
UpdateEmailEventOnReceived,
} from './auth-types';
import { NftCheckoutEventEmit, NftCheckoutEventOnReceived } from './nft-types';
import { NftCheckoutIntermediaryEvents } from './nft-types';

export type IntermediaryEvents =
// EmailOTP
Expand All @@ -32,5 +32,4 @@ export type IntermediaryEvents =
// Third Party Wallet Events
| `${ThirdPartyWalletEvents}`
// Nft Checkout Events
| `${NftCheckoutEventEmit}`
| `${NftCheckoutEventOnReceived}`;
| `${NftCheckoutIntermediaryEvents}`;
18 changes: 10 additions & 8 deletions packages/@magic-sdk/types/src/modules/nft-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export interface NFTCheckoutRequest {

export type NFTCheckoutResponse = NFTResponse;

export type NFTCheckoutEvents = {
disconnect: () => void;
'nft-checkout-initiated': (rawTransaction: string) => void;
};

export interface NFTTransferRequest {
tokenId: string;
contractAddress: string;
Expand All @@ -60,18 +65,15 @@ export interface NFTTransferRequest {

export type NFTTransferResponse = NFTResponse;

export enum NftCheckoutEventEmit {
export enum NftCheckoutIntermediaryEvents {
Success = 'nft-checkout-success',
Failure = 'nft-checkout-failure',
}

export enum NftCheckoutEventOnReceived {
Initiated = 'nft-checkout-initiated',
Disconnect = 'disconnect',
}

export type NftCheckoutEventHandler = {
[NftCheckoutEventOnReceived.Initiated]: (rawTransaction: string) => void;

[NftCheckoutEventEmit.Success]: (signedTransaction: string) => void;
[NftCheckoutEventEmit.Failure]: () => void;
[NftCheckoutIntermediaryEvents.Initiated]: (rawTransaction: string) => void;
[NftCheckoutIntermediaryEvents.Success]: (signedTransaction: string) => void;
[NftCheckoutIntermediaryEvents.Failure]: () => void;
};