Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
bridge-backend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
movabridge
bridge-backend
Commits
927e42b6
Commit
927e42b6
authored
Nov 27, 2025
by
vicotor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix bug
parent
2f23c22f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
15 deletions
+34
-15
method.go
contract/router/method.go
+22
-10
db.go
dao/db.go
+12
-5
No files found.
contract/router/method.go
View file @
927e42b6
...
...
@@ -4,8 +4,8 @@ import (
"context"
"errors"
"fmt"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"math/big"
...
...
@@ -52,19 +52,31 @@ func GetAmountsOut(
addrPath
=
append
(
addrPath
,
common
.
HexToAddress
(
addrStr
))
}
contract
:=
bind
.
NewBoundContract
(
router
,
parsedRouterABI
,
client
,
client
,
client
)
method
:=
parsedRouterABI
.
Methods
[
"getAmountsOut"
]
packedArgs
,
err
:=
method
.
Inputs
.
Pack
(
amountIn
,
addrPath
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to pack arguments: %w"
,
err
)
}
callData
:=
append
(
method
.
ID
,
packedArgs
...
)
callOpts
:=
&
bind
.
CallOpts
{
Context
:
context
.
Background
(),
From
:
user
,
// optional; included for completeness (some routers ignore).
res
,
err
:=
client
.
CallContract
(
context
.
Background
(),
ethereum
.
CallMsg
{
From
:
user
,
To
:
&
router
,
Data
:
callData
,
},
nil
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to call contract: %w"
,
err
)
}
var
amounts
=
make
([]
interface
{},
len
(
path
))
if
err
:=
contract
.
Call
(
callOpts
,
&
amounts
,
"getAmountsOut"
,
amountIn
,
addrPath
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"
call getAmountsOut (amountIn=%s pathLen=%d): %w"
,
amountIn
.
String
(),
len
(
path
)
,
err
)
var
amounts
[]
*
big
.
Int
if
err
:=
parsedRouterABI
.
UnpackIntoInterface
(
&
amounts
,
"getAmountsOut"
,
res
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"
failed to unpack results: %w"
,
err
)
}
amountOut
:=
*
abi
.
ConvertType
(
amounts
[
len
(
path
)
-
1
],
new
(
*
big
.
Int
))
.
(
**
big
.
Int
)
if
len
(
amounts
)
==
0
{
return
nil
,
errors
.
New
(
"getAmountsOut returned no amounts"
)
}
return
amount
Out
,
nil
return
amount
s
[
len
(
amounts
)
-
1
]
,
nil
}
dao/db.go
View file @
927e42b6
...
...
@@ -588,13 +588,18 @@ func (d *Dao) GetSwapTokenBalance(chainId int64, user string) (balances apiModel
}
func
(
d
*
Dao
)
QuoteSwap
(
param
apiModel
.
QuoteSwapParam
)
(
quote
apiModel
.
QuoteResult
,
err
error
)
{
c
hainInfo
,
err
:=
d
.
GetChainConfig
(
param
.
FromChainId
)
fromC
hainInfo
,
err
:=
d
.
GetChainConfig
(
param
.
FromChainId
)
if
err
!=
nil
{
log
.
Error
(
"not found chain config for chain id:"
,
param
.
FromChainId
)
return
quote
,
fmt
.
Errorf
(
"not found chain for chain id"
)
}
toChainInfo
,
err
:=
d
.
GetChainConfig
(
param
.
ToChainId
)
if
err
!=
nil
{
log
.
Error
(
"not found chain config for chain id:"
,
param
.
ToChainId
)
return
quote
,
fmt
.
Errorf
(
"not found chain for chain id"
)
}
_
,
balance
,
err
:=
d
.
tokenRepo
.
RetriveTokenInfoAndBalance
(
c
hainInfo
.
cli
,
param
.
Path
.
SwapFromToken
,
param
.
User
)
_
,
balance
,
err
:=
d
.
tokenRepo
.
RetriveTokenInfoAndBalance
(
fromC
hainInfo
.
cli
,
param
.
Path
.
SwapFromToken
,
param
.
User
)
if
err
!=
nil
{
log
.
WithFields
(
log
.
Fields
{
"chain_id"
:
param
.
FromChainId
,
...
...
@@ -624,7 +629,7 @@ func (d *Dao) QuoteSwap(param apiModel.QuoteSwapParam) (quote apiModel.QuoteResu
}
quote
=
apiModel
.
QuoteResult
{
ToContract
:
c
hainInfo
.
conf
.
BridgeContract
,
ToContract
:
fromC
hainInfo
.
conf
.
BridgeContract
,
OutAmount
:
inputAmount
.
String
(),
}
...
...
@@ -641,13 +646,14 @@ func (d *Dao) QuoteSwap(param apiModel.QuoteSwapParam) (quote apiModel.QuoteResu
})
.
Error
(
"not found swap config for swap from token to bridge from token"
)
return
quote
,
err
}
swapFromAmount
,
err
:=
router
.
GetAmountsOut
(
c
hainInfo
.
cli
,
common
.
HexToAddress
(
param
.
User
),
common
.
HexToAddress
(
swapConfig
.
SwapContract
),
inputAmount
,
swapConfig
.
SwapPath
)
swapFromAmount
,
err
:=
router
.
GetAmountsOut
(
fromC
hainInfo
.
cli
,
common
.
HexToAddress
(
param
.
User
),
common
.
HexToAddress
(
swapConfig
.
SwapContract
),
inputAmount
,
swapConfig
.
SwapPath
)
if
err
!=
nil
{
log
.
WithFields
(
log
.
Fields
{
"chain_id"
:
param
.
FromChainId
,
"swap_from_token"
:
param
.
Path
.
SwapFromToken
,
"bridge_from_token"
:
param
.
Path
.
BridgeFromToken
,
"input_amount"
:
inputAmount
.
String
(),
"swap_contract"
:
swapConfig
.
SwapContract
,
"error"
:
err
,
})
.
Error
(
"get swap amounts out failed"
)
return
quote
,
fmt
.
Errorf
(
"internal error"
)
...
...
@@ -701,13 +707,14 @@ func (d *Dao) QuoteSwap(param apiModel.QuoteSwapParam) (quote apiModel.QuoteResu
})
.
Error
(
"not found swap config for bridge to token to swap to token"
)
return
quote
,
fmt
.
Errorf
(
"internal error"
)
}
swapToAmount
,
err
:=
router
.
GetAmountsOut
(
c
hainInfo
.
cli
,
common
.
HexToAddress
(
param
.
User
),
common
.
HexToAddress
(
swapConfig
.
SwapContract
),
outAmount
,
swapConfig
.
SwapPath
)
swapToAmount
,
err
:=
router
.
GetAmountsOut
(
toC
hainInfo
.
cli
,
common
.
HexToAddress
(
param
.
User
),
common
.
HexToAddress
(
swapConfig
.
SwapContract
),
outAmount
,
swapConfig
.
SwapPath
)
if
err
!=
nil
{
log
.
WithFields
(
log
.
Fields
{
"chain_id"
:
param
.
ToChainId
,
"bridge_to_token"
:
param
.
Path
.
BridgeToToken
,
"swap_to_token"
:
param
.
Path
.
SwapToToken
,
"input_amount"
:
outAmount
.
String
(),
"swap_contract"
:
swapConfig
.
SwapContract
,
"error"
:
err
,
})
.
Error
(
"get swap amounts out failed"
)
return
quote
,
fmt
.
Errorf
(
"internal error"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment