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 65 commits
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
22 changes: 20 additions & 2 deletions app/components/UI/Swaps/components/TokenSelectModal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { useCallback, useMemo, useRef, useState } from 'react';
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import PropTypes from 'prop-types';
import {
StyleSheet,
Expand Down Expand Up @@ -155,6 +161,7 @@ function TokenSelectModal({
providerConfig,
networkConfigurations,
balances,
initialSearchString = '',
}) {
const navigation = useNavigation();
const { trackAnonymousEvent } = useMetrics();
Expand Down Expand Up @@ -301,6 +308,14 @@ function TokenSelectModal({
showTokenImportModal();
}, [showTokenImportModal]);

useEffect(() => {
!!initialSearchString && setSearchString(initialSearchString);

if (tokenMetadata.valid) {
handleShowImportToken();
}
}, [initialSearchString, handleShowImportToken, tokenMetadata.valid]);

const handlePressImportToken = useCallback(
(item) => {
const { address, symbol } = item;
Expand Down Expand Up @@ -384,7 +399,6 @@ function TokenSelectModal({
setSearchString('');
searchInput?.current?.focus();
}, [setSearchString]);

return (
<Modal
isVisible={isVisible}
Expand Down Expand Up @@ -564,6 +578,10 @@ TokenSelectModal.propTypes = {
* Network configurations
*/
networkConfigurations: PropTypes.object,
/**
* Initial search string with token address
* */
initialSearchString: PropTypes.string,
};

const mapStateToProps = (state) => ({
Expand Down
31 changes: 26 additions & 5 deletions app/components/UI/Swaps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ function SwapsAmountView({
const explorer = useBlockExplorer(providerConfig, networkConfigurations);
const initialSource = route.params?.sourceToken ?? SWAPS_NATIVE_ADDRESS;
const initialDestination = route.params?.destinationToken;
const initialAmount = route.params?.amount;

const [amount, setAmount] = useState('0');
const [slippage, setSlippage] = useState(AppConstants.SWAPS.DEFAULT_SLIPPAGE);
Expand All @@ -233,6 +234,7 @@ function SwapsAmountView({
toLowerCaseEquals(token.address, initialDestination),
),
);
const [initialDestinationSearch, setInitialDestinationSearch] = useState('');
const [hasDismissedTokenAlert, setHasDismissedTokenAlert] = useState(true);
const [contractBalance, setContractBalance] = useState(null);
const [contractBalanceAsUnits, setContractBalanceAsUnits] = useState(
Expand Down Expand Up @@ -366,13 +368,31 @@ function SwapsAmountView({
useEffect(() => {
if (canSetAnInitialTokenDestination) {
setIsDestinationSet(true);
setDestinationToken(
swapsTokens.find((token) =>
toLowerCaseEquals(token.address, initialDestination),
),
const destinationTokenOnList = swapsTokens.find((token) =>
toLowerCaseEquals(token.address, initialDestination),
);
if (destinationTokenOnList) {
setDestinationToken(destinationTokenOnList);
} else {
toggleDestinationModal();
setInitialDestinationSearch(initialDestination);
}
}
}, [
canSetAnInitialTokenDestination,
initialDestination,
swapsTokens,
toggleDestinationModal,
]);

const canSetInitialAmount =
destinationToken && sourceToken && initialAmount && swapsControllerTokens;

useEffect(() => {
if (canSetInitialAmount) {
setAmount(parseInt(initialAmount, 16).toString());
}
}, [canSetAnInitialTokenDestination, initialDestination, swapsTokens]);
}, [initialAmount, canSetInitialAmount]);

useEffect(() => {
setHasDismissedTokenAlert(false);
Expand Down Expand Up @@ -777,6 +797,7 @@ function SwapsAmountView({
]}
onItemPress={handleDestinationTokenPress}
excludeAddresses={[sourceToken?.address]}
initialSearchString={initialDestinationSearch}
/>
</View>
<View>
Expand Down
4 changes: 2 additions & 2 deletions app/components/Views/confirmations/SendFlow/Amount/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ import { PREFIX_HEX_STRING } from '../../../../../constants/transaction';
import Routes from '../../../../../constants/navigation/Routes';
import { getRampNetworks } from '../../../../../reducers/fiatOrders';
import { swapsLivenessSelector } from '../../../../../reducers/swaps';
import { isSwapsAllowed } from '../../../../UI/Swaps/utils';
import { isSwapsAllowed } from '../../../../../components/UI/Swaps/utils';
import { swapsUtils } from '@metamask/swaps-controller';
import { regex } from '../../../../../util/regex';
import { AmountViewSelectorsIDs } from '../../../../../../e2e/selectors/SendFlow/AmountView.selectors';
import { isNetworkRampNativeTokenSupported } from '../../../../../components/UI/Ramp/utils';
import { withMetricsAwareness } from '../../../../../components/hooks/useMetrics';
import { selectGasFeeEstimates } from '../../../../../selectors/confirmTransaction';
import { selectGasFeeControllerEstimateType } from '../../../../../selectors/gasFeeController';
import { isNetworkRampNativeTokenSupported } from '../../../../../components/UI/Ramp/utils';

const KEYBOARD_OFFSET = Device.isSmallDevice() ? 80 : 120;

Expand Down
1 change: 1 addition & 0 deletions app/core/Permissions/specifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ export const unrestrictedMethods = Object.freeze([
'metamask_logWeb3ShimUsage',
'wallet_switchEthereumChain',
'wallet_addEthereumChain',
'wallet_swapAsset',
///: BEGIN:ONLY_INCLUDE_IF(preinstalled-snaps,external-snaps)
'wallet_getAllSnaps',
'wallet_getSnaps',
Expand Down
Loading
Loading