Skip to content

Commit

Permalink
feat: mint honey
Browse files Browse the repository at this point in the history
  • Loading branch information
tiennampham23 committed Apr 21, 2024
1 parent c86c575 commit c344b54
Show file tree
Hide file tree
Showing 12 changed files with 632 additions and 64 deletions.
3 changes: 2 additions & 1 deletion pkg/dapp/bex/add_liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
dappCommon "github.com/tiennampham23/berachain-airdrop/pkg/dapp/common"
"github.com/tiennampham23/berachain-airdrop/pkg/log"
"github.com/tiennampham23/berachain-airdrop/pkg/utilties/eth"
"math/big"
Expand All @@ -22,7 +23,7 @@ func AddLiquidityForSingleToken(
from, _ = eth.GetAddressFromPrivateKey(privateKey)
)

err := ApproveIfNeed(c, privateKey, tokenIn, amount)
err := dappCommon.ApproveIfNeed(c, privateKey, tokenIn, amount, Erc20ModuleContract)
if err != nil {
log.Logger().Errorw("Approve failed", "err", err)
return nil, err
Expand Down
57 changes: 0 additions & 57 deletions pkg/dapp/bex/bex.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package bex

import (
"context"
"crypto/ecdsa"
"errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/go-resty/resty/v2"
"github.com/tiennampham23/berachain-airdrop/pkg/log"
"github.com/tiennampham23/berachain-airdrop/pkg/utilties/erc20"
"github.com/tiennampham23/berachain-airdrop/pkg/utilties/eth"
"math/big"
"time"
)
Expand All @@ -34,53 +27,3 @@ var (
func init() {
client = resty.New().SetTimeout(10 * time.Second)
}

func ApproveIfNeed(c *ethclient.Client, privateKey *ecdsa.PrivateKey, tokenIn common.Address, amount *big.Int) error {
var (
from, _ = eth.GetAddressFromPrivateKey(privateKey)
)
balance, err := erc20.BalanceOf(c, from, tokenIn)
if err != nil {
log.Logger().Errorw("Failed to get balance", "error", err)
return err
}

if balance.Cmp(amount) < 0 {
return errors.New("insufficient balance")
}
allowance, err := erc20.Allowance(c, from, Erc20ModuleContract, tokenIn)
if err != nil {
return err
}

if allowance.Cmp(amount) < 0 {
approvePayload, err := erc20.Approve(Erc20ModuleContract, ApproveMax)
if err != nil {
log.Logger().Errorw("Failed to approve payload", "error", err)
return err
}

tx, err := eth.SendTx(c, privateKey, approvePayload, nil, tokenIn)
if err != nil {
log.Logger().Errorw("Failed to send approval tx", "error", err)
return err
}
log.Logger().Infow("Approve tx", "tx", tx.Hash())

for {
_, isPending, err := c.TransactionByHash(context.Background(), tx.Hash())
if err != nil {
log.Logger().Errorw("Failed to get approve transaction by hash", "error", err)
time.Sleep(5 * time.Second)
continue
}
if isPending {
time.Sleep(5 * time.Second)
continue
}
break
}
}

return nil
}
3 changes: 2 additions & 1 deletion pkg/dapp/bex/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
dappCommon "github.com/tiennampham23/berachain-airdrop/pkg/dapp/common"
"github.com/tiennampham23/berachain-airdrop/pkg/log"
"github.com/tiennampham23/berachain-airdrop/pkg/utilties/eth"
"math/big"
Expand Down Expand Up @@ -109,7 +110,7 @@ func Swap(
return nil, errors.New("insufficient balance")
}
} else {
err := ApproveIfNeed(c, privateKey, tokenIn, amount)
err := dappCommon.ApproveIfNeed(c, privateKey, tokenIn, amount, Erc20ModuleContract)
if err != nil {
log.Logger().Errorw("Approve failed", "err", err)
return nil, err
Expand Down
68 changes: 68 additions & 0 deletions pkg/dapp/common/approve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package common

import (
"context"
"crypto/ecdsa"
"errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/tiennampham23/berachain-airdrop/pkg/log"
"github.com/tiennampham23/berachain-airdrop/pkg/utilties/erc20"
"github.com/tiennampham23/berachain-airdrop/pkg/utilties/eth"
"math/big"
"time"
)

var (
ApproveMax, _ = new(big.Int).SetString("57896044618658097711785492504343953926634992332820282019728792003956564819967", 10)
)

func ApproveIfNeed(c *ethclient.Client, privateKey *ecdsa.PrivateKey, tokenIn common.Address, amount *big.Int, contract common.Address) error {
var (
from, _ = eth.GetAddressFromPrivateKey(privateKey)
)
balance, err := erc20.BalanceOf(c, from, tokenIn)
if err != nil {
log.Logger().Errorw("Failed to get balance", "error", err)
return err
}

if balance.Cmp(amount) < 0 {
return errors.New("insufficient balance")
}
allowance, err := erc20.Allowance(c, from, contract, tokenIn)
if err != nil {
return err
}

if allowance.Cmp(amount) < 0 {
approvePayload, err := erc20.Approve(contract, ApproveMax)
if err != nil {
log.Logger().Errorw("Failed to approve payload", "error", err)
return err
}

tx, err := eth.SendTx(c, privateKey, approvePayload, nil, tokenIn)
if err != nil {
log.Logger().Errorw("Failed to send approval tx", "error", err)
return err
}
log.Logger().Infow("Approve tx", "tx", tx.Hash())

for {
_, isPending, err := c.TransactionByHash(context.Background(), tx.Hash())
if err != nil {
log.Logger().Errorw("Failed to get approve transaction by hash", "error", err)
time.Sleep(5 * time.Second)
continue
}
if isPending {
time.Sleep(5 * time.Second)
continue
}
break
}
}

return nil
}
1 change: 1 addition & 0 deletions pkg/dapp/honey/gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abigen --abi=honey_abi.json --pkg=honey --out=honey_generated.go -type HoneyGenerated
37 changes: 37 additions & 0 deletions pkg/dapp/honey/honey_abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[{"inputs": [{"internalType": "contract IERC20", "name": "_honey", "type": "address"}],
"stateMutability": "nonpayable", "type": "constructor"}, {"inputs": [], "name": "erc20Module",
"outputs": [
{"internalType": "contract IERC20Module",
"name": "", "type": "address"}],
"stateMutability": "view", "type": "function"},
{"inputs": [], "name": "getExchangable", "outputs": [{"components": [
{"internalType": "contract IERC20", "name": "collateral", "type": "address"},
{"internalType": "bool", "name": "enabled", "type": "bool"},
{"internalType": "uint256", "name": "mintRate", "type": "uint256"},
{"internalType": "uint256", "name": "redemptionRate", "type": "uint256"}],
"internalType": "struct ERC20Honey.ERC20Exchangable[]",
"name": "", "type": "tuple[]"}],
"stateMutability": "nonpayable", "type": "function"}, {"inputs": [], "name": "honey", "outputs": [
{"internalType": "contract IERC20", "name": "", "type": "address"}], "stateMutability": "view",
"type": "function"},
{"inputs": [], "name": "honeyModule",
"outputs": [{"internalType": "contract IHoneyModule", "name": "", "type": "address"}],
"stateMutability": "view", "type": "function"}, {
"inputs": [{"internalType": "address", "name": "to", "type": "address"},
{"internalType": "contract IERC20", "name": "collateral", "type": "address"},
{"internalType": "uint256", "name": "amount", "type": "uint256"}], "name": "mint",
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
"stateMutability": "nonpayable", "type": "function"}, {
"inputs": [{"internalType": "contract IERC20", "name": "collateral", "type": "address"},
{"internalType": "uint256", "name": "amount", "type": "uint256"}], "name": "previewMint",
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "stateMutability": "view",
"type": "function"}, {
"inputs": [{"internalType": "contract IERC20", "name": "collateral", "type": "address"},
{"internalType": "uint256", "name": "amount", "type": "uint256"}], "name": "previewRedeem",
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "stateMutability": "view",
"type": "function"}, {"inputs": [{"internalType": "address", "name": "to", "type": "address"},
{"internalType": "uint256", "name": "amount", "type": "uint256"},
{"internalType": "contract IERC20", "name": "collateral",
"type": "address"}], "name": "redeem",
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
"stateMutability": "nonpayable", "type": "function"}]
Loading

0 comments on commit c344b54

Please sign in to comment.