|
| 1 | +import { Service } from './service'; |
| 2 | + |
| 3 | +import { ServiceProps } from '../types/service'; |
| 4 | +import { Integration } from '../types/integrations'; |
| 5 | +import api from '../api'; |
| 6 | +import { DecodedTxRaw } from "@cosmjs/proto-signing"; |
| 7 | +import { DydxSignedTx, DydxTxHash, DydxTxStatus, DydxTx } from "../types/dydx"; |
| 8 | + |
| 9 | +export class DydxService extends Service { |
| 10 | + |
| 11 | + constructor({ testnet }: ServiceProps) { |
| 12 | + super({ testnet }); |
| 13 | + } |
| 14 | + |
| 15 | + /** |
| 16 | + * Convert DYDX to ADYDX |
| 17 | + * @param amountDydx |
| 18 | + */ |
| 19 | + dydxToAdydx(amountDydx: string): string { |
| 20 | + return (parseFloat(amountDydx) * 10 ** 18).toFixed(); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Craft dydx staking transaction |
| 25 | + * @param accountId id of the kiln account to use for the stake transaction |
| 26 | + * @param pubkey wallet pubkey, this is different from the wallet address |
| 27 | + * @param validatorAddress validator address to delegate to |
| 28 | + * @param amountDydx how many tokens to stake in DYDX |
| 29 | + */ |
| 30 | + async craftStakeTx( |
| 31 | + accountId: string, |
| 32 | + pubkey: string, |
| 33 | + validatorAddress: string, |
| 34 | + amountDydx: number, |
| 35 | + ): Promise<DydxTx> { |
| 36 | + try { |
| 37 | + const { data } = await api.post<DydxTx>( |
| 38 | + `/v1/dydx/transaction/stake`, |
| 39 | + { |
| 40 | + account_id: accountId, |
| 41 | + pubkey: pubkey, |
| 42 | + validator: validatorAddress, |
| 43 | + amount_adydx: this.dydxToAdydx(amountDydx.toString()), |
| 44 | + }); |
| 45 | + return data; |
| 46 | + } catch (err: any) { |
| 47 | + throw new Error(err); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Craft dydx withdraw rewards transaction |
| 53 | + * @param pubkey wallet pubkey, this is different from the wallet address |
| 54 | + * @param validatorAddress validator address to which the delegation has been made |
| 55 | + */ |
| 56 | + async craftWithdrawRewardsTx( |
| 57 | + pubkey: string, |
| 58 | + validatorAddress: string, |
| 59 | + ): Promise<DydxTx> { |
| 60 | + try { |
| 61 | + const { data } = await api.post<DydxTx>( |
| 62 | + `/v1/dydx/transaction/withdraw-rewards`, |
| 63 | + { |
| 64 | + pubkey: pubkey, |
| 65 | + validator: validatorAddress, |
| 66 | + }); |
| 67 | + return data; |
| 68 | + } catch (err: any) { |
| 69 | + throw new Error(err); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Craft dydx unstaking transaction |
| 75 | + * @param pubkey wallet pubkey, this is different from the wallet address |
| 76 | + * @param validatorAddress validator address to which the delegation has been made |
| 77 | + * @param amountDydx how many tokens to undelegate in DYDX |
| 78 | + */ |
| 79 | + async craftUnstakeTx( |
| 80 | + pubkey: string, |
| 81 | + validatorAddress: string, |
| 82 | + amountDydx?: number, |
| 83 | + ): Promise<DydxTx> { |
| 84 | + try { |
| 85 | + const { data } = await api.post<DydxTx>( |
| 86 | + `/v1/dydx/transaction/unstake`, |
| 87 | + { |
| 88 | + pubkey: pubkey, |
| 89 | + validator: validatorAddress, |
| 90 | + amount_adydx: amountDydx ? this.dydxToAdydx(amountDydx.toString()) : undefined, |
| 91 | + }); |
| 92 | + return data; |
| 93 | + } catch (err: any) { |
| 94 | + throw new Error(err); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Craft dydx redelegate transaction |
| 100 | + * @param accountId id of the kiln account to use for the new stake |
| 101 | + * @param pubkey wallet pubkey, this is different from the wallet address |
| 102 | + * @param validatorSourceAddress validator address of the current delegation |
| 103 | + * @param validatorDestinationAddress validator address to which the delegation will be moved |
| 104 | + * @param amountDydx how many tokens to redelegate in DYDX |
| 105 | + */ |
| 106 | + async craftRedelegateTx( |
| 107 | + accountId: string, |
| 108 | + pubkey: string, |
| 109 | + validatorSourceAddress: string, |
| 110 | + validatorDestinationAddress: string, |
| 111 | + amountDydx?: number, |
| 112 | + ): Promise<DydxTx> { |
| 113 | + try { |
| 114 | + const { data } = await api.post<DydxTx>( |
| 115 | + `/v1/dydx/transaction/redelegate`, |
| 116 | + { |
| 117 | + account_id: accountId, |
| 118 | + pubkey: pubkey, |
| 119 | + validator_source: validatorSourceAddress, |
| 120 | + validator_destination: validatorDestinationAddress, |
| 121 | + amount_adydx: amountDydx ? this.dydxToAdydx(amountDydx.toString()) : undefined, |
| 122 | + }); |
| 123 | + return data; |
| 124 | + } catch (err: any) { |
| 125 | + throw new Error(err); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Sign transaction with given integration |
| 131 | + * @param integration custody solution to sign with |
| 132 | + * @param tx raw transaction |
| 133 | + * @param note note to identify the transaction in your custody solution |
| 134 | + */ |
| 135 | + async sign(integration: Integration, tx: DydxTx, note?: string): Promise<DydxSignedTx> { |
| 136 | + const payload = { |
| 137 | + rawMessageData: { |
| 138 | + messages: [ |
| 139 | + { |
| 140 | + 'content': tx.data.unsigned_tx_hash, |
| 141 | + }, |
| 142 | + ], |
| 143 | + }, |
| 144 | + }; |
| 145 | + const fbNote = note ? note : 'DYDX tx from @kilnfi/sdk'; |
| 146 | + const signer = this.getFbSigner(integration); |
| 147 | + const fbTx = await signer.signWithFB(payload, this.testnet ? 'DV4TNT_TEST' : 'DYDX_DYDX', fbNote); |
| 148 | + const signature: string = fbTx.signedMessages![0].signature.fullSig; |
| 149 | + const { data } = await api.post<DydxSignedTx>( |
| 150 | + `/v1/dydx/transaction/prepare`, |
| 151 | + { |
| 152 | + pubkey: tx.data.pubkey, |
| 153 | + tx_body: tx.data.tx_body, |
| 154 | + tx_auth_info: tx.data.tx_auth_info, |
| 155 | + signature: signature, |
| 156 | + }); |
| 157 | + data.data.fireblocks_tx = fbTx; |
| 158 | + return data; |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | + /** |
| 163 | + * Broadcast transaction to the network |
| 164 | + * @param signedTx |
| 165 | + */ |
| 166 | + async broadcast(signedTx: DydxSignedTx): Promise<DydxTxHash> { |
| 167 | + try { |
| 168 | + const { data } = await api.post<DydxTxHash>( |
| 169 | + `/v1/dydx/transaction/broadcast`, |
| 170 | + { |
| 171 | + tx_serialized: signedTx.data.signed_tx_serialized, |
| 172 | + }); |
| 173 | + return data; |
| 174 | + } catch (e: any) { |
| 175 | + throw new Error(e); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Get transaction status |
| 181 | + * @param txHash |
| 182 | + */ |
| 183 | + async getTxStatus(txHash: string): Promise<DydxTxStatus> { |
| 184 | + try { |
| 185 | + const { data } = await api.get<DydxTxStatus>( |
| 186 | + `/v1/dydx/transaction/status?tx_hash=${txHash}`); |
| 187 | + return data; |
| 188 | + } catch (e: any) { |
| 189 | + throw new Error(e); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Decode transaction |
| 195 | + * @param txSerialized transaction serialized |
| 196 | + */ |
| 197 | + async decodeTx(txSerialized: string): Promise<DecodedTxRaw> { |
| 198 | + try { |
| 199 | + const { data } = await api.get<DecodedTxRaw>( |
| 200 | + `/v1/dydx/transaction/decode?tx_serialized=${txSerialized}`); |
| 201 | + return data; |
| 202 | + } catch (error: any) { |
| 203 | + throw new Error(error); |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments