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

Effects typing fixes #947

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

eumemic
Copy link

@eumemic eumemic commented Apr 28, 2024

Parameterize BaseEffectRecord by effect type literals so that it's possible to discriminate based on the type field. With this change code like the following type checks whereas before it would have had errors because the type of effect wouldn't get appropriately narrowed in each case based on the value of effect.type:

switch (effect.type) {
  case "trade":
    console.log("trade", effect.sold_amount, effect.bought_amount);
    break;
  case "liquidity_pool_trade":
    console.log("liquidity_pool_trade", effect.liquidity_pool.reserves);
    break;
  // ...
}

Also added the type_i field to the Trade interface since it was missing, which was preventing even type_i from working correctly to discriminate effect types.

@eumemic eumemic changed the title Improve effect record typing to make use of discriminated union Allow discriminating effect types based on type field Apr 28, 2024
@eumemic eumemic changed the title Allow discriminating effect types based on type field Effects typing fixes Apr 28, 2024
type_i: EffectType.account_credited;
amount: string;
}
export interface AccountDebited extends BaseEffectRecord {
export interface AccountDebited extends BaseEffectRecord<'account_debited'>, OfferAsset {
Copy link
Author

Choose a reason for hiding this comment

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

Also took the opportunity to fix this, AccountDebited has OfferAsset fields.

Copy link
Contributor

@Shaptic Shaptic left a comment

Choose a reason for hiding this comment

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

Nice, I like it, Picasso 🤌 thank you!

@Shaptic
Copy link
Contributor

Shaptic commented Jul 17, 2024

@eumemic apologies for taking so long to review this - do you think you can sign the commits to get this merged? The repo blocks those commits otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants