aftermath-ts-sdk
Version:
Aftermath TypeScript SDK
135 lines (134 loc) • 5.9 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Wallet = void 0;
const caller_1 = require("../utils/caller");
/**
* The `Wallet` class allows querying a user's balances and transactions.
* It handles fetching coin balances, transactions, and more by leveraging
* an `AftermathApi.Wallet` provider.
*/
class Wallet extends caller_1.Caller {
/**
* Creates a new `Wallet` instance for a specific address.
*
* @param address - The Sui address for this wallet (e.g., "0x<address>").
* @param config - An optional caller configuration including network and authentication.
* @param Provider - An optional `AftermathApi` instance for wallet-specific methods.
*/
constructor(address, config, Provider) {
super(config, `wallet/${address}`);
this.address = address;
this.Provider = Provider;
// =========================================================================
// Private Helpers
// =========================================================================
/**
* Internal helper to return the `Wallet` provider from `AftermathApi`, throwing
* an error if the provider is not defined.
*/
this.useProvider = () => {
var _a;
const provider = (_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Wallet();
if (!provider)
throw new Error("missing AftermathApi Provider");
return provider;
};
}
// =========================================================================
// Balances
// =========================================================================
/**
* Fetches the balance for a single coin type in this wallet.
*
* @param inputs - An object containing the `coin` type to look up (e.g., "0x2::sui::SUI").
* @returns A promise that resolves to the coin balance as a bigint.
*
* @example
* ```typescript
*
* const afSdk = new Aftermath("MAINNET");
* await afSdk.init(); // initialize provider
*
* const wallet = afSdk.Wallet("0x<address>");
*
* const suiBalance = await wallet.getBalance({ coin: "0x2::sui::SUI" });
* console.log("SUI Balance:", suiBalance.toString());
* ```
*/
getBalance(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.useProvider().fetchCoinBalance(Object.assign(Object.assign({}, inputs), { walletAddress: this.address }));
});
}
/**
* Fetches the balances for multiple specified coin types in this wallet.
* This method currently returns an array of balances in the same order
* as the requested coins.
*
* @param inputs - An object containing an array of `coins` (coin types).
* @returns A promise resolving to an array of `Balance`s, each matching the corresponding coin in `inputs.coins`.
*
* @example
* ```typescript
* const wallet = new Wallet("0x<address>");
* const balances = await wallet.getBalances({ coins: ["0x2::sui::SUI", "0x<...>"] });
* console.log(balances); // e.g. [1000000000n, 50000000000n]
* ```
*/
getBalances(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi(`balances/coins`, inputs);
});
}
/**
* Fetches all coin balances held by this wallet address, returning a record
* keyed by coin type.
*
* @returns A promise resolving to an object mapping coin types to balances (bigints).
*
* @example
* ```typescript
* const wallet = new Wallet("0x<address>");
* const allBalances = await wallet.getAllBalances();
* console.log(allBalances); // { "0x2::sui::SUI": 1000000000n, "0x<other_coin>": 5000000000n, ... }
* ```
*/
getAllBalances() {
return __awaiter(this, void 0, void 0, function* () {
return this.useProvider().fetchAllCoinBalances({
walletAddress: this.address,
});
});
}
// =========================================================================
// Transactions
// =========================================================================
/**
* Fetches a paginated list of past transactions for this wallet address.
*
* @param inputs - An object implementing `ApiTransactionsBody`, which includes pagination parameters (`cursor`, `limit`) and an optional `order` or other fields.
* @returns A promise that resolves to transaction details, including a cursor if more results exist.
*
* @example
* ```typescript
* const wallet = new Wallet("0x<address>");
* const txHistory = await wallet.getPastTransactions({ cursor: "abc123", limit: 10 });
* console.log(txHistory.transactions, txHistory.nextCursor);
* ```
*/
getPastTransactions(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.useProvider().fetchPastTransactions(Object.assign(Object.assign({}, inputs), { walletAddress: this.address }));
});
}
}
exports.Wallet = Wallet;