-
Notifications
You must be signed in to change notification settings - Fork 7
fix(router): adds caller verification when the swap callback #1005
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
Merged
jinoosss
merged 5 commits into
main
from
fix-router-adds-caller-verification-when-the-swap-callback
Dec 1, 2025
+239
−0
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
145d449
fix(router): adds caller verification when the swap callback
jinoosss caa065d
test: add swap callback tests
jinoosss 7db435f
Merge branch 'main' into fix-router-adds-caller-verification-when-the…
jinoosss 94baf52
test: assert tests
jinoosss 2333ae3
Merge branch 'main' into fix-router-adds-caller-verification-when-the…
jinoosss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| package v1 | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "gno.land/p/nt/testutils" | ||
| "gno.land/p/nt/uassert" | ||
| ) | ||
|
|
||
| // TestSwapCallback tests the SwapCallback function with various scenarios | ||
| func TestSwapCallback(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| setupFunc func(t *testing.T) address // returns payer address | ||
| amount0Delta string | ||
| amount1Delta string | ||
| callerRealmPath string | ||
| shouldError bool | ||
| expectedErrorMsg string | ||
| }{ | ||
| { | ||
| name: "success with token0 payment", | ||
| setupFunc: func(t *testing.T) address { | ||
| initRouterTest(t) | ||
| CreatePoolWithoutFee(t) | ||
| MakeMintPositionWithoutFee(t) | ||
| TokenFaucet(t, barPath, routerAddr) | ||
| TokenFaucet(t, bazPath, routerAddr) | ||
| return routerAddr | ||
| }, | ||
| amount0Delta: "1000", | ||
| amount1Delta: "0", | ||
| callerRealmPath: "gno.land/r/gnoswap/router/v1", | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| name: "success with token1 payment", | ||
| setupFunc: func(t *testing.T) address { | ||
| initRouterTest(t) | ||
| CreatePoolWithoutFee(t) | ||
| MakeMintPositionWithoutFee(t) | ||
| TokenFaucet(t, barPath, routerAddr) | ||
| TokenFaucet(t, bazPath, routerAddr) | ||
| return routerAddr | ||
| }, | ||
| amount0Delta: "0", | ||
| amount1Delta: "1000", | ||
| callerRealmPath: "gno.land/r/gnoswap/router/v1", | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| name: "success with both deltas zero", | ||
| setupFunc: func(t *testing.T) address { | ||
| initRouterTest(t) | ||
| CreatePoolWithoutFee(t) | ||
| return routerAddr | ||
| }, | ||
| amount0Delta: "0", | ||
| amount1Delta: "0", | ||
| callerRealmPath: "gno.land/r/gnoswap/router/v1", | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| name: "success with user as payer", | ||
| setupFunc: func(t *testing.T) address { | ||
| initRouterTest(t) | ||
| CreatePoolWithoutFee(t) | ||
| MakeMintPositionWithoutFee(t) | ||
| testUser := testutils.TestAddress("testUser") | ||
| TokenFaucet(t, barPath, testUser) | ||
| TokenFaucet(t, bazPath, testUser) | ||
| TokenApprove(t, barPath, testUser, routerAddr, maxApprove) | ||
| TokenApprove(t, bazPath, testUser, routerAddr, maxApprove) | ||
| return testUser | ||
| }, | ||
| amount0Delta: "1000", | ||
| amount1Delta: "0", | ||
| callerRealmPath: "gno.land/r/gnoswap/router/v1", | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| name: "fail with invalid caller", | ||
| setupFunc: func(t *testing.T) address { | ||
| initRouterTest(t) | ||
| CreatePoolWithoutFee(t) | ||
| return routerAddr | ||
| }, | ||
| amount0Delta: "1000", | ||
| amount1Delta: "0", | ||
| callerRealmPath: "gno.land/r/unauthorized/contract", | ||
| shouldError: true, | ||
| expectedErrorMsg: "[GNOSWAP-ROUTER-005]", | ||
| }, | ||
| { | ||
| name: "fail with insufficient balance", | ||
| setupFunc: func(t *testing.T) address { | ||
| initRouterTest(t) | ||
| CreatePoolWithoutFee(t) | ||
| MakeMintPositionWithoutFee(t) | ||
| poorUser := testutils.TestAddress("poorUser") | ||
| return poorUser | ||
| }, | ||
| amount0Delta: "1000", | ||
| amount1Delta: "0", | ||
| callerRealmPath: "gno.land/r/gnoswap/router/v1", | ||
| shouldError: true, | ||
| expectedErrorMsg: "insufficient balance", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Setup | ||
| payer := tt.setupFunc(t) | ||
|
|
||
| // Set caller realm | ||
| testing.SetRealm(testing.NewCodeRealm(tt.callerRealmPath)) | ||
|
|
||
| // Execute | ||
| if tt.shouldError { | ||
| uassert.AbortsContains(t, tt.expectedErrorMsg, func() { | ||
| mockInstanceSwapCallback( | ||
| barPath, | ||
| bazPath, | ||
| tt.amount0Delta, | ||
| tt.amount1Delta, | ||
| payer, | ||
| ) | ||
| }) | ||
| } else { | ||
| err := mockInstanceSwapCallback( | ||
| barPath, | ||
| bazPath, | ||
| tt.amount0Delta, | ||
| tt.amount1Delta, | ||
| payer, | ||
| ) | ||
| uassert.NoError(t, err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestSwapCallback_AssertIsRouterV1 tests the assertIsRouterV1 function with various caller types | ||
| func TestSwapCallback_AssertIsRouterV1(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| callerAddress address | ||
| shouldError bool | ||
| expectedErrorMsg string | ||
| }{ | ||
| { | ||
| name: "valid caller from router v1", | ||
| callerAddress: routerV1Addr, | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| name: "invalid caller from unauthorized contract", | ||
| callerAddress: testutils.TestAddress("unauthorized"), | ||
| shouldError: true, | ||
| expectedErrorMsg: "[GNOSWAP-ROUTER-005]", | ||
| }, | ||
| { | ||
| name: "invalid caller from user realm", | ||
| callerAddress: testutils.TestAddress("maliciousUser"), | ||
| shouldError: true, | ||
| expectedErrorMsg: "[GNOSWAP-ROUTER-005]", | ||
| }, | ||
| { | ||
| name: "invalid caller from different contract", | ||
| callerAddress: testutils.TestAddress("different"), | ||
| shouldError: true, | ||
| expectedErrorMsg: "[GNOSWAP-ROUTER-005]", | ||
| }, | ||
| { | ||
| name: "invalid caller from pool contract", | ||
| callerAddress: poolAddr, | ||
| shouldError: true, | ||
| expectedErrorMsg: "[GNOSWAP-ROUTER-005]", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Execute and verify | ||
| if tt.shouldError { | ||
| uassert.PanicsContains(t, tt.expectedErrorMsg, func() { | ||
| assertIsRouterV1(tt.callerAddress) | ||
| }) | ||
| } else { | ||
| // Should not panic | ||
| assertIsRouterV1(tt.callerAddress) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.