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

:fix swaps SA approval #3461

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions .changeset/new-avocados-float.md
Original file line number Diff line number Diff line change
@@ -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
9 changes: 1 addition & 8 deletions packages/adapters/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,7 @@
}

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)
Expand Down Expand Up @@ -263,7 +256,7 @@
connectors.forEach(connector => {
const key = connector === 'coinbase' ? 'coinbaseWalletSDK' : connector

const injectedConnector = connector === ConstantsUtil.INJECTED_CONNECTOR_ID

Check warning on line 259 in packages/adapters/ethers/src/client.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Variable name `injectedConnector` must have one of the following prefixes: is, has, can, should, will, did

if (this.namespace) {
this.addConnector({
Expand Down
6 changes: 3 additions & 3 deletions packages/adapters/ethers/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/ethers/src/utils/EthersMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
},
Expand Down
9 changes: 1 addition & 8 deletions packages/adapters/ethers5/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,7 @@
}

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)
Expand Down Expand Up @@ -264,7 +257,7 @@
connectors.forEach(connector => {
const key = connector === 'coinbase' ? 'coinbaseWalletSDK' : connector

const injectedConnector = connector === ConstantsUtil.INJECTED_CONNECTOR_ID

Check warning on line 260 in packages/adapters/ethers5/src/client.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Variable name `injectedConnector` must have one of the following prefixes: is, has, can, should, will, did

if (this.namespace) {
this.addConnector({
Expand Down
6 changes: 3 additions & 3 deletions packages/adapters/ethers5/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/ethers5/src/utils/Ethers5Methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
},
Expand Down
6 changes: 3 additions & 3 deletions packages/adapters/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
customConnectors.push(injected({ shimDisconnect: true }))
}

const emailEnabled =

Check warning on line 223 in packages/adapters/wagmi/src/client.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Variable name `emailEnabled` must have one of the following prefixes: is, has, can, should, will, did
options.features?.email === undefined
? CoreConstantsUtil.DEFAULT_FEATURES.email
: options.features?.email
Expand Down Expand Up @@ -291,11 +291,11 @@
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 }
Expand Down Expand Up @@ -377,7 +377,7 @@
filteredConnectors.forEach(connector => {
const shouldSkip = ConstantsUtil.AUTH_CONNECTOR_ID === connector.id

const injectedConnector = connector.id === ConstantsUtil.INJECTED_CONNECTOR_ID

Check warning on line 380 in packages/adapters/wagmi/src/client.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Variable name `injectedConnector` must have one of the following prefixes: is, has, can, should, will, did

if (!shouldSkip && this.namespace) {
this.addConnector({
Expand Down
6 changes: 3 additions & 3 deletions packages/adapters/wagmi/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 4 additions & 9 deletions packages/appkit/src/adapters/ChainAdapterBlueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this if unused

}

export type WriteContractResult = {
Expand Down
5 changes: 4 additions & 1 deletion packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,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 })
Comment on lines +935 to +938
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this change needed?


return result?.hash || ''
}
Expand Down
12 changes: 12 additions & 0 deletions packages/common/src/contracts/swap.ts
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion packages/common/src/utils/ContractUtil.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -10,5 +11,7 @@ export const ContractUtil = {
}

return erc20ABI
}
},

getSwapAbi: () => swapABI
}
6 changes: 3 additions & 3 deletions packages/core/src/controllers/SendController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/controllers/SwapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/utils/TypeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading