Skip to content

Swaps support for local testnet#10658

Merged
danjm merged 5 commits intodevelopfrom
support-swaps-testnet
Mar 18, 2021
Merged

Swaps support for local testnet#10658
danjm merged 5 commits intodevelopfrom
support-swaps-testnet

Conversation

@danjm
Copy link
Copy Markdown
Contributor

@danjm danjm commented Mar 16, 2021

This PR allows swaps to work on local test networks with chainId of 0x539. It also abstracts swaps logic dependent on chainId in a way that will allow us to add support for future networks simply by editing the shared/constants/swaps.js file.

Video demo of swaps working on a local test network:

swap-testnet-support.mp4

@danjm danjm requested a review from a team as a code owner March 16, 2021 10:17
@danjm danjm requested a review from darkwing March 16, 2021 10:17
@github-actions
Copy link
Copy Markdown
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [daa8a2a]
Page Load Metrics (755 ± 53 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint479366115
domContentLoaded57792675411053
load57993175511153
domInteractive57792675311053

txMeta.txParams.from,
txMeta.destinationTokenDecimals,
approvalTxMeta,
chainId,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

txMeta should now include the chainId for the transaction. I think this is safer than getting the current chainId if in the event that the network switches while this flow is in flight.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

addressed in 0a89657


const METASWAP_ETH_API_HOST = 'https://api.metaswap.codefi.network';

const SWAPS_TESTNET_CHAIN_ID = '0x539';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what prevents a local ganache that doesn't fork mainnet or have the ability to interact with swaps from using this API?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hmmm.... good point... nothing prevents that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think that's okay though... no risk of fund loss, and it is only developers using local ganache

return (
txParams?.to === recipientAddress ||
(txParams?.to === SWAPS_CONTRACT_ADDRESS &&
(txParams?.to === SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[chainId] &&
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[Q] It's been a while since I viewed this code, and regret not having added documentation to it when I touched it last. If I understand correctly this is to help filter out transactions that aren't to the specified token. So when we're on, let's say DAI's asset page, we only see transactions interacting with DAI's contract address and swaps's contract address as long as the swap interacted with DAI's contract address?

I was able to walk through that logic in the current develop branch without the addition of chainId. If my assumption is correct it might be worth documenting the filter logic so that we don't lose the intent of these filters as logic changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So when we're on, let's say DAI's asset page, we only see transactions interacting with DAI's contract address and swaps's contract address as long as the swap interacted with DAI's contract address?

yes, exactly

let swapsFeatureIsLive = false;
try {
swapsFeatureIsLive = await fetchSwapsFeatureLiveness();
swapsFeatureIsLive = await fetchSwapsFeatureLiveness(chainId);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I just noticed that this method is in a utility file that exists in pages/swaps/swap.util but is also used in the background. Could we move these shared utility methods into shared/ ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have started this on a separate branch that I will PR after the present PR is merged https://github.com/MetaMask/metamask-extension/pull/new/swaps-utils-shared-directory

export const signAndSendTransactions = (history, metaMetricsEvent) => {
return async (dispatch, getState) => {
const state = getState();
const chainId = getCurrentChainId(state);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[Q] would it be worth it to extract fetchSwapsFeatureLiveness into its own dispatch action / thunk so that anywhere we call it in the UI code it can be responsible for getting chainid itself? Seems like it'd cut down on repetition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have made this change on another branch that I will PR once this one is merged: https://github.com/MetaMask/metamask-extension/pull/new/fetchswapsfeatureliveness-thunk

const conversionRate = useSelector(getConversionRate);
const currentCurrency = useSelector(getCurrentCurrency);

const defaultTokenSymbol = SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId].symbol;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[Q] Do you expect defaultTokenSymbol to be different than defaultSwapsToken.symbol here? Seems like getSwapsDefaultToken returns this same object?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, I have addressed this as part of 04e7af6

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

that commit is dependent on b20c98f

@brad-decker
Copy link
Copy Markdown
Contributor

@danjm the changes to tests will need to be applied onto the relocated versions that are now colocated with the code. Sorry for the inconvenience 🙏🏻

@danjm danjm force-pushed the support-swaps-testnet branch from daa8a2a to 04e7af6 Compare March 17, 2021 07:22
@danjm
Copy link
Copy Markdown
Contributor Author

danjm commented Mar 17, 2021

@brad-decker This has been rebased and test related conflicts handled.

Note that there is one commit of updates that I made that are not directly from review: b20c98f "Create util method for comparison of token addresses/symbols to default swaps token"

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [04e7af6]
Page Load Metrics (600 ± 15 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint43715384
domContentLoaded5536605983215
load5546616003215
domInteractive5536605983215

// If the swap is from the selected chain's default token, subtract
// the sourceAmount from the total cost. Otherwise, the total fee
// is simply trade.value plus gas fees.
const ethFee = isSwapsDefaultTokenAddress(sourceToken, chainId)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Do we want a more descriptive var name here? defaultTokenFee or transactionTokenFee? Just a thought. Naming is hard though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a good point. There actually are a bunch of variables related to swaps with 'eth' in the name that need to be updated. I think "chainCurrency" is a good alternative to "eth". Because the changes are so extensive, I'd like to do this in a separate/future PR, to minimize chance of missing bugs. I have started the work on this branch https://github.com/MetaMask/metamask-extension/pull/new/eth-naming-updates-swaps-alt-nets

txMeta.txParams.from,
txMeta.destinationTokenDecimals,
approvalTxMeta,
txMeta.chainId,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking at the number of arguments going to this function, we're going to want to shift to passing an object sooon.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have put this changed on another branch and will PR it once this PR is merged https://github.com/MetaMask/metamask-extension/pull/new/getSwapsTokensReceivedFromTxMeta-object-param

) => {
return ({ initialTransaction: { txParams } }) => {
return (
txParams?.to === recipientAddress ||
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we do a check here for:

if (!txParams) {  return ____ }

Might be shorter than a bunch of ?. optional chaining checks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Given our lint rules, this would have to be over 3 lines, meanwhile two other lines just become shorter by a single character. So might be best to leave as is, unless adding the explicit if check and return is a readability improvement?

toToken;
const toTokenIsNotEth =
const toTokenIsNotDefault =
selectedToToken?.address &&
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think we need this check because isSwapsDefaultTokenAddress inherently would return false

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

right, but line 137 is "**!**isSwapsDefaultTokenAddress(selectedToToken?.address, chainId)", so when selectedToToken?.address is a falsy value, toTokenIsNotDefault is true, because of the !

@darkwing
Copy link
Copy Markdown
Contributor

I've made a few minor suggestions. Really well done, can't wait to use this!

@danjm danjm merged commit 480512d into develop Mar 18, 2021
@danjm danjm deleted the support-swaps-testnet branch March 18, 2021 10:20
@github-actions github-actions bot locked and limited conversation to collaborators Mar 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants