From 49eb430e8583c1144e1166f19a28541384f09911 Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Sat, 23 Dec 2023 21:41:39 -0500 Subject: [PATCH] Convert to fula (#205) * TransferToFula added * go mod tidy * disabled resource manager for mobile as well --- blockchain/bl_account.go | 43 ++++++++++++++++++++++++++++++++++++++++ blockchain/blockchain.go | 6 ++++++ blockchain/interface.go | 17 ++++++++++++++++ cmd/blox/main.go | 2 +- mobile/blockchain.go | 19 +++++++++++++++++- 5 files changed, 85 insertions(+), 2 deletions(-) diff --git a/blockchain/bl_account.go b/blockchain/bl_account.go index 0edcf06a..069d5c47 100644 --- a/blockchain/bl_account.go +++ b/blockchain/bl_account.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -249,3 +250,45 @@ func (bl *FxBlockchain) AssetsBalance(ctx context.Context, to peer.ID, r AssetsB return b, nil } + +func (bl *FxBlockchain) TransferToFula(ctx context.Context, to peer.ID, r TransferToFulaRequest) ([]byte, error) { + if bl.allowTransientConnection { + ctx = network.WithUseTransient(ctx, "fx.blockchain") + } + + var buf bytes.Buffer + if err := json.NewEncoder(&buf).Encode(r); err != nil { + return nil, err + } + action := "" + switch r.Chain { + case "mumbai": + action = actionTransferToMumbai + case "goerli": + action = actionTransferToGoerli + default: + action = "" + } + + if action != "" { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://"+to.String()+".invalid/"+action, &buf) + if err != nil { + return nil, err + } + resp, err := bl.c.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + b, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + if resp.StatusCode != http.StatusAccepted { + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + } + + return b, nil + } + return []byte{}, errors.New("selected chain is not supported yet") +} diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index d482d94f..1cf4b637 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -326,6 +326,12 @@ func (bl *FxBlockchain) serve(w http.ResponseWriter, r *http.Request) { actionAssetsBalance: func(from peer.ID, w http.ResponseWriter, r *http.Request) { bl.handleAction(http.MethodPost, actionAssetsBalance, from, w, r) }, + actionTransferToGoerli: func(from peer.ID, w http.ResponseWriter, r *http.Request) { + bl.handleAction(http.MethodPost, actionTransferToGoerli, from, w, r) + }, + actionTransferToMumbai: func(from peer.ID, w http.ResponseWriter, r *http.Request) { + bl.handleAction(http.MethodPost, actionTransferToMumbai, from, w, r) + }, actionPoolCreate: func(from peer.ID, w http.ResponseWriter, r *http.Request) { //TODO: We should check if from owns the blox bl.handleAction(http.MethodPost, actionPoolCreate, from, w, r) diff --git a/blockchain/interface.go b/blockchain/interface.go index 3af3dd75..a17946d2 100644 --- a/blockchain/interface.go +++ b/blockchain/interface.go @@ -16,6 +16,8 @@ const ( actionAccountFund = "account-fund" actionAccountBalance = "account-balance" actionAssetsBalance = "assets-balance" + actionTransferToMumbai = "fula-mumbai-convert_tokens" + actionTransferToGoerli = "fula-goerli-convert_tokens" actionPoolCreate = "fula-pool-create" actionPoolJoin = "fula-pool-join" actionPoolCancelJoin = "fula-pool-cancel_join" @@ -106,6 +108,16 @@ type AssetsBalanceResponse struct { Amount string `json:"amount"` } +type TransferToFulaRequest struct { + Wallet string `json:"wallet_account"` + Amount uint64 `json:"amount"` + Chain string `json:"chain"` +} +type TransferToFulaResponse struct { + Msg string `json:"msg"` + Description string `json:"description"` +} + type PoolCreateRequest struct { PoolName string `json:"pool_name"` PeerID string `json:"peer_id"` @@ -312,6 +324,7 @@ type Blockchain interface { AccountFund(context.Context, peer.ID, AccountFundRequest) ([]byte, error) AccountBalance(context.Context, peer.ID, AccountBalanceRequest) ([]byte, error) AssetsBalance(context.Context, peer.ID, AssetsBalanceRequest) ([]byte, error) + TransferToFula(context.Context, peer.ID, TransferToFulaRequest) ([]byte, error) PoolCreate(context.Context, peer.ID, PoolCreateRequest) ([]byte, error) PoolJoin(context.Context, peer.ID, PoolJoinRequest) ([]byte, error) PoolCancelJoin(context.Context, peer.ID, PoolCancelJoinRequest) ([]byte, error) @@ -361,6 +374,8 @@ var requestTypes = map[string]reflect.Type{ actionManifestRemoveStorer: reflect.TypeOf(ManifestRemoveStorerRequest{}), actionManifestRemoveStored: reflect.TypeOf(ManifestRemoveStoredRequest{}), actionAssetsBalance: reflect.TypeOf(AssetsBalanceRequest{}), + actionTransferToGoerli: reflect.TypeOf(TransferToFulaRequest{}), + actionTransferToMumbai: reflect.TypeOf(TransferToFulaRequest{}), //Hardware actionBloxFreeSpace: reflect.TypeOf(wifi.BloxFreeSpaceRequest{}), @@ -395,6 +410,8 @@ var responseTypes = map[string]reflect.Type{ actionManifestRemoveStorer: reflect.TypeOf(ManifestRemoveStorerResponse{}), actionManifestRemoveStored: reflect.TypeOf(ManifestRemoveStoredResponse{}), actionAssetsBalance: reflect.TypeOf(AssetsBalanceResponse{}), + actionTransferToGoerli: reflect.TypeOf(TransferToFulaResponse{}), + actionTransferToMumbai: reflect.TypeOf(TransferToFulaResponse{}), //Hardware actionBloxFreeSpace: reflect.TypeOf(wifi.BloxFreeSpaceResponse{}), diff --git a/cmd/blox/main.go b/cmd/blox/main.go index 0f3a28bc..33b70ae7 100644 --- a/cmd/blox/main.go +++ b/cmd/blox/main.go @@ -327,7 +327,7 @@ func init() { Aliases: []string{"drm"}, Usage: "Weather to disable the libp2p resource manager.", Destination: &app.config.DisableResourceManger, - Value: false, + Value: true, }), altsrc.NewIntFlag(&cli.IntFlag{ Name: "maxCIDPushRate", diff --git a/mobile/blockchain.go b/mobile/blockchain.go index 518e9507..c0df16bd 100644 --- a/mobile/blockchain.go +++ b/mobile/blockchain.go @@ -2,6 +2,8 @@ package fulamobile import ( "context" + "fmt" + "strconv" "github.com/functionland/go-fula/blockchain" wifi "github.com/functionland/go-fula/wap/pkg/wifi" @@ -34,6 +36,21 @@ func (c *Client) AssetsBalance(account string, assetId string, classId string) ( return c.bl.AssetsBalance(ctx, c.bloxPid, blockchain.AssetsBalanceRequest{Account: account, AssetId: assetId, ClassId: classId}) } +func (c *Client) TransferToFula(amountStr string, walletAccount string, chain string) ([]byte, error) { + ctx := context.TODO() + // Convert amount from string to uint64 + amount, err := strconv.ParseUint(amountStr, 10, 64) + if err != nil { + return nil, fmt.Errorf("invalid amount: %v", err) + } + convertInput := blockchain.TransferToFulaRequest{ + Wallet: walletAccount, + Amount: amount, // Use the converted amount + Chain: chain, + } + return c.bl.TransferToFula(ctx, c.bloxPid, convertInput) +} + // PoolJoin requests blox at Config.BloxAddr to join a pool with the id. // the addr must be a valid multiaddr that includes peer ID. // Note that this call is only allowed on a user's own blox @@ -146,7 +163,7 @@ func (c *Client) DeleteFulaConfig() ([]byte, error) { return c.bl.DeleteFulaConfig(ctx, c.bloxPid) } -// AssetsBalance requests blox at Config.BloxAddr to get the balance of the account. +// GetAccount requests blox at Config.BloxAddr to get the balance of the account. // the addr must be a valid multiaddr that includes peer ID. func (c *Client) GetAccount() ([]byte, error) { ctx := context.TODO()