Skip to content

Commit

Permalink
feat(): fix uploadFolder sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
zyjblockchain committed Aug 24, 2022
1 parent ace62cf commit 8bc1f86
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 34 deletions.
5 changes: 2 additions & 3 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ func (s *Arseeding) runAPI(port string) {
// ANS-104 bundle Data api
v1.GET("/bundle/bundler", s.getBundler)
v1.POST("/bundle/tx/:currency", s.submitItem)
if s.NoFee {
v1.POST("/bundle/tx", s.submitItem)
}
v1.POST("/bundle/tx", s.submitItem)

v1.GET("/bundle/tx/:itemId", s.getItemMeta) // get item meta, without data
v1.GET("/bundle/tx/:itemId/:field", s.getItemField)
v1.GET("/bundle/itemIds/:arId", s.getItemIdsByArId)
Expand Down
2 changes: 1 addition & 1 deletion example/bundle-item/batchSendItem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestSendItem(t *testing.T) {
return
}
// submit to arseed
order, err := arseedSdk.SubmitItem(item.ItemBinary, "usdt")
order, err := arseedSdk.SubmitItem(item.ItemBinary, "usdt", "")
if err != nil {
t.Log("send failed", "idx", idx, "err", err)
return
Expand Down
2 changes: 1 addition & 1 deletion example/bundle-item/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestItemUseCase(t *testing.T) {
}

// send bundle item to arseeding with arseeding sdk
order, err := arseedSdk.SubmitItem(item.ItemBinary, "USDC") // use "USDC" token payment fee
order, err := arseedSdk.SubmitItem(item.ItemBinary, "USDC", "") // use "USDC" token payment fee
if err != nil {
t.Log(err)
}
Expand Down
12 changes: 10 additions & 2 deletions sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,18 @@ func (a *ArSeedCli) GetBundler() (string, error) {
return addr, err
}

func (a *ArSeedCli) SubmitItem(itemBinary []byte, currency string) (*schema.RespOrder, error) {
func (a *ArSeedCli) SubmitItem(itemBinary []byte, currency string, apikey string) (*schema.RespOrder, error) {
req := a.SCli.Post()
req.Path(fmt.Sprintf("/bundle/tx/%s", currency))
if currency != "" {
req.Path(fmt.Sprintf("/bundle/tx/%s", currency))
} else {
req.Path("/bundle/tx")
}

req.SetHeader("Content-Type", "application/octet-stream")
if len(apikey) > 0 {
req.SetHeader("X-API-KEY", apikey)
}

req.Body(bytes.NewReader(itemBinary))

Expand Down
34 changes: 27 additions & 7 deletions sdk/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
seedSchema "github.com/everFinance/arseeding/schema"
"github.com/everFinance/arseeding/sdk/schema"
paySchema "github.com/everFinance/everpay-go/pay/schema"
"github.com/everFinance/goar/types"
"github.com/panjf2000/ants/v2"
"io/ioutil"
Expand All @@ -14,7 +15,26 @@ import (
"sync"
)

func (s *SDK) UploadFolder(rootPath string, batchSize int, indexFile string, currency string) ([]*seedSchema.RespOrder, error) {
func (s *SDK) UploadFolder(rootPath string, batchSize int, indexFile string, currency string) (orders []*seedSchema.RespOrder, manifestId string, err error) {
orders, manifestId, err = s.uploadFolder(rootPath, batchSize, indexFile, currency, "")
return
}

func (s *SDK) UploadFolderAndPay(rootPath string, batchSize int, indexFile string, currency string) (orders []*seedSchema.RespOrder, manifestId string, everTxs []*paySchema.Transaction, err error) {
orders, manifestId, err = s.uploadFolder(rootPath, batchSize, indexFile, currency, "")
if err != nil {
return
}
everTxs, err = s.BatchPayOrders(orders)
return
}

func (s *SDK) UploadFolderWithNoFee(rootPath string, batchSize int, indexFile string, noFeeApikey string) (orders []*seedSchema.RespOrder, manifestId string, err error) {
orders, manifestId, err = s.uploadFolder(rootPath, batchSize, indexFile, "", noFeeApikey)
return
}

func (s *SDK) uploadFolder(rootPath string, batchSize int, indexFile string, currency string, noFeeApikey string) ([]*seedSchema.RespOrder, string, error) {
if indexFile == "" {
indexFile = "index.html"
}
Expand All @@ -31,7 +51,7 @@ func (s *SDK) UploadFolder(rootPath string, batchSize int, indexFile string, cur

pathFiles, err := getPathFiles(rootPath)
if err != nil {
return nil, err
return nil, "", err
}

orders := make([]*seedSchema.RespOrder, 0, len(pathFiles))
Expand All @@ -51,7 +71,7 @@ func (s *SDK) UploadFolder(rootPath string, batchSize int, indexFile string, cur
panic(err)
}
// bundle item and send to arseeding
order, err := s.SendData(data, currency, nil)
order, err := s.SendData(data, currency, noFeeApikey, nil)
if err != nil {
panic(err)
}
Expand All @@ -78,16 +98,16 @@ func (s *SDK) UploadFolder(rootPath string, batchSize int, indexFile string, cur
// submit manifest file
manifestFileBy, err := json.Marshal(manifestFile)
if err != nil {
return nil, err
return nil, "", err
}
order, err := s.SendData(manifestFileBy, currency, &schema.OptionItem{
order, err := s.SendData(manifestFileBy, currency, noFeeApikey, &schema.OptionItem{
Tags: []types.Tag{{Name: "Type", Value: "manifest"}, {Name: "Content-Type", Value: "application/x.arweave-manifest+json"}},
})
if err != nil {
return nil, err
return nil, "", err
}
orders = append(orders, order)
return orders, nil
return orders, order.ItemId, nil
}

func readFileData(rootPath, filePath string) ([]byte, error) {
Expand Down
59 changes: 42 additions & 17 deletions sdk/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,55 @@ import (
"testing"
)

func TestSDK_UploadFolder(t *testing.T) {
func TestSDK_UploadFolderAndPay(t *testing.T) {
priKey := "1d8bdd0d2f1e73dffe1111111118325b7e195669541f76559760ef615a588be3"
eccSigner, err := goether.NewSigner(priKey)
if err != nil {
panic(err)
}
seedUrl := "http://127.0.0.1:8080"
payUrl := "https://api-dev.everpay.io"
assert.NoError(t, err)
seedUrl := "https://arseed-dev.web3infra.dev"
payUrl := "https://api.everpay.io"
sdk, err := NewSDK(seedUrl, payUrl, eccSigner)
if err != nil {
panic(err)
}
assert.NoError(t, err)

rootPath := "./dist"
orders, err := sdk.UploadFolder(rootPath, 20, "", "usdt")
if err != nil {
panic(err)
}
t.Log(orders[len(orders)-1].ItemId)
orders, manifestId, everTxs, err := sdk.UploadFolderAndPay(rootPath, 20, "index.html", "usdt")
assert.NoError(t, err)
t.Log(len(orders))
t.Log("manifestId:", manifestId)
t.Log("everTx:", everTxs[0].HexHash())
}

// pay orders
func TestSDK_UploadFolder(t *testing.T) {
priKey := "1d8bdd0d2f1e73dffe1111111118325b7e195669541f76559760ef615a588be3"
eccSigner, err := goether.NewSigner(priKey)
assert.NoError(t, err)
seedUrl := "https://arseed-dev.web3infra.dev"
payUrl := "https://api.everpay.io"
sdk, err := NewSDK(seedUrl, payUrl, eccSigner)
assert.NoError(t, err)

rootPath := "./dist"
orders, manifestId, err := sdk.UploadFolder(rootPath, 20, "index.html", "usdt")
assert.NoError(t, err)
t.Log(len(orders))
t.Log("manifestId:", manifestId)
// pay fee
everTxs, err := sdk.BatchPayOrders(orders)
t.Log("everTx:", everTxs[0].HexHash())
}

func TestSDK_UploadFolderWithNoFee(t *testing.T) {
priKey := "1d8bdd0d2f1e73dffe1111111118325b7e195669541f76559760ef615a588be3"
eccSigner, err := goether.NewSigner(priKey)
assert.NoError(t, err)
t.Log(len(everTxs))
t.Log(everTxs[0].HexHash())
seedUrl := "https://arseed-dev.web3infra.dev"
payUrl := "https://api.everpay.io"
sdk, err := NewSDK(seedUrl, payUrl, eccSigner)
assert.NoError(t, err)
apikey := "xxxxxxxxxx"

rootPath := "./dist"
orders, manifestId, err := sdk.UploadFolderWithNoFee(rootPath, 20, "index.html", apikey)
assert.NoError(t, err)
t.Log(len(orders))
t.Log("manifestId:", manifestId)
}
6 changes: 3 additions & 3 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewSDK(arseedUrl, payUrl string, signer interface{}) (*SDK, error) {
}

func (s *SDK) SendDataAndPay(data []byte, currency string, option *schema.OptionItem) (everTx *paySchema.Transaction, itemId string, err error) {
order, err := s.SendData(data, currency, option)
order, err := s.SendData(data, currency, "", option)
if err != nil {
return
}
Expand All @@ -45,7 +45,7 @@ func (s *SDK) SendDataAndPay(data []byte, currency string, option *schema.Option
return
}

func (s *SDK) SendData(data []byte, currency string, option *schema.OptionItem) (order *arseedSchema.RespOrder, err error) {
func (s *SDK) SendData(data []byte, currency string, apikey string, option *schema.OptionItem) (order *arseedSchema.RespOrder, err error) {
bundleItem := types.BundleItem{}
if option != nil {
bundleItem, err = s.ItemSigner.CreateAndSignItem(data, option.Target, option.Anchor, option.Tags)
Expand All @@ -55,7 +55,7 @@ func (s *SDK) SendData(data []byte, currency string, option *schema.OptionItem)
if err != nil {
return
}
order, err = s.Cli.SubmitItem(bundleItem.ItemBinary, currency)
order, err = s.Cli.SubmitItem(bundleItem.ItemBinary, currency, apikey)
return
}

Expand Down

0 comments on commit 8bc1f86

Please sign in to comment.