0.3.0 • Published 1 year ago

@aori/aori-ts v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Aori TypeScript SDK

aori-ts banner

https://devs.aori.io GitHub issues

Getting Started

Installation

bun add @aori/aori-ts

or

npm install @aori/aori-ts

or

yarn add @aori/aori-ts

Authorization

Interacting with the Aori API currently requires an API key. Inquire at https://aori.io/contact

When you have your API key, you can include it in any API request by passing it as an additional parameter to any of the SDK functions:

import { getQuote, submitSwap } from '@aori/aori-ts';

// Load API key from your preferred method
const apiKey = process.env.AORI_API_KEY;

// Use it with any API call
const quote = await getQuote(quoteRequest, 'https://api.aori.io', apiKey);

// Then submit a swap with the same key
const swap = await submitSwap(swapRequest, 'https://api.aori.io', apiKey);

You can also use API keys with WebSocket connections:

import { AoriWebSocket } from '@aori/aori-ts';

// Initialize websocket with API key
const ws = new AoriWebSocket('wss://api.aori.io', {
  onMessage: (event) => console.log(event),
  onConnect: () => console.log('Connected!'),
}, apiKey);

await ws.connect();

API Reference

MethodEndpointDescriptionRequest Body
GET/chainsGet a list of supported chains-
POST/quoteGet a quote<QuoteRequest>
POST/swapExecute Swap<SwapRequest>
GET/dataQuery Historical Orders Database-
GET/data/status/{orderHash}Get Swap Details/Status-
WS/streamOpen a Websocket Connection-

/quote

The swap endpoint acts as the primary endpoint for users to request quotes.

Example QuateRequest

curl -X POST https://v3development.api.aori.io/quote \
-H "Content-Type: application/json" \
-H "x-api-key: your_api_key_here" \
-d '{
    "offerer": "0x...",
    "recipient": "0x...",
    "inputToken": "0x...",
    "outputToken": "0x...",
    "inputAmount": "1000000000000000000",
    "inputChain": "base",
    "outputChain": "arbitrum"
}'

Example QuoteResponse

{
  "orderHash": "0x9a3af...",
  "signingHash": "0xas23f...",
  "offerer": "0x...",
  "recipient": "0x...",
  "inputToken": "0x...",
  "outputToken": "0x...",
  "inputAmount": "1000000000000000000",
  "outputAmount": "1000000000000000000",
  "inputChain": "base",
  "outputChain": "arbitrum",
  "startTime": "1700000000",
  "endTime": "1700000010",
  "estimatedTime": 3000,
}

/swap

The swap endpoint acts as the primary endpoint for users to post signed orders for execution.

Example SwapRequest

curl -X POST https://api.aori.io/swap \
-H "Content-Type: application/json" \
-d  'orderHash: "0x...",
    'signature': "0x...",

Example SwapResponse

{
  "orderHash": "0x9a3af...",
  "offerer": "0x0000000000000000000000000000000000000001",
  "recipient": "0x0000000000000000000000000000000000000001",
  "inputToken": "0x0000000000000000000000000000000000000002",
  "outputToken": "0x0000000000000000000000000000000000000003",
  "inputAmount": "1000000000000000000",
  "outputAmount": "1000000000000000000",
  "inputChain": "base",
  "outputChain": "arbitrum",
  "startTime": "1700000000",
  "endTime": "1700000010",
  "status": "pending",
  "createdAt": "1700000000"
}

/data

The data endpoint acts as the primary endpoint for users to query historical orders.

Parameters

ParameterTypeDescription
order_hashStringHash of the order
offererStringAddress of the order creator
recipientStringAddress of the order recipient
input_tokenStringAddress of the token being sent
input_amountStringAmount of input token
output_tokenStringAddress of the token being received
output_amountStringAmount of output token
input_chainStringChain key for the input token (e.g., "arbitrum")
output_chainStringChain key for the output token (e.g., "base")
src_txOptionSource chain transaction hash
dst_txOptionDestination chain transaction hash
statusStringOrder status (Pending, Received, Filled, Confirmed, Failed)
min_timeu64Unix timestamp, start of filter range by created_at
max_timeu64Unix timestamp, end of filter range by created_at
pageu64Page number (1-x)
limitu64Results per page (1-100)

Chains

ChainchainKeychainIdeidaddressvm
Ethereumethereum1301010x98AD96Ef787ba5180814055039F8E37d98ADea63EVM
Basebase8453301840xFfe691A6dDb5D2645321e0a920C2e7Bdd00dD3D8EVM
Arbitrumarbitrum42161301100xFfe691A6dDb5D2645321e0a920C2e7Bdd00dD3D8EVM
Optimismoptimism10301110xFfe691A6dDb5D2645321e0a920C2e7Bdd00dD3D8EVM

SDK Functions

FunctionDescriptionParametersReturn Type
getQuoteRequests a quote for a token swaprequest: QuoteRequest, baseUrl?: string, apiKey?: stringPromise<QuoteResponse>
signOrderSigns an order using the provided private keyquoteResponse: QuoteResponse, signer: SignerTypePromise<string>
signReadableOrderSigns an order using EIP-712 typed data (for frontends)quoteResponse: QuoteResponse, signer: TypedDataSigner, userAddress: stringPromise<{orderHash: string, signature: string}>
submitSwapSubmits a signed swap order to the Aori API.request: SwapRequest, baseUrl?: string, apiKey?: stringPromise<SwapResponse>
getOrderStatusGets the current status of an order.orderHash: string, baseUrl?: string, apiKey?: stringPromise<OrderStatus>
getChainsFetches the list of supported chains and their configurations.baseUrl?: string, apiKey?: stringPromise<ChainInfo[]>
pollOrderStatusPolls the status of an order until completion or timeout.orderHash: string, baseUrl?: string, options?: PollOrderStatusOptions, apiKey?: stringPromise<OrderStatus>
getOrderDetailsFetches detailed information about an order.orderHash: string, baseUrl?: string, apiKey?: stringPromise<OrderDetails>
queryOrdersQueries orders with filtering criteria.baseUrl: string, params: QueryOrdersParams, apiKey?: stringPromise<QueryOrdersResponse>

Examples

Using API Keys with Environment Variables

This example demonstrates how to use API keys from environment variables:

import dotenv from 'dotenv';
import { 
  getQuote, 
  submitSwap, 
  getOrderStatus 
} from '@aori/aori-ts';

// Load environment variables
dotenv.config();

async function executeSwap() {
  // Get API key from environment
  const apiKey = process.env.AORI_API_KEY;
  
  // Create a quote request
  const quoteRequest = {
    offerer: '0xYourAddress',
    recipient: '0xYourAddress',
    inputToken: '0xInputTokenAddress',
    outputToken: '0xOutputTokenAddress',
    inputAmount: '1000000000000000000', // 1 token with 18 decimals
    inputChain: 'arbitrum',
    outputChain: 'base'
  };
  
  // Include API key in all requests
  const quote = await getQuote(quoteRequest, 'https://api.aori.io', apiKey);
  console.log('Quote received:', quote);
  
  // ... sign the quote ...
  
  // Submit the swap with API key
  const swapResponse = await submitSwap({
    orderHash: quote.orderHash,
    signature: signature
  }, 'https://api.aori.io', apiKey);
  
  // Check order status with API key
  const status = await getOrderStatus(swapResponse.orderHash, 'https://api.aori.io', apiKey);
  console.log('Order status:', status);
}

executeSwap().catch(console.error);

Executing an Order with a Wallet in a frontend application

This example demonstrates how to use the SDK with a wallet in a frontend application:

import { useAccount } from "wagmi";
import {
  getQuote,
  signReadableOrder,
  submitSwap,
  getOrderStatus,
} from "aori-ts";

// React component example
function SwapComponent() {
  const { address, connector } = useAccount();
  
  // Get API key from environment variables or a secured source
  const apiKey = process.env.REACT_APP_AORI_API_KEY;

  const handleSwap = async () => {
    try {
      // 1. Get the quote first
      const quoteRequest = {
        offerer: address,
        recipient: address,
        inputToken: "0x...",
        outputToken: "0x...",
        inputAmount: "1000000000",
        inputChain: "base",
        outputChain: "arbitrum",
      };

      const quote = await getQuote(quoteRequest, "https://api.aori.io", apiKey);

      // 2. Get the wallet client:
      const walletClient = await connector?.getWalletClient();

      // 3. Create a wrapper for the wallet client:
      const walletWrapper = {
        signTypedData: async (params) => {
          return walletClient.signTypedData({
            account: params.account,
            domain: params.domain,
            types: params.types,
            primaryType: params.primaryType,
            message: params.message,
          });
        },
      };

      // 4. Sign the order using EIP-712:
      const { orderHash, signature } = await signReadableOrder(
        quote,
        walletWrapper,
        address
      );

      // 5. Submit the swap with signature (including API key):
      const swapRequest = {
        orderHash,
        signature,
      };

      const swapResponse = await submitSwap(swapRequest, "https://api.aori.io", apiKey);
      console.log("Swap submitted successfully:", swapResponse);

      // 6. Check current order status (including API key):
      const status = await getOrderStatus(swapResponse.orderHash, "https://api.aori.io", apiKey);
      console.log(`Current order status: ${status.type}`);
      // });
    } catch (error) {
      console.error("Swap failed:", error);
    }
  };

  return <button onClick={handleSwap}> Swap Tokens </button>;
}

## License

This project is released under the [MIT License](LICENSE.MD).
0.3.0

1 year ago

0.0.25

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.21

1 year ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago