Skip to content

Commit

Permalink
Merge pull request #2 from zarbanio/update-dex-trader-deploy-script
Browse files Browse the repository at this point in the history
chore: update dex trader deploy script
  • Loading branch information
amintalebi authored Aug 27, 2024
2 parents 959061d + 78189cc commit bd1dfa3
Show file tree
Hide file tree
Showing 13 changed files with 392 additions and 132 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Edit `configs/local/config.yaml` and set the necessary values.

### Run Locally:
```
go run main.go server --config=configs/local/config.yaml
go run main.go run --config=configs/local/config.yaml
```

## Contributing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ MarketMaker:
Slippage: 0.001 # 0.001 is 0.1%

Chain:
Url: ${MARKET_MAKER_KEEPER_RPC}
Url: "YOUR_ARBITRUM_CHAIN_URL"
BlockInterval: 500ms

Tokens:
Expand All @@ -28,28 +28,26 @@ Uniswap:

Nobitex:
Url: "https://api.nobitex.ir"
Key: ${NOBITEX_API_KEY}
Key: "YOUR_NOBITEX_API_KEY"
MinimumOrderToman: 300_000
Timeout: 60s
OrderStatusInterval: 2s
RetryTimeOut: 360s
RetrySleepDuration: 5s

ExecutorWallet:
PrivateKey: ${EXECUTOR_PRIVATE_KEY}

Indexer:
StartBlock: ${DEX_TRADER_START_BLOCK}
StartBlock: 123

Contracts:
DexTrader: "0xb993318B8af82DbA6D30B8a459c954Cb9550714b"
DexTrader: "YOUR_DEX_TRADER_CONTRACT_ADDRESS"
UniswapV3Factory: "0x1F98431c8aD98523631AE4a59f267346ea31F984"
UniswapV3Quoter: "0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6"

Postgres:
Host: ${ZARBAN_MAINNET_DB_HOST}
Port: ${ZARBAN_MAINNET_DB_PORT}
User: ${ZARBAN_MAINNET_DB_USER}
Password: ${ZARBAN_MAINNET_DB_PASS}
DB: ${DB_NAME}
Host: "localhost"
Port: 5432
User: "postgres"
Password: "postgres"
DB: "postgres"
MigrationsPath: '/migrations'
125 changes: 125 additions & 0 deletions configs/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package configs

import (
"log"
"time"

"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
)

const (
EnvironmentMainnet = "mainnet"
EnvironmentTestnet = "testnet"
)

type General struct {
Environment string `yaml:"Environment"`
LogLevel string `yaml:"LogLevel"`
}

type MarketMaker struct {
StartQty float64 `yaml:"StartQty"`
StepQty float64 `yaml:"StepQty"`
EndQty int64 `yaml:"EndQty"`
ProfitThreshold int64 `yaml:"ProfitThreshold"`
Interval time.Duration `yaml:"Interval"`
Slippage float64 `yaml:"Slippage"`
}

type Chain struct {
Url string `yaml:"Url"`
BlockInterval time.Duration `yaml:"BlockInterval"`
}

type Token struct {
Address string `yaml:"Address"`
Decimals int `yaml:"Decimals"`
Symbol string `yaml:"Symbol"`
}

type Uniswap struct {
PoolFee float64 `yaml:"PoolFee"`
}

type Nobitex struct {
Url string `yaml:"Url"`
Key string `yaml:"Key"`
MinimumOrderToman int64 `yaml:"MinimumOrderToman"`
Timeout time.Duration `yaml:"Timeout"`
OrderStatusInterval time.Duration `yaml:"OrderStatusInterval"`
RetryTimeOut time.Duration `yaml:"RetryTimeOut"`
RetrySleepDuration time.Duration `yaml:"RetrySleepDuration"`
}

type Contracts struct {
DexTrader string `yaml:"DexTrader"`
UniswapV3Factory string `yaml:"UniswapV3Factory"`
UniswapV3Quoter string `yaml:"UniswapV3Quoter"`
}

type Indexer struct {
StartBlock uint64 `yaml:"StartBlock"`
}

type Postgres struct {
Host string `yaml:"Host"`
Port int `yaml:"Port"`
User string `yaml:"User"`
Password string `yaml:"Password"`
DB string `yaml:"DB"`
MigrationsPath string `yaml:"MigrationsPath"`
}

type Config struct {
General General `yaml:"General"`
MarketMaker MarketMaker `yaml:"MarketMaker"`
Chain Chain `yaml:"Chain"`
Tokens []Token `yaml:"Tokens"`
Uniswap Uniswap `yaml:"Uniswap"`
Nobitex Nobitex `yaml:"Nobitex"`
Contracts Contracts `yaml:"Contracts"`
Indexer Indexer `yaml:"Indexer"`
Postgres Postgres `yaml:"Postgres"`
}

func ReadConfig(configFile string) Config {
defaultConfig := DefaultConfig()

c := &Config{}
*c = defaultConfig

err := c.Unmarshal(c, configFile)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
return *c
}

func (c *Config) Unmarshal(rawVal interface{}, fileName string) error {
viper.SetConfigFile(fileName)
err := viper.ReadInConfig()
if err != nil {
return err
}
var input interface{} = viper.AllSettings()
config := defaultDecoderConfig(rawVal)
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(input)
}

func defaultDecoderConfig(output interface{}) *mapstructure.DecoderConfig {
c := &mapstructure.DecoderConfig{
Metadata: nil,
Result: output,
WeaklyTypedInput: true,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
),
}
return c
}
8 changes: 8 additions & 0 deletions configs/config.minimal.sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Chain:
Url: "YOUR_ARBITRUM_CHAIN_URL"

Nobitex:
Key: "YOUR_NOBITEX_API_KEY"

Contracts:
DexTrader: "YOUR_DEX_TRADER_CONTRACT_ADDRESS"
64 changes: 64 additions & 0 deletions configs/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package configs

import (
"time"
)

func DefaultConfig() Config {
return Config{
General: General{
Environment: EnvironmentMainnet,
LogLevel: "info",
},
MarketMaker: MarketMaker{
StartQty: 10.0,
StepQty: 20.0,
EndQty: 400, // max trade DAI in strategy0 and strategy1
ProfitThreshold: 50000, // 50_000 TMN
Interval: time.Minute * 10,
Slippage: 0.001,
},
Chain: Chain{
BlockInterval: time.Millisecond * 500,
},
Tokens: []Token{
{
Address: "0xd946188a614a0d9d0685a60f541bba1e8cc421ae",
Decimals: 18,
Symbol: "ZAR",
},
{
Address: "0xda10009cbd5d07dd0cecc66161fc93d7c9000da1",
Decimals: 18,
Symbol: "DAI",
},
},
Uniswap: Uniswap{
PoolFee: 0.01,
},
Nobitex: Nobitex{
Url: "https://api.nobitex.ir",
Key: "", // Assuming no default value for Key
MinimumOrderToman: 300000,
Timeout: time.Second * 60, // 60s
OrderStatusInterval: time.Second * 2, // 2s
RetryTimeOut: time.Second * 360, // 360s
RetrySleepDuration: time.Second * 5, // 5s
},
Contracts: Contracts{
UniswapV3Factory: "0x1F98431c8aD98523631AE4a59f267346ea31F984",
UniswapV3Quoter: "0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6",
},
Indexer: Indexer{
StartBlock: 247010149,
},
Postgres: Postgres{
Host: "localhost",
Port: 5432,
User: "postgres",
Password: "postgres",
DB: "postgres",
MigrationsPath: "/migrations",
},
}
}
26 changes: 25 additions & 1 deletion dextrader/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
require("@nomicfoundation/hardhat-toolbox");

/** @type import('hardhat/config').HardhatUserConfig */
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const ARBISCAN_KEY = process.env.ARBISCAN_KEY;

module.exports = {
solidity: {
version: "0.8.17",
settings: {
optimizer: { enabled: true, runs: 200 },
},
},
etherscan: {
apiKey: {
arb: ARBISCAN_KEY,
},
customChains: [
{
network: "arb",
chainId: 42161,
urls: {
apiURL: "https://api.arbiscan.io/api",
browserURL: "https://arbiscan.io",
},
},
],
},
networks: {
arb: {
url: "https://1rpc.io/arb",
chainId: 42161,
accounts: [PRIVATE_KEY],
},
},
};
11 changes: 10 additions & 1 deletion dextrader/package-lock.json

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

3 changes: 2 additions & 1 deletion dextrader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@openzeppelin/contracts": "^4.7.3",
"@uniswap/v3-periphery": "^1.4.3",
"bignumber.js": "^9.1.1",
"dotenv": "^16.0.3"
"dotenv": "^16.0.3",
"qrcode-terminal": "^0.12.0"
}
}
28 changes: 28 additions & 0 deletions dextrader/scripts/account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { ethers } = require("hardhat");
const qrcode = require('qrcode-terminal'); // Import the qrcode-terminal package

require('dotenv').config();

async function main() {
const [executor] = await ethers.getSigners();
console.log(`Deploying contracts with the account: ${executor.address}`);
console.log(`Account balance: ${ethers.utils.formatEther(await executor.getBalance())} ETH`);

// Generate and display the QR code for the address
qrcode.generate(executor.address, { small: true }, (qrCode) => {
console.log('\nAccount Address QR Code:\n');
console.log(qrCode);
});

console.log('\nCaution!');
console.log('To fund this account, transfer ETH to the following address on the Arbitrum network:');
console.log(`\n${executor.address}\n`);
console.log('Scan the QR code above with your wallet to send ETH.');
console.log('Ensure you are connected to the Arbitrum network when making the transfer.');
console.log('Do not send ETH to this address on any other network.');
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
25 changes: 25 additions & 0 deletions dextrader/scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { ethers } = require("hardhat");
const { verifyArbiscanContract } = require("./verify");

require('dotenv').config()

const UniswapV3Router = '0xE592427A0AEce92De3Edee1F18E0157C05861564';

async function main() {
const [executor] = await ethers.getSigners();
console.log(`Deploying contracts with the account: ${executor.address}`);
console.log(`Account balance: ${ethers.utils.formatEther(await executor.getBalance())} ETH`);

const DexTrader = await ethers.getContractFactory("DexTrader");
const dexTrader = await DexTrader.deploy(UniswapV3Router, executor.address);

await dexTrader.deployed();
console.log(`DexTrader contract deployed to ${dexTrader.address}`);

await verifyArbiscanContract(dexTrader.address, [UniswapV3Router, executor.address])
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Loading

0 comments on commit bd1dfa3

Please sign in to comment.