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: update swap lifecyclestatus #1249

Merged
merged 1 commit into from
Sep 14, 2024
Merged

fix: update swap lifecyclestatus #1249

merged 1 commit into from
Sep 14, 2024

Conversation

alessey
Copy link
Contributor

@alessey alessey commented Sep 12, 2024

What changed? Why?

  • remove context states and use lifecyclestatus as the source of truth
  • persist all lifecycle status data (except errors)

Notes to reviewers

How has it been tested?

Copy link

vercel bot commented Sep 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
onchainkit-coverage ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 13, 2024 5:19pm
onchainkit-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 13, 2024 5:19pm
onchainkit-routes ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 13, 2024 5:19pm

@alessey alessey changed the title Alessey/lifecyclestatus v2 fix: update lifecyclestatus Sep 12, 2024
@alessey alessey force-pushed the alessey/lifecyclestatusV2 branch from f4f4ce4 to 5a8edd8 Compare September 13, 2024 15:48
@alessey alessey force-pushed the alessey/lifecyclestatusV2 branch from f310997 to bd087b0 Compare September 13, 2024 15:52
@alessey alessey force-pushed the alessey/lifecyclestatusV2 branch from bd087b0 to 138f24a Compare September 13, 2024 15:53
@alessey alessey force-pushed the alessey/lifecyclestatusV2 branch from 138f24a to f75d96f Compare September 13, 2024 15:57
});
},
[],
);
Copy link
Contributor Author

@alessey alessey Sep 13, 2024

Choose a reason for hiding this comment

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

This is the main update, creating a setState callback function here that persists all statusData except errors, this data can be overwritten, but will persist until init is called again. This has a slightly different type than LifeCycleStatus since shared data is persisted it is not required except in init.

Copy link
Contributor

Choose a reason for hiding this comment

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

very very clever, and can't wait for next PRs to have this abstract even more, so we can use it in other components.

!from.token || !to.token || !from.amount || !to.amount,
},
});
}, [from, to, updateLifeCycleStatus]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seemed like we should be triggering an amountChange on toggle since from and to switch, thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

You right! Great point

isMissingRequiredField: false,
maxSlippage,
},
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't believe this is needed, unless it is possible to swap even if the quote fails. Status init doesn't make sense at this point when you are in the middle of the process.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah makes sense.

>;
})
: never
: never;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is some complex ts to make LifeCycleStatusDataShared optional when updating state in all statuses except for init. the alternative would just be to explicitly duplicate LifeCycleStatus with this logic, but would require adding/updating state types in two places.

Copy link
Contributor

Choose a reason for hiding this comment

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

As this complexity will be shared across many experiences, this is a complexity that is fair to pay.

Plus, for everyone reading, we tested all other options and we still found this trade-off being the most simple overall.

transactionHash: txHash,
transactionType: useAggregator ? 'ERC20' : 'Permit2',
},
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently we are showing confirm in wallet until the receipt, seemed like we should emit the transaction status and then you'll get success after receipt.

Copy link
Contributor

Choose a reason for hiding this comment

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

I lean towards your point, but can we ask also @0xAlec @cpcramer @mykcryptodev @abcrane123 what they think about this.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

@alessey alessey marked this pull request as ready for review September 13, 2024 17:29
@alessey alessey changed the title fix: update lifecyclestatus fix: update swap lifecyclestatus Sep 13, 2024
}
if (lifeCycleStatus.statusName === 'transactionApproved') {
setPendingTransaction(false);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no more reacting to state changes to set state

Copy link
Contributor

Choose a reason for hiding this comment

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

so smoooooooth

@@ -151,7 +168,6 @@ export function SwapProvider({
dToken?: Token,
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO Refactor this component
) => {
const maxSlippage = lifeCycleStatus.statusData.maxSlippage;
Copy link
Contributor

Choose a reason for hiding this comment

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

so clean

};

// make all keys in T optional if they are in K
Copy link
Contributor

Choose a reason for hiding this comment

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

Dark magic, love it!!!!

@@ -32,23 +29,25 @@ export function getSwapMessage({
return SwapMessage.INSUFFICIENT_BALANCE;
}
// handle pending transaction
if (isTransactionPending) {
if (lifeCycleStatus.statusName === 'transactionPending') {
Copy link
Contributor

Choose a reason for hiding this comment

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

so clean

@Zizzamia
Copy link
Contributor

Zizzamia commented Sep 14, 2024

Great job @alessey !!!
Was not an easy refactor, and can't wait to see this as the standard across all component experiences.

@Zizzamia Zizzamia merged commit 477fc52 into main Sep 14, 2024
16 checks passed
@Zizzamia Zizzamia deleted the alessey/lifecyclestatusV2 branch September 14, 2024 03:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants