Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/soft-colts-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@reown/appkit-controllers': patch
'@reown/appkit-scaffold-ui': patch
---

Improves send flow events handling
112 changes: 42 additions & 70 deletions packages/controllers/src/controllers/SendController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
type ChainNamespace,
ConstantsUtil as CommonConstantsUtil,
ErrorUtil,
NumberUtil,
UserRejectedRequestError
NumberUtil
} from '@reown/appkit-common'
import { ContractUtil } from '@reown/appkit-common'
import { W3mFrameRpcConstants } from '@reown/appkit-wallet/utils'
Expand All @@ -22,6 +21,7 @@ import {
import { ConstantsUtil } from '../utils/ConstantsUtil.js'
import { CoreHelperUtil } from '../utils/CoreHelperUtil.js'
import { SwapApiUtil } from '../utils/SwapApiUtil.js'
import type { Event } from '../utils/TypeUtil.js'
import { withErrorBoundary } from '../utils/withErrorBoundary.js'
import { ChainController } from './ChainController.js'
import { ConnectionController } from './ConnectionController.js'
Expand Down Expand Up @@ -130,21 +130,58 @@ const controller = {
async sendToken() {
try {
SendController.setLoading(true)
EventsController.sendEvent({
type: 'track',
event: 'SEND_INITIATED',
properties: {
isSmartAccount:
getPreferredAccountType(ChainController.state.activeChain) ===
W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT,
token: SendController.state.token?.address || SendController.state.token?.symbol || '',
amount: SendController.state.sendTokenAmount ?? 0,
network: ChainController.state.activeCaipNetwork?.caipNetworkId || ''
}
})
switch (ChainController.state.activeCaipNetwork?.chainNamespace) {
case 'eip155':
await SendController.sendEvmToken()
break

return
case 'solana':
await SendController.sendSolanaToken()

return
break
default:
throw new Error('Unsupported chain')
}

EventsController.sendEvent({
type: 'track',
event: 'SEND_SUCCESS',
properties: {
isSmartAccount:
getPreferredAccountType(ChainController.state.activeChain) ===
W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT,
token: SendController.state.token?.symbol || '',
amount: SendController.state.sendTokenAmount ?? 0,
network: ChainController.state.activeCaipNetwork?.caipNetworkId || '',
hash: state.hash ?? ''
}
})
SendController.resetSend()
} catch (err) {
const event: Event = {
type: 'track',
event: 'SEND_ERROR',
properties: SendController.getSdkEventProperties(err)
}
if (ErrorUtil.isUserRejectedRequestError(err)) {
throw new UserRejectedRequestError(err)
EventsController.sendEvent({
...event,
event: 'SEND_REJECTED'
})
} else {
EventsController.sendEvent(event)
}

throw err
Expand All @@ -160,8 +197,6 @@ const controller = {
throw new Error('SendController:sendEvmToken - activeChainNamespace is required')
}

const activeAccountType = getPreferredAccountType(activeChainNamespace)

if (!SendController.state.sendTokenAmount || !SendController.state.receiverAddress) {
throw new Error('An amount and receiver address are required')
}
Expand All @@ -171,16 +206,6 @@ const controller = {
}

if (SendController.state.token?.address) {
EventsController.sendEvent({
type: 'track',
event: 'SEND_INITIATED',
properties: {
isSmartAccount: activeAccountType === W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT,
token: SendController.state.token.address,
amount: SendController.state.sendTokenAmount,
network: ChainController.state.activeCaipNetwork?.caipNetworkId || ''
}
})
const { hash } = await SendController.sendERC20Token({
receiverAddress: SendController.state.receiverAddress,
tokenAddress: SendController.state.token.address,
Expand All @@ -192,16 +217,6 @@ const controller = {
state.hash = hash
}
} else {
EventsController.sendEvent({
type: 'track',
event: 'SEND_INITIATED',
properties: {
isSmartAccount: activeAccountType === W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT,
token: SendController.state.token.symbol || '',
amount: SendController.state.sendTokenAmount,
network: ChainController.state.activeCaipNetwork?.caipNetworkId || ''
}
})
const { hash } = await SendController.sendNativeToken({
receiverAddress: SendController.state.receiverAddress,
sendTokenAmount: SendController.state.sendTokenAmount,
Expand Down Expand Up @@ -295,21 +310,7 @@ const controller = {
value: value ?? BigInt(0)
})

EventsController.sendEvent({
type: 'track',
event: 'SEND_SUCCESS',
properties: {
isSmartAccount:
getPreferredAccountType('eip155') === W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT,
token: SendController.state.token?.symbol || '',
amount: params.sendTokenAmount,
network: ChainController.state.activeCaipNetwork?.caipNetworkId || '',
hash: hash || ''
}
})

ConnectionController._getClient()?.updateBalance('eip155')
SendController.resetSend()

return { hash }
},
Expand Down Expand Up @@ -343,21 +344,6 @@ const controller = {
chainNamespace: CommonConstantsUtil.CHAIN.EVM
})

EventsController.sendEvent({
type: 'track',
event: 'SEND_SUCCESS',
properties: {
isSmartAccount:
getPreferredAccountType('eip155') === W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT,
token: SendController.state.token?.symbol || '',
amount: params.sendTokenAmount,
network: ChainController.state.activeCaipNetwork?.caipNetworkId || '',
hash: hash || ''
}
})

SendController.resetSend()

return { hash }
}

Expand Down Expand Up @@ -400,20 +386,6 @@ const controller = {
}

ConnectionController._getClient()?.updateBalance('solana')

EventsController.sendEvent({
type: 'track',
event: 'SEND_SUCCESS',
properties: {
isSmartAccount: false,
token: SendController.state.token?.symbol || '',
amount: SendController.state.sendTokenAmount,
network: ChainController.state.activeCaipNetwork?.caipNetworkId || '',
hash: hash || ''
}
})

SendController.resetSend()
},

resetSend() {
Expand Down
133 changes: 129 additions & 4 deletions packages/controllers/tests/controllers/SendController.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { type Balance } from '@reown/appkit-common'
import { type Balance, UserRejectedRequestError } from '@reown/appkit-common'

import {
type AccountState,
Expand Down Expand Up @@ -223,6 +223,7 @@ describe('SendController', () => {

describe('sendSolanaToken()', () => {
beforeEach(() => {
vi.restoreAllMocks()
vi.spyOn(RouterController, 'pushTransactionStack').mockImplementation(() => {})
vi.spyOn(RouterController, 'replace').mockImplementation(() => {})
vi.spyOn(ConnectionController, 'sendTransaction').mockResolvedValue(undefined)
Expand Down Expand Up @@ -253,7 +254,6 @@ describe('SendController', () => {
value: 0.1
})
expect(ConnectionController._getClient()?.updateBalance).toHaveBeenCalledWith('solana')
expect(SendController.resetSend).toHaveBeenCalled()
})

it('should call sendTransaction with tokenMint', async () => {
Expand Down Expand Up @@ -288,7 +288,6 @@ describe('SendController', () => {
value: 50
})
expect(ConnectionController._getClient()?.updateBalance).toHaveBeenCalledWith('solana')
expect(SendController.resetSend).toHaveBeenCalled()
})

it('should trigger SEND_SUCCESS event after successful transaction', async () => {
Expand All @@ -313,7 +312,7 @@ describe('SendController', () => {
SendController.setTokenAmount(50)
SendController.setReceiverAddress('9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM')

await SendController.sendSolanaToken()
await SendController.sendToken()

expect(EventsController.sendEvent).toHaveBeenCalledWith({
type: 'track',
Expand All @@ -340,4 +339,130 @@ describe('SendController', () => {
expect(SendController.state.hash).toEqual('0x123')
})
})

describe('sendToken events', () => {
let resetSend: ReturnType<typeof vi.spyOn>
beforeEach(() => {
vi.restoreAllMocks()
resetSend = vi.spyOn(SendController, 'resetSend')
mockChainControllerState({
activeCaipNetwork: extendedMainnet
})
vi.spyOn(EventsController, 'sendEvent').mockImplementation(() => {})
})

it('should fire SEND_INITIATED then SEND_SUCCESS on successful send', async () => {
// Arrange
SendController.setToken(token as SendControllerState['token'])
SendController.setTokenAmount(sendTokenAmount)
SendController.setReceiverAddress(receiverAddress)
vi.spyOn(SendController, 'sendEvmToken').mockResolvedValue()

// Act
await SendController.sendToken()

// Assert
expect(EventsController.sendEvent).toHaveBeenNthCalledWith(1, {
type: 'track',
event: 'SEND_INITIATED',
properties: {
isSmartAccount: false,
token: token.address,
amount: sendTokenAmount,
network: extendedMainnet.caipNetworkId
}
})
expect(EventsController.sendEvent).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
type: 'track',
event: 'SEND_SUCCESS',
properties: expect.objectContaining({
isSmartAccount: false,
token: token.symbol,
amount: sendTokenAmount,
network: extendedMainnet.caipNetworkId
})
})
)
expect(resetSend).toHaveBeenCalledTimes(1)
})

it('should fire SEND_INITIATED then SEND_REJECTED when user rejects', async () => {
// Arrange
SendController.setToken(token as SendControllerState['token'])
SendController.setTokenAmount(sendTokenAmount)
SendController.setReceiverAddress(receiverAddress)
vi.spyOn(SendController, 'sendEvmToken').mockRejectedValue(new UserRejectedRequestError({}))

// Act
await expect(SendController.sendToken()).rejects.toBeTruthy()

// Assert
expect(EventsController.sendEvent).toHaveBeenNthCalledWith(1, {
type: 'track',
event: 'SEND_INITIATED',
properties: {
isSmartAccount: false,
token: token.address,
amount: sendTokenAmount,
network: extendedMainnet.caipNetworkId
}
})
expect(EventsController.sendEvent).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
type: 'track',
event: 'SEND_REJECTED',
properties: expect.objectContaining({
isSmartAccount: false,
token: token.symbol,
amount: sendTokenAmount,
network: extendedMainnet.caipNetworkId
})
})
)
expect(EventsController.sendEvent).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ type: 'track', event: 'SEND_REJECTED' })
)
expect(EventsController.sendEvent).toHaveBeenCalledTimes(2)
})

it('should fire SEND_INITIATED -> SEND_ERROR when random error occurs', async () => {
// Arrange
SendController.setToken(token as SendControllerState['token'])
SendController.setTokenAmount(sendTokenAmount)
SendController.setReceiverAddress(receiverAddress)
vi.spyOn(SendController, 'sendEvmToken').mockRejectedValue(new Error('Random failure'))

// Act
await expect(SendController.sendToken()).rejects.toBeTruthy()

// Assert
expect(EventsController.sendEvent).toHaveBeenNthCalledWith(1, {
type: 'track',
event: 'SEND_INITIATED',
properties: {
isSmartAccount: false,
token: token.address,
amount: sendTokenAmount,
network: extendedMainnet.caipNetworkId
}
})
expect(EventsController.sendEvent).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
type: 'track',
event: 'SEND_ERROR',
properties: expect.objectContaining({
isSmartAccount: false,
token: token.symbol,
amount: sendTokenAmount,
network: extendedMainnet.caipNetworkId
})
})
)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ConstantsUtil as CommonConstantsUtil, ErrorUtil } from '@reown/appkit-c
import {
AppKitError,
ChainController,
EventsController,
RouterController,
SendController,
SnackController
Expand Down Expand Up @@ -185,12 +184,6 @@ export class W3mWalletSendPreviewView extends LitElement {
}
}

EventsController.sendEvent({
type: 'track',
event: isUserRejectedRequestError ? 'SEND_REJECTED' : 'SEND_ERROR',
properties: SendController.getSdkEventProperties(error)
})

SnackController.showError(errMessage)
}
}
Expand Down
Loading
Loading