Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release: prepare for release v0.2.6 #200

Merged
merged 8 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ on:
- develop

env:
GreenfieldTag: v0.2.5
GreenfieldStorageProviderTag: v0.2.5-alpha.3
GreenfieldTag: v0.2.6
GreenfieldStorageProviderTag: v0.2.6
GOPRIVATE: github.com/bnb-chain
GH_ACCESS_TOKEN: ${{ secrets.GH_TOKEN }}
MYSQL_USER: root
Expand Down
190 changes: 154 additions & 36 deletions client/api_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"fmt"

"cosmossdk.io/math"
"github.com/bnb-chain/greenfield-go-sdk/types"
Expand All @@ -12,7 +13,12 @@ import (
bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

type Account interface {
// IAccountClient - Client APIs for operating Greenfield accounts.
type IAccountClient interface {
SetDefaultAccount(account *types.Account)
GetDefaultAccount() (*types.Account, error)
MustGetDefaultAccount() *types.Account

GetAccount(ctx context.Context, address string) (authTypes.AccountI, error)
GetAccountBalance(ctx context.Context, address string) (*sdk.Coin, error)
GetPaymentAccount(ctx context.Context, address string) (*paymentTypes.PaymentAccount, error)
Expand All @@ -25,14 +31,54 @@ type Account interface {
MultiTransfer(ctx context.Context, details []types.TransferDetail, txOption gnfdSdkTypes.TxOption) (string, error)
}

// GetAccount retrieves account information for a given address.
// It takes a context and an address as input and returns an AccountI interface and an error (if any).
func (c *client) GetAccount(ctx context.Context, address string) (authTypes.AccountI, error) {
// SetDefaultAccount - Set the default account of the Client.
//
// If you call other APIs without specifying the account, it will be assumed that you are operating on the default
// account. This includes sending transactions and other actions.
//
// - account: The account to be set as the default account, should be created using a private key or a mnemonic phrase.
func (c *Client) SetDefaultAccount(account *types.Account) {
c.defaultAccount = account
c.chainClient.SetKeyManager(account.GetKeyManager())
}

// GetDefaultAccount - Get the default account of the Client.
//
// - ret1: The default account of the Client.
//
// - ret2: Return error when default account doesn't exist, otherwise return nil.
func (c *Client) GetDefaultAccount() (*types.Account, error) {
if c.defaultAccount == nil {
return nil, types.ErrorDefaultAccountNotExist
}
return c.defaultAccount, nil
}

// MustGetDefaultAccount - Get the default account of the Client, panic when account not found.
//
// - ret1: The default account of the Client.
func (c *Client) MustGetDefaultAccount() *types.Account {
if c.defaultAccount == nil {
panic("Default account not exist, Use SetDefaultAccount to set ")
}
return c.defaultAccount
}

// GetAccount - Retrieve on-chain account information for a given address.
//
// - ctx: Context variables for the current API call.
//
// - address: The given address for retrieving.
//
// - ret1: The account interface for the given address.
//
// - ret2: Return error when getting account failed, otherwise return nil.
func (c *Client) GetAccount(ctx context.Context, address string) (authTypes.AccountI, error) {
accAddress, err := sdk.AccAddressFromHexUnsafe(address)
if err != nil {
return nil, err
}
// Call the DefaultAccount method of the chain client with a QueryAccountRequest containing the address.
// Call the DefaultAccount method of the chain Client with a QueryAccountRequest containing the address.
response, err := c.chainClient.Account(ctx, &authTypes.QueryAccountRequest{Address: accAddress.String()})
if err != nil {
// Return an error if there was an issue retrieving the account.
Expand All @@ -51,22 +97,45 @@ func (c *client) GetAccount(ctx context.Context, address string) (authTypes.Acco
return &baseAccount, err
}

// CreatePaymentAccount creates a new payment account on the blockchain using the provided address.
// It returns a TxResponse containing information about the transaction, or an error if the transaction failed.
func (c *client) CreatePaymentAccount(ctx context.Context, address string, txOption gnfdSdkTypes.TxOption) (string, error) {
// CreatePaymentAccount - Create a new payment account for the given address.
//
// The payment account is used to pay for the storage and read quota fee of objects. When you need to pay for different
// buckets separately, you can create different payment accounts to do so. Note that the payment account does not have a
// private key, and only the owner of the payment account can withdraw funds from it. Once the owner revokes permission
// for withdrawal, the funds in the payment account can only be utilized to cover storage and read quota fees.
//
// - ctx: Context variables for the current API call.
//
// - address: The owner address of the new payment account.
//
// - txOption: The txOption for sending transactions.
//
// - ret1: Return the transaction hash if created successfully, otherwise return empty string.
//
// - ret2: Return error when created failed, otherwise return nil.
func (c *Client) CreatePaymentAccount(ctx context.Context, address string, txOption gnfdSdkTypes.TxOption) (string, error) {
accAddress, err := sdk.AccAddressFromHexUnsafe(address)
if err != nil {
return "", err
}
msgCreatePaymentAccount := paymentTypes.NewMsgCreatePaymentAccount(accAddress.String())
tx, err := c.chainClient.BroadcastTx(ctx, []sdk.Msg{msgCreatePaymentAccount}, &txOption)
tx, err := c.BroadcastTx(ctx, []sdk.Msg{msgCreatePaymentAccount}, &txOption)
if err != nil {
return "", err
}
return tx.TxResponse.TxHash, nil
}

func (c *client) GetModuleAccountByName(ctx context.Context, name string) (authTypes.ModuleAccountI, error) {
// GetModuleAccountByName - Get module account by module name.
//
// - ctx: Context variables for the current API call.
//
// - name: Module name.
//
// - ret1: The account interface for the given module name.
//
// - ret2: Return error when getting failed, otherwise return nil.
func (c *Client) GetModuleAccountByName(ctx context.Context, name string) (authTypes.ModuleAccountI, error) {
response, err := c.chainClient.ModuleAccountByName(ctx, &authTypes.QueryModuleAccountByNameRequest{Name: name})
if err != nil {
return nil, err
Expand All @@ -83,7 +152,14 @@ func (c *client) GetModuleAccountByName(ctx context.Context, name string) (authT
return &moduleAccount, err
}

func (c *client) GetModuleAccounts(ctx context.Context) ([]authTypes.ModuleAccountI, error) {
// GetModuleAccounts - Get all module accounts.
//
// - ctx: Context variables for the current API call.
//
// - ret1: The account interface lists for all the module accounts.
//
// - ret2: Return error when getting failed, otherwise return nil.
func (c *Client) GetModuleAccounts(ctx context.Context) ([]authTypes.ModuleAccountI, error) {
response, err := c.chainClient.ModuleAccounts(ctx, &authTypes.QueryModuleAccountsRequest{})
if err != nil {
return nil, err
Expand All @@ -101,9 +177,16 @@ func (c *client) GetModuleAccounts(ctx context.Context) ([]authTypes.ModuleAccou
return accounts, err
}

// GetAccountBalance retrieves balance information of an account for a given address.
// It takes a context and an address as input and returns an sdk.Coin interface and an error (if any).
func (c *client) GetAccountBalance(ctx context.Context, address string) (*sdk.Coin, error) {
// GetAccountBalance - Get the bank balance for the given address.
//
// - ctx: Context variables for the current API call.
//
// - address: The given address for retrieving.
//
// - ret1: The balance info for the given address, in sdk.Coin format.
//
// - ret2: Return error when getting failed, otherwise return nil.
func (c *Client) GetAccountBalance(ctx context.Context, address string) (*sdk.Coin, error) {
accAddress, err := sdk.AccAddressFromHexUnsafe(address)
if err != nil {
return nil, err
Expand All @@ -116,10 +199,16 @@ func (c *client) GetAccountBalance(ctx context.Context, address string) (*sdk.Co
return response.Balance, nil
}

// GetPaymentAccount function takes a context and an address string as parameters and returns a pointer to a paymentTypes.PaymentAccount struct and an error.
// This function uses the PaymentAccount method of the chainClient field of the client struct to query the payment account associated with the given address.
// If there is an error, the function returns nil and the error. If there is no error, the function returns a pointer to the PaymentAccount struct and nil.
func (c *client) GetPaymentAccount(ctx context.Context, address string) (*paymentTypes.PaymentAccount, error) {
// GetPaymentAccount - Get payment account by the payment account's address.
//
// - ctx: Context variables for the current API call.
//
// - address: The given payment account address for retrieving.
//
// - ret1: The payment account info for the given address.
//
// - ret2: Return error when getting failed, otherwise return nil.
func (c *Client) GetPaymentAccount(ctx context.Context, address string) (*paymentTypes.PaymentAccount, error) {
accAddress, err := sdk.AccAddressFromHexUnsafe(address)
if err != nil {
return nil, err
Expand All @@ -131,14 +220,21 @@ func (c *client) GetPaymentAccount(ctx context.Context, address string) (*paymen
return &pa.PaymentAccount, nil
}

// GetPaymentAccountsByOwner retrieves all payment accounts owned by the given address
// and returns a slice of PaymentAccount pointers and an error (if any).
func (c *client) GetPaymentAccountsByOwner(ctx context.Context, owner string) ([]*paymentTypes.PaymentAccount, error) {
// GetPaymentAccountsByOwner - Get all payment accounts owned by the given owner address.
//
// - ctx: Context variables for the current API call.
//
// - address: The given owner account address for retrieving.
//
// - ret1: The payment accounts list for the given owner address.
//
// - ret2: Return error when getting failed, otherwise return nil.
func (c *Client) GetPaymentAccountsByOwner(ctx context.Context, owner string) ([]*paymentTypes.PaymentAccount, error) {
ownerAcc, err := sdk.AccAddressFromHexUnsafe(owner)
if err != nil {
return nil, err
}
// Call the GetPaymentAccountsByOwner method of the chain client with a QueryGetPaymentAccountsByOwnerRequest containing the owner address.
// Call the GetPaymentAccountsByOwner method of the chain Client with a QueryGetPaymentAccountsByOwnerRequest containing the owner address.
accountsByOwnerResponse, err := c.chainClient.PaymentAccountsByOwner(ctx, &paymentTypes.QueryPaymentAccountsByOwnerRequest{Owner: ownerAcc.String()})
if err != nil {
return nil, err
Expand All @@ -149,7 +245,7 @@ func (c *client) GetPaymentAccountsByOwner(ctx context.Context, owner string) ([

// Iterate over each account address returned in the response.
for _, accAddress := range accountsByOwnerResponse.PaymentAccounts {
// Call the GetPaymentAccount method of the client to retrieve the PaymentAccount object for the given address.
// Call the GetPaymentAccount method of the Client to retrieve the PaymentAccount object for the given address.
pa, err := c.GetPaymentAccount(ctx, accAddress)
if err != nil {
return nil, err
Expand All @@ -162,33 +258,55 @@ func (c *client) GetPaymentAccountsByOwner(ctx context.Context, owner string) ([
return paymentAccounts, nil
}

// Transfer function takes a context, a toAddress string, an amount of type math.Int, and a txOption of
// type gnfdSdkTypes.TxOption as parameters and returns a pointer to an sdk.TxResponse struct and an error.
// This function first parses the toAddress parameter into an sdk.AccAddress object, and if there is an error,
// it returns nil and the error.
// Then it generates a MsgSend message using the NewMsgSend method of the types3 package and broadcasts the
// transaction to the chain by calling the BroadcastTx method of the chainClient field of the client struct.
// If there is an error during the broadcasting, the function returns nil and the error. If there is no error,
// the function returns a pointer to the TxResponse struct and nil
func (c *client) Transfer(ctx context.Context, toAddress string, amount math.Int, txOption gnfdSdkTypes.TxOption) (string, error) {
// Transfer - Transfer BNB from sender to receiver.
//
// - ctx: Context variables for the current API call.
//
// - toAddress: The address who will receive the BNB.
//
// - amount: The BNB amount to transfer, 1e18 denotes 1BNB.
//
// - txOption: The txOption for sending transactions.
//
// - ret1: Return the transaction hash if transferred successfully, otherwise return empty string.
//
// - ret2: Return error if transferred failed, otherwise return nil.
func (c *Client) Transfer(ctx context.Context, toAddress string, amount math.Int, txOption gnfdSdkTypes.TxOption) (string, error) {
toAddr, err := sdk.AccAddressFromHexUnsafe(toAddress)
if err != nil {
return "", err
}
msgSend := bankTypes.NewMsgSend(c.MustGetDefaultAccount().GetAddress(), toAddr, sdk.Coins{sdk.Coin{Denom: gnfdSdkTypes.Denom, Amount: amount}})
tx, err := c.chainClient.BroadcastTx(ctx, []sdk.Msg{msgSend}, &txOption)
tx, err := c.BroadcastTx(ctx, []sdk.Msg{msgSend}, &txOption)
if err != nil {
return "", err
}
return tx.TxResponse.TxHash, nil
}

// MultiTransfer makes transfers from an account to multiple accounts with respect amounts
func (c *client) MultiTransfer(ctx context.Context, details []types.TransferDetail, txOption gnfdSdkTypes.TxOption) (string, error) {
// MultiTransfer - Transfer BNB from sender to multiple receivers.
//
// - ctx: Context variables for the current API call.
//
// - details: The receiver address and transfer amount list.
//
// - txOption: The txOption for sending transactions.
//
// - ret1: Return the transaction hash if transferred successfully, otherwise return empty string.
//
// - ret2: Return error if transferred failed, otherwise return nil.
func (c *Client) MultiTransfer(ctx context.Context, details []types.TransferDetail, txOption gnfdSdkTypes.TxOption) (string, error) {
outputs := make([]bankTypes.Output, 0)
denom := gnfdSdkTypes.Denom
sum := math.NewInt(0)
for i := 0; i < len(details); i++ {
_, err := sdk.AccAddressFromHexUnsafe(details[i].ToAddress)
if err != nil {
return "", err
}
if details[i].Amount.IsNil() || details[i].Amount.IsNegative() {
return "", fmt.Errorf("transfer amount is not valid")
}
outputs = append(outputs, bankTypes.Output{
Address: details[i].ToAddress,
Coins: []sdk.Coin{{Denom: denom, Amount: details[i].Amount}},
Expand All @@ -203,7 +321,7 @@ func (c *client) MultiTransfer(ctx context.Context, details []types.TransferDeta
Inputs: []bankTypes.Input{in},
Outputs: outputs,
}
tx, err := c.chainClient.BroadcastTx(ctx, []sdk.Msg{msg}, &txOption)
tx, err := c.BroadcastTx(ctx, []sdk.Msg{msg}, &txOption)
if err != nil {
return "", err
}
Expand Down
Loading
Loading