Skip to content

Conversation

@krishgka
Copy link
Contributor

The model was incorrectly using the call_createpair table which stores tokens in the order they were passed to the function (tokenA, tokenB). However, the Uniswap V2 factory contract sorts addresses internally before creating pairs (smaller address = token0, larger address = token1).

This caused buy/sell directions to be reversed when tokenA > tokenB.

Changes:

  • Switch from somnexammfactory_call_createpair to somnexammfactory_evt_paircreated
  • Use event's token0/token1 (sorted) instead of call's tokenA/tokenB (unsorted)
  • Update seed file with correct test data (buy/sell were previously swapped)
  • Add missing transaction (evt_index 7) to seed file

Thank you for contributing to Spellbook 🪄

Please open the PR in draft and mark as ready when you want to request a review.

Description:

[...]


quick links for more information:

@github-actions github-actions bot marked this pull request as draft October 17, 2025 16:26
@github-actions github-actions bot added WIP work in progress dbt: dex covers the DEX dbt subproject labels Oct 17, 2025
@krishgka krishgka changed the title Fix Somnex V2 Somnia CUR2-757 Fix Somnex V2 Somnia Oct 19, 2025
The model was incorrectly using the call_createpair table which stores tokens
in the order they were passed to the function (tokenA, tokenB). However, the
Uniswap V2 factory contract sorts addresses internally before creating pairs
(smaller address = token0, larger address = token1).

This caused buy/sell directions to be reversed when tokenA > tokenB.

Additionally, removed the 1e10 division logic that was incorrectly applied to
all swap amounts. Testing showed that the Swap events match ERC20 transfers
exactly with no inflation bug, so the division was creating errors rather than
fixing them.

Changes:
- Switch from somnexammfactory_call_createpair to somnexammfactory_evt_paircreated
- Use event's token0/token1 (sorted) instead of call's tokenA/tokenB (unsorted)
- Remove incorrect 1e10 division logic for amounts
- Update seed file with correct test data (buy/sell were previously swapped)
- Add missing transaction (evt_index 7) to seed file
@krishgka krishgka requested a review from jeff-dude October 20, 2025 13:52
@krishgka krishgka marked this pull request as ready for review October 20, 2025 13:52
@github-actions github-actions bot added ready-for-review this PR development is complete, please review and removed WIP work in progress labels Oct 20, 2025
@jeff-dude
Copy link
Member

@krishgka without looking too closely, does this mean we can then also update this model to use the fork macro instead to keep clean? logic looks similar 🤔

@jeff-dude jeff-dude self-assigned this Oct 20, 2025
@jeff-dude jeff-dude added in review Assignee is currently reviewing the PR and removed ready-for-review this PR development is complete, please review labels Oct 20, 2025
Reduces code from 63 lines to 20 lines by using the standard macro that
187 other Uniswap V2 compatible DEXes use. This makes the code:
- More maintainable
- Consistent with other DEX models
- Easier to review and understand
- Automatically includes any future macro improvements

The macro produces identical output to the manual code.
@krishgka
Copy link
Contributor Author

@krishgka without looking too closely, does this mean we can then also update this model to use the fork macro instead to keep clean? logic looks similar 🤔

Yup you're right, changed it!

@jeff-dude jeff-dude added ready-for-merging and removed in review Assignee is currently reviewing the PR labels Oct 20, 2025
@krishgka krishgka merged commit f4e0744 into main Oct 20, 2025
4 checks passed
@krishgka krishgka deleted the somnia_dex_fix branch October 20, 2025 19:42
@github-actions github-actions bot locked and limited conversation to collaborators Oct 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

dbt: dex covers the DEX dbt subproject ready-for-merging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants