@t2000/sdk 0.2.7 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.cjs +72 -43
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +72 -44
- package/dist/index.js.map +1 -1
- package/package.json +12 -12
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { EventEmitter } from 'eventemitter3';
|
|
2
2
|
import { SuiClient } from '@mysten/sui/client';
|
|
3
3
|
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
|
|
4
|
-
import { Transaction } from '@mysten/sui/transactions';
|
|
4
|
+
import { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions';
|
|
5
5
|
|
|
6
6
|
interface T2000Options {
|
|
7
7
|
keyPath?: string;
|
|
@@ -373,6 +373,12 @@ interface ProtocolFeeInfo {
|
|
|
373
373
|
rawAmount: bigint;
|
|
374
374
|
}
|
|
375
375
|
declare function calculateFee(operation: FeeOperation, amount: number): ProtocolFeeInfo;
|
|
376
|
+
/**
|
|
377
|
+
* Add on-chain fee collection to an existing PTB via t2000::treasury::collect_fee().
|
|
378
|
+
* The Move function splits the fee from the payment coin and stores it in the
|
|
379
|
+
* Treasury's internal Balance<T>. Atomic — reverts with the operation if it fails.
|
|
380
|
+
*/
|
|
381
|
+
declare function addCollectFeeToTx(tx: Transaction, paymentCoin: TransactionObjectArgument, operation: FeeOperation): void;
|
|
376
382
|
|
|
377
383
|
interface SimulationResult {
|
|
378
384
|
success: boolean;
|
|
@@ -448,4 +454,4 @@ interface GasStatusResponse {
|
|
|
448
454
|
}
|
|
449
455
|
declare function getGasStatus(address?: string): Promise<GasStatusResponse>;
|
|
450
456
|
|
|
451
|
-
export { type AutoTopUpResult, BPS_DENOMINATOR, type BalanceResponse, type BorrowResult, CLOCK_ID, DEFAULT_NETWORK, type DepositInfo, type EarningsResult, type FeeOperation, type FundStatusResult, type GasExecutionResult, type GasMethod, type GasRequestType, type GasReserve, type GasSponsorResponse, type GasStatusResponse, type HealthFactorResult, MIST_PER_SUI, type MaxBorrowResult, type MaxWithdrawResult, type PositionEntry, type PositionsResult, type ProtocolFeeInfo, type RatesResult, type RepayResult, SENTINEL, SUI_DECIMALS, SUPPORTED_ASSETS, type SaveResult, type SendResult, type SentinelAgent, type SentinelAttackResult, type SentinelVerdict, type SimulationResult, type SupportedAsset, type SwapResult, T2000, T2000Error, type T2000ErrorCode, type T2000ErrorData, type T2000Options, type TransactionRecord, USDC_DECIMALS, type WithdrawResult, calculateFee, executeAutoTopUp, executeWithGas, exportPrivateKey, formatSui, formatUsd, generateKeypair, getAddress, getGasStatus, getPoolPrice, getRates, getSentinelInfo, getSwapQuote, keypairFromPrivateKey, listSentinels, loadKey, mapMoveAbortCode, mapWalletError, mistToSui, rawToUsdc, requestAttack, saveKey, attack as sentinelAttack, settleAttack, shouldAutoTopUp, simulateTransaction, solveHashcash, submitPrompt, suiToMist, throwIfSimulationFailed, truncateAddress, usdcToRaw, validateAddress, walletExists };
|
|
457
|
+
export { type AutoTopUpResult, BPS_DENOMINATOR, type BalanceResponse, type BorrowResult, CLOCK_ID, DEFAULT_NETWORK, type DepositInfo, type EarningsResult, type FeeOperation, type FundStatusResult, type GasExecutionResult, type GasMethod, type GasRequestType, type GasReserve, type GasSponsorResponse, type GasStatusResponse, type HealthFactorResult, MIST_PER_SUI, type MaxBorrowResult, type MaxWithdrawResult, type PositionEntry, type PositionsResult, type ProtocolFeeInfo, type RatesResult, type RepayResult, SENTINEL, SUI_DECIMALS, SUPPORTED_ASSETS, type SaveResult, type SendResult, type SentinelAgent, type SentinelAttackResult, type SentinelVerdict, type SimulationResult, type SupportedAsset, type SwapResult, T2000, T2000Error, type T2000ErrorCode, type T2000ErrorData, type T2000Options, type TransactionRecord, USDC_DECIMALS, type WithdrawResult, addCollectFeeToTx, calculateFee, executeAutoTopUp, executeWithGas, exportPrivateKey, formatSui, formatUsd, generateKeypair, getAddress, getGasStatus, getPoolPrice, getRates, getSentinelInfo, getSwapQuote, keypairFromPrivateKey, listSentinels, loadKey, mapMoveAbortCode, mapWalletError, mistToSui, rawToUsdc, requestAttack, saveKey, attack as sentinelAttack, settleAttack, shouldAutoTopUp, simulateTransaction, solveHashcash, submitPrompt, suiToMist, throwIfSimulationFailed, truncateAddress, usdcToRaw, validateAddress, walletExists };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { EventEmitter } from 'eventemitter3';
|
|
2
2
|
import { SuiClient } from '@mysten/sui/client';
|
|
3
3
|
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
|
|
4
|
-
import { Transaction } from '@mysten/sui/transactions';
|
|
4
|
+
import { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions';
|
|
5
5
|
|
|
6
6
|
interface T2000Options {
|
|
7
7
|
keyPath?: string;
|
|
@@ -373,6 +373,12 @@ interface ProtocolFeeInfo {
|
|
|
373
373
|
rawAmount: bigint;
|
|
374
374
|
}
|
|
375
375
|
declare function calculateFee(operation: FeeOperation, amount: number): ProtocolFeeInfo;
|
|
376
|
+
/**
|
|
377
|
+
* Add on-chain fee collection to an existing PTB via t2000::treasury::collect_fee().
|
|
378
|
+
* The Move function splits the fee from the payment coin and stores it in the
|
|
379
|
+
* Treasury's internal Balance<T>. Atomic — reverts with the operation if it fails.
|
|
380
|
+
*/
|
|
381
|
+
declare function addCollectFeeToTx(tx: Transaction, paymentCoin: TransactionObjectArgument, operation: FeeOperation): void;
|
|
376
382
|
|
|
377
383
|
interface SimulationResult {
|
|
378
384
|
success: boolean;
|
|
@@ -448,4 +454,4 @@ interface GasStatusResponse {
|
|
|
448
454
|
}
|
|
449
455
|
declare function getGasStatus(address?: string): Promise<GasStatusResponse>;
|
|
450
456
|
|
|
451
|
-
export { type AutoTopUpResult, BPS_DENOMINATOR, type BalanceResponse, type BorrowResult, CLOCK_ID, DEFAULT_NETWORK, type DepositInfo, type EarningsResult, type FeeOperation, type FundStatusResult, type GasExecutionResult, type GasMethod, type GasRequestType, type GasReserve, type GasSponsorResponse, type GasStatusResponse, type HealthFactorResult, MIST_PER_SUI, type MaxBorrowResult, type MaxWithdrawResult, type PositionEntry, type PositionsResult, type ProtocolFeeInfo, type RatesResult, type RepayResult, SENTINEL, SUI_DECIMALS, SUPPORTED_ASSETS, type SaveResult, type SendResult, type SentinelAgent, type SentinelAttackResult, type SentinelVerdict, type SimulationResult, type SupportedAsset, type SwapResult, T2000, T2000Error, type T2000ErrorCode, type T2000ErrorData, type T2000Options, type TransactionRecord, USDC_DECIMALS, type WithdrawResult, calculateFee, executeAutoTopUp, executeWithGas, exportPrivateKey, formatSui, formatUsd, generateKeypair, getAddress, getGasStatus, getPoolPrice, getRates, getSentinelInfo, getSwapQuote, keypairFromPrivateKey, listSentinels, loadKey, mapMoveAbortCode, mapWalletError, mistToSui, rawToUsdc, requestAttack, saveKey, attack as sentinelAttack, settleAttack, shouldAutoTopUp, simulateTransaction, solveHashcash, submitPrompt, suiToMist, throwIfSimulationFailed, truncateAddress, usdcToRaw, validateAddress, walletExists };
|
|
457
|
+
export { type AutoTopUpResult, BPS_DENOMINATOR, type BalanceResponse, type BorrowResult, CLOCK_ID, DEFAULT_NETWORK, type DepositInfo, type EarningsResult, type FeeOperation, type FundStatusResult, type GasExecutionResult, type GasMethod, type GasRequestType, type GasReserve, type GasSponsorResponse, type GasStatusResponse, type HealthFactorResult, MIST_PER_SUI, type MaxBorrowResult, type MaxWithdrawResult, type PositionEntry, type PositionsResult, type ProtocolFeeInfo, type RatesResult, type RepayResult, SENTINEL, SUI_DECIMALS, SUPPORTED_ASSETS, type SaveResult, type SendResult, type SentinelAgent, type SentinelAttackResult, type SentinelVerdict, type SimulationResult, type SupportedAsset, type SwapResult, T2000, T2000Error, type T2000ErrorCode, type T2000ErrorData, type T2000Options, type TransactionRecord, USDC_DECIMALS, type WithdrawResult, addCollectFeeToTx, calculateFee, executeAutoTopUp, executeWithGas, exportPrivateKey, formatSui, formatUsd, generateKeypair, getAddress, getGasStatus, getPoolPrice, getRates, getSentinelInfo, getSwapQuote, keypairFromPrivateKey, listSentinels, loadKey, mapMoveAbortCode, mapWalletError, mistToSui, rawToUsdc, requestAttack, saveKey, attack as sentinelAttack, settleAttack, shouldAutoTopUp, simulateTransaction, solveHashcash, submitPrompt, suiToMist, throwIfSimulationFailed, truncateAddress, usdcToRaw, validateAddress, walletExists };
|
package/dist/index.js
CHANGED
|
@@ -38,9 +38,9 @@ var SUPPORTED_ASSETS = {
|
|
|
38
38
|
symbol: "SUI"
|
|
39
39
|
}
|
|
40
40
|
};
|
|
41
|
-
process.env.T2000_PACKAGE_ID ?? "
|
|
42
|
-
process.env.T2000_CONFIG_ID ?? "
|
|
43
|
-
process.env.T2000_TREASURY_ID ?? "
|
|
41
|
+
var T2000_PACKAGE_ID = process.env.T2000_PACKAGE_ID ?? "0xab92e9f1fe549ad3d6a52924a73181b45791e76120b975138fac9ec9b75db9f3";
|
|
42
|
+
var T2000_CONFIG_ID = process.env.T2000_CONFIG_ID ?? "0x408add9aa9322f93cfd87523d8f603006eb8713894f4c460283c58a6888dae8a";
|
|
43
|
+
var T2000_TREASURY_ID = process.env.T2000_TREASURY_ID ?? "0x3bb501b8300125dca59019247941a42af6b292a150ce3cfcce9449456be2ec91";
|
|
44
44
|
var DEFAULT_NETWORK = "mainnet";
|
|
45
45
|
var DEFAULT_RPC_URL = "https://fullnode.mainnet.sui.io:443";
|
|
46
46
|
var DEFAULT_KEY_PATH = "~/.t2000/wallet.key";
|
|
@@ -100,7 +100,8 @@ function mapMoveAbortCode(code) {
|
|
|
100
100
|
6: "Not authorized",
|
|
101
101
|
7: "Package version mismatch \u2014 upgrade required",
|
|
102
102
|
8: "Timelock is active \u2014 wait for expiry",
|
|
103
|
-
9: "No pending change to execute"
|
|
103
|
+
9: "No pending change to execute",
|
|
104
|
+
10: "Already at current version"
|
|
104
105
|
};
|
|
105
106
|
return abortMessages[code] ?? `Move abort code: ${code}`;
|
|
106
107
|
}
|
|
@@ -371,6 +372,61 @@ function inferAction(txBlock) {
|
|
|
371
372
|
if (kind === "ProgrammableTransaction") return "transaction";
|
|
372
373
|
return kind ?? "unknown";
|
|
373
374
|
}
|
|
375
|
+
|
|
376
|
+
// src/protocols/protocolFee.ts
|
|
377
|
+
var FEE_RATES = {
|
|
378
|
+
save: SAVE_FEE_BPS,
|
|
379
|
+
swap: SWAP_FEE_BPS,
|
|
380
|
+
borrow: BORROW_FEE_BPS
|
|
381
|
+
};
|
|
382
|
+
var OP_CODES = {
|
|
383
|
+
save: 0,
|
|
384
|
+
swap: 1,
|
|
385
|
+
borrow: 2
|
|
386
|
+
};
|
|
387
|
+
function calculateFee(operation, amount) {
|
|
388
|
+
const bps = FEE_RATES[operation];
|
|
389
|
+
const feeAmount = amount * Number(bps) / Number(BPS_DENOMINATOR);
|
|
390
|
+
const rawAmount = usdcToRaw(feeAmount);
|
|
391
|
+
return {
|
|
392
|
+
amount: feeAmount,
|
|
393
|
+
asset: "USDC",
|
|
394
|
+
rate: Number(bps) / Number(BPS_DENOMINATOR),
|
|
395
|
+
rawAmount
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
function addCollectFeeToTx(tx, paymentCoin, operation) {
|
|
399
|
+
const bps = FEE_RATES[operation];
|
|
400
|
+
if (bps <= 0n) return;
|
|
401
|
+
tx.moveCall({
|
|
402
|
+
target: `${T2000_PACKAGE_ID}::treasury::collect_fee`,
|
|
403
|
+
typeArguments: [SUPPORTED_ASSETS.USDC.type],
|
|
404
|
+
arguments: [
|
|
405
|
+
tx.object(T2000_TREASURY_ID),
|
|
406
|
+
tx.object(T2000_CONFIG_ID),
|
|
407
|
+
paymentCoin,
|
|
408
|
+
tx.pure.u8(OP_CODES[operation])
|
|
409
|
+
]
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
async function reportFee(agentAddress, operation, feeAmount, feeRate, txDigest) {
|
|
413
|
+
try {
|
|
414
|
+
await fetch(`${API_BASE_URL}/api/fees`, {
|
|
415
|
+
method: "POST",
|
|
416
|
+
headers: { "Content-Type": "application/json" },
|
|
417
|
+
body: JSON.stringify({
|
|
418
|
+
agentAddress,
|
|
419
|
+
operation,
|
|
420
|
+
feeAmount: feeAmount.toString(),
|
|
421
|
+
feeRate: feeRate.toString(),
|
|
422
|
+
txDigest
|
|
423
|
+
})
|
|
424
|
+
});
|
|
425
|
+
} catch {
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/protocols/navi.ts
|
|
374
430
|
var ENV = { env: "prod" };
|
|
375
431
|
var USDC_TYPE = SUPPORTED_ASSETS.USDC.type;
|
|
376
432
|
var RATE_DECIMALS = 27;
|
|
@@ -413,7 +469,7 @@ async function updateOracle(tx, client, address) {
|
|
|
413
469
|
} catch {
|
|
414
470
|
}
|
|
415
471
|
}
|
|
416
|
-
async function buildSaveTx(client, address, amount) {
|
|
472
|
+
async function buildSaveTx(client, address, amount, options = {}) {
|
|
417
473
|
const rawAmount = Number(usdcToRaw(amount));
|
|
418
474
|
const coins = await getCoins(address, { coinType: USDC_TYPE, client });
|
|
419
475
|
if (!coins || coins.length === 0) {
|
|
@@ -422,6 +478,9 @@ async function buildSaveTx(client, address, amount) {
|
|
|
422
478
|
const tx = new Transaction();
|
|
423
479
|
tx.setSender(address);
|
|
424
480
|
const coinObj = mergeCoinsPTB(tx, coins, { balance: rawAmount });
|
|
481
|
+
if (options.collectFee) {
|
|
482
|
+
addCollectFeeToTx(tx, coinObj, "save");
|
|
483
|
+
}
|
|
425
484
|
await depositCoinPTB(tx, USDC_TYPE, coinObj, ENV);
|
|
426
485
|
return tx;
|
|
427
486
|
}
|
|
@@ -439,12 +498,15 @@ async function buildWithdrawTx(client, address, amount) {
|
|
|
439
498
|
tx.transferObjects([withdrawnCoin], address);
|
|
440
499
|
return { tx, effectiveAmount };
|
|
441
500
|
}
|
|
442
|
-
async function buildBorrowTx(client, address, amount) {
|
|
501
|
+
async function buildBorrowTx(client, address, amount, options = {}) {
|
|
443
502
|
const rawAmount = Number(usdcToRaw(amount));
|
|
444
503
|
const tx = new Transaction();
|
|
445
504
|
tx.setSender(address);
|
|
446
505
|
await updateOracle(tx, client, address);
|
|
447
506
|
const borrowedCoin = await borrowCoinPTB(tx, USDC_TYPE, rawAmount, ENV);
|
|
507
|
+
if (options.collectFee) {
|
|
508
|
+
addCollectFeeToTx(tx, borrowedCoin, "borrow");
|
|
509
|
+
}
|
|
448
510
|
tx.transferObjects([borrowedCoin], address);
|
|
449
511
|
return tx;
|
|
450
512
|
}
|
|
@@ -652,40 +714,6 @@ async function getSwapQuote(client, fromAsset, toAsset, amount) {
|
|
|
652
714
|
}
|
|
653
715
|
}
|
|
654
716
|
|
|
655
|
-
// src/protocols/protocolFee.ts
|
|
656
|
-
var FEE_RATES = {
|
|
657
|
-
save: SAVE_FEE_BPS,
|
|
658
|
-
swap: SWAP_FEE_BPS,
|
|
659
|
-
borrow: BORROW_FEE_BPS
|
|
660
|
-
};
|
|
661
|
-
function calculateFee(operation, amount) {
|
|
662
|
-
const bps = FEE_RATES[operation];
|
|
663
|
-
const feeAmount = amount * Number(bps) / Number(BPS_DENOMINATOR);
|
|
664
|
-
const rawAmount = usdcToRaw(feeAmount);
|
|
665
|
-
return {
|
|
666
|
-
amount: feeAmount,
|
|
667
|
-
asset: "USDC",
|
|
668
|
-
rate: Number(bps) / Number(BPS_DENOMINATOR),
|
|
669
|
-
rawAmount
|
|
670
|
-
};
|
|
671
|
-
}
|
|
672
|
-
async function reportFee(agentAddress, operation, feeAmount, feeRate, txDigest) {
|
|
673
|
-
try {
|
|
674
|
-
await fetch(`${API_BASE_URL}/api/fees`, {
|
|
675
|
-
method: "POST",
|
|
676
|
-
headers: { "Content-Type": "application/json" },
|
|
677
|
-
body: JSON.stringify({
|
|
678
|
-
agentAddress,
|
|
679
|
-
operation,
|
|
680
|
-
feeAmount: feeAmount.toString(),
|
|
681
|
-
feeRate: feeRate.toString(),
|
|
682
|
-
txDigest
|
|
683
|
-
})
|
|
684
|
-
});
|
|
685
|
-
} catch {
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
|
|
689
717
|
// src/protocols/yieldTracker.ts
|
|
690
718
|
async function getEarnings(client, keypair) {
|
|
691
719
|
const hf = await getHealthFactor(client, keypair);
|
|
@@ -1278,12 +1306,12 @@ var T2000 = class _T2000 extends EventEmitter {
|
|
|
1278
1306
|
const gasResult = await executeWithGas(
|
|
1279
1307
|
this.client,
|
|
1280
1308
|
this.keypair,
|
|
1281
|
-
() => buildSaveTx(this.client, this._address, saveAmount)
|
|
1309
|
+
() => buildSaveTx(this.client, this._address, saveAmount, { collectFee: true })
|
|
1282
1310
|
);
|
|
1283
1311
|
const rates = await getRates(this.client);
|
|
1284
1312
|
reportFee(this._address, "save", fee.amount, fee.rate, gasResult.digest);
|
|
1285
1313
|
this.emitBalanceChange("USDC", saveAmount, "save", gasResult.digest);
|
|
1286
|
-
let savingsBalance = saveAmount
|
|
1314
|
+
let savingsBalance = saveAmount;
|
|
1287
1315
|
try {
|
|
1288
1316
|
const positions = await this.positions();
|
|
1289
1317
|
savingsBalance = positions.positions.filter((p) => p.type === "save").reduce((sum, p) => sum + p.amount, 0);
|
|
@@ -1367,7 +1395,7 @@ var T2000 = class _T2000 extends EventEmitter {
|
|
|
1367
1395
|
const gasResult = await executeWithGas(
|
|
1368
1396
|
this.client,
|
|
1369
1397
|
this.keypair,
|
|
1370
|
-
() => buildBorrowTx(this.client, this._address, borrowAmount)
|
|
1398
|
+
() => buildBorrowTx(this.client, this._address, borrowAmount, { collectFee: true })
|
|
1371
1399
|
);
|
|
1372
1400
|
const hf = await getHealthFactor(this.client, this.keypair);
|
|
1373
1401
|
reportFee(this._address, "borrow", fee.amount, fee.rate, gasResult.digest);
|
|
@@ -1622,6 +1650,6 @@ function parseMoveAbort(errorStr) {
|
|
|
1622
1650
|
return { reason: errorStr };
|
|
1623
1651
|
}
|
|
1624
1652
|
|
|
1625
|
-
export { BPS_DENOMINATOR, CLOCK_ID, DEFAULT_NETWORK, MIST_PER_SUI, SENTINEL, SUI_DECIMALS, SUPPORTED_ASSETS, T2000, T2000Error, USDC_DECIMALS, calculateFee, executeAutoTopUp, executeWithGas, exportPrivateKey, formatSui, formatUsd, generateKeypair, getAddress, getGasStatus, getPoolPrice, getRates, getSentinelInfo, getSwapQuote, keypairFromPrivateKey, listSentinels, loadKey, mapMoveAbortCode, mapWalletError, mistToSui, rawToUsdc, requestAttack, saveKey, attack as sentinelAttack, settleAttack, shouldAutoTopUp, simulateTransaction, solveHashcash, submitPrompt, suiToMist, throwIfSimulationFailed, truncateAddress, usdcToRaw, validateAddress, walletExists };
|
|
1653
|
+
export { BPS_DENOMINATOR, CLOCK_ID, DEFAULT_NETWORK, MIST_PER_SUI, SENTINEL, SUI_DECIMALS, SUPPORTED_ASSETS, T2000, T2000Error, USDC_DECIMALS, addCollectFeeToTx, calculateFee, executeAutoTopUp, executeWithGas, exportPrivateKey, formatSui, formatUsd, generateKeypair, getAddress, getGasStatus, getPoolPrice, getRates, getSentinelInfo, getSwapQuote, keypairFromPrivateKey, listSentinels, loadKey, mapMoveAbortCode, mapWalletError, mistToSui, rawToUsdc, requestAttack, saveKey, attack as sentinelAttack, settleAttack, shouldAutoTopUp, simulateTransaction, solveHashcash, submitPrompt, suiToMist, throwIfSimulationFailed, truncateAddress, usdcToRaw, validateAddress, walletExists };
|
|
1626
1654
|
//# sourceMappingURL=index.js.map
|
|
1627
1655
|
//# sourceMappingURL=index.js.map
|