Skip to content

Commit

Permalink
Update wbtc and weth handling
Browse files Browse the repository at this point in the history
  • Loading branch information
martonp committed Nov 27, 2023
1 parent 2b1feea commit 92301e7
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 41 deletions.
4 changes: 2 additions & 2 deletions client/asset/polygon/polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ const (
var (
simnetTokenID, _ = dex.BipSymbolID("dextt.polygon")
usdcTokenID, _ = dex.BipSymbolID("usdc.polygon")
wethTokenID, _ = dex.BipSymbolID("eth.polygon")
wbtcTokenID, _ = dex.BipSymbolID("btc.polygon")
wethTokenID, _ = dex.BipSymbolID("weth.polygon")
wbtcTokenID, _ = dex.BipSymbolID("wbtc.polygon")
// WalletInfo defines some general information about a Polygon Wallet(EVM
// Compatible).

Expand Down
109 changes: 80 additions & 29 deletions client/mm/libxc/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,51 @@ type bncAssetConfig struct {
}

// TODO: check all symbols
func mapDEXSymbolToBinance(symbol string) string {
if symbol == "POLYGON" {
return "MATIC"
var dexToBinanceSymbol = map[string]string{
"POLYGON": "MATIC",
"WETH": "ETH",
}

var binanceToDexSymbol = make(map[string]string)

func init() {
for key, value := range dexToBinanceSymbol {
binanceToDexSymbol[value] = key
}
}

func mapDexToBinanceSymbol(symbol string) string {
if binanceSymbol, found := dexToBinanceSymbol[symbol]; found {
return binanceSymbol
}
return symbol
}

func binanceCoinNetworkToDexSymbol(coin, network string) string {
if coin == "ETH" && network == "ETH" {
return "eth"
}

var dexSymbol, dexNetwork string
if symbol, found := binanceToDexSymbol[coin]; found {
dexSymbol = strings.ToLower(symbol)
} else {
dexSymbol = strings.ToLower(coin)
}

if symbol, found := binanceToDexSymbol[network]; found && network != "ETH" {
dexNetwork = strings.ToLower(symbol)
} else {
dexNetwork = strings.ToLower(network)
}

if dexSymbol == dexNetwork {
return dexSymbol
}

return fmt.Sprintf("%s.%s", dexSymbol, dexNetwork)
}

func bncAssetCfg(assetID uint32) (*bncAssetConfig, error) {
symbol := dex.BipIDSymbol(assetID)
if symbol == "" {
Expand All @@ -97,8 +135,7 @@ func bncAssetCfg(assetID uint32) (*bncAssetConfig, error) {

coin := strings.ToUpper(symbol)
chain := strings.ToUpper(symbol)
if token := asset.TokenInfo(assetID); token != nil {
parts := strings.Split(symbol, ".")
if parts := strings.Split(symbol, "."); len(parts) > 1 {
coin = strings.ToUpper(parts[0])
chain = strings.ToUpper(parts[1])
}
Expand All @@ -110,8 +147,8 @@ func bncAssetCfg(assetID uint32) (*bncAssetConfig, error) {

return &bncAssetConfig{
symbol: symbol,
coin: mapDEXSymbolToBinance(coin),
chain: mapDEXSymbolToBinance(chain),
coin: mapDexToBinanceSymbol(coin),
chain: mapDexToBinanceSymbol(chain),
conversionFactor: ui.Conventional.ConversionFactor,
}, nil
}
Expand Down Expand Up @@ -226,6 +263,12 @@ func (bnc *binance) getCoinInfo(ctx context.Context) error {

tokenIDs := make(map[string][]uint32)
for _, nfo := range coins {
if nfo.Coin == "WBTC" {
bnc.log.Infof("WBTC INFO: %+v", nfo)
for _, netInfo := range nfo.NetworkList {
bnc.log.Infof("%+v", netInfo)
}
}
tokenSymbol := strings.ToLower(nfo.Coin)
chainIDs, isToken := dex.TokenChains[tokenSymbol]
if !isToken {
Expand Down Expand Up @@ -523,6 +566,8 @@ func (bnc *binance) Withdraw(ctx context.Context, assetID uint32, qty uint64, ad
return false, 0, ""
}

bnc.log.Tracef("Withdrawal status: %+v", status)

amt := status.Amount * float64(assetCfg.conversionFactor)
return status.Status == 6, uint64(amt), status.TxID
}
Expand Down Expand Up @@ -576,45 +621,52 @@ func (bnc *binance) ConfirmDeposit(ctx context.Context, txID string, onConfirm f
waitingUserConfirmStatus = 8
)

checkDepositStatus := func() (success, done bool) {
var resp []*struct {
Amount float64 `json:"amount,string"`
Coin string `json:"coin"`
Network string `json:"network"`
Status int `json:"status"`
Address string `json:"address"`
AddressTag string `json:"addressTag"`
TxID string `json:"txId"`
InsertTime int64 `json:"insertTime"`
TransferType int `json:"transferType"`
ConfirmTimes string `json:"confirmTimes"`
checkDepositStatus := func() (success, done bool, amt uint64) {
var resp []struct {
Amount float64 `json:"amount,string"`
Coin string `json:"coin"`
Network string `json:"network"`
Status int `json:"status"`
TxID string `json:"txId"`
}
err := bnc.getAPI(ctx, "/sapi/v1/capital/deposit/hisrec", nil, true, true, resp)
err := bnc.getAPI(ctx, "/sapi/v1/capital/deposit/hisrec", nil, true, true, &resp)
if err != nil {
bnc.log.Errorf("error getting deposit status: %v", err)
return false, false
return false, false, 0
}

for _, status := range resp {
if status.TxID == txID {
switch status.Status {
case successStatus, creditedStatus:
return true, true
dexSymbol := binanceCoinNetworkToDexSymbol(status.Coin, status.Network)
assetID, found := dex.BipSymbolID(dexSymbol)
if !found {
bnc.log.Errorf("Failed to find DEX asset ID for Coin: %s, Network %s", status.Coin, status.Network)
return false, true, 0
}
ui, err := asset.UnitInfo(assetID)
if err != nil {
bnc.log.Errorf("Failed to find unit info for asset ID %d", assetID)
return false, true, 0
}
amount := uint64(status.Amount * float64(ui.Conventional.ConversionFactor))
return true, true, amount
case pendingStatus:
return false, false
return false, false, 0
case waitingUserConfirmStatus:
// This shouldn't ever happen.
bnc.log.Errorf("Deposit %s to binance requires user confirmation!")
return false, false
return false, true, 0
case wrongDepositStatus:
return false, true
return false, true, 0
default:
bnc.log.Errorf("Deposit %s to binance has an unknown status %d", status.Status)
}
}
}

return false, false
return false, false, 0
}

go func() {
Expand All @@ -626,10 +678,9 @@ func (bnc *binance) ConfirmDeposit(ctx context.Context, txID string, onConfirm f
case <-ctx.Done():
return
case <-ticker.C:
success, done := checkDepositStatus()
success, done, amt := checkDepositStatus()
if done {
// TODO: get amount
onConfirm(success, 0)
onConfirm(success, amt)
return
}
}
Expand Down
35 changes: 33 additions & 2 deletions client/mm/libxc/binance_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ func init() {
},
},
}, &asset.WalletDefinition{}, dex.Mainnet, dex.Testnet, dex.Simnet)
asset.RegisterToken(966002, &dex.Token{
ParentID: 966,
Name: "WETH",
UnitInfo: dex.UnitInfo{
Conventional: dex.Denomination{
ConversionFactor: 1e9,
},
},
}, &asset.WalletDefinition{}, dex.Mainnet, dex.Testnet, dex.Simnet)
}

func TestConnect(t *testing.T) {
Expand Down Expand Up @@ -235,7 +244,7 @@ func TestVWAP(t *testing.T) {
}

func TestWithdrawal(t *testing.T) {
bnc := tNewBinance(t, dex.Testnet)
bnc := tNewBinance(t, dex.Mainnet)
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()

Expand All @@ -251,7 +260,7 @@ func TestWithdrawal(t *testing.T) {
wg.Done()
}

err = bnc.Withdraw(ctx, 60001, 4e10, "", onComplete)
err = bnc.Withdraw(ctx, 966, 2e10, "", onComplete)
if err != nil {
fmt.Printf("withdrawal error: %v", err)
return
Expand All @@ -260,6 +269,28 @@ func TestWithdrawal(t *testing.T) {
wg.Wait()
}

func TestConfirmDeposit(t *testing.T) {
bnc := tNewBinance(t, dex.Mainnet)
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
defer cancel()

_, err := bnc.Connect(ctx)
if err != nil {
t.Fatalf("Connect error: %v", err)
}

wg := sync.WaitGroup{}
wg.Add(1)
onComplete := func(success bool, amount uint64) {
t.Logf("deposit complete: %v, %v", success, amount)
wg.Done()
}

bnc.ConfirmDeposit(ctx, "", onComplete)

wg.Wait()
}

func TestGetDepositAddress(t *testing.T) {
bnc := tNewBinance(t, dex.Mainnet)
ctx, cancel := context.WithTimeout(context.Background(), time.Hour*23)
Expand Down
19 changes: 19 additions & 0 deletions client/mm/libxc/binance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,22 @@ func TestSubscribeTradeUpdates(t *testing.T) {
t.Fatalf("id2 not found")
}
}

func TestBinanceToDexSymbol(t *testing.T) {
tests := map[[2]string]string{
{"ETH", "ETH"}: "eth",
{"ETH", "MATIC"}: "weth.polygon",
{"MATIC", "MATIC"}: "polygon",
{"USDC", "ETH"}: "usdc.eth",
{"USDC", "MATIC"}: "usdc.polygon",
{"BTC", "BTC"}: "btc",
{"WBTC", "ETH"}: "wbtc.eth",
}

for test, expected := range tests {
dexSymbol := binanceCoinNetworkToDexSymbol(test[0], test[1])
if expected != dexSymbol {
t.Fatalf("expected %s but got %v", expected, dexSymbol)
}
}
}
3 changes: 1 addition & 2 deletions client/webserver/site/src/js/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ const BipIDs: Record<number, string> = {
966000: 'dextt.polygon',
966001: 'usdc.polygon',
966002: 'weth.polygon',
966003: 'wbtc.polygon',
147: 'zcl'
966003: 'wbtc.polygon'
}

const BipSymbolIDs: Record<string, number> = {};
Expand Down
4 changes: 2 additions & 2 deletions dex/bip-id.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,8 @@ var bipIDs = map[uint32]string{
// Polygon reserved token range 966000-966999
966000: "dextt.polygon",
966001: "usdc.polygon",
966002: "eth.polygon",
966003: "btc.polygon",
966002: "weth.polygon",
966003: "wbtc.polygon",
// END Polygon reserved token range
1171337: "ilt",
1313114: "etho",
Expand Down
4 changes: 2 additions & 2 deletions dex/networks/polygon/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ var (

testTokenID, _ = dex.BipSymbolID("dextt.polygon")
usdcTokenID, _ = dex.BipSymbolID("usdc.polygon")
wethTokenID, _ = dex.BipSymbolID("eth.polygon")
wbtcTokenID, _ = dex.BipSymbolID("btc.polygon")
wethTokenID, _ = dex.BipSymbolID("weth.polygon")
wbtcTokenID, _ = dex.BipSymbolID("wbtc.polygon")

Tokens = map[uint32]*dexeth.Token{
testTokenID: TestToken,
Expand Down
4 changes: 2 additions & 2 deletions server/asset/polygon/polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ const (
var (
testTokenID, _ = dex.BipSymbolID("dextt.polygon")
usdcID, _ = dex.BipSymbolID("usdc.polygon")
wethTokenID, _ = dex.BipSymbolID("eth.polygon")
wbtcTokenID, _ = dex.BipSymbolID("btc.polygon")
wethTokenID, _ = dex.BipSymbolID("weth.polygon")
wbtcTokenID, _ = dex.BipSymbolID("wbtc.polygon")

// blockPollInterval is the delay between calls to bestBlockHash to check
// for new blocks. Modify at compile time via blockPollIntervalStr:
Expand Down

0 comments on commit 92301e7

Please sign in to comment.