Skip to content

Commit

Permalink
Convert to fula (#205)
Browse files Browse the repository at this point in the history
* TransferToFula added

* go mod tidy

* disabled resource manager for mobile as well
  • Loading branch information
ehsan6sha authored Dec 24, 2023
1 parent 386866e commit 49eb430
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
43 changes: 43 additions & 0 deletions blockchain/bl_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -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")
}
6 changes: 6 additions & 0 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions blockchain/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{}),
Expand Down Expand Up @@ -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{}),
Expand Down
2 changes: 1 addition & 1 deletion cmd/blox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 18 additions & 1 deletion mobile/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 49eb430

Please sign in to comment.