githubEdit

Accounts

Initialization

To use the Accounts class, you first need to initialize an HttpClient object with your API base URL and security worker. The security worker is a function that returns the headers to be included in the API requests. In this case, it returns an authorization header with a bearer token.

import { HttpClient, Accounts } from '@moonup/moon-api';

const http = new HttpClient({
  baseUrl: 'https://beta.usemoon.ai',
  securityWorker: async (securityData) => {
    return {
      headers: {
        Authorization: `Bearer ${securityData.token}`,
      },
    };
  },
});

const accounts = new Accounts(http);

Methods

broadcastTx(accountName: string, data: BroadcastInput, params: RequestParams = {})

Broadcasts a raw transaction to the blockchain.

  • Parameters:

    • accountName: The name of the account.

    • data: An object containing the chainId and rawTransaction properties.

    • params: Optional request parameters.

  • Returns: A promise that resolves to a BroadcastTxData object.

  • Example:

createAccount(data: CreateAccountInput, params: RequestParams = {})

Creates a new Ethereum account.

  • Parameters:

    • data: An optional object containing the private_key property. If not provided, a new private key will be generated.

    • params: Optional request parameters.

  • Returns: A promise that resolves to a CreateAccountData object.

  • Example:

deleteAccount(accountName: string, params: RequestParams = {})

Deletes an existing Ethereum account.

  • Parameters:

    • accountName: The name of the account to delete.

    • params: Optional request parameters.

  • Returns: A promise that resolves to a DeleteAccountData object.

  • Example:

deployContract(accountName: string, data: DeployInput, params: RequestParams = {})

Deploys a new smart contract to the blockchain.

  • Parameters:

    • accountName: The name of the account that will deploy the contract.

    • data: An object containing the abi, bytecode, chain_id, and constructor_args properties.

    • params: Optional request parameters.

  • Returns: A promise that resolves to a DeployContractData object.

  • Example:

encodeData(data: AbiEncodeInput, params: RequestParams = {})

Encodes data using the ABI of a smart contract.

  • Parameters:

    • data: An object containing the abi, functionName, and params properties.

    • params: Optional request parameters.

  • Returns: A promise that resolves to an EncodeDataData object.

  • Example:

estimateGas(accountName: string, data: InputBody, params: RequestParams = {})

Estimates the amount of gas required to execute a transaction.

  • Parameters:

    • accountName: The name of the account that will execute the transaction.

    • data: An object containing the transaction details.

    • params: Optional request parameters.

  • Returns: A promise that resolves to an EstimateGasData object.

  • Example:

getAccount(accountName: string, params: RequestParams = {})

Retrieves information about an Ethereum account.

  • Parameters:

    • accountName: The name of the account.

    • params: Optional request parameters.

  • Returns: A promise that resolves to a GetAccountData object.

  • Example:

getBalance({ accountName, ...query }: GetBalanceParams, params: RequestParams = {})

Retrieves the balance of an Ethereum account.

  • Parameters:

    • accountName: The name of the account.

    • query: Optional query parameters, including token_address and token_id.

    • params: Optional request parameters.

  • Returns: A promise that resolves to a GetBalanceData object.

  • Example:

getNonce(accountName: string, params: RequestParams = {})

Retrieves the nonce of an Ethereum account.

  • Parameters:

    • accountName: The name of the account.

    • params: Optional request parameters.

  • Returns: A promise that resolves to a GetNonceData object.

  • Example:

listAccounts(params: RequestParams = {})

Retrieves a list of all Ethereum accounts.

  • Parameters:

    • params: Optional request parameters.

  • Returns: A promise that resolves to a ListAccountsData object.

  • Example:

signMessage(accountName: string, data: SignMessage, params: RequestParams = {})

Signs a message using an Ethereum account.

  • Parameters:

    • accountName: The name of the account.

    • data: An object containing the message property.

    • params: Optional request parameters.

  • Returns: A promise that resolves to a SignMessageData object.

  • Example:

signTransaction(accountName: string, data: InputBody, params: RequestParams = {})

Signs a transaction using an Ethereum account.

  • Parameters:

    • accountName: The name of the account.

    • data: An object containing the transaction details.

    • params: Optional request parameters.

  • Returns: A promise that resolves to a SignTransactionData object.

  • Example:

signTypedData(accountName: string, data: SignTypedData, params: RequestParams = {})

Signs a typed data message using an Ethereum account.

  • Parameters:

    • accountName: The name of the account.

    • data: An object containing the data property.

    • params: Optional request parameters.

  • Returns: A promise that resolves to a SignTypedDataData object.

  • Example:

suggestGasPrice({ accountName, ...query }: SuggestGasPriceParams, params: RequestParams = {})

Suggests a gas price for a transaction.

  • Parameters:

    • accountName: The name of the account.

    • query: Optional query parameters, including speed.

    • params: Optional request parameters.

  • Returns: A promise that resolves to a SuggestGasPriceData object.

  • Example:

transferEth(accountName: string, data: InputBody, params: RequestParams = {})

Transfers ETH from one Ethereum account to another.

  • Parameters:

    • accountName: The name of the source account.

    • data: An object containing the transaction details.

    • params: Optional request parameters.

  • Returns: A promise that resolves to a TransferEthData object.

  • Example:

These examples assume that you have already initialized the HttpClient and Accounts objects as shown in the provided code snippet. Make sure to replace the placeholders with actual values.

Last updated