|
| 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 { CosmosSignedTx, CosmosTx, CosmosTxHash, CosmosTxStatus } from "../types/cosmos"; |
| 8 | + |
| 9 | +export class InjService extends Service { |
| 10 | + constructor({ testnet }: ServiceProps) { |
| 11 | + super({ testnet }); |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * Convert INJ to inj |
| 16 | + * @param amount |
| 17 | + */ |
| 18 | + injToAinj(amount: string): string { |
| 19 | + return (parseFloat(amount) * 10 ** 18).toFixed(); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Craft inj staking transaction |
| 24 | + * @param accountId id of the kiln account to use for the stake transaction |
| 25 | + * @param pubkey wallet pubkey, this is different from the wallet address |
| 26 | + * @param validatorAddress validator address to delegate to |
| 27 | + * @param amountInj how many tokens to stake in INJ |
| 28 | + * @param restakeRewards If enabled, the rewards will be automatically restaked |
| 29 | + */ |
| 30 | + async craftStakeTx( |
| 31 | + accountId: string, |
| 32 | + pubkey: string, |
| 33 | + validatorAddress: string, |
| 34 | + amountInj: number, |
| 35 | + restakeRewards: boolean = false, |
| 36 | + ): Promise<CosmosTx> { |
| 37 | + const { data } = await api.post<CosmosTx>(`/v1/inj/transaction/stake`, { |
| 38 | + account_id: accountId, |
| 39 | + pubkey: pubkey, |
| 40 | + validator: validatorAddress, |
| 41 | + amount_inj: this.injToAinj(amountInj.toString()), |
| 42 | + restake_rewards: restakeRewards, |
| 43 | + }); |
| 44 | + return data; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Craft inj withdraw rewards transaction |
| 49 | + * @param pubkey wallet pubkey, this is different from the wallet address |
| 50 | + * @param validatorAddress validator address to which the delegation has been made |
| 51 | + */ |
| 52 | + async craftWithdrawRewardsTx(pubkey: string, validatorAddress: string): Promise<CosmosTx> { |
| 53 | + const { data } = await api.post<CosmosTx>(`/v1/inj/transaction/withdraw-rewards`, { |
| 54 | + pubkey: pubkey, |
| 55 | + validator: validatorAddress, |
| 56 | + }); |
| 57 | + return data; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Craft inj restake rewards transaction |
| 62 | + * @param pubkey wallet pubkey, this is different from the wallet address |
| 63 | + * @param validatorAccount validator account address (wallet controlling the validator) |
| 64 | + * @param validatorAddress validator address to which the delegation has been made |
| 65 | + */ |
| 66 | + async craftRestakeRewardsTx(pubkey: string, validatorAddress: string): Promise<CosmosTx> { |
| 67 | + const { data } = await api.post<CosmosTx>(`/v1/inj/transaction/restake-rewards`, { |
| 68 | + pubkey: pubkey, |
| 69 | + validator_address: validatorAddress, |
| 70 | + }); |
| 71 | + return data; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Craft inj unstaking transaction |
| 76 | + * @param pubkey wallet pubkey, this is different from the wallet address |
| 77 | + * @param validatorAddress validator address to which the delegation has been made |
| 78 | + * @param amountInj how many tokens to undelegate in INJ |
| 79 | + */ |
| 80 | + async craftUnstakeTx(pubkey: string, validatorAddress: string, amountInj?: number): Promise<CosmosTx> { |
| 81 | + const { data } = await api.post<CosmosTx>(`/v1/inj/transaction/unstake`, { |
| 82 | + pubkey: pubkey, |
| 83 | + validator: validatorAddress, |
| 84 | + amount_inj: amountInj ? this.injToAinj(amountInj.toString()) : undefined, |
| 85 | + }); |
| 86 | + return data; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Craft inj redelegate transaction |
| 91 | + * @param accountId id of the kiln account to use for the new stake |
| 92 | + * @param pubkey wallet pubkey, this is different from the wallet address |
| 93 | + * @param validatorSourceAddress validator address of the current delegation |
| 94 | + * @param validatorDestinationAddress validator address to which the delegation will be moved |
| 95 | + * @param amountInj how many tokens to redelegate in INJ |
| 96 | + */ |
| 97 | + async craftRedelegateTx( |
| 98 | + accountId: string, |
| 99 | + pubkey: string, |
| 100 | + validatorSourceAddress: string, |
| 101 | + validatorDestinationAddress: string, |
| 102 | + amountInj?: number, |
| 103 | + ): Promise<CosmosTx> { |
| 104 | + const { data } = await api.post<CosmosTx>(`/v1/inj/transaction/redelegate`, { |
| 105 | + account_id: accountId, |
| 106 | + pubkey: pubkey, |
| 107 | + validator_source: validatorSourceAddress, |
| 108 | + validator_destination: validatorDestinationAddress, |
| 109 | + amount_inj: amountInj ? this.injToAinj(amountInj.toString()) : undefined, |
| 110 | + }); |
| 111 | + return data; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Sign transaction with given integration |
| 116 | + * @param integration custody solution to sign with |
| 117 | + * @param tx raw transaction |
| 118 | + * @param note note to identify the transaction in your custody solution |
| 119 | + */ |
| 120 | + async sign(integration: Integration, tx: CosmosTx, note?: string): Promise<CosmosSignedTx> { |
| 121 | + const payload = { |
| 122 | + rawMessageData: { |
| 123 | + messages: [ |
| 124 | + { |
| 125 | + content: tx.data.unsigned_tx_hash, |
| 126 | + }, |
| 127 | + ], |
| 128 | + }, |
| 129 | + }; |
| 130 | + const fbNote = note ? note : "INJ tx from @kilnfi/sdk"; |
| 131 | + const signer = this.getFbSigner(integration); |
| 132 | + const fbTx = await signer.signWithFB(payload, "INJ_INJ", fbNote); |
| 133 | + const signature: string = fbTx.signedMessages![0].signature.fullSig; |
| 134 | + const { data } = await api.post<CosmosSignedTx>(`/v1/inj/transaction/prepare`, { |
| 135 | + pubkey: tx.data.pubkey, |
| 136 | + tx_body: tx.data.tx_body, |
| 137 | + tx_auth_info: tx.data.tx_auth_info, |
| 138 | + signature: signature, |
| 139 | + }); |
| 140 | + data.data.fireblocks_tx = fbTx; |
| 141 | + return data; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Broadcast transaction to the network |
| 146 | + * @param signedTx |
| 147 | + */ |
| 148 | + async broadcast(signedTx: CosmosSignedTx): Promise<CosmosTxHash> { |
| 149 | + const { data } = await api.post<CosmosTxHash>(`/v1/inj/transaction/broadcast`, { |
| 150 | + tx_serialized: signedTx.data.signed_tx_serialized, |
| 151 | + }); |
| 152 | + return data; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Get transaction status |
| 157 | + * @param txHash |
| 158 | + */ |
| 159 | + async getTxStatus(txHash: string): Promise<CosmosTxStatus> { |
| 160 | + const { data } = await api.get<CosmosTxStatus>(`/v1/inj/transaction/status?tx_hash=${txHash}`); |
| 161 | + return data; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Decode transaction |
| 166 | + * @param txSerialized transaction serialized |
| 167 | + */ |
| 168 | + async decodeTx(txSerialized: string): Promise<DecodedTxRaw> { |
| 169 | + const { data } = await api.get<DecodedTxRaw>(`/v1/inj/transaction/decode?tx_serialized=${txSerialized}`); |
| 170 | + return data; |
| 171 | + } |
| 172 | +} |
0 commit comments