import { TronsaveSDK } from "tronsave-sdk";
const main = async () => {
const apiKey = "your_api_key";
const sdk = new TronsaveSDK({ network: "testnet", apiKey });
const userInfo = await sdk.getUserInfo();
console.log(userInfo);
// Example: Estimate cost for 32,000 ENERGY units for 1 hour
const estimate = await sdk.estimateBuyResource({
receiver: "TAk6jzZqHwNUkUcbvMyAE1YAoUPk7r2T6h",
resourceType: "ENERGY",
durationSec: 3600, // 1 hour
resourceAmount: 32000,
});
console.log(estimate);
if (estimate.estimateTrx > Number(userInfo.balance)) throw new Error("Insufficient balance");
const { orderId } = await sdk.buyResource({
receiver: "TAk6jzZqHwNUkUcbvMyAE1YAoUPk7r2T6h",
resourceType: "ENERGY",
durationSec: 3600, // 1 hour
resourceAmount: 32000,
});
console.log(`Buy resource success -> orderId: ${orderId}`);
// Wait for 5 seconds for the order to be filled
console.log("Waiting for 5 seconds for the order to be filled...");
await new Promise((resolve) => setTimeout(resolve, 5000));
const order = await sdk.getOrder(orderId);
console.log(order);
if (order.fulfilledPercent < 100) console.log("Order is not filled");
else console.log("Order is filled");
};
main();