SDK Reference

Note: The SDK is still in beta and subject to change

SwayeClient Class

Represents a client for interacting with various blockchain APIs. This class provides access to a range of blockchain-related operations including blocks, ERC20 and ERC721 tokens, transactions, user management, chain information, and smart contract interactions.

Properties

  • block: BlockApi Handles operations related to blockchain blocks. Provides methods to query and manipulate block data.

  • erc20: ERC20Api Manages ERC20 token related operations. Allows for interaction with ERC20 standard tokens, including transfers and balance queries.

  • transaction: TransactionApi Manages blockchain transaction operations. Enables the creation, submission, and tracking of transactions on the blockchain.

  • user: UserApi Manages user-related operations in the blockchain context. This can include user account information and activities on the blockchain.

  • erc721: ERC721Api Handles operations related to ERC721 tokens. Facilitates interaction with NFTs (Non-Fungible Tokens) adhering to the ERC721 standard.

  • chain: ChainApi Manages operations related to blockchain chains. Provides functionalities like querying chain status and properties.

  • contract: ContractApi Manages smart contract related operations. Allows for deploying, interacting with, and querying smart contracts on the blockchain.

Constructor

  • constructor(baseUrl: string, bearerToken?: string) Constructs a new SwayeClient instance.

    • baseUrl: string - The base URL for the API client.

    • bearerToken?: string - Optional. The bearer token for authentication. If provided, it is used to set the authorization header for HTTP requests.

Methods

  • setBearerToken(bearerToken: string): void Sets or updates the bearer token for authentication with the blockchain services.

    • bearerToken: string - The new bearer token to be used for subsequent requests.

Usage

The SwayeClient class is instantiated with the base URL of the blockchain service and an optional bearer token. Once created, it provides properties to access various aspects of the blockchain such as blocks, transactions, tokens, and smart contracts.

const client = new SwayeClient("https://api.example.com", "your-bearer-token");
// Use the client to interact with blockchain
client.setBearerToken("New Bearer here")

BlockApi Class

Handles API calls related to blockchain blocks.

Methods

  • getBlock(blockNumber: number | string): Promise<Block> Fetches a block by its number or hash.

    • blockNumber: number | string - The block number or hash as a number or string.

    • Returns: A promise that resolves to the Block object.

    • Throws: An error if the API call fails.

  • getBlockNumber(): Promise<number | undefined> Fetches the current block number.

    • Returns: A promise that resolves to the current block number.

    • Throws: An error if the API call fails.

Usage

The BlockApi class is used to interact with the blockchain block-related functionalities.

// Fetch a specific block
client.block.getBlock(1234).then(block => {
  console.log(block);
});

// Fetch the current block number
client.api.getBlockNumber().then(blockNumber => {
  console.log(blockNumber);
});

ChainApi Class

Handles API calls related to blockchain chain data. This class is responsible for fetching information specific to the blockchain network, such as its chain ID.

Methods

  • getChainId(): Promise<number> Retrieves the unique identifier (ID) of the blockchain network.

    • Returns: A promise that resolves to the chain ID as a number.

    • Throws: An error if the API call fails.

Usage

The ChainApi class is used to interact with chain-related data of the blockchain. It requires an instance of ApiClient to make API requests.

// Fetch the chain ID
client.chain.getChainId().then(chainId => {
  console.log(`Current Chain ID: ${chainId}`);
});

ContractApi Class

Responsible for handling API calls related to smart contracts on the blockchain. This class provides functionalities for interacting with smart contracts, including calling contract functions.

Methods

  • callContract(data: CallContractRequest): Promise<string> Calls a function on a smart contract.

    CallContractRequest

    • Interface representing a request to call a contract function.

      • functionSignature: string - The signature of the contract function to be called.

      • params: string[] - An array of parameters for the contract function.

      • contractAddress: string - The address of the smart contract on the blockchain.

    • Returns: A promise that resolves to the response from the contract call as a string.

    • Throws: An error if the API call fails.

Usage

const request: CallContractRequest = {
    functionSignature:"function balanceOf(address) view returns (uint256)",
    params:["0xABD....."]
    contractAddress: "0x0000...."
}

ERC20Api Class

Handles API calls related to ERC20 tokens. This class provides functionalities for various ERC20 token operations, such as querying balances, initiating transfers, and managing allowances.

Methods

  • getBalance(request: ERC20BalanceRequest): Promise<string> Retrieves the balance of an ERC20 token for a given address.

    ERC20BalanceRequest

    • Interface for ERC20 balance request.

      • address: string - The address whose token balance is being queried.

      • contractAddress: string - The contract address of the ERC20 token.

    • Parameters

      • request: ERC20BalanceRequest - The request data.

    • Returns: A promise that resolves to the token balance as a string.

    • Throws: An error if the API call fails.

    • Usage:

const balanceRequest: ERC20BalanceRequest = {
  address: '0x123...',
  contractAddress: '0xABC...'
};

client.erc20.getBalance(balanceRequest).then(balance => {
  console.log(`Token Balance: ${balance}`);
});
  • transfer(request: ERC20TransferRequest): Promise<string> Initiates a transfer of ERC20 tokens.

    ERC20TransferRequest

    • Interface for ERC20 transfer request.

      • address: string - The address initiating the transfer.

      • amount: string - The amount of tokens to transfer.

      • contractAddress: string - The contract address of the ERC20 token.

    • Parameters

      • request: ERC20TransferRequest - The request data.

    • Returns: A promise that resolves to the transaction result as a string.

    • Throws: An error if the API call fails.

    • Usage:

const transferRequest: ERC20TransferRequest = {
  address: '0x123...',
  amount: '100',
  contractAddress: '0xABC...'
};

client.erc20.transfer(transferRequest).then(result => {
  console.log(`Transfer Result: ${result}`);
});
  • approve(request: ERC20ApproveRequest): Promise<string> Approves a spender to withdraw from your account, multiple times, up to the specified amount.

    ERC20ApproveRequest

    • Interface for ERC20 approve request.

      • address: string - The address granting the approval.

      • amount: string - The maximum amount the spender can withdraw.

      • contractAddress: string - The contract address of the ERC20 token.

    • Parameters

      • request: ERC20ApproveRequest - The request data.

    • Returns: A promise that resolves to the approval result as a string.

    • Throws: An error if the API call fails.

    • Usage:

const approveRequest: ERC20ApproveRequest = {
  address: '0x123...',
  amount: '1000',
  contractAddress: '0xABC...'
};

client.erc20.approve(approveRequest).then(result => {
  console.log(`Approve Result: ${result}`);
});
  • transferFrom(request: ERC20TransferFromRequest): Promise<string> Transfers tokens from one address to another.

    ERC20TransferFromRequest

    • Interface for ERC20 transferFrom request.

      • contractAddress: string - The contract address of the ERC20 token.

      • from: string - The address from which the tokens are withdrawn.

      • to: string - The address to which the tokens are sent.

      • amount: string - The amount of tokens to transfer.

    • Parameters

      • request: ERC20TransferFromRequest - The request data.

    • Returns: A promise that resolves to the transaction result as a string.

    • Throws: An error if the API call fails.

    • Usage:

const transferFromRequest: ERC20TransferFromRequest = {
  contractAddress: '0xABC...',
  from: '0x123...',
  to: '0x456...',
  amount: '500'
};

client.erc20.transferFrom(transferFromRequest).then(result => {
  console.log(`TransferFrom Result: ${result}`);
});
  • allowance(request: ERC20AllowanceRequest): Promise<string> Retrieves the amount which a spender is still allowed to withdraw from an owner.

    ERC20AllowanceRequest

    • Interface for ERC20 allowance request.

      • owner: string - The address of the token owner.

      • spender: string - The address of the spender.

      • contractAddress: string - The contract address of the ERC20 token.

    • Parameters

      • request: ERC20AllowanceRequest - The request data.

    • Returns: A promise that resolves to the allowance amount as a string.

    • Throws: An error if the API call fails.

    • Usage:

const allowanceRequest: ERC20AllowanceRequest = {
  owner: '0x123...',
  spender: '0x456...',
  contractAddress: '0xABC...'
};

client.erc20.allowance(allowanceRequest).then(allowance => {
  console.log(`Allowance: ${allowance}`);
});
  • totalSupply(request: ERC20TotalSupplyRequest): Promise<string> Gets the total token supply.

    ERC20TotalSupplyRequest

    • Interface for ERC20 total supply request.

      • contractAddress: string - The contract address of the ERC20 token.

    • Parameters

      • request: ERC20TotalSupplyRequest - The request data.

    • Returns: A promise that resolves to the total token supply as a string.

    • Throws: An error if the API call fails

    • Usage:

const totalSupplyRequest: ERC20TotalSupplyRequest = {contractAddress: '0xABC...'};

client.erc20.totalSupply(totalSupplyRequest).then(totalSupply => {
  console.log(`Total Supply: ${totalSupply}`);
});


ERC721Api Class

Handles API calls for ERC721 token operations.

Methods

  1. getBalance(request: ERC721BalanceRequest): Promise<string>

    • Gets the balance of ERC721 tokens for a given address.

    • Parameters:

      • Interface for ERC721 balance request

        • address: string address to query balance of

        • contractAddress: string contract address of the ERC721 Token

      • request: An object of type ERC721BalanceRequest

    • Returns: A promise that resolves to the token balance as a string.

    • Usage Example:

      client.erc721.getBalance({ address: '0x...', contractAddress: '0x...' })
        .then(balance => console.log(balance))
        .catch(error => console.error(error));
  2. transfer(request: ERC721TransferRequest): Promise<string>

    • Initiates a transfer of ERC721 tokens.

    • Parameters:

      • Interface of ERC721 Transfer request

        • from: string sender address

        • to: string reciepent address

        • tokenId: string ERC721 tokenId

        • contractAddress: string contract address of the ERC721 token.

      • request: An object of type ERC721TransferRequest

    • Returns: A promise that resolves to the transaction result.

    • Usage Example:

      client.erc721.transfer({ from: '0x...', to: '0x...', tokenId: '123', contractAddress: '0x...' })
        .then(result => console.log(result))
        .catch(error => console.error(error));
  3. approve(request: ERC721ApproveRequest): Promise<string>

    • Approves an address to transfer a specific token.

    • Parameters:

      • Interface of ERC721 approve request

        • approvedAddress: string address to approve

        • tokenId: string ERC721 token Id

        • contractAddress: string contract address of ERC721 Token

      • request: An object of type ERC721ApproveRequest

    • Returns: A promise that resolves to the approval result.

    • Usage Example:

      client.erc721.approve({ approvedAddress: '0x...', tokenId: '123', contractAddress: '0x...' })
        .then(approval => console.log(approval))
        .catch(error => console.error(error));
  4. safeTransferFrom(request: ERC721TransferRequest): Promise<string>

    • Safely transfers ERC721 tokens.

    • Parameters:

      • request: Same as in transfer method.

    • Returns: A promise that resolves to the transaction result.

    • Usage Example:

      client.erc721.safeTransferFrom({ from: '0x...', to: '0x...', tokenId: '123', contractAddress: '0x...' })
        .then(result => console.log(result))
        .catch(error => console.error(error));
  5. approved(request: ERC721ApprovedRequest): Promise<string>

    • Retrieves the approved address for a specific token.

    • Parameters:

      • Interface of ERC721 Approved request

        • tokenId: string token Id of ERC721

        • contractAddress: string contract address of ERC721 Token

      • request: An object of type ERC721ApprovedRequest

    • Returns: A promise that resolves to the approved address.

    • Usage Example:

      client.erc721.approved({ tokenId: '123', contractAddress: '0x...' })
        .then(address => console.log(address))
        .catch(error => console.error(error));
  6. setApprovalForAll(request: ERC721SetApprovalForAllRequest): Promise<string>

    • Sets or unsets the approval of an operator.

    • Parameters:

      • Interface of Set approval for all request

        • operator: string address to give approval to

        • approved: boolean true to give approval, false to revoke

        • contractAddress: string contract address of the ERC721 token

      • request: An object of type ERC721SetApprovalForAllRequest containing operator, approved, and contractAddress.

    • Returns: A promise that resolves to the transaction result.

    • Usage Example:

      client.erc721.setApprovalForAll({ operator: '0x...', approved: true, contractAddress: '0x...' })
        .then(result => console.log(result))
        .catch(error => console.error(error));
  7. isApprovedForAll(request: ERC721IsApprovedForAllRequest): Promise<string>

    • Checks if an operator is approved to manage all tokens of an owner.

    • Parameters:

      • Interface of Is approved for all request

        • owner: string owner address

        • operator: string operator address

        • contractAddress: string contract address of ERC721 Token

      • request: An object of type ERC721IsApprovedForAllRequest

    • Returns: A promise that resolves to a boolean indicating approval status.

    • Usage Example:

client.erc721.isApprovedForAll({ owner: '0x...', operator: '0x...', contractAddress: '0x...' })
      .then(approved => console.log(approved))
      .catch(error => console.error(error));
  1. getTokenOfOwnerByIndex(request: ECR721GetTokenOfOwnerByIndexRequest): Promise<string>

    • Gets the tokenId at a given index of tokens owned by an address.

    • Parameters:

      • Interface of Get token of owner by index request

        • address: string owner address

        • index: number index of token to get Id of

        • contractAddress: string contract address of ERC721 Token

      • request: An object of type ECR721GetTokenOfOwnerByIndexRequest

    • Returns: A promise that resolves to the tokenId.

    • Usage Example:

 client.erc721.getTokenOfOwnerByIndex({ address: '0x...', contractAddress: '0x...', index: 0 })
      .then(tokenId => console.log(tokenId))
      .catch(error => console.error(error));
  1. getAllTokensOfOwner(request: ERC721GetAllTokensOfOwnerRequest): Promise<string[]>

    • Gets all tokenIds of tokens owned by an address.

    • Parameters:

      • Interface of Get all tokens of owner request

        • address: string owner address

        • contractAddress: string contract address of ERC721 Token

      • request: An object of type ERC721GetAllTokensOfOwnerRequest

    • Returns: A promise that resolves to an array of tokenIds.

    • Usage Example:

client.erc721.getAllTokensOfOwner({ address: '0x...', contractAddress: '0x...' })
      .then(tokenIds => console.log(tokenIds))
      .catch(error => console.error(error));
  1. getTokenURI(request: ECR721GetTokenURI): Promise<string>

    • Gets the URI of an ERC721 token.

    • Parameters:

      • Interface of Get Token URI

        • tokenId: string ERC721 token Id

        • contractAddress: string contract address of ERC721 Token

      • request: An object of type ECR721GetTokenURI containing tokenId and contractAddress.

    • Returns: A promise that resolves to the token URI.

    • Usage Example:

client.erc721.getTokenURI({ tokenId: '123', contractAddress: '0x...' })
       .then(uri => console.log(uri))
       .catch(error => console.error(error));

TransactionApi Class

Handles API calls for blockchain transaction operations.

Methods

  1. submitTransactionRequest(transactionRequest: TransactionRequest): Promise<TransactionResponse>

    • Submits a transaction request to the blockchain.

    • Parameters:

      • transactionRequest: An object of type TransactionRequest containing details of the transaction such as functionSignature, params, contractAddress, and optionally value.

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

    • Usage Example:

      client.transaction.submitTransactionRequest({
        functionSignature: '0x...',
        params: [...],
        contractAddress: '0x...',
        value: '0x...'
      })
      .then(response => console.log(response.hash))
      .catch(error => console.error(error));
  2. getTransaction(hash: string): Promise<Transaction>

    • Retrieves a transaction by its hash.

    • Parameters:

      • hash: The hash of the transaction to be retrieved.

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

    • Usage Example:

      client.transaction.getTransaction('0x...')
        .then(transaction => console.log(transaction))
        .catch(error => console.error(error));
  3. getTransactionReceipt(hash: string): Promise<TransactionReceipt>

    • Retrieves the receipt of a transaction by its hash.

    • Parameters:

      • hash: The hash of the transaction for which the receipt is to be retrieved.

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

    • Usage Example:

      client.transaction.getTransactionReceipt('0x...')
        .then(receipt => console.log(receipt))
        .catch(error => console.error(error));

Based on the TypeScript class provided, here's the reference documentation for the UserApi class, which handles API calls related to user information and accounts. Each function's description includes its purpose, input parameters, return type, and a usage example.


UserApi Class

Handles API calls for user information and account operations.

Methods

  1. getUser(): Promise<User>

    • Retrieves the details of the current user.

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

    • Usage Example:

      client.user.getUser()
        .then(user => console.log(user))
        .catch(error => console.error(error));
  2. getBalance(): Promise<string>

    • Retrieves the balance of the current user.

    • Returns: A promise that resolves to the balance as a string.

    • Usage Example:

      client.user.getBalance()
        .then(balance => console.log(balance))
        .catch(error => console.error(error));
  3. getAccounts(): Promise<string[]>

    • Retrieves the accounts associated with the current user.

    • Returns: A promise that resolves to an array of account strings.

    • Usage Example:

      client.user.getAccounts()
        .then(accounts => console.log(accounts))
        .catch(error => console.error(error));

Last updated