-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: fund from exchange status and events #4822
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ecf8fd2
feat: add routing on status updates. Update events
tomiir 1ba2418
Merge branch 'main' into feat/buy-status-fund-from-exchange
tomiir 4ed9964
chore: subscribe to missing state fields
tomiir c386fcb
Merge branch 'main' of github.com:reown-com/appkit into feat/buy-stat…
tomiir ecd1c78
Merge branch 'feat/buy-status-fund-from-exchange' of github.com:reown…
tomiir 0153c07
chore: amend after deposit flow
tomiir 1a4c0ea
chore: fix usage of waituntilcomplete
tomiir 14f51e4
chore: add test
tomiir 1353a40
chore: add query params to pay requests
tomiir 6f5ac52
chore: add extra check before calling handlePaymentInProgress
0xmkh ebbb29b
fix: tests
0xmkh 6ee9e7f
Merge branch 'main' into feat/buy-status-fund-from-exchange
0xmkh 0fe2e1d
fix: tests
0xmkh 852dfe5
chore: remove .catch()
0xmkh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,13 @@ import { type CaipNetworkId } from '@reown/appkit-common' | |
|
|
||
| import { getActiveNetworkTokenAddress } from '../utils/ChainControllerUtil.js' | ||
| import { CoreHelperUtil } from '../utils/CoreHelperUtil.js' | ||
| import { formatCaip19Asset, getExchanges, getPayUrl } from '../utils/ExchangeUtil.js' | ||
| import { | ||
| type GetBuyStatusResult, | ||
| formatCaip19Asset, | ||
| getBuyStatus, | ||
| getExchanges, | ||
| getPayUrl | ||
| } from '../utils/ExchangeUtil.js' | ||
| import type { Exchange, PayUrlParams } from '../utils/ExchangeUtil.js' | ||
| import { AccountController } from './AccountController.js' | ||
| import { BlockchainApiController } from './BlockchainApiController.js' | ||
|
|
@@ -31,7 +37,9 @@ const DEFAULT_STATE: ExchangeControllerState = { | |
| error: null, | ||
| exchanges: [], | ||
| isLoading: false, | ||
| currentPayment: undefined | ||
| currentPayment: undefined, | ||
| isPaymentInProgress: false, | ||
| paymentId: '' | ||
| } | ||
|
|
||
| // -- Types --------------------------------------------- // | ||
|
|
@@ -66,31 +74,15 @@ export interface ExchangeControllerState { | |
| exchanges: Exchange[] | ||
| currentPayment?: CurrentPayment | ||
| paymentAsset: PaymentAsset | ||
| isPaymentInProgress: boolean | ||
| paymentId: string | ||
| } | ||
|
|
||
| type StateKey = keyof ExchangeControllerState | ||
| type PaymentType = 'wallet' | 'exchange' | ||
|
|
||
| // -- State --------------------------------------------- // | ||
| const state = proxy<ExchangeControllerState>({ | ||
| paymentAsset: { | ||
| network: 'eip155:1', | ||
| asset: 'native', | ||
| metadata: { | ||
| name: 'Ethereum', | ||
| symbol: 'ETH', | ||
| decimals: 18 | ||
| } | ||
| }, | ||
| amount: 0, | ||
| tokenAmount: 0, | ||
| tokenPrice: null, | ||
| priceLoading: false, | ||
| error: null, | ||
| exchanges: [], | ||
| isLoading: false, | ||
| currentPayment: undefined | ||
| }) | ||
| const state = proxy<ExchangeControllerState>(DEFAULT_STATE) | ||
|
|
||
| // -- Controller ---------------------------------------- // | ||
| export const ExchangeController = { | ||
|
|
@@ -188,6 +180,7 @@ export const ExchangeController = { | |
| type: 'exchange', | ||
| exchangeId | ||
| }, | ||
| source: 'fund-from-exchange', | ||
| headless: false | ||
| } | ||
| }) | ||
|
|
@@ -207,6 +200,9 @@ export const ExchangeController = { | |
| throw new Error('No account connected') | ||
| } | ||
|
|
||
| state.isPaymentInProgress = true | ||
| state.paymentId = crypto.randomUUID() | ||
|
|
||
| state.currentPayment = { | ||
| type: 'exchange', | ||
| exchangeId | ||
|
|
@@ -233,5 +229,101 @@ export const ExchangeController = { | |
| state.error = 'Unable to initiate payment' | ||
| SnackController.showError(state.error) | ||
| } | ||
| }, | ||
|
|
||
| async waitUntilComplete({ | ||
| exchangeId, | ||
| sessionId, | ||
| paymentId, | ||
| retries = 20 | ||
| }: { | ||
| exchangeId: string | ||
| sessionId: string | ||
| paymentId: string | ||
| retries?: number | ||
| }): Promise<GetBuyStatusResult> { | ||
| const status = await this.getBuyStatus(exchangeId, sessionId, paymentId) | ||
| if (status.status === 'SUCCESS' || status.status === 'FAILED') { | ||
| return status | ||
| } | ||
|
|
||
| if (retries === 0) { | ||
| throw new Error('Unable to get deposit status') | ||
| } | ||
|
|
||
| // Wait 1 second before checking again | ||
| await new Promise(resolve => { | ||
| setTimeout(resolve, 5000) | ||
| }) | ||
|
Comment on lines
+254
to
+257
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like it's 5 seconds?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lmk if it should be 5 or 1 seconds |
||
|
|
||
| return this.waitUntilComplete({ | ||
| exchangeId, | ||
| sessionId, | ||
| paymentId, | ||
| retries: retries - 1 | ||
| }) | ||
| }, | ||
|
|
||
| async getBuyStatus(exchangeId: string, sessionId: string, paymentId: string) { | ||
| try { | ||
| if (!state.currentPayment) { | ||
| throw new Error('No current payment') | ||
| } | ||
|
|
||
| const status = await getBuyStatus({ sessionId, exchangeId }) | ||
| state.currentPayment.status = status.status | ||
| if (status.status === 'SUCCESS' || status.status === 'FAILED') { | ||
| state.currentPayment.result = status.txHash | ||
| state.isPaymentInProgress = false | ||
| EventsController.sendEvent({ | ||
| type: 'track', | ||
| event: status.status === 'SUCCESS' ? 'PAY_SUCCESS' : 'PAY_ERROR', | ||
| properties: { | ||
| source: 'fund-from-exchange', | ||
| paymentId, | ||
| configuration: { | ||
| network: state.paymentAsset.network, | ||
| asset: state.paymentAsset.asset, | ||
| recipient: AccountController.state.address || '', | ||
| amount: state.amount | ||
| }, | ||
| currentPayment: { | ||
| type: 'exchange', | ||
| exchangeId: state.currentPayment?.exchangeId, | ||
| sessionId: state.currentPayment?.sessionId, | ||
| result: status.txHash | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| return status | ||
| } catch (error) { | ||
| return { | ||
| status: 'UNKNOWN', | ||
| txHash: '' | ||
| } as GetBuyStatusResult | ||
| } | ||
| }, | ||
| reset() { | ||
| state.currentPayment = undefined | ||
| state.isPaymentInProgress = false | ||
| state.paymentId = '' | ||
| state.paymentAsset = { | ||
| network: 'eip155:1', | ||
| asset: 'native', | ||
| metadata: { | ||
| name: 'Ethereum', | ||
| symbol: 'ETH', | ||
| decimals: 0 | ||
| } | ||
| } | ||
| state.amount = 0 | ||
| state.tokenAmount = 0 | ||
| state.tokenPrice = null | ||
| state.priceLoading = false | ||
| state.error = null | ||
| state.exchanges = [] | ||
| state.isLoading = false | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is 20 retries too much?