Skip to content

Commit

Permalink
Add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
perrornet committed Jul 15, 2024
1 parent 3478c16 commit dbd985d
Show file tree
Hide file tree
Showing 16 changed files with 1,828 additions and 71 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/status-im/keycard-go v0.2.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tklauser/go-sysconf v0.3.13 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cA
github.com/status-im/keycard-go v0.2.0 h1:QDLFswOQu1r5jsycloeQh3bVU8n/NatHHaZobtDnDzA=
github.com/status-im/keycard-go v0.2.0/go.mod h1:wlp8ZLbsmrF6g6WjugPAx+IzoLrkdf9+mHxBEeo3Hbg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
3 changes: 2 additions & 1 deletion utils/bot/balance_on_chain/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package balance_on_chain

import (
"context"
"omni-balance/utils/bot"

"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
"github.com/sirupsen/logrus"
"omni-balance/utils/bot"
)

func init() {
Expand Down
86 changes: 86 additions & 0 deletions utils/bot/balance_on_chain/monitor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package balance_on_chain

import (
"context"
"omni-balance/utils/bot"
"omni-balance/utils/configs"
"omni-balance/utils/constant"
"omni-balance/utils/wallets/wallet_mocks"
"strconv"
"testing"

"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
)

func TestBalanceOnChain_Check(t *testing.T) {
testConf := &configs.Config{
Chains: []configs.Chain{
{
Id: 1,
Name: constant.Ethereum,
NativeToken: "ETH",
Tokens: []configs.Token{
{
ContractAddress: "0x0000000000000000000000000000000000000000",
Decimals: 18,
Name: "ETH",
},
},
},
},
Wallets: []configs.Wallet{
configs.Wallet{
Address: constant.ZeroAddress.Hex(),
Tokens: []configs.WalletToken{
configs.WalletToken{
Name: "ETH",
Amount: decimal.RequireFromString("1"),
Threshold: decimal.RequireFromString("1000"),
Chains: []string{constant.Ethereum},
},
},
},
},
}
testConf.Init()
b := new(BalanceOnChain)
w := wallet_mocks.NewWallets(t)
w.On("GetExternalBalance", context.Background(), constant.ZeroAddress, int32(18), nil).Return(decimal.RequireFromString("1000"), nil)
w.On("GetAddress").Return(constant.ZeroAddress)
tasks, Type, err := b.Check(context.Background(), bot.Params{
Conf: *testConf,
Info: bot.Config{
Wallet: w,
TokenName: "ETH",
Chain: constant.Ethereum,
},
})
assert.NoError(t, err)
assert.Equal(t, bot.Queue, Type)
assert.Len(t, tasks, 1)
assert.Equal(t, "1", tasks[0].Amount.String())
assert.Equal(t, constant.ZeroAddress.Hex(), tasks[0].Wallet)
assert.Equal(t, "ETH", tasks[0].TokenOutName)
assert.Equal(t, constant.Ethereum, tasks[0].TokenOutChainName)

w = wallet_mocks.NewWallets(t)
w.On("GetExternalBalance", context.Background(), constant.ZeroAddress, int32(18), nil).Return(decimal.RequireFromString("1"), nil)
w.On("GetAddress").Return(constant.ZeroAddress)
tasks, Type, err = b.Check(context.Background(), bot.Params{
Conf: *testConf,
Info: bot.Config{
Wallet: w,
TokenName: "ETH",
Chain: constant.Ethereum,
},
})
assert.NoError(t, err)
assert.Equal(t, bot.Queue, Type)
assert.Len(t, tasks, 1)
// 1000 + (1000 * 0.3)
assert.Equal(t, strconv.Itoa(1000+(1000*0.3)), tasks[0].Amount.String())
assert.Equal(t, constant.ZeroAddress.Hex(), tasks[0].Wallet)
assert.Equal(t, "ETH", tasks[0].TokenOutName)
assert.Equal(t, constant.Ethereum, tasks[0].TokenOutChainName)
}
7 changes: 5 additions & 2 deletions utils/bot/gate_liquidity/gate_liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ func init() {
}

type GateLiquidity struct {
balance_on_chain.BalanceOnChain
Bot bot.Bot
}

func (g GateLiquidity) Name() string {
return "gate_liquidity"
}

func (b GateLiquidity) Check(ctx context.Context, args bot.Params) ([]bot.Task, bot.ProcessType, error) {
tasks, processType, err := b.BalanceOnChain.Check(ctx, args)
if b.Bot == nil {
b.Bot = balance_on_chain.BalanceOnChain{}
}
tasks, processType, err := b.Bot.Check(ctx, args)
if err != nil {
return nil, processType, err
}
Expand Down
90 changes: 90 additions & 0 deletions utils/bot/gate_liquidity/gate_liquidity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package gate_liquidity

import (
"context"
"omni-balance/utils/bot"
"omni-balance/utils/bot_mocks"
"omni-balance/utils/configs"
"omni-balance/utils/constant"
"testing"

"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestGateLiquidity_Check(t *testing.T) {
testConf := &configs.Config{
Chains: []configs.Chain{
{
Id: 1,
Name: constant.Ethereum,
NativeToken: "ETH",
Tokens: []configs.Token{
{
ContractAddress: "0x0000000000000000000000000000000000000000",
Decimals: 18,
Name: "ETH",
},
},
},
},
Wallets: []configs.Wallet{
configs.Wallet{
Address: constant.ZeroAddress.Hex(),
BotTypes: []configs.BotConfig{
{
Name: "gate_liquidity",
TokenChains: map[string][]string{
"ETH": []string{constant.Ethereum},
},
Config: map[string]interface{}{
"toChain": constant.Arbitrum,
},
},
},
Tokens: []configs.WalletToken{
configs.WalletToken{

Name: "ETH",
Amount: decimal.RequireFromString("1"),
Threshold: decimal.RequireFromString("1000"),
Chains: []string{constant.Ethereum},
},
},
},
},
}
testConf.Init()
g := new(GateLiquidity)
b := bot_mocks.NewBot(t)
b.On("Check", mock.Anything, mock.Anything).Return(
[]bot.Task{
{
Wallet: constant.ZeroAddress.Hex(),
TokenInName: "ETH",
TokenOutName: "ETH",
TokenOutChainName: constant.Ethereum,
Amount: decimal.RequireFromString("1"),
},
},
bot.Queue,
nil,
)
g.Bot = b
tasks, Type, err := g.Check(context.Background(), bot.Params{
Conf: *testConf,
Info: bot.Config{
TokenName: "ETH",
Chain: constant.Ethereum,
},
})
assert.NoError(t, err)
assert.Equal(t, bot.Queue, Type)
assert.Len(t, tasks, 1)
assert.Equal(t, "1", tasks[0].Amount.String())
assert.Equal(t, constant.ZeroAddress.Hex(), tasks[0].Wallet)
assert.Equal(t, "ETH", tasks[0].TokenOutName)
assert.Equal(t, constant.Arbitrum, tasks[0].TokenOutChainName)
assert.Equal(t, constant.Ethereum, tasks[0].TokenInChainName)
}
15 changes: 14 additions & 1 deletion utils/bot/helix_liquidity/aave_debt.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ var (
},
},
},
constant.ArbitrumSepolia: AvaeConfig{
Chain: constant.ArbitrumSepolia,
DebtTokens: map[string]debtTokens{
"USDC": debtTokens{
Name: "USDC",
AToken: common.HexToAddress("0x460b97BD498E1157530AEb3086301d5225b91216"),
VToken: common.HexToAddress("0x4fBE3A94C60A5085dA6a2D309965DcF34c36711d"),
UnderlyingToken: common.HexToAddress("0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d"),
Decimals: 6,
},
},
},
}
)

Expand All @@ -80,14 +92,15 @@ type Aave struct {

func (a Aave) BalanceOf(ctx context.Context, args DebtParams) (decimal.Decimal, error) {
conf, ok := aaveAddressBook[args.Chain]
if !ok {
if !ok || conf.Chain == "" || conf.DebtTokens[args.Token].Name == "" {
return decimal.Zero, errors.Errorf("chain %s not support", args.Chain)
}
atokenBalance, err := chains.GetTokenBalance(ctx, args.Client, conf.DebtTokens[args.Token].AToken.Hex(),
args.Address.Hex(), conf.DebtTokens[args.Token].Decimals)
if err != nil {
return decimal.Zero, errors.Wrap(err, "get atoken balance error")
}

vtokenBalance, err := chains.GetTokenBalance(ctx, args.Client, conf.DebtTokens[args.Token].VToken.Hex(),
args.Address.Hex(), conf.DebtTokens[args.Token].Decimals)
if err != nil {
Expand Down
44 changes: 44 additions & 0 deletions utils/bot/helix_liquidity/aave_debt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package helix_liquidity

import (
"context"
"omni-balance/utils/chains"
"omni-balance/utils/chains/chain_mocks"
"omni-balance/utils/constant"
"omni-balance/utils/erc20"
"strconv"
"testing"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestAave_BalanceOf(t *testing.T) {
mockClient := chain_mocks.NewMockClient(t)

erc20Abi, err := erc20.TokenMetaData.GetAbi()
assert.NoError(t, err)
input, err := erc20Abi.Pack("balanceOf", constant.ZeroAddress)
assert.NoError(t, err)
token := common.HexToAddress("0x460b97BD498E1157530AEb3086301d5225b91216")
mockClient.On("CallContract", context.TODO(), ethereum.CallMsg{To: &token, Data: input}, mock.Anything).Return(
chains.EthToWei(decimal.RequireFromString("1000"), 6).Bytes(), nil,
)

vtoken := common.HexToAddress("0x4fBE3A94C60A5085dA6a2D309965DcF34c36711d")
mockClient.On("CallContract", context.TODO(), ethereum.CallMsg{To: &vtoken, Data: input}, mock.Anything).Return(
chains.EthToWei(decimal.RequireFromString("900"), 6).Bytes(), nil,
)

balance, err := new(Aave).BalanceOf(context.TODO(), DebtParams{
Address: constant.ZeroAddress,
Token: "USDC",
Client: mockClient,
Chain: constant.ArbitrumSepolia,
})
assert.NoError(t, err)
assert.Equal(t, balance.String(), strconv.Itoa(1000-900))
}
84 changes: 84 additions & 0 deletions utils/bot_mocks/bot_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dbd985d

Please sign in to comment.