@suiware/ai-tools
Version:
Pluggable tools for Vercel AI SDK which allow AI assistants to interact with Sui Network and perform various actions.
108 lines (106 loc) • 3.41 kB
JavaScript
import { SuiService, __async, suiToMist, formatBalance } from './chunk-HFORSGSH.mjs';
import { Transaction } from '@mysten/sui/transactions';
import { SUI_SYSTEM_STATE_OBJECT_ID } from '@mysten/sui/utils';
var SuiStakingService = class {
constructor() {
// @todo: Make the validator address configurable.
this.VALIDATOR_ADDRESSES = {
mainnet: "0x4fffd0005522be4bc029724c7f0f6ed7093a6bf3a09b90e62f61dc15181e1a3e",
// Mysten-1
testnet: "0x6d6e9f9d3d81562a0f9b767594286c69c21fea741b1c2303c5b7696d6c63618a"
// Mysten-1
};
this.suiService = SuiService.getInstance();
}
/**
* Stake SUI tokens to a validator.
*
* @param amount - The amount to stake in whole SUI units.
* @returns The transaction digest.
*/
stake(amount) {
return __async(this, null, function* () {
const amountInMist = suiToMist(amount);
const tx = new Transaction();
const [coin] = tx.splitCoins(tx.gas, [tx.pure.u64(amountInMist)]);
const validatorAddress = this.VALIDATOR_ADDRESSES[this.suiService.getNetwork()];
tx.moveCall({
target: "0x3::sui_system::request_add_stake",
arguments: [
tx.object(SUI_SYSTEM_STATE_OBJECT_ID),
coin,
tx.pure.address(validatorAddress)
]
});
const response = yield this.suiService.executeTransaction(tx);
if (!response.digest) {
throw new Error("Staking transaction failed");
}
return response.digest;
});
}
/**
* Unstake all stakes.
*
* @returns The transaction digests.
*/
unstake() {
return __async(this, null, function* () {
const stakedObjects = yield this.getStakedObjects();
if (stakedObjects.length === 0) {
throw new Error("No staked objects found");
}
const tx = new Transaction();
stakedObjects.forEach((x) => {
x.stakes.forEach(({ stakedSuiId }) => {
tx.moveCall({
target: "0x3::sui_system::request_withdraw_stake",
arguments: [
tx.object(SUI_SYSTEM_STATE_OBJECT_ID),
tx.object(stakedSuiId)
]
});
});
});
const response = yield this.suiService.executeTransaction(tx);
if (!response.digest) {
throw new Error("Unstaking transaction failed");
}
return response.digest;
});
}
/**
* Get all staked objects.
*
* @returns Array of delegated stake objects.
*/
getStakedObjects() {
return __async(this, null, function* () {
const stakedObjects = yield this.suiService.getSuiClient().getStakes({
owner: this.suiService.getAddress()
});
return stakedObjects;
});
}
/**
* Get the total staked Sui balance.
*
* @returns The total staked balance.
*/
getTotalStakedBalance() {
return __async(this, null, function* () {
const stakedObjects = yield this.getStakedObjects();
const amountMist = stakedObjects.reduce((total, delegatedStake) => {
const stakesTotal = delegatedStake.stakes.reduce(
(stakeTotal, stake) => stakeTotal + BigInt(stake.principal),
BigInt(0)
);
return total + stakesTotal;
}, BigInt(0));
return formatBalance(amountMist, 9);
});
}
};
export { SuiStakingService };
//# sourceMappingURL=chunk-EIQJRNDC.mjs.map
//# sourceMappingURL=chunk-EIQJRNDC.mjs.map