-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
1,160 additions
and
809 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
local | ||
.run | ||
./config.yaml | ||
omni-balance.db | ||
start.sh |
This file contains 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 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 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 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 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,93 @@ | ||
package bot | ||
|
||
import ( | ||
"context" | ||
"github.com/ethereum/go-ethereum/ethclient/simulated" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
"omni-balance/internal/daemons/market" | ||
"omni-balance/utils/bot" | ||
"omni-balance/utils/bot/balance_on_chain" | ||
"omni-balance/utils/chains" | ||
"omni-balance/utils/configs" | ||
"sync" | ||
) | ||
|
||
func Run(ctx context.Context, conf configs.Config) error { | ||
existBuyTokens, err := getExistingBuyTokens() | ||
if err != nil { | ||
return errors.Wrap(err, "find buy tokens error") | ||
} | ||
var ( | ||
clients = make(map[string]simulated.Client) | ||
) | ||
for _, chain := range conf.Chains { | ||
client, err := chains.NewTryClient(ctx, chain.RpcEndpoints) | ||
if err != nil { | ||
return errors.Wrap(err, "create client error") | ||
} | ||
clients[chain.Name] = client | ||
} | ||
defer func() { | ||
for _, client := range clients { | ||
client.(*chains.Client).Close() | ||
} | ||
}() | ||
|
||
var ( | ||
ignoreTokens = createIgnoreTokens(existBuyTokens) | ||
w sync.WaitGroup | ||
) | ||
|
||
for _, wallet := range conf.Wallets { | ||
for _, token := range wallet.Tokens { | ||
for _, chainName := range token.Chains { | ||
if ignoreTokens.Contains(token.Name, chainName, wallet.Address) { | ||
logrus.Debugf("ignore token %s on chain %s", token.Name, chainName) | ||
continue | ||
} | ||
monitorType := token.MonitorTypes[chainName] | ||
if monitorType == "" { | ||
monitorType = balance_on_chain.BalanceOnChain{}.Name() | ||
} | ||
fn := process(ctx, conf, wallet.Address, token.Name, chainName, monitorType, clients[chainName]) | ||
w.Add(1) | ||
go func() { | ||
defer w.Done() | ||
tasks, processType, err := fn() | ||
if err != nil { | ||
logrus.Errorf("bot error: %s", err) | ||
return | ||
} | ||
_, taskId, err := createOrder(tasks, processType) | ||
if err != nil { | ||
logrus.Errorf("create order error: %s", err) | ||
return | ||
} | ||
market.PushTask(market.Task{ | ||
Id: taskId, | ||
ProcessType: processType, | ||
}) | ||
}() | ||
} | ||
} | ||
} | ||
w.Wait() | ||
return nil | ||
} | ||
|
||
func process(ctx context.Context, conf configs.Config, walletAddress, tokenName, chainName, monitorType string, | ||
client simulated.Client) func() ([]bot.Task, bot.ProcessType, error) { | ||
m := bot.GetMonitor(monitorType) | ||
return func() ([]bot.Task, bot.ProcessType, error) { | ||
return m.Check(ctx, bot.Params{ | ||
Conf: conf, | ||
Info: bot.Config{ | ||
Wallet: conf.GetWallet(walletAddress), | ||
TokenName: tokenName, | ||
Chain: chainName, | ||
}, | ||
Client: client, | ||
}) | ||
} | ||
} |
This file contains 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,15 @@ | ||
package bot | ||
|
||
import ( | ||
"omni-balance/internal/daemons" | ||
"time" | ||
) | ||
|
||
func init() { | ||
daemons.RegisterIntervalTask(daemons.Task{ | ||
Name: "bot", | ||
Description: "Check the balance based on the specific bot and create an order", | ||
TaskFunc: Run, | ||
DefaultInterval: time.Minute * 3, | ||
}) | ||
} |
This file contains 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,71 @@ | ||
package bot | ||
|
||
import ( | ||
uuid "github.com/satori/go.uuid" | ||
"omni-balance/internal/db" | ||
"omni-balance/internal/models" | ||
"omni-balance/utils" | ||
"omni-balance/utils/bot" | ||
"omni-balance/utils/provider" | ||
) | ||
|
||
func getExistingBuyTokens() ([]*models.Order, error) { | ||
var existBuyTokens []*models.Order | ||
err := db.DB().Where("status != ? ", provider.TxStatusSuccess).Find(&existBuyTokens).Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
return existBuyTokens, nil | ||
} | ||
|
||
func createIgnoreTokens(existBuyTokens []*models.Order) IgnoreTokens { | ||
var ignoreTokens []IgnoreToken | ||
for _, v := range existBuyTokens { | ||
ignoreTokens = append(ignoreTokens, IgnoreToken{ | ||
Name: v.TokenOutName, | ||
Chain: v.TargetChainName, | ||
Address: v.Wallet, | ||
}) | ||
} | ||
return ignoreTokens | ||
} | ||
|
||
func createOrder(tasks []bot.Task, processType bot.ProcessType) (orders []*models.Order, taskId string, err error) { | ||
if len(tasks) == 0 { | ||
return | ||
} | ||
var ( | ||
txn = db.DB().Begin() | ||
) | ||
|
||
taskId = uuid.NewV4().String() | ||
|
||
for _, v := range tasks { | ||
o := &models.Order{ | ||
Wallet: v.Wallet, | ||
TokenInName: v.TokenInName, | ||
TokenOutName: v.TokenOutName, | ||
SourceChainName: v.TokenInChainName, | ||
TargetChainName: v.TokenOutChainName, | ||
CurrentChainName: v.CurrentChainName, | ||
Amount: v.Amount, | ||
Status: v.Status, | ||
ProviderType: v.ProviderType, | ||
ProviderName: v.ProviderName, | ||
Order: utils.Object2JsonRawMessage(v.Order), | ||
TaskId: taskId, | ||
ProcessType: string(processType), | ||
} | ||
|
||
if err = txn.Create(o).Error; err != nil { | ||
txn.Rollback() | ||
return | ||
} | ||
orders = append(orders, o) | ||
} | ||
if err = txn.Commit().Error; err != nil { | ||
txn.Rollback() | ||
return | ||
} | ||
return | ||
} |
This file contains 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,25 @@ | ||
package bot | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type IgnoreTokens []IgnoreToken | ||
|
||
type IgnoreToken struct { | ||
Name string `json:"name"` | ||
Chain string `json:"chain"` | ||
Address string `json:"wallet"` | ||
} | ||
|
||
func (i IgnoreTokens) Contains(name, chain, address string) bool { | ||
key := fmt.Sprintf("%s_%s_%s", name, chain, address) | ||
for _, token := range i { | ||
if !strings.EqualFold(key, fmt.Sprintf("%s_%s_%s", token.Name, token.Chain, token.Address)) { | ||
continue | ||
} | ||
return true | ||
} | ||
return false | ||
} |
This file contains 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,16 @@ | ||
package market | ||
|
||
import ( | ||
"omni-balance/internal/daemons" | ||
"time" | ||
) | ||
|
||
func init() { | ||
daemons.RegisterIntervalTask(daemons.Task{ | ||
Name: "market", | ||
Description: "Look for tasks in the database that are not being processed and process them.", | ||
TaskFunc: Run, | ||
DefaultInterval: time.Minute * 3, | ||
RunOnStart: true, | ||
}) | ||
} |
Oops, something went wrong.