|
| 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 | +} |
0 commit comments