Skip to content

Commit 1bdd798

Browse files
authored
add POL support + deprecate MATIC (#118)
1 parent 5eec788 commit 1bdd798

File tree

5 files changed

+269
-2
lines changed

5 files changed

+269
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ Check out the [full documentation](https://docs.kiln.fi/v1/connect/overview).
1717
- INJ (mainnet)
1818
- KAVA (mainnet)
1919
- KSM (mainnet)
20-
- MATIC (mainnet and sepolia)
20+
- MATIC (deprecated, use POL instead) (mainnet and sepolia)
2121
- NEAR (mainnet and testnet)
2222
- NOBLE (mainnet)
2323
- OSMO (mainnet)
24+
- POL (mainnet and sepolia)
2425
- SOL (mainnet and testnet)
2526
- TIA (mainnet)
2627
- TON (mainnet and testnet)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kilnfi/sdk",
3-
"version": "2.21.2",
3+
"version": "2.22.0",
44
"autor": "Kiln <[email protected]> (https://kiln.fi)",
55
"license": "BUSL-1.1",
66
"description": "JavaScript sdk for Kiln API",

src/services/matic.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class MaticService extends Service {
1212
}
1313

1414
/**
15+
* @deprecated Please use POL functions instead
1516
* Craft an approve transaction to the MATIC token contract allowing the contract given to spend the amount given
1617
* If no amount is provided, an infinite amount will be approved
1718
* @param walletAddress wallet address signing the transaction
@@ -32,6 +33,7 @@ export class MaticService extends Service {
3233
}
3334

3435
/**
36+
* @deprecated Please use POL functions instead
3537
* Craft a buyVoucher transaction to a ValidatorShare proxy contract
3638
* It also links the stake to the account id given
3739
* @param accountId id of the kiln account to use for the stake transaction
@@ -55,6 +57,7 @@ export class MaticService extends Service {
5557
}
5658

5759
/**
60+
* @deprecated Please use POL functions instead
5861
* Craft a sellVoucher transaction to a ValidatorShare proxy contract
5962
* Note there that your tokens will be unbonding and locked for 21 days after this transaction
6063
* @param walletAddress address delegating
@@ -75,6 +78,7 @@ export class MaticService extends Service {
7578
}
7679

7780
/**
81+
* @deprecated Please use POL functions instead
7882
* Craft an unstakeClaimTokens transaction to a ValidatorShare proxy contract
7983
* Note that your tokens must be unbonded before you can claim them
8084
* @param walletAddress address delegating
@@ -89,6 +93,7 @@ export class MaticService extends Service {
8993
}
9094

9195
/**
96+
* @deprecated Please use POL functions instead
9297
* Craft an withdrawRewards transaction to a ValidatorShare proxy contract
9398
* All rewards earned are transferred to the delegator's wallet
9499
* @param walletAddress address delegating
@@ -103,6 +108,7 @@ export class MaticService extends Service {
103108
}
104109

105110
/**
111+
* @deprecated Please use POL functions instead
106112
* Craft an withdrawRewards transaction to a ValidatorShare proxy contract
107113
* All rewards earned are then re-delegated
108114
* @param walletAddress address delegating
@@ -117,6 +123,7 @@ export class MaticService extends Service {
117123
}
118124

119125
/**
126+
* @deprecated Please use POL functions instead
120127
* Sign transaction with given integration
121128
* @param integration custody solution to sign with
122129
* @param tx raw transaction
@@ -151,6 +158,7 @@ export class MaticService extends Service {
151158
}
152159

153160
/**
161+
* @deprecated Please use POL functions instead
154162
* Sign transaction with given integration
155163
* @param integration custody solution to sign with
156164
* @param tx raw transaction
@@ -179,6 +187,7 @@ export class MaticService extends Service {
179187
}
180188

181189
/**
190+
* @deprecated Please use POL functions instead
182191
* Broadcast transaction to the network
183192
* @param signedTx
184193
*/
@@ -190,6 +199,7 @@ export class MaticService extends Service {
190199
}
191200

192201
/**
202+
* @deprecated Please use POL functions instead
193203
* Get transaction status
194204
* @param txHash transaction hash
195205
*/
@@ -199,6 +209,7 @@ export class MaticService extends Service {
199209
}
200210

201211
/**
212+
* @deprecated Please use POL functions instead
202213
* Decode transaction
203214
* @param txSerialized transaction serialized
204215
*/
@@ -208,6 +219,7 @@ export class MaticService extends Service {
208219
}
209220

210221
/**
222+
* @deprecated Please use POL functions instead
211223
* Convert MATIC to WEI
212224
* @param matic
213225
*/

src/services/pol.ts

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
import api from "../api";
2+
import { Service } from "./service";
3+
import { utils } from "ethers";
4+
import { ServiceProps } from "../types/service";
5+
import { PolDecodedTx, PolSignedTx, PolTx, PolTxHash, PolTxStatus } from "../types/pol";
6+
import { Integration } from "../types/integrations";
7+
import { TransactionResponse } from "fireblocks-sdk";
8+
9+
export class PolService extends Service {
10+
constructor({ testnet }: ServiceProps) {
11+
super({ testnet });
12+
}
13+
14+
/**
15+
* Craft an approve transaction to the POL token contract allowing the contract given to spend the amount given
16+
* If no amount is provided, an infinite amount will be approved
17+
* @param walletAddress wallet address signing the transaction
18+
* @param contractAddressToApprove contract address that you allow to spend the token
19+
* @param amountPol how many tokens to approve the spending, if not specified an infinite amount will be approved
20+
*/
21+
async craftApproveTx(walletAddress: string, contractAddressToApprove: string, amountPol?: number): Promise<PolTx> {
22+
const { data } = await api.post<PolTx>(`/v1/pol/transaction/approve`, {
23+
wallet: walletAddress,
24+
contract: contractAddressToApprove,
25+
amount_wei: amountPol ? this.polToWei(amountPol.toString()) : undefined,
26+
});
27+
return data;
28+
}
29+
30+
/**
31+
* Craft a buyVoucher transaction to a ValidatorShare proxy contract
32+
* It also links the stake to the account id given
33+
* @param accountId id of the kiln account to use for the stake transaction
34+
* @param walletAddress withdrawal creds /!\ losing it => losing the ability to withdraw
35+
* @param validatorShareProxyAddress ValidatorShare proxy contract address of the validator
36+
* @param amountPol how many tokens to stake in POL
37+
*/
38+
async craftBuyVoucherTx(
39+
accountId: string,
40+
walletAddress: string,
41+
validatorShareProxyAddress: string,
42+
amountPol: number,
43+
): Promise<PolTx> {
44+
const { data } = await api.post<PolTx>(`/v1/pol/transaction/buy-voucher`, {
45+
account_id: accountId,
46+
wallet: walletAddress,
47+
amount_wei: this.polToWei(amountPol.toString()),
48+
validator_share_proxy_address: validatorShareProxyAddress,
49+
});
50+
return data;
51+
}
52+
53+
/**
54+
* Craft a sellVoucher transaction to a ValidatorShare proxy contract
55+
* Note there that your tokens will be unbonding and locked for 21 days after this transaction
56+
* @param walletAddress address delegating
57+
* @param validatorShareProxyAddress ValidatorShare proxy contract address of the validator
58+
* @param amountPol how many tokens to unbond in POL
59+
*/
60+
async craftSellVoucherTx(
61+
walletAddress: string,
62+
validatorShareProxyAddress: string,
63+
amountPol: number,
64+
): Promise<PolTx> {
65+
const { data } = await api.post<PolTx>(`/v1/pol/transaction/sell-voucher`, {
66+
wallet: walletAddress,
67+
amount_wei: this.polToWei(amountPol.toString()),
68+
validator_share_proxy_address: validatorShareProxyAddress,
69+
});
70+
return data;
71+
}
72+
73+
/**
74+
* Craft an unstakeClaimTokens transaction to a ValidatorShare proxy contract
75+
* Note that your tokens must be unbonded before you can claim them
76+
* @param walletAddress address delegating
77+
* @param validatorShareProxyAddress ValidatorShare proxy contract address of the validator
78+
*/
79+
async craftUnstakeClaimTokensTx(walletAddress: string, validatorShareProxyAddress: string): Promise<PolTx> {
80+
const { data } = await api.post<PolTx>(`/v1/pol/transaction/unstake-claim-tokens`, {
81+
wallet: walletAddress,
82+
validator_share_proxy_address: validatorShareProxyAddress,
83+
});
84+
return data;
85+
}
86+
87+
/**
88+
* Craft an withdrawRewards transaction to a ValidatorShare proxy contract
89+
* All rewards earned are transferred to the delegator's wallet
90+
* @param walletAddress address delegating
91+
* @param validatorShareProxyAddress ValidatorShare proxy contract address of the validator
92+
*/
93+
async craftWithdrawRewardsTx(walletAddress: string, validatorShareProxyAddress: string): Promise<PolTx> {
94+
const { data } = await api.post<PolTx>(`/v1/pol/transaction/withdraw-rewards`, {
95+
wallet: walletAddress,
96+
validator_share_proxy_address: validatorShareProxyAddress,
97+
});
98+
return data;
99+
}
100+
101+
/**
102+
* Craft an withdrawRewards transaction to a ValidatorShare proxy contract
103+
* All rewards earned are then re-delegated
104+
* @param walletAddress address delegating
105+
* @param validatorShareProxyAddress ValidatorShare proxy contract address of the validator
106+
*/
107+
async craftRestakeRewardsTx(walletAddress: string, validatorShareProxyAddress: string): Promise<PolTx> {
108+
const { data } = await api.post<PolTx>(`/v1/pol/transaction/restake-rewards`, {
109+
wallet: walletAddress,
110+
validator_share_proxy_address: validatorShareProxyAddress,
111+
});
112+
return data;
113+
}
114+
115+
/**
116+
* Sign transaction with given integration
117+
* @param integration custody solution to sign with
118+
* @param tx raw transaction
119+
* @param note note to identify the transaction in your custody solution
120+
*/
121+
async sign(integration: Integration, tx: PolTx, note?: string): Promise<PolSignedTx> {
122+
const payload = {
123+
rawMessageData: {
124+
messages: [
125+
{
126+
content: tx.data.unsigned_tx_hash,
127+
preHash: {
128+
content: tx.data.unsigned_tx_serialized,
129+
hashAlgorithm: "KECCAK256",
130+
},
131+
},
132+
],
133+
},
134+
};
135+
136+
const fbSigner = this.getFbSigner(integration);
137+
const fbNote = note ? note : "POL tx from @kilnfi/sdk";
138+
const fbTx = await fbSigner.sign(payload, this.testnet ? "ETH_TEST5" : "ETH", fbNote);
139+
const { data } = await api.post<PolSignedTx>(`/v1/pol/transaction/prepare`, {
140+
unsigned_tx_serialized: tx.data.unsigned_tx_serialized,
141+
r: `0x${fbTx?.signedMessages?.[0].signature.r}`,
142+
s: `0x${fbTx?.signedMessages?.[0].signature.s}`,
143+
v: fbTx?.signedMessages?.[0].signature.v ?? 0,
144+
});
145+
data.data.fireblocks_tx = fbTx;
146+
return data;
147+
}
148+
149+
/**
150+
* Sign transaction with given integration
151+
* @param integration custody solution to sign with
152+
* @param tx raw transaction
153+
* @param note note to identify the transaction in your custody solution
154+
*/
155+
async signAndBroadcast(integration: Integration, tx: PolTx, note?: string): Promise<TransactionResponse> {
156+
if (!integration.fireblocksDestinationId) {
157+
throw new Error("Fireblocks destination id is missing in integration");
158+
}
159+
160+
const payload = {
161+
contractCallData: tx.data.contract_call_data,
162+
};
163+
164+
const fbSigner = this.getFbSigner(integration);
165+
const fbNote = note ? note : "POL tx from @kilnfi/sdk";
166+
const assetId = this.testnet ? "ETH_TEST5" : "ETH";
167+
return await fbSigner.signAndBroadcastWith(
168+
payload,
169+
assetId,
170+
tx,
171+
integration.fireblocksDestinationId,
172+
false,
173+
fbNote,
174+
);
175+
}
176+
177+
/**
178+
* Broadcast transaction to the network
179+
* @param signedTx
180+
*/
181+
async broadcast(signedTx: PolSignedTx): Promise<PolTxHash> {
182+
const { data } = await api.post<PolTxHash>(`/v1/pol/transaction/broadcast`, {
183+
tx_serialized: signedTx.data.signed_tx_serialized,
184+
});
185+
return data;
186+
}
187+
188+
/**
189+
* Get transaction status
190+
* @param txHash transaction hash
191+
*/
192+
async getTxStatus(txHash: string): Promise<PolTxStatus> {
193+
const { data } = await api.get<PolTxStatus>(`/v1/pol/transaction/status?tx_hash=${txHash}`);
194+
return data;
195+
}
196+
197+
/**
198+
* Decode transaction
199+
* @param txSerialized transaction serialized
200+
*/
201+
async decodeTx(txSerialized: string): Promise<PolDecodedTx> {
202+
const { data } = await api.get<PolDecodedTx>(`/v1/pol/transaction/decode?tx_serialized=${txSerialized}`);
203+
return data;
204+
}
205+
206+
/**
207+
* Convert POL to WEI
208+
* @param pol
209+
*/
210+
polToWei(pol: string): string {
211+
return utils.parseEther(pol).toString();
212+
}
213+
}

src/types/pol.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { TransactionResponse } from "fireblocks-sdk";
2+
import { TransactionSerializableLegacy, TransactionReceipt } from "viem";
3+
4+
export type PolTx = {
5+
data: {
6+
unsigned_tx_hash: string;
7+
unsigned_tx_serialized: string;
8+
to: string;
9+
contract_call_data: string;
10+
amount_wei?: string;
11+
nonce: number;
12+
gas_limit: number;
13+
max_priority_fee_per_gas_wei: string;
14+
max_fee_per_gas_wei: string;
15+
chain_id: number;
16+
};
17+
};
18+
19+
export type PolTxHash = {
20+
data: {
21+
tx_hash: string;
22+
};
23+
};
24+
25+
export type PolTxStatus = {
26+
data: {
27+
status: "success" | "error" | "pending_confirmation";
28+
receipt: TransactionReceipt | null;
29+
};
30+
};
31+
export type PolSignedTx = {
32+
data: {
33+
fireblocks_tx: TransactionResponse;
34+
signed_tx_serialized: string;
35+
};
36+
};
37+
38+
export type PolDecodedTx = TransactionSerializableLegacy & {
39+
functionName?: string;
40+
args?: any[];
41+
};

0 commit comments

Comments
 (0)