Giter VIP home page Giter VIP logo

4swap-sdk-go's Introduction

4swap SDK go

4swap

4swap is a decentralized protocol implement for automated liquidity provision on Mixin Network

Install

go get github.com/fox-one/4swap-sdk-go/v2

Authorization

4swap supports two kinds of access tokens:

  1. the access token that complete the OAuth flow at 4swap's webpage: https://app.4swap.org
  2. the access token that generated by your own Mixn Application. The token should sign the URL /me and the scope should be "FULL". Please read this document for more details.

Example

func TestPreOrder(t *testing.T) {
    ctx := context.Background()

    c := New()
    c.UseToken("your auth token")
    
    pairs, err := c.ListPairs(ctx)
    if err != nil {
        t.Fatal(err)
    }
    
    req := &PreOrderReq{
        PayAssetID:  "4d8c508b-91c5-375b-92b0-ee702ed2dac5",
        FillAssetID: "31d2ea9c-95eb-3355-b65b-ba096853bc18",
        PayAmount:   decimal.NewFromFloat(0.1),
    }
    
    preOrder, err := PreOrderWithPairs(pairs, req)
    if err != nil {
        t.Fatal(err)
    }
    
    t.Logf("fill amount: %s", preOrder.FillAmount)
    
    followID := uuid.NewString()
    minAmount := preOrder.FillAmount.Mul(decimal.NewFromFloat(0.99)).Truncate(8)
    memo := BuildSwap(followID, req.FillAssetID, preOrder.Paths, minAmount)
    
    t.Logf("memo: %s", memo)
    
    group, err := c.ReadGroup(ctx)
    if err != nil {
        t.Fatal(err)
    }
    
    t.Logf("target mix address: %s", group.MixAddress)
    
    transfer := &mixin.TransferInput{
        AssetID:    req.PayAssetID,
        OpponentID: group.MixAddress,
        Amount:     req.PayAmount,
        TraceID:    followID,
        Memo:       memo,
    }
    
    t.Log(mixin.URL.SafePay(transfer))
    
    // transfer pay asset to mix address
    
    // view order detail
    order, err := c.ReadOrder(ctx, followID)
    if err != nil {
        assert.True(t, IsErrorCode(err, 401))
    } else {
        t.Logf("order state: %s", order.State)
    }
}

4swap-sdk-go's People

Contributors

halimao avatar iam9k avatar kuri-su avatar lyricat avatar pando-ci avatar tooooolong avatar yiplee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

4swap-sdk-go's Issues

Feat: Add a new order state indicating transfer is sucessful

Feat: Add a new order state indicating transfer is sucessful

After make a transfer request to mtg-transaction, the order state (response from ReadOrder) only has three state

const (
	OrderStateTrading  = "Trading"
	OrderStateRejected = "Rejected"
	OrderStateDone     = "Done"
)

thereby, we couldn't know is the transfer of this order successful then?
Maybe we can add a new state just like OrderStateTransferDone = "TransferDone", and update order state to OrderStateTransferDone after 4swap transfer to MixinNetwork.

[Question] 是否错误标注 PreOrderReq.MinAmount 字段 deprecated

从新增的字段上来看, 似乎deprecated 的是 Amount 字段,而非 MinAmount 字段. 从 Swap 的角度, 也不太可能取消PreOrder的 滑点吧?

4swap-sdk-go/order.go

Lines 34 to 46 in 35cd409

type PreOrderReq struct {
PayAssetID string `json:"pay_asset_id,omitempty"`
FillAssetID string `json:"fill_asset_id,omitempty"`
// pay amount 和 fill amount 二选一
PayAmount decimal.Decimal `json:"pay_amount,omitempty"`
FillAmount decimal.Decimal `json:"fill_amount,omitempty"`
// deprecated
Funds decimal.Decimal `json:"funds,omitempty"`
Amount decimal.Decimal `json:"amount,omitempty"`
// deprecated
MinAmount decimal.Decimal `json:"min_amount,omitempty"`
}

[问题]: Transfer amount too small.

如题目,我想尝试使用 4swap 用 usdt 购买 rum

根据文档 mtg_swap.md,我的理解是,首先要利用 actions 生成 memo,然后放在后续将要使用到的 mixin raw transaction API

我的 action 如下

{
  "action": "3,867b6eb6-83d6-4389-a0d4-68a2caa5af63,51f77f59-c791-421d-9d04-9b2b749af5c2,4f2ec12c-22f4-3a9e-b757-c84b6415ea8f,,0.000001"
}

向 /api/actions POST 后,返回了

{
  "ts": 1636980813601,
  "data": {
    "action": "8OFPv4Xo18jDC2zwLvVYek5oB0QwG6KWDwMWfO3p+nBn/oS5Bx3HYdsbQcg9dhBIFJg98u3iq8IIFOJ5tHwdjaWs0F9cj2P903nZ2pTnqOBIJvo74IXvr+F7yqDQq2OF",
    "follow_id": "51f77f59-c791-421d-9d04-9b2b749af5c2"
  }
}

这一步看起来没问题。

接下来尝试向多签地址发送转账,我的请求体类似

{
  "asset_id": "4d8c508b-91c5-375b-92b0-ee702ed2dac5",
  "opponent_multisig": {
    "receivers": [
      "a753e0eb-3010-4c4a-a7b2-a7bda4063f62",
      "099627f8-4031-42e3-a846-006ee598c56e",
      "aefbfd62-727d-4424-89db-ae41f75d2e04",
      "d68ca71f-0e2c-458a-bb9c-1d6c2eed2497",
      "e4bc0740-f8fe-418c-ae1b-32d9926f5863"
    ],
    "threshold": 3
  },
  "ammount": "0.01",
  "pin": "xxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "trace_id": "51f77f59-c791-421d-9d04-9b2b749af5c2",
  "memo": "8OFPv4Xo18jDC2zwLvVYek5oB0QwG6KWDwMWfO3p+nBn/oS5Bx3HYdsbQcg9dhBIFJg98u3iq8IIFOJ5tHwdjaWs0F9cj2P903nZ2pTnqOBIJvo74IXvr+F7yqDQq2OF"
}

然后 mixin 返回了

{
  "error": {
    "status": 202,
    "code": 20120,
    "description": "Transfer amount too small."
  }
}

请问我的使用方式是不是正确,或者说 API 有交易最低数量的限制(我还尝试过 0.1),手动通过 mixin 客户端,在 4swap 的 bot 里面,是可以发起 0.01 usdt 的 swap 请求的。

pre-order wrong price

I wrote a function using preorder to query BOX price.
Pic1
At that moment, what I got in return is 5.3734 USDT/BOX.
Pic2
But when I query the API with POST.
Pic3
It shows the price is 6.0031 USDT/BOX
pic4
I'm not sure if this is a bug.

交易路径走到 Fill Asset 之后可以继续往下走么?

根据 SDK 的代码,在遍历交易路径时,当一个路径走到 fill asset 时,他就会比对当前最佳结果,之后会被抛弃或者替换当前最佳,都没有往下走。

但其实交易路径还可以往下走,比如 ZEC 换 BTC,可以是 ZEC > BTC > BOX > ETH > BTC,虽然经过 2 次 BTC,但其中没有重复的 RouteID。同时 ZEC > BTC > BOX > ETH > BTC 有可能比 ZEC > BTC 更优,比如 BTC-BOX 出现极端行情时。

我们经常能看到第 4 个 asset 和第 1 个 asset相同的情况,却从没看到过第 2 个和第 5 个相同的情况。既然允许第一种,第二种也该也没什么问题吧。

同时,如果不允许往下走,那么币种 A 到币种 B 的路径数量和反过来币种 B 到币种 A 的路径数量就会不一致,无法完全对称。

所以,是不是可以允许到了 fill asset 之后,继续延伸交易路径?现在如果手动设置一个到了 fill asset 还继续延伸的路径转账给 4swap,路径会被截断,比如上述的 ZEC-BTC 长路径,会在第一次到达 BTC 后就停止。

4swap-sdk-go/route.go

Lines 106 to 119 in 0d0043c

next := &node{
Result: r,
p: n,
d: n.d + 1,
}
if !arrived {
next.route(g, fillAsset, best)
continue
}
if next.Cmp(best) > 0 {
*best = *next
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.