-
Notifications
You must be signed in to change notification settings - Fork 0
/
sui.ts
156 lines (135 loc) · 4.8 KB
/
sui.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {
Connection,
JsonRpcProvider,
RawSigner,
SUI_SYSTEM_STATE_OBJECT_ID,
SuiTransactionBlockResponse,
TransactionBlock,
} from "@mysten/sui.js";
import { delay } from "$std/async/delay.ts";
import config from "./config.ts";
import { Stake, ValidatorOperationCapContent } from "./types.ts";
import { CommissionPrompt, RgpPrompt, SendPrompt } from "./bin/toolkit.ts";
export const provider = new JsonRpcProvider(
new Connection({
fullnode: config.SUI_RPC_URL,
}),
);
const getSelfStakes = async (address: string): Promise<Stake[]> => {
const stakes = await provider.getStakes({
owner: address,
});
return stakes.find((stake) => stake.validatorAddress === address)?.stakes || [];
};
const getValidatorOperationCapabilityId = async (address: string, validator: string | undefined): Promise<string | undefined> => {
const { data } = await provider.getOwnedObjects({
owner: address,
filter: {
StructType: "0x3::validator_cap::UnverifiedValidatorOperationCap",
},
options: {
showContent: true,
},
});
const validatorOperationCap = data.find((object) => {
return (object.data?.content as ValidatorOperationCapContent).fields.authorizer_validator_address ===
(validator ? validator : address);
});
return validatorOperationCap?.data?.objectId;
};
export const withdrawStakeObjects = async (signer: RawSigner): Promise<SuiTransactionBlockResponse[]> => {
const address = await signer.getAddress();
const stakes = await getSelfStakes(address);
const transactions: SuiTransactionBlockResponse[] = [];
for (const stake of stakes) {
const txb = new TransactionBlock();
txb.moveCall({
target: "0x3::sui_system::request_withdraw_stake",
arguments: [
txb.object(SUI_SYSTEM_STATE_OBJECT_ID),
txb.object(stake.stakedSuiId),
],
});
try {
const tx = await signer.signAndExecuteTransactionBlock({
transactionBlock: txb,
});
console.log(tx);
transactions.push(tx);
// We wait 3 seconds to allow the network process consecutive transactions.
await delay(3000);
} catch (e) {
throw new Error(`Failed to withdraw staked Sui object ${stake.stakedSuiId}`, {
cause: e,
});
}
}
return transactions;
};
export const sendSuiObjects = async (signer: RawSigner, { amount, recipient }: SendPrompt): Promise<SuiTransactionBlockResponse> => {
const txb = new TransactionBlock();
const coin = txb.splitCoins(txb.gas, [txb.pure(amount * 1e9)]);
txb.transferObjects([coin], txb.pure(recipient));
try {
return await signer.signAndExecuteTransactionBlock({
transactionBlock: txb,
});
} catch (e) {
throw new Error(`Failed to send ${amount} SUI to ${recipient}`, {
cause: e,
});
}
};
export const updateReferenceGasPrice = async (signer: RawSigner, { price, validator }: RgpPrompt): Promise<SuiTransactionBlockResponse> => {
const address = await signer.getAddress();
const capabilityId = await getValidatorOperationCapabilityId(address, validator);
if (!capabilityId) {
throw new Error(`No validator operation capability found for address ${address}`);
}
const txb = new TransactionBlock();
txb.moveCall({
target: "0x3::sui_system::request_set_gas_price",
arguments: [
txb.object(SUI_SYSTEM_STATE_OBJECT_ID),
txb.object(capabilityId),
txb.pure(price),
],
});
try {
return await signer.signAndExecuteTransactionBlock({
transactionBlock: txb,
});
} catch (e) {
throw new Error(`Failed to update reference gas price`, {
cause: e,
});
}
};
export const updateCommissionRate = async (
signer: RawSigner,
{ rate, validator }: CommissionPrompt,
): Promise<SuiTransactionBlockResponse> => {
const address = await signer.getAddress();
const capabilityId = await getValidatorOperationCapabilityId(address, validator);
if (!capabilityId) {
throw new Error(`No validator operation capability found for address ${address}`);
}
const txb = new TransactionBlock();
txb.moveCall({
target: "0x3::sui_system::request_set_commission_rate",
arguments: [
txb.object(SUI_SYSTEM_STATE_OBJECT_ID),
txb.pure(rate),
],
});
txb.setGasBudget(2000000);
try {
return await signer.signAndExecuteTransactionBlock({
transactionBlock: txb,
});
} catch (e) {
throw new Error(`Failed to update commission rate`, {
cause: e,
});
}
};