# Whales Pre-Market Aptos SDK

TypeScript SDK for interacting with the Whales Pre-Market smart contract on Aptos blockchain.

## Installation

```bash
npm install whales-pre-market-aptos-sdk
```

## Usage

```typescript
import {
  PreMarketAptos,
  OfferType,
  signDiscount,
  WEI6,
} from "whales-pre-market-aptos-sdk";
import { Network, APTOS_FA } from "@aptos-labs/ts-sdk";

// Initialize the SDK
const sdk = new PreMarketAptos(
  {
    network: Network.MAINNET, // or 'TESTNET'
    fullnode: "custom_fullnode_url",
    clientConfig: {
      WITH_CREDENTIALS: true,
      API_KEY: "api_key_here",
    },
  },
  {
    apiUrl: "no_code_indexing_api",
    authorizationKey: "api_key",
  }
);

// Get configuration
const config = await sdk.getConfig();

// Get token configuration
const tokenConfig = await sdk.getTokenConfig("token_config_address");

// Create an offer
const keypair = new Ed25519PrivateKey("private_key_here");
const account = Account.fromPrivateKey({ privateKey: keypair });

const tokenConfig = "token_config_address";
const exToken = APTOS_FA;
const offerType = OfferType.Buy;
const amount = 100;
const value = 1000;
const fullMatch = true;

const { rawTx } = await preMarket.buildCreateOffer(
  account.accountAddress,
  tokenConfig,
  exToken,
  offerType,
  amount,
  value,
  fullMatch
);

const pendingTxn = await preMarket.aptos.signAndSubmitTransaction({
  signer: account,
  transaction: rawTx,
});
const response = await preMarket.aptos.waitForTransaction({
  transactionHash: pendingTxn.hash,
});
console.log("Tx succeed. - ", response.hash);

// build cancel with discount
const offer = "offer_object_address";
const discountData = {
  id: "offer_object_address",
  sellerDiscount: WEI6 / 2,
  buyerDiscount: WEI6 / 5,
};

const settleDiscount = signDiscount(
  "discount_signer_private_key",
  discountData.id,
  discountData.buyerDiscount,
  discountData.sellerDiscount
);

const cancelTx = await preMarket.buildCancelOfferWithDiscount(
  ownerAccount.accountAddress,
  offer,
  settleDiscount
);

// crawl events
let startIndex = 0;
const limit = 25;

while (true) {
  const events = await preMarket.eventCrawler.getEvents(startIndex, limit);
  console.log(events);
  startIndex += events.length;

  if (events.length === 0) {
    break;
  }
}

// crawl events with graphQL
let lastTxVersion = 0;
const limit = 25;

while (true) {
  const events = await preMarket.eventCrawler.getEventsV2(lastTxVersion, limit);
  console.log(events);

  if (events.length > 0) {
    lastTxVersion = events[events.length - 1]?.transaction_version;
  }

  if (events.length === 0) {
    break;
  }
}
```

## Features

- ✅ Configuration management
- ✅ Token operations
- ✅ Offer creation and management
- ✅ Order settlement
- ✅ Role-based access control
- ✅ GraphQL client for event querying
- ✅ TypeScript support with full type definitions

## API Documentation

### Core Methods

#### Configuration & Query

- `getConfig()` - Get platform configuration
- `getTokenConfig(tokenConfigId)` - Get token configuration
- `getOffer(offerId)` - Get offer details
- `getOrder(orderId)` - Get order details
- `getRole(user)` - Get user role
- `isTokenAccepted(token)` - Check if token is accepted

#### Owner Functions

- `buildMigrate(owner)` - Migrate contract
- `buildUpdateConfig(owner, params)` - Update platform configuration
- `buildAddRole(owner, user, role)` - Add user role
- `buildRemoveRole(owner, user)` - Remove user role

#### Admin Functions

- `buildTokenForceCancelSettlePhase(admin, tokenConfig)` - Force cancel token settle phase
- `buildCancelToken(admin, tokenConfig)` - Cancel token

#### Operator Functions

- `buildAcceptExToken(operator, token)` - Accept external token
- `buildRemoveExToken(operator, token)` - Remove external token
- `buildCreateToken(operator, tokenIndex, settleDuration)` - Create new token
- `buildTokenToSettlePhase(operator, tokenConfig, token, settleRate)` - Move token to settle phase
- `buildToggleTokenStatus(operator, tokenConfig)` - Toggle token status
- `buildUpdateSettleDuration(operator, tokenConfig, newDuration)` - Update settle duration
- `buildForceCancelOrder(operator, order)` - Force cancel order

#### User Functions

- `buildCreateOffer(sender, tokenConfig, exToken, offerType, amount, value, fullMatch)` - Create a new offer
- `buildFillOffer(sender, offer, amount)` - Fill an existing offer
- `buildCancelOffer(sender, offer)` - Cancel an offer
- `buildCancelOfferWithDiscount(sender, offer, discount)` - Cancel offer with discount
- `buildSettleFilled(sender, order)` - Settle a filled order
- `buildSettleFilledWithDiscount(sender, order, discount)` - Settle filled order with discount
- `buildSettleCancelled(sender, order)` - Settle a cancelled order
- `buildSettleCancelledWithDiscount(sender, order, discount)` - Settle cancelled order with discount

## License

MIT
