Skip to content

Commit

Permalink
Revert "Revert "Revert "Feat/transak bump (#639)" (#643)" (#645)" (#648)
Browse files Browse the repository at this point in the history
This reverts commit 24aec7c.
  • Loading branch information
juanmahidalgo authored Nov 14, 2024
1 parent 3f76740 commit ae0a055
Show file tree
Hide file tree
Showing 10 changed files with 420 additions and 298 deletions.
399 changes: 337 additions & 62 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@dcl/schemas": "^14.0.0",
"@dcl/single-sign-on-client": "^0.1.0",
"@dcl/ui-env": "^1.5.0",
"@transak/transak-sdk": "^3.1.3",
"@transak/transak-sdk": "^1.0.31",
"@types/flat": "0.0.28",
"@types/segment-analytics": "^0.0.38",
"@well-known-components/fetch-component": "^2.0.1",
Expand Down
14 changes: 0 additions & 14 deletions src/lib/marketplaceApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AuthIdentity } from 'decentraland-crypto-fetch'
import { WertMessageWithTarget } from '../modules/gateway/types'
import { OrderResponse } from '../modules/gateway/transak/types'
import { BaseClient } from './BaseClient'

export class MarketplaceAPI extends BaseClient {
Expand All @@ -22,17 +21,4 @@ export class MarketplaceAPI extends BaseClient {
throw new Error((error as Error).message)
}
}
/**
* Given the order id, returns relevant data related to status changes (status & tx hash).
*
* @param orderId - Transak Order ID.
*/
async getOrder(
orderId: string,
identity: AuthIdentity
): Promise<OrderResponse> {
return await this.fetch<OrderResponse>(`/v1/transak/orders/${orderId}`, {
identity
})
}
}
85 changes: 22 additions & 63 deletions src/modules/gateway/sagas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import {
} from './types'
import { getPendingManaPurchase, getPendingPurchases } from './selectors'
import { OrderResponse, TransakOrderStatus } from './transak/types'
import { generateIdentityFailure } from '../identity'

jest.mock('@wert-io/widget-initializer')
jest.mock('../../lib/marketplaceApi')
Expand Down Expand Up @@ -789,65 +788,27 @@ describe('when handling the action signaling the load of the local storage into
.mockResolvedValue(mockTransakOrderResponse)
})

describe("and there is no identity and it's generate function failed", () => {
it('should put the action signaling the failure of the poll purchase status request', async () => {
const transakPurchase = {
...mockPurchase,
id: mockTransakOrderResponse.data.id,
gateway: NetworkGatewayType.TRANSAK
}

return expectSaga(gatewaySaga)
.provide([[select(getPendingPurchases), [transakPurchase]]])
.dispatch(load({}))
.dispatch(generateIdentityFailure('', ''))
.put(pollPurchaseStatusRequest(transakPurchase))

.not.put(
setPurchase({
...transakPurchase,
status: PurchaseStatus.COMPLETE,
txHash: mockTransakOrderResponse.data.transactionHash!,
failureReason: null
})
)
.put(pollPurchaseStatusFailure(NO_IDENTITY_ERROR))
.silentRun()
})
})

describe('and there is identity', () => {
beforeEach(() => {
jest
.spyOn(MarketplaceAPI.prototype, 'getOrder')
.mockResolvedValue(mockTransakOrderResponse)
})
it('should put the action signaling the start pof the purchase status polling request', async () => {
const transakPurchase = {
...mockPurchase,
id: mockTransakOrderResponse.data.id,
gateway: NetworkGatewayType.TRANSAK
}
it('should put the action signaling the start pof the purchase status polling request', async () => {
const transakPurchase = {
...mockPurchase,
id: mockTransakOrderResponse.data.id,
gateway: NetworkGatewayType.TRANSAK
}

return expectSaga(gatewaySaga)
.provide([
[select(getPendingPurchases), [transakPurchase]],
[call(getIdentityOrRedirect), {}]
])
.dispatch(load({}))
.dispatch(generateIdentityFailure('', ''))
.put(pollPurchaseStatusRequest(transakPurchase))
.put(
setPurchase({
...transakPurchase,
status: PurchaseStatus.COMPLETE,
txHash: mockTransakOrderResponse.data.transactionHash!,
failureReason: null
})
)
.put(pollPurchaseStatusSuccess())
.silentRun()
})
return expectSaga(gatewaySaga)
.provide([[select(getPendingPurchases), [transakPurchase]]])
.dispatch(load({}))
.put(pollPurchaseStatusRequest(transakPurchase))
.put(
setPurchase({
...transakPurchase,
status: PurchaseStatus.COMPLETE,
txHash: mockTransakOrderResponse.data.transactionHash!,
failureReason: null
})
)
.put(pollPurchaseStatusSuccess())
.silentRun()
})
})
})
Expand All @@ -864,13 +825,12 @@ describe('when handling the action signaling the load of the local storage into
describe('when it is possible to get the order from Transak API', () => {
beforeEach(() => {
jest
.spyOn(MarketplaceAPI.prototype, 'getOrder')
.spyOn(Transak.prototype, 'getOrder')
.mockResolvedValue(mockTransakOrderResponse)
})

it('should get the order, put the action signaling the set of updated purchase, and the action signaling the success of the poll purchase status request', async () => {
return expectSaga(gatewaySaga)
.provide([[call(getIdentityOrRedirect), {}]])
.put(
setPurchase({
...transakPurchase,
Expand All @@ -888,13 +848,12 @@ describe('when handling the action signaling the load of the local storage into
describe('when it is not possible to get the order from Transak API because the request fails', () => {
beforeEach(() => {
jest
.spyOn(MarketplaceAPI.prototype, 'getOrder')
.spyOn(Transak.prototype, 'getOrder')
.mockRejectedValue(new Error(error))
})

it('should get the order, put the action signaling the set of updated purchase, and the action signaling the success of the poll purchase status request', async () => {
return expectSaga(gatewaySaga)
.provide([[call(getIdentityOrRedirect), {}]])
.put(pollPurchaseStatusFailure(error))
.dispatch(pollPurchaseStatusRequest(transakPurchase))
.silentRun()
Expand Down
33 changes: 2 additions & 31 deletions src/modules/gateway/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import {
delay,
ForkEffect,
put,
race,
select,
take,
takeEvery,
takeLatest
} from 'redux-saga/effects'
Expand Down Expand Up @@ -67,11 +65,6 @@ import {
import { isManaPurchase, purchaseEventsChannel } from './utils'
import { Wallet } from '../wallet/types'
import { isErrorWithMessage } from '../../lib/error'
import {
GENERATE_IDENTITY_FAILURE,
GENERATE_IDENTITY_SUCCESS,
GenerateIdentitySuccessAction
} from '../identity'

const DEFAULT_POLLING_DELAY = 3000
const BUY_MANA_WITH_FIAT_FEEDBACK_MODAL_NAME = 'BuyManaWithFiatFeedbackModal'
Expand Down Expand Up @@ -329,35 +322,13 @@ export function createGatewaySaga(config: GatewaySagasConfig) {
throw new Error('Transak config not found')
}

let identity: AuthIdentity | null = yield call(getIdentityOrRedirect)

if (!identity) {
const { success } = (yield race({
success: take(GENERATE_IDENTITY_SUCCESS),
failure: take(GENERATE_IDENTITY_FAILURE)
})) as { success: GenerateIdentitySuccessAction }

if (success) {
identity = (yield call(getIdentityOrRedirect)) as AuthIdentity
} else {
throw new Error(NO_IDENTITY_ERROR)
}
}

const marketplaceAPI = new MarketplaceAPI(
transakConfig.marketplaceServerURL
)
const transak = new Transak(transakConfig, {}, identity)
const transak = new Transak(transakConfig)
let statusHasChanged = false

while (!statusHasChanged) {
const {
data: { status, transactionHash, errorMessage }
}: OrderResponse = yield call(
[marketplaceAPI, 'getOrder'],
id,
identity
)
}: OrderResponse = yield call([transak, transak.getOrder], id)
const newStatus: PurchaseStatus = yield call(
[transak, transak.getPurchaseStatus],
status
Expand Down
70 changes: 13 additions & 57 deletions src/modules/gateway/transak/Transak.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { select } from 'redux-saga/effects'
import { expectSaga } from 'redux-saga-test-plan'
import transakSDK from '@transak/transak-sdk'
import { ChainId } from '@dcl/schemas/dist/dapps/chain-id'
import { Network } from '@dcl/schemas/dist/dapps/network'
import { NetworkGatewayType } from 'decentraland-ui'
Expand All @@ -20,21 +21,6 @@ import { OrderData, TradeType, TransakOrderStatus } from './types'

jest.mock('../../../lib/eth')

let initMock
jest.mock('@transak/transak-sdk', () => {
const actualTransakSDK = jest.requireActual('@transak/transak-sdk')

return {
__esModule: true,
...actualTransakSDK,
Transak: jest.fn().mockImplementation(config => {
return {
init: initMock
}
})
}
})

const mockGetChainIdByNetwork = getChainIdByNetwork as jest.MockedFunction<
typeof getChainIdByNetwork
>
Expand All @@ -51,7 +37,6 @@ const mockConfig: GatewaySagasConfig = {
pollingDelay: 50
},
[NetworkGatewayType.TRANSAK]: {
marketplaceServerURL: 'https://marketplace-server.decentraland.zone',
apiBaseUrl: 'http://transak-base.url.xyz',
key: 'transak-key',
env: 'TEST',
Expand Down Expand Up @@ -100,8 +85,8 @@ const mockOrderDataWithNftAssetInfo = {
...mockOrderData.status,
isNFTOrder: true,
nftAssetInfo: {
collection: 'contractAddress',
tokenId: '123',
contractAddress: 'contractAddress',
tokenId: 'anId',
tradeType: TradeType.PRIMARY
}
}
Expand All @@ -123,9 +108,9 @@ const mockNftPurchase: NFTPurchase = {
...mockManaPurchase,
nft: {
contractAddress: 'contractAddress',
tokenId: '123',
itemId: null,
tradeType: TradeType.SECONDARY,
itemId: 'anId',
tokenId: undefined,
tradeType: TradeType.PRIMARY,
cryptoAmount: 10
}
}
Expand Down Expand Up @@ -178,55 +163,26 @@ describe('when interacting with Transak', () => {
})

describe('when purchasing an NFT', () => {
let originalHref
beforeEach(() => {
jest.clearAllMocks()
originalHref = window.location.href
Object.defineProperty(window, 'location', {
writable: true, // Allow href to be writable
value: {
href: originalHref
}
})
})
describe('when it belongs to the primary market', () => {
beforeEach(() => {
Object.defineProperty(window, 'location', {
value: {
href:
'https://decentraland.zone/contracts/contractAddress/tokens/123'
}
})
})

it('should put a new message in the channel signaling the set of the purchase with the nft info and the item id', () => {
transak.emitPurchaseEvent(
mockOrderDataWithNftAssetInfo.status,
Network.ETHEREUM
)

return expectSaga(gatewaySaga)
.put(setPurchase({ ...mockNftPurchase, amount: 1 }))
.silentRun()
})
})

describe('when it belongs to the primary market', () => {
beforeEach(() => {
Object.defineProperty(window, 'location', {
value: {
href:
'https://decentraland.zone/contracts/contractAddress/items/234'
}
})
})
it('should put a new message in the channel signaling the set of the purchase with the nft info and the item id', () => {
transak.emitPurchaseEvent(
{
...mockOrderDataWithNftAssetInfo.status,
nftAssetInfo: {
...mockOrderDataWithNftAssetInfo.status.nftAssetInfo,
tradeType: TradeType.PRIMARY
tradeType: TradeType.SECONDARY
}
},
Network.ETHEREUM
Expand All @@ -238,9 +194,9 @@ describe('when interacting with Transak', () => {
amount: 1,
nft: {
...mockNftPurchase.nft,
tokenId: null,
itemId: '234',
tradeType: TradeType.PRIMARY
tradeType: TradeType.SECONDARY,
itemId: undefined,
tokenId: 'anId'
}
})
)
Expand All @@ -250,14 +206,14 @@ describe('when interacting with Transak', () => {
})
})

describe('when opening the widget', () => {
describe('when opnening the widget', () => {
beforeEach(() => {
initMock = jest.fn()
jest.spyOn(transakSDK.prototype, 'init').mockImplementation(() => {})
})

it('should call the method init from the Transak SDK', () => {
transak.openWidget(mockAddress, Network.ETHEREUM)
expect(initMock).toHaveBeenCalled()
return expect(transakSDK.prototype.init).toHaveBeenCalled()
})
})
})
Loading

0 comments on commit ae0a055

Please sign in to comment.