Skip to content

Commit

Permalink
Merge pull request #89 from everFinance/feature/bundle-pay
Browse files Browse the repository at this point in the history
feat(): add bundle everTx payment
  • Loading branch information
zyjblockchain authored Feb 1, 2024
2 parents ad2f4cc + 4a3b5d0 commit 320607d
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 56 deletions.
4 changes: 2 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,10 +684,10 @@ func (s *Arseeding) getSignData(c *gin.Context) {
}

if size > schema.AllowStreamMinItemSize { // the body size > schema.AllowStreamMinItemSize, need write to tmp file
item, err = s.bundlerItemSigner.CreateItemStream(dataFile, "", "", tags)
item, err = s.bundlerItemSigner.CreateAndSignItemStream(dataFile, "", "", tags)

} else {
item, err = s.bundlerItemSigner.CreateItem(dataBuf.Bytes(), "", "", tags)
item, err = s.bundlerItemSigner.CreateAndSignItem(dataBuf.Bytes(), "", "", tags)
}

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion arseeding.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/everFinance/arseeding/schema"
"github.com/everFinance/arseeding/sdk"
"github.com/everFinance/go-everpay/common"
paySdk "github.com/everFinance/go-everpay/sdk"
"github.com/everFinance/goar"
"github.com/everFinance/goar/types"
paySdk "github.com/everVision/everpay-kits/sdk"
"github.com/gin-gonic/gin"
"github.com/go-co-op/gocron"
"os"
Expand Down
71 changes: 43 additions & 28 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
module github.com/everFinance/arseeding

go 1.21
go 1.21.1

toolchain go1.21.5

require (
github.com/aliyun/aliyun-oss-go-sdk v2.2.6+incompatible
github.com/aws/aws-sdk-go v1.27.0
github.com/ethereum/go-ethereum v1.10.20
github.com/everFinance/goar v1.5.8-0.20240123085808-62661ba6ed60
github.com/ethereum/go-ethereum v1.13.5
github.com/everFinance/goar v1.5.8
github.com/everFinance/goether v1.1.9
github.com/gin-gonic/gin v1.7.7
github.com/gin-gonic/gin v1.8.1
github.com/go-co-op/gocron v1.11.0
github.com/google/uuid v1.5.0
github.com/gorilla/handlers v1.4.2
Expand All @@ -19,7 +21,7 @@ require (
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.14.4
github.com/ulule/limiter/v3 v3.10.0
github.com/urfave/cli/v2 v2.24.4
github.com/urfave/cli/v2 v2.25.7
go.etcd.io/bbolt v1.3.6
gopkg.in/h2non/gentleman.v2 v2.0.5
gorm.io/datatypes v1.0.1
Expand All @@ -33,69 +35,80 @@ require (
github.com/allegro/bigcache/v3 v3.1.0
github.com/everFinance/go-everpay v0.2.0
github.com/everFinance/goarns v0.0.3
github.com/everVision/everpay-kits v0.0.6-0.20240201142725-21cc7715d94d
github.com/segmentio/kafka-go v0.4.40
go.mongodb.org/mongo-driver v1.11.4
)

require (
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.7.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/everFinance/ethrpc v1.0.4 // indirect
github.com/everFinance/gojwk v1.0.0 // indirect
github.com/everFinance/ttcrsa v1.1.3 // indirect
github.com/fxamacker/cbor/v2 v2.5.0 // indirect
github.com/getsentry/sentry-go v0.11.0 // indirect
github.com/getsentry/sentry-go v0.25.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/go-webauthn/webauthn v0.10.0 // indirect
github.com/go-webauthn/x v0.1.6 // indirect
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/go-webauthn/webauthn v0.8.3 // indirect
github.com/go-webauthn/x v0.1.2 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/go-tpm v0.9.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hamba/avro v1.5.6 // indirect
github.com/inconshreveable/log15 v0.0.0-20201112154412-8562bdadbbac // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/inconshreveable/log15 v2.16.0+incompatible // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-sqlite3 v1.14.5 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pierrec/lz4/v4 v4.1.15 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/client_model v0.2.1-0.20210607210712-147c58e9608a // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.4 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/vektah/gqlparser/v2 v2.5.1 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
Expand All @@ -104,12 +117,14 @@ require (
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
google.golang.org/protobuf v1.28.1 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/protobuf v1.29.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
17 changes: 13 additions & 4 deletions jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import (
"github.com/everFinance/arseeding/schema"
"github.com/everFinance/go-everpay/account"
"github.com/everFinance/go-everpay/config"
sdkSchema "github.com/everFinance/go-everpay/sdk/schema"
paySchema "github.com/everFinance/go-everpay/token/schema"
tokUtils "github.com/everFinance/go-everpay/token/utils"
"github.com/everFinance/goar"
"github.com/everFinance/goar/types"
"github.com/everFinance/goar/utils"
sdkSchema "github.com/everVision/everpay-kits/schema"
"github.com/google/uuid"
"github.com/panjf2000/ants/v2"
"github.com/shopspring/decimal"
Expand Down Expand Up @@ -204,7 +203,6 @@ func (s *Arseeding) watchEverReceiptTxs() {
subTx := s.everpaySdk.Cli.SubscribeTxs(sdkSchema.FilterQuery{
StartCursor: int64(startCursor),
Address: s.bundler.Signer.Address,
Action: paySchema.TxActionTransfer,
})
defer subTx.Unsubscribe()

Expand All @@ -219,6 +217,17 @@ func (s *Arseeding) watchEverReceiptTxs() {
log.Error("account.IDCheck(tt.From)", "err", err, "from", tt.From)
continue
}
// decode payment meta
paymentMeta := schema.PaymentMeta{}
if err = json.Unmarshal([]byte(tt.Data), &paymentMeta); err != nil {
log.Error("json.Unmarshal([]byte(tt.Data), &paymentMeta)", "err", err, "everTx", tt.EverHash)
continue
}
newData, err := json.Marshal(paymentMeta)
if err != nil {
log.Error("json.Marshal(paymentMeta)", "err", err, "paymentMeta", paymentMeta)
continue
}

res := schema.ReceiptEverTx{
RawId: uint64(tt.RawId),
Expand All @@ -228,7 +237,7 @@ func (s *Arseeding) watchEverReceiptTxs() {
TokenTag: tokUtils.Tag(tt.ChainType, tt.TokenSymbol, tt.TokenID),
From: from,
Amount: tt.Amount,
Data: tt.Data,
Data: string(newData),
Sig: tt.Sig,
Status: schema.UnSpent,
}
Expand Down
6 changes: 6 additions & 0 deletions schema/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ const (
DefaultPaymentExpiredRange = int64(2592000) // 30 days
DefaultExpectedRange = 50 // block height range
)

type PaymentMeta struct {
AppName string `json:"appName"`
Action string `json:"action"`
ItemIds []string `json:"itemIds"`
}
4 changes: 2 additions & 2 deletions sdk/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"
seedSchema "github.com/everFinance/arseeding/schema"
"github.com/everFinance/arseeding/sdk/schema"
paySchema "github.com/everFinance/go-everpay/pay/schema"
"github.com/everFinance/goar/types"
paySchema "github.com/everVision/everpay-kits/schema"
"github.com/panjf2000/ants/v2"
"io/ioutil"
"mime"
Expand All @@ -26,7 +26,7 @@ func (s *SDK) UploadFolderAndPay(rootPath string, batchSize int, indexFile strin
if err != nil {
return
}
everTxs, err = s.BatchPayOrders(orders)
everTxs, err = s.BatchPayOrders(orders, nil)
return
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestSDK_UploadFolder(t *testing.T) {
t.Log(len(orders))
t.Log("manifestId:", manifestId)
// pay fee
everTxs, err := sdk.BatchPayOrders(orders)
everTxs, err := sdk.BatchPayOrders(orders, nil)
t.Log("everTx:", everTxs[0].HexHash())
}

Expand Down
6 changes: 6 additions & 0 deletions sdk/schema/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ type OptionItem struct {
Anchor string
Tags []types.Tag
}

type Reward struct {
Tag string // token tag
Recipient string
Amount string
}
58 changes: 42 additions & 16 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"fmt"
arseedSchema "github.com/everFinance/arseeding/schema"
"github.com/everFinance/arseeding/sdk/schema"
paySchema "github.com/everFinance/go-everpay/pay/schema"
paySdk "github.com/everFinance/go-everpay/sdk"
"github.com/everFinance/goar"
"github.com/everFinance/goar/types"
"github.com/everFinance/goar/utils"
paySchema "github.com/everVision/everpay-kits/schema"
paySdk "github.com/everVision/everpay-kits/sdk"
"math/big"
"os"
"time"
)

type SDK struct {
Expand All @@ -38,23 +39,23 @@ func NewSDK(arseedUrl, payUrl string, signer interface{}) (*SDK, error) {
}, nil
}

func (s *SDK) SendDataAndPay(data []byte, currency string, option *schema.OptionItem, needSequence bool) (everTx *paySchema.Transaction, itemId string, err error) {
func (s *SDK) SendDataAndPay(data []byte, currency string, option *schema.OptionItem, needSequence bool, rewards []schema.Reward) (everTx *paySchema.Transaction, itemId string, err error) {
order, err := s.SendData(data, currency, "", option, needSequence)
if err != nil {
return
}
itemId = order.ItemId
everTx, err = s.PayOrders([]*arseedSchema.RespOrder{order})
everTx, err = s.PayOrders([]*arseedSchema.RespOrder{order}, rewards)
return
}

func (s *SDK) SendDataStreamAndPay(data *os.File, currency string, option *schema.OptionItem, needSequence bool) (everTx *paySchema.Transaction, itemId string, err error) {
func (s *SDK) SendDataStreamAndPay(data *os.File, currency string, option *schema.OptionItem, needSequence bool, rewards []schema.Reward) (everTx *paySchema.Transaction, itemId string, err error) {
order, err := s.SendDataStream(data, currency, "", option, needSequence)
if err != nil {
return
}
itemId = order.ItemId
everTx, err = s.PayOrders([]*arseedSchema.RespOrder{order})
everTx, err = s.PayOrders([]*arseedSchema.RespOrder{order}, rewards)
return
}

Expand Down Expand Up @@ -90,9 +91,9 @@ func (s *SDK) SendDataStream(data *os.File, currency string, apikey string, opti
return
}

func (s *SDK) BatchPayOrders(orders []*arseedSchema.RespOrder) (everTxs []*paySchema.Transaction, err error) {
func (s *SDK) BatchPayOrders(orders []*arseedSchema.RespOrder, rewards []schema.Reward) (everTxs []*paySchema.Transaction, err error) {
if len(orders) <= 500 {
everTx, err := s.PayOrders(orders)
everTx, err := s.PayOrders(orders, rewards)
if err != nil {
return nil, err
}
Expand All @@ -104,7 +105,7 @@ func (s *SDK) BatchPayOrders(orders []*arseedSchema.RespOrder) (everTxs []*paySc
end := 500
for {
subOrders := orders[start:end]
everTx, err := s.PayOrders(subOrders)
everTx, err := s.PayOrders(subOrders, rewards)
if err != nil {
return nil, err
}
Expand All @@ -122,7 +123,7 @@ func (s *SDK) BatchPayOrders(orders []*arseedSchema.RespOrder) (everTxs []*paySc
return
}

func (s *SDK) PayOrders(orders []*arseedSchema.RespOrder) (everTx *paySchema.Transaction, err error) {
func (s *SDK) PayOrders(orders []*arseedSchema.RespOrder, rewards []schema.Reward) (everTx *paySchema.Transaction, err error) {
if len(orders) == 0 {
return nil, errors.New("order is null")
}
Expand Down Expand Up @@ -156,11 +157,7 @@ func (s *SDK) PayOrders(orders []*arseedSchema.RespOrder) (everTx *paySchema.Tra
itemIds = append(itemIds, ord.ItemId)
}

payTxData := struct {
AppName string `json:"appName"`
Action string `json:"action"`
ItemIds []string `json:"itemIds"`
}{
payTxData := arseedSchema.PaymentMeta{
AppName: "arseeding",
Action: "payment",
ItemIds: itemIds,
Expand Down Expand Up @@ -198,7 +195,16 @@ func (s *SDK) PayOrders(orders []*arseedSchema.RespOrder) (everTx *paySchema.Tra
return
}

everTx, err = s.Pay.Transfer(useTag, totalFee, orders[0].Bundler, string(dataJs))
if len(rewards) > 0 {
var bundleSigs paySchema.BundleWithSigs
bundleSigs, err = s.assembleEverBundleItems(rewards)
if err != nil {
return
}
everTx, err = s.Pay.BundleWithData(useTag, orders[0].Bundler, totalFee, bundleSigs, string(dataJs))
} else {
everTx, err = s.Pay.Transfer(useTag, totalFee, orders[0].Bundler, string(dataJs))
}
return
}

Expand Down Expand Up @@ -229,3 +235,23 @@ func (s *SDK) PayApikey(tokenTag string, amount *big.Int) (everHash string, err
everHash = everTx.HexHash()
return
}

func (s *SDK) assembleEverBundleItems(rewards []schema.Reward) (paySchema.BundleWithSigs, error) {
items := make([]paySchema.BundleItem, 0, len(rewards))
tokens := s.Pay.GetTokens()
for _, rd := range rewards {
tok, ok := tokens[rd.Tag]
if !ok {
return paySchema.BundleWithSigs{}, errors.New("token tag not exist")
}
items = append(items, paySchema.BundleItem{
Tag: rd.Tag,
ChainID: tok.ChainID,
From: s.Pay.AccId,
To: rd.Recipient,
Amount: rd.Amount,
})
}
bundleWithoutSig := paySdk.GenBundle(items, time.Now().Unix()+100)
return s.Pay.SignBundleData(bundleWithoutSig)
}
Loading

0 comments on commit 320607d

Please sign in to comment.