From e18eefe339aab5d02743faee26b0aac0f624b678 Mon Sep 17 00:00:00 2001 From: Sven <38101365+svenvoskamp@users.noreply.github.com> Date: Tue, 17 Dec 2024 03:40:45 +0100 Subject: [PATCH] :fix swaps SA approval (#3461) Co-authored-by: tomiir --- .changeset/new-avocados-float.md | 22 +++++++++++++++++++ packages/adapters/ethers/src/client.ts | 9 +------- .../adapters/ethers/src/tests/client.test.ts | 6 ++--- .../ethers/src/utils/EthersMethods.ts | 2 +- packages/adapters/ethers5/src/client.ts | 9 +------- .../adapters/ethers5/src/tests/client.test.ts | 6 ++--- .../ethers5/src/utils/Ethers5Methods.ts | 2 +- packages/adapters/wagmi/src/client.ts | 6 ++--- .../adapters/wagmi/src/tests/client.test.ts | 6 ++--- .../src/adapters/ChainAdapterBlueprint.ts | 13 ++++------- packages/appkit/src/client.ts | 5 ++++- packages/common/src/contracts/swap.ts | 12 ++++++++++ packages/common/src/utils/ContractUtil.ts | 5 ++++- .../core/src/controllers/SendController.ts | 6 ++--- .../core/src/controllers/SwapController.ts | 3 ++- packages/core/src/utils/TypeUtil.ts | 6 ++--- 16 files changed, 70 insertions(+), 48 deletions(-) create mode 100644 .changeset/new-avocados-float.md create mode 100644 packages/common/src/contracts/swap.ts diff --git a/.changeset/new-avocados-float.md b/.changeset/new-avocados-float.md new file mode 100644 index 0000000000..22a07c137a --- /dev/null +++ b/.changeset/new-avocados-float.md @@ -0,0 +1,22 @@ +--- +'@reown/appkit-adapter-ethers5': patch +'@reown/appkit-adapter-ethers': patch +'@reown/appkit-adapter-solana': patch +'@reown/appkit-adapter-wagmi': patch +'@reown/appkit-utils': patch +'@reown/appkit': patch +'@reown/appkit-common': patch +'@reown/appkit-core': patch +'@reown/appkit-siwe': patch +'@reown/appkit-cdn': patch +'@reown/appkit-cli': patch +'@reown/appkit-experimental': patch +'@reown/appkit-polyfills': patch +'@reown/appkit-scaffold-ui': patch +'@reown/appkit-siwx': patch +'@reown/appkit-ui': patch +'@reown/appkit-wallet': patch +'@reown/appkit-wallet-button': patch +--- + +Fix an issue where approving with Smart Account didn't work diff --git a/packages/adapters/ethers/src/client.ts b/packages/adapters/ethers/src/client.ts index 1ecde2036f..7a485601c9 100644 --- a/packages/adapters/ethers/src/client.ts +++ b/packages/adapters/ethers/src/client.ts @@ -146,14 +146,7 @@ export class EthersAdapter extends AdapterBlueprint { } const result = await EthersMethods.writeContract( - { - abi: params.abi, - method: params.method, - fromAddress: params.caipAddress as `0x${string}`, - receiverAddress: params.receiverAddress as `0x${string}`, - tokenAmount: params.tokenAmount, - tokenAddress: params.tokenAddress as `0x${string}` - }, + params, params.provider as Provider, params.caipAddress, Number(params.caipNetwork?.id) diff --git a/packages/adapters/ethers/src/tests/client.test.ts b/packages/adapters/ethers/src/tests/client.test.ts index 284b320bcb..aa122a1543 100644 --- a/packages/adapters/ethers/src/tests/client.test.ts +++ b/packages/adapters/ethers/src/tests/client.test.ts @@ -182,11 +182,11 @@ describe('EthersAdapter', () => { method: 'transfer', caipAddress: 'eip155:1:0x123', fromAddress: '0x123', - receiverAddress: '0x456', - tokenAmount: BigInt(1000), + args: ['0x789', BigInt(1000)], tokenAddress: '0x789', provider: mockProvider, - caipNetwork: mockCaipNetworks[0] + caipNetwork: mockCaipNetworks[0], + chainNamespace: 'eip155' }) expect(result.hash).toBe(mockTxHash) diff --git a/packages/adapters/ethers/src/utils/EthersMethods.ts b/packages/adapters/ethers/src/utils/EthersMethods.ts index 559a480ac1..4db939b8de 100644 --- a/packages/adapters/ethers/src/utils/EthersMethods.ts +++ b/packages/adapters/ethers/src/utils/EthersMethods.ts @@ -112,7 +112,7 @@ export const EthersMethods = { } const method = contract[data.method] if (method) { - return await method(data.receiverAddress, data.tokenAmount) + return await method(...data.args) } throw new Error('Contract method is undefined') }, diff --git a/packages/adapters/ethers5/src/client.ts b/packages/adapters/ethers5/src/client.ts index 240e48074c..78173cca04 100644 --- a/packages/adapters/ethers5/src/client.ts +++ b/packages/adapters/ethers5/src/client.ts @@ -147,14 +147,7 @@ export class Ethers5Adapter extends AdapterBlueprint { } const result = await Ethers5Methods.writeContract( - { - abi: params.abi, - method: params.method, - fromAddress: params.caipAddress as `0x${string}`, - receiverAddress: params.receiverAddress as `0x${string}`, - tokenAmount: params.tokenAmount, - tokenAddress: params.tokenAddress as `0x${string}` - }, + params, params.provider as Provider, params.caipAddress, Number(params.caipNetwork?.id) diff --git a/packages/adapters/ethers5/src/tests/client.test.ts b/packages/adapters/ethers5/src/tests/client.test.ts index 9fee873dc4..dd88b7d8d4 100644 --- a/packages/adapters/ethers5/src/tests/client.test.ts +++ b/packages/adapters/ethers5/src/tests/client.test.ts @@ -174,11 +174,11 @@ describe('Ethers5Adapter', () => { method: 'transfer', caipAddress: 'eip155:1:0x123', fromAddress: '0x123', - receiverAddress: '0x456', - tokenAmount: BigInt(1000), + args: ['0x789', BigInt(1000)], tokenAddress: '0x789', provider: mockProvider, - caipNetwork: mockCaipNetworks[0] + caipNetwork: mockCaipNetworks[0], + chainNamespace: 'eip155' }) expect(result.hash).toBe(mockTxHash) diff --git a/packages/adapters/ethers5/src/utils/Ethers5Methods.ts b/packages/adapters/ethers5/src/utils/Ethers5Methods.ts index ee30bdbaa5..b7637af9c9 100644 --- a/packages/adapters/ethers5/src/utils/Ethers5Methods.ts +++ b/packages/adapters/ethers5/src/utils/Ethers5Methods.ts @@ -106,7 +106,7 @@ export const Ethers5Methods = { } const method = contract[data.method] if (method) { - return await method(data.receiverAddress, data.tokenAmount) + return await method(...data.args) } throw new Error('Contract method is undefined') }, diff --git a/packages/adapters/wagmi/src/client.ts b/packages/adapters/wagmi/src/client.ts index 94e840c4ba..fd2cbe43f9 100644 --- a/packages/adapters/wagmi/src/client.ts +++ b/packages/adapters/wagmi/src/client.ts @@ -291,11 +291,11 @@ export class WagmiAdapter extends AdapterBlueprint { const tx = await wagmiWriteContract(this.wagmiConfig, { chain: this.wagmiChains?.[chainId], chainId, - address: data.tokenAddress as Hex, - account: data.fromAddress as Hex, + address: data.tokenAddress, + account: data.fromAddress, abi: data.abi, functionName: data.method, - args: [data.receiverAddress, data.tokenAmount] + args: data.args }) return { hash: tx } diff --git a/packages/adapters/wagmi/src/tests/client.test.ts b/packages/adapters/wagmi/src/tests/client.test.ts index 2f164ec1a9..c1bacc5be2 100644 --- a/packages/adapters/wagmi/src/tests/client.test.ts +++ b/packages/adapters/wagmi/src/tests/client.test.ts @@ -189,10 +189,10 @@ describe('WagmiAdapter', () => { caipAddress: 'eip155:1:0x123', tokenAddress: '0x123', fromAddress: '0x456', - receiverAddress: '0x789', - tokenAmount: BigInt(1000), + args: ['0x789', BigInt(1000)], abi: [], - method: 'transfer' + method: 'transfer', + chainNamespace: 'eip155' }) expect(result.hash).toBe(mockTxHash) diff --git a/packages/appkit/src/adapters/ChainAdapterBlueprint.ts b/packages/appkit/src/adapters/ChainAdapterBlueprint.ts index 3a7c0f09e9..8a8f034fc4 100644 --- a/packages/appkit/src/adapters/ChainAdapterBlueprint.ts +++ b/packages/appkit/src/adapters/ChainAdapterBlueprint.ts @@ -14,7 +14,8 @@ import { type Connector as AppKitConnector, type AuthConnector, type Metadata, - type Tokens + type Tokens, + type WriteContractArgs } from '@reown/appkit-core' import type UniversalProvider from '@walletconnect/universal-provider' import type { W3mFrameProvider } from '@reown/appkit-wallet' @@ -433,17 +434,11 @@ export namespace AdapterBlueprint { gas: bigint } - export type WriteContractParams = { - receiverAddress: string - tokenAmount: bigint - tokenAddress: string - fromAddress: string - method: 'send' | 'transfer' | 'call' - // eslint-disable-next-line @typescript-eslint/no-explicit-any - abi: any + export type WriteContractParams = WriteContractArgs & { caipNetwork: CaipNetwork provider?: AppKitConnector['provider'] caipAddress: CaipAddress + // eslint-disable-next-line @typescript-eslint/no-explicit-any } export type WriteContractResult = { diff --git a/packages/appkit/src/client.ts b/packages/appkit/src/client.ts index aecc9c58c5..fc62a7a222 100644 --- a/packages/appkit/src/client.ts +++ b/packages/appkit/src/client.ts @@ -934,7 +934,10 @@ export class AppKit { if (args.chainNamespace === 'eip155') { const adapter = this.getAdapter(ChainController.state.activeChain as ChainNamespace) - const result = await adapter?.sendTransaction(args) + const provider = ProviderUtil.getProvider( + ChainController.state.activeChain as ChainNamespace + ) + const result = await adapter?.sendTransaction({ ...args, provider }) return result?.hash || '' } diff --git a/packages/common/src/contracts/swap.ts b/packages/common/src/contracts/swap.ts new file mode 100644 index 0000000000..da5c9d0211 --- /dev/null +++ b/packages/common/src/contracts/swap.ts @@ -0,0 +1,12 @@ +export const swapABI = [ + { + type: 'function', + name: 'approve', + stateMutability: 'nonpayable', + inputs: [ + { name: 'spender', type: 'address' }, + { name: 'amount', type: 'uint256' } + ], + outputs: [{ type: 'bool' }] + } +] as const diff --git a/packages/common/src/utils/ContractUtil.ts b/packages/common/src/utils/ContractUtil.ts index 7e21d59237..a65a679db6 100644 --- a/packages/common/src/utils/ContractUtil.ts +++ b/packages/common/src/utils/ContractUtil.ts @@ -1,4 +1,5 @@ import { erc20ABI } from '../contracts/erc20.js' +import { swapABI } from '../contracts/swap.js' import { usdtABI } from '../contracts/usdt.js' import { ConstantsUtil } from './ConstantsUtil.js' @@ -10,5 +11,7 @@ export const ContractUtil = { } return erc20ABI - } + }, + + getSwapAbi: () => swapABI } diff --git a/packages/core/src/controllers/SendController.ts b/packages/core/src/controllers/SendController.ts index f20a4e5e2c..d634e90eb1 100644 --- a/packages/core/src/controllers/SendController.ts +++ b/packages/core/src/controllers/SendController.ts @@ -234,10 +234,10 @@ export const SendController = { await ConnectionController.writeContract({ fromAddress: AccountController.state.address as `0x${string}`, tokenAddress, - receiverAddress: params.receiverAddress as `0x${string}`, - tokenAmount: amount ?? BigInt(0), + args: [params.receiverAddress as `0x${string}`, amount ?? BigInt(0)], method: 'transfer', - abi: ContractUtil.getERC20Abi(tokenAddress) + abi: ContractUtil.getERC20Abi(tokenAddress), + chainNamespace: 'eip155' }) SnackController.showSuccess('Transaction started') diff --git a/packages/core/src/controllers/SwapController.ts b/packages/core/src/controllers/SwapController.ts index 149233a616..4b6a1364c0 100644 --- a/packages/core/src/controllers/SwapController.ts +++ b/packages/core/src/controllers/SwapController.ts @@ -717,8 +717,9 @@ export const SwapController = { address: fromAddress as `0x${string}`, to: data.to as `0x${string}`, data: data.data as `0x${string}`, - value: BigInt(data.value), + gas: data.gas, gasPrice: BigInt(data.gasPrice), + value: data.value, chainNamespace: 'eip155' }) diff --git a/packages/core/src/utils/TypeUtil.ts b/packages/core/src/utils/TypeUtil.ts index 01d5de238c..91ef4341ae 100644 --- a/packages/core/src/utils/TypeUtil.ts +++ b/packages/core/src/utils/TypeUtil.ts @@ -910,13 +910,13 @@ export type EstimateGasTransactionArgs = } export interface WriteContractArgs { - receiverAddress: `0x${string}` - tokenAmount: bigint tokenAddress: `0x${string}` fromAddress: `0x${string}` - method: 'send' | 'transfer' | 'call' + method: 'send' | 'transfer' | 'call' | 'approve' // eslint-disable-next-line @typescript-eslint/no-explicit-any abi: any + args: unknown[] + chainNamespace: ChainNamespace } export interface NetworkControllerClient {