Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fox-one/4swap-sdk-go

4swap go sdk
https://github.com/fox-one/4swap-sdk-go

go mixin swap uniswap

Last synced: about 2 months ago
JSON representation

4swap go sdk

Awesome Lists containing this project

README

        

# 4swap SDK go

## 4swap

[4swap](https://4swap.org) is a decentralized protocol implement for automated liquidity provision on [Mixin Network](https://mixin.one)

## Install

```bash
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](https://developers.mixin.one/docs/dapp/guide/generate-jwt-token) for more details.

### Example

```golang
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)
}
}
```