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

feat: New json rpc for swaps #7509

Draft
wants to merge 70 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
328168a
swaps when amount is insufficient
tommasini Aug 7, 2023
48db8ba
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Aug 21, 2023
71c4fad
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Aug 24, 2023
69dee6b
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Sep 15, 2023
9f41fa4
added new label swap tokens and re used the warning of transactions m…
tommasini Sep 15, 2023
0a083af
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Sep 21, 2023
d288988
merge main
tommasini Oct 9, 2023
350d2ca
add events for metrics purposes, remove swaps button and added the na…
tommasini Oct 9, 2023
841b44d
remove unused imports
tommasini Oct 10, 2023
544750b
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Oct 11, 2023
b7702fe
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Oct 12, 2023
91b9bf3
address review, by changing conditions into variables for better self…
tommasini Oct 12, 2023
035f466
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Oct 12, 2023
e9bfa9f
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Oct 13, 2023
8ae884c
refactor for better variable names
tommasini Oct 13, 2023
34a8a07
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Oct 16, 2023
b67ff9d
added a new json rpc called wallet_swap, to enable dapps to ask for a…
tommasini Oct 16, 2023
9347ce6
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Oct 26, 2023
7b5cf0d
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Oct 27, 2023
aba6299
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Oct 27, 2023
22910be
Merge branch 'feat/swaps-on-send-flow' into feature/wallet-swap-rpc-m…
tommasini Oct 27, 2023
f734eea
fix lint
tommasini Oct 27, 2023
8e9fa59
add select chain Id
tommasini Oct 27, 2023
26f147d
fix unit test
tommasini Oct 27, 2023
5d82d82
solve conflicts and merge main
tommasini Nov 10, 2023
abcf16d
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Nov 10, 2023
a9a8be9
Merge branch 'feat/swaps-on-send-flow' into feature/wallet-swap-rpc-m…
tommasini Nov 10, 2023
98ee256
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Nov 10, 2023
cee58b9
update amount to receive wei on hexadecimal format
tommasini Nov 10, 2023
53967d2
Merge branch 'feat/swaps-on-send-flow' into feature/wallet-swap-rpc-m…
tommasini Nov 10, 2023
cbca9f9
change message when the dapp asks for a chain id on the swap and the …
tommasini Nov 11, 2023
6d5c130
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Nov 13, 2023
049ffee
Merge branch 'feat/swaps-on-send-flow' into feature/wallet-swap-rpc-m…
tommasini Nov 13, 2023
593f446
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Nov 24, 2023
1d0eb16
merge origin branch and solve conflicts
tommasini Nov 24, 2023
70aaf44
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Dec 4, 2023
3f15d44
change old util to new util of ramp and new route to buy ramp
tommasini Dec 5, 2023
a2e5aba
Merge branch 'feat/swaps-on-send-flow' into feature/wallet-swap-rpc-m…
tommasini Dec 5, 2023
3377693
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Dec 5, 2023
224dfba
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Dec 6, 2023
fbb0f69
Merge branch 'feat/swaps-on-send-flow' into feature/wallet-swap-rpc-m…
tommasini Dec 6, 2023
577af97
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Dec 20, 2023
37ca6c1
Merge branch 'feat/swaps-on-send-flow' into feature/wallet-swap-rpc-m…
tommasini Dec 20, 2023
e7791a3
taking into account the address connected to the dapp, if it is the d…
tommasini Dec 21, 2023
2489a16
added logic for future support of multiple swap tokens and cross chai…
tommasini Dec 21, 2023
3acba75
change name of json rpc method to wallet_swapAsset
tommasini Dec 21, 2023
e457aac
update tests. ask if the user wants to switch network when the chain …
tommasini Dec 21, 2023
e29df93
merge main and solve conflicts
tommasini Dec 21, 2023
523fc8d
Merge branch 'feat/swaps-on-send-flow' into feature/wallet-swap-rpc-m…
tommasini Dec 21, 2023
0ff692e
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Dec 21, 2023
5275670
Merge branch 'feat/swaps-on-send-flow' into feature/wallet-swap-rpc-m…
tommasini Dec 21, 2023
3cf462f
Merge branch 'main' into feat/swaps-on-send-flow
tommasini Dec 21, 2023
36ddc1d
update snapshot
tommasini Dec 21, 2023
9596bfb
Merge branch 'feat/swaps-on-send-flow' into feature/wallet-swap-rpc-m…
tommasini Dec 21, 2023
98bbea3
merge main and solve conflicts
tommasini Mar 4, 2024
bd944c9
Merge branch 'main' into feature/wallet-swap-rpc-method
tommasini Apr 24, 2024
5d347cf
remove chain id verifications
tommasini Apr 25, 2024
074f7b0
Merge branch 'main' into feature/wallet-swap-rpc-method
tommasini May 17, 2024
6f675ad
added methodNotSupported error if there is a try of multi tokens swap…
tommasini May 17, 2024
d88b883
enable wallet swap asset and navigate to the import sheet when the to…
tommasini May 20, 2024
d3a369c
add unit tests for each error scenario
tommasini May 20, 2024
d3842b8
Merge branch 'main' into feature/wallet-swap-rpc-method
tommasini Jun 21, 2024
f7cee6e
added caip 10 addresses support
tommasini Jun 24, 2024
85b36d4
merge main and solve conflicts
tommasini Jul 1, 2024
8287ac8
apply caip 10 util methods from metamask utils repo and delete the ut…
tommasini Jul 1, 2024
4d63433
fix types on rpc method middleware swapAsset
tommasini Jul 1, 2024
b32dc57
fix typescript issues on rpcmethod middleware
tommasini Jul 1, 2024
9d8376d
fix unit tests
tommasini Jul 1, 2024
c430b11
user_address to userAddress
tommasini Jul 1, 2024
e56bd3b
update the swap could not be completed as requested
tommasini Jul 2, 2024
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
28 changes: 21 additions & 7 deletions app/core/RPCMethods/RPCMethodMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
PermissionController,
permissionRpcMethods,
} from '@metamask/permission-controller';
import { CaipAccountId, parseCaipAccountId } from '@metamask/utils';
import Networks, {
blockTagParamIndex,
getAllNetworks,
Expand Down Expand Up @@ -47,7 +48,6 @@
import DevLogger from '../SDKConnect/utils/DevLogger';
import { addTransaction } from '../../util/transaction-controller';
import { selectSelectedInternalAccountChecksummedAddress } from '../../selectors/accountsController';
import { parseCaip10Address } from '../../util/caip10Address';

// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -397,16 +397,22 @@
const selectedAddress = selectSelectedInternalAccountChecksummedAddress(
store.getState(),
);
let parsedCaip10UserAddress;

Check failure on line 400 in app/core/RPCMethods/RPCMethodMiddleware.ts

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Variable 'parsedCaip10UserAddress' implicitly has type 'any' in some locations where its type cannot be determined.
try {
parsedCaip10UserAddress = parseCaip10Address(user_address);
parsedCaip10UserAddress = parseCaipAccountId(
user_address as CaipAccountId,
tommasini marked this conversation as resolved.
Show resolved Hide resolved
);
} catch (error) {
throw rpcErrors.invalidParams('Invalid caip-10 user address');
}
if (parsedCaip10UserAddress.chain.namespace !== 'eip155') {
throw new Error('Only support Ethereum addresses at the moment');
}

const dappConnectedAccount = Object.keys(identities).find(
(address) =>
safeToChecksumAddress(address) ===
safeToChecksumAddress(parsedCaip10UserAddress.address),

Check failure on line 415 in app/core/RPCMethods/RPCMethodMiddleware.ts

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Variable 'parsedCaip10UserAddress' implicitly has an 'any' type.
);

if (!dappConnectedAccount) {
Expand All @@ -425,32 +431,40 @@

let parsedCaip10FromTokenAddress;
try {
parsedCaip10FromTokenAddress = parseCaip10Address(
parsedCaip10FromTokenAddress = parseCaipAccountId(
fromToken[0].address,
);
} catch (error) {
throw rpcErrors.invalidParams('Invalid caip-10 fromToken address');
}

if (parsedCaip10FromTokenAddress.chain.namespace !== 'eip155') {
throw new Error('Only support Ethereum addresses at the moment');
}

let parsedCaip10ToTokenAddress;
try {
parsedCaip10ToTokenAddress = parseCaip10Address(toToken.address);
parsedCaip10ToTokenAddress = parseCaipAccountId(toToken.address);
} catch (error) {
throw rpcErrors.invalidParams('Invalid caip-10 toToken address');
}

if (parsedCaip10ToTokenAddress.chain.namespace !== 'eip155') {
throw new Error('Only support Ethereum addresses at the moment');
}

if (
parsedCaip10FromTokenAddress.chainId !==
parsedCaip10ToTokenAddress.chainId
) {
throw rpcErrors.methoddNotSupported(
throw rpcErrors.methodNotSupported(
'Cross-chain swaps are currently not supported. Both fromToken and toToken must be on the same blockchain.',
);
}

const chainId = selectChainId(store.getState());

if (chainId !== toHex(parsedCaip10FromTokenAddress.chainId)) {
if (chainId !== toHex(parsedCaip10FromTokenAddress.chain.reference)) {
throw rpcErrors.invalidParams(
`Invalid parameters: active chainId is different than the one provided.`,
);
Expand All @@ -460,7 +474,7 @@
safeToChecksumAddress(dappConnectedAccount);

if (selectedAddress !== checksummedDappConnectedAccount) {
Engine.setSelectedAccount(checksummedDappConnectedAccount);
Engine.setSelectedAddress(checksummedDappConnectedAccount);
}

// switch to the chain id asked from the dapp
Expand Down
80 changes: 0 additions & 80 deletions app/util/caip10Address/index.test.ts

This file was deleted.

47 changes: 0 additions & 47 deletions app/util/caip10Address/index.ts

This file was deleted.

Loading