whales-pre-market-aptos-sdk
v1.5.1
Published
TypeScript SDK for Whales Pre-Market on Aptos blockchain
Maintainers
Readme
Whales Pre-Market Aptos SDK
TypeScript SDK for interacting with the Whales Pre-Market smart contract on Aptos blockchain.
Installation
npm install whales-pre-market-aptos-sdkUsage
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 configurationgetTokenConfig(tokenConfigId)- Get token configurationgetOffer(offerId)- Get offer detailsgetOrder(orderId)- Get order detailsgetRole(user)- Get user roleisTokenAccepted(token)- Check if token is accepted
Owner Functions
buildMigrate(owner)- Migrate contractbuildUpdateConfig(owner, params)- Update platform configurationbuildAddRole(owner, user, role)- Add user rolebuildRemoveRole(owner, user)- Remove user role
Admin Functions
buildTokenForceCancelSettlePhase(admin, tokenConfig)- Force cancel token settle phasebuildCancelToken(admin, tokenConfig)- Cancel token
Operator Functions
buildAcceptExToken(operator, token)- Accept external tokenbuildRemoveExToken(operator, token)- Remove external tokenbuildCreateToken(operator, tokenIndex, settleDuration)- Create new tokenbuildTokenToSettlePhase(operator, tokenConfig, token, settleRate)- Move token to settle phasebuildToggleTokenStatus(operator, tokenConfig)- Toggle token statusbuildUpdateSettleDuration(operator, tokenConfig, newDuration)- Update settle durationbuildForceCancelOrder(operator, order)- Force cancel order
User Functions
buildCreateOffer(sender, tokenConfig, exToken, offerType, amount, value, fullMatch)- Create a new offerbuildFillOffer(sender, offer, amount)- Fill an existing offerbuildCancelOffer(sender, offer)- Cancel an offerbuildCancelOfferWithDiscount(sender, offer, discount)- Cancel offer with discountbuildSettleFilled(sender, order)- Settle a filled orderbuildSettleFilledWithDiscount(sender, order, discount)- Settle filled order with discountbuildSettleCancelled(sender, order)- Settle a cancelled orderbuildSettleCancelledWithDiscount(sender, order, discount)- Settle cancelled order with discount
License
MIT
