From 78cac0d00245c711af1dbff2ea7cdbada4a6fc79 Mon Sep 17 00:00:00 2001 From: Arno Simon Date: Thu, 1 Aug 2024 11:43:32 +0200 Subject: [PATCH] add kava --- src/integrations/fb_signer.ts | 4 +- src/kiln.ts | 3 + src/services/fireblocks.ts | 2 +- src/services/inj.ts | 1 + src/services/kava.ts | 177 ++++++++++++++++++++++++++++++++++ 5 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 src/services/kava.ts diff --git a/src/integrations/fb_signer.ts b/src/integrations/fb_signer.ts index 717c760..97744e9 100644 --- a/src/integrations/fb_signer.ts +++ b/src/integrations/fb_signer.ts @@ -35,7 +35,8 @@ export type AssetId = | "CELESTIA" | "INJ_INJ" | "TON_TEST" - | "TON"; + | "TON" + | "KAVA_KAVA"; export class FbSigner { protected fireblocks: FireblocksSDK; @@ -99,6 +100,7 @@ export class FbSigner { note, extraParameters: payloadToSign, }; + console.log(tx); const fbTx = await this.fireblocks.createTransaction(tx); return await this.waitForTxCompletion(fbTx); } catch (err: any) { diff --git a/src/kiln.ts b/src/kiln.ts index 883101f..812e2c9 100644 --- a/src/kiln.ts +++ b/src/kiln.ts @@ -19,6 +19,7 @@ import { TonService } from "./services/ton"; import { XtzService } from "./services/xtz"; import { ZetaService } from "./services/zeta"; import { KILN_VALIDATORS as v } from "./validators"; +import { KavaService } from "./services/kava"; type Config = { apiToken: string; @@ -48,6 +49,7 @@ export class Kiln { inj: InjService; ton: TonService; zeta: ZetaService; + kava: KavaService; constructor({ testnet, apiToken, baseUrl }: Config) { api.defaults.headers.common.Authorization = `Bearer ${apiToken}`; @@ -73,5 +75,6 @@ export class Kiln { this.inj = new InjService({ testnet }); this.ton = new TonService({ testnet }); this.zeta = new ZetaService({ testnet }); + this.kava = new KavaService({ testnet }); } } diff --git a/src/services/fireblocks.ts b/src/services/fireblocks.ts index dbd3aaa..7895ce0 100644 --- a/src/services/fireblocks.ts +++ b/src/services/fireblocks.ts @@ -8,7 +8,7 @@ export class FireblocksService extends Service { * @param integration * @param assetId */ - async getPubkey(integration: Integration, assetId: string): Promise { + async getPubkey(integration: Integration, assetId: string): Promise { const fbSdk = this.getFbSdk(integration); const data = await fbSdk.getPublicKeyInfoForVaultAccount({ assetId: assetId, diff --git a/src/services/inj.ts b/src/services/inj.ts index c44d50b..09f9475 100644 --- a/src/services/inj.ts +++ b/src/services/inj.ts @@ -135,6 +135,7 @@ export class InjService extends Service { const fbNote = note ? note : "INJ tx from @kilnfi/sdk"; const signer = this.getFbSigner(integration); const fbTx = await signer.sign(payload, "INJ_INJ", fbNote); + console.log(fbTx); const signature: string = fbTx.signedMessages![0].signature.fullSig; const { data } = await api.post(`/v1/inj/transaction/prepare`, { pubkey: tx.data.pubkey, diff --git a/src/services/kava.ts b/src/services/kava.ts new file mode 100644 index 0000000..9906b15 --- /dev/null +++ b/src/services/kava.ts @@ -0,0 +1,177 @@ +import { Service } from "./service"; + +import { ServiceProps } from "../types/service"; +import { Integration } from "../types/integrations"; +import api from "../api"; +import { DecodedTxRaw } from "@cosmjs/proto-signing"; +import { CosmosSignedTx, CosmosTx, CosmosTxHash, CosmosTxStatus } from "../types/cosmos"; +import { parseUnits } from "viem"; + +export class KavaService extends Service { + constructor({ testnet }: ServiceProps) { + super({ testnet }); + } + + /** + * Convert KAVA to ukava + * @param amount + */ + kavaToUkava(amount: string): string { + return parseUnits(amount, 6).toString(); + } + + /** + * Craft kava staking transaction + * @param accountId id of the kiln account to use for the stake transaction + * @param pubkey wallet pubkey, this is different from the wallet address + * @param validatorAddress validator address to delegate to + * @param amountKava how many tokens to stake in KAVA + * @param restakeRewards If enabled, the rewards will be automatically restaked + * @param granteeAddress validator grantee address + */ + async craftStakeTx( + accountId: string, + pubkey: string, + validatorAddress: string, + amountKava: number, + restakeRewards: boolean = false, + granteeAddress?: string, + ): Promise { + const { data } = await api.post(`/v1/kava/transaction/stake`, { + account_id: accountId, + pubkey: pubkey, + validator: validatorAddress, + amount_ukava: this.kavaToUkava(amountKava.toString()), + restake_rewards: restakeRewards, + grantee_address: granteeAddress, + }); + return data; + } + + /** + * Craft kava withdraw rewards transaction + * @param pubkey wallet pubkey, this is different from the wallet address + * @param validatorAddress validator address to which the delegation has been made + */ + async craftWithdrawRewardsTx(pubkey: string, validatorAddress: string): Promise { + const { data } = await api.post(`/v1/kava/transaction/withdraw-rewards`, { + pubkey: pubkey, + validator: validatorAddress, + }); + return data; + } + + /** + * Craft kava restake rewards transaction + * @param pubkey wallet pubkey, this is different from the wallet address + * @param validatorAddress validator address to which the delegation has been made + * @param granteeAddress validator grantee address + */ + async craftRestakeRewardsTx(pubkey: string, validatorAddress: string, granteeAddress: string): Promise { + const { data } = await api.post(`/v1/kava/transaction/restake-rewards`, { + pubkey: pubkey, + validator_address: validatorAddress, + grantee_address: granteeAddress, + }); + return data; + } + + /** + * Craft kava unstaking transaction + * @param pubkey wallet pubkey, this is different from the wallet address + * @param validatorAddress validator address to which the delegation has been made + * @param amountKava how many tokens to undelegate in KAVA + */ + async craftUnstakeTx(pubkey: string, validatorAddress: string, amountKava?: number): Promise { + const { data } = await api.post(`/v1/kava/transaction/unstake`, { + pubkey: pubkey, + validator: validatorAddress, + amount_kava: amountKava ? this.kavaToUkava(amountKava.toString()) : undefined, + }); + return data; + } + + /** + * Craft kava redelegate transaction + * @param accountId id of the kiln account to use for the new stake + * @param pubkey wallet pubkey, this is different from the wallet address + * @param validatorSourceAddress validator address of the current delegation + * @param validatorDestinationAddress validator address to which the delegation will be moved + * @param amountKava how many tokens to redelegate in KAVA + */ + async craftRedelegateTx( + accountId: string, + pubkey: string, + validatorSourceAddress: string, + validatorDestinationAddress: string, + amountKava?: number, + ): Promise { + const { data } = await api.post(`/v1/kava/transaction/redelegate`, { + account_id: accountId, + pubkey: pubkey, + validator_source: validatorSourceAddress, + validator_destination: validatorDestinationAddress, + amount_ukava: amountKava ? this.kavaToUkava(amountKava.toString()) : undefined, + }); + return data; + } + + /** + * Sign transaction with given integration + * @param integration custody solution to sign with + * @param tx raw transaction + * @param note note to identify the transaction in your custody solution + */ + async sign(integration: Integration, tx: CosmosTx, note?: string): Promise { + const payload = { + rawMessageData: { + messages: [ + { + content: tx.data.unsigned_tx_hash, + }, + ], + }, + }; + const fbNote = note ? note : "KAVA tx from @kilnfi/sdk"; + const signer = this.getFbSigner(integration); + const fbTx = await signer.sign(payload, "KAVA_KAVA", fbNote); + const signature: string = fbTx.signedMessages![0].signature.fullSig; + const { data } = await api.post(`/v1/kava/transaction/prepare`, { + pubkey: tx.data.pubkey, + tx_body: tx.data.tx_body, + tx_auth_info: tx.data.tx_auth_info, + signature: signature, + }); + data.data.fireblocks_tx = fbTx; + return data; + } + + /** + * Broadcast transaction to the network + * @param signedTx + */ + async broadcast(signedTx: CosmosSignedTx): Promise { + const { data } = await api.post(`/v1/kava/transaction/broadcast`, { + tx_serialized: signedTx.data.signed_tx_serialized, + }); + return data; + } + + /** + * Get transaction status + * @param txHash + */ + async getTxStatus(txHash: string): Promise { + const { data } = await api.get(`/v1/kava/transaction/status?tx_hash=${txHash}`); + return data; + } + + /** + * Decode transaction + * @param txSerialized transaction serialized + */ + async decodeTx(txSerialized: string): Promise { + const { data } = await api.get(`/v1/kava/transaction/decode?tx_serialized=${txSerialized}`); + return data; + } +}