diff --git a/README.md b/README.md index d110129..51ff12d 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,14 @@ Check out the [full documentation](https://docs.kiln.fi/v1/connect/overview). - ADA - ATOM - DOT +- DYDX - ETH - MATIC - NEAR +- OSMO - SOL +- TIA - XTZ -- OSMO -- DYDX - More protocol to come, don't hesitate to contact us (support@kiln.fi) ## Installation diff --git a/package.json b/package.json index dc71a16..f3c1e66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kilnfi/sdk", - "version": "2.9.0", + "version": "2.10.0", "autor": "Kiln (https://kiln.fi)", "license": "BUSL-1.1", "description": "JavaScript sdk for Kiln API", diff --git a/src/integrations/fb_signer.ts b/src/integrations/fb_signer.ts index 09334e4..1d01380 100644 --- a/src/integrations/fb_signer.ts +++ b/src/integrations/fb_signer.ts @@ -32,6 +32,7 @@ type AssetId = | 'DOT' | 'DV4TNT_TEST' | 'DYDX_DYDX' + | 'CELESTIA' ; export class FbSigner { diff --git a/src/kiln.ts b/src/kiln.ts index 5d93936..328f4c9 100644 --- a/src/kiln.ts +++ b/src/kiln.ts @@ -12,6 +12,7 @@ import { MaticService } from './services/matic'; import { OsmoService } from './services/osmo'; import { FireblocksService } from "./services/fireblocks"; import { DydxService } from "./services/dydx"; +import { TiaService } from "./services/tia"; type Config = { apiToken: string; @@ -34,6 +35,7 @@ export class Kiln { matic: MaticService; osmo: OsmoService; dydx: DydxService; + tia: TiaService; constructor({ testnet, apiToken, baseUrl }: Config) { api.defaults.headers.common.Authorization = `Bearer ${apiToken}`; @@ -55,5 +57,6 @@ export class Kiln { this.matic = new MaticService({ testnet }); this.osmo = new OsmoService({ testnet }); this.dydx = new DydxService({ testnet }); + this.tia = new TiaService({ testnet }); } } \ No newline at end of file diff --git a/src/services/ada.ts b/src/services/ada.ts index e3393e1..055a666 100644 --- a/src/services/ada.ts +++ b/src/services/ada.ts @@ -30,18 +30,14 @@ export class AdaService extends Service { walletAddress: string, poolId: string, ): Promise { - try { - const { data } = await api.post( - `/v1/ada/transaction/stake`, - { - account_id: accountId, - wallet: walletAddress, - pool_id: poolId, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + const { data } = await api.post( + `/v1/ada/transaction/stake`, + { + account_id: accountId, + wallet: walletAddress, + pool_id: poolId, + }); + return data; } /** @@ -53,17 +49,13 @@ export class AdaService extends Service { walletAddress: string, amountAda?: number, ): Promise { - try { - const { data } = await api.post( - `/v1/ada/transaction/withdraw-rewards`, - { - wallet: walletAddress, - amount_lovelace: amountAda ? this.adaToLovelace(amountAda.toString()) : undefined, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + const { data } = await api.post( + `/v1/ada/transaction/withdraw-rewards`, + { + wallet: walletAddress, + amount_lovelace: amountAda ? this.adaToLovelace(amountAda.toString()) : undefined, + }); + return data; } /** @@ -73,16 +65,12 @@ export class AdaService extends Service { async craftUnstakeTx( walletAddress: string, ): Promise { - try { - const { data } = await api.post( - `/v1/ada/transaction/unstake`, - { - wallet: walletAddress, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + const { data } = await api.post( + `/v1/ada/transaction/unstake`, + { + wallet: walletAddress, + }); + return data; } /** @@ -100,51 +88,47 @@ export class AdaService extends Service { * @param note note to identify the transaction in your custody solution */ async sign(integration: Integration, tx: AdaTx, note?: string): Promise { - try { - const fbSigner = this.getFbSigner(integration); - const payload = { - rawMessageData: { - messages: [ - { - 'content': tx.data.unsigned_tx_hash, - }, - { - 'content': tx.data.unsigned_tx_hash, - 'bip44change': 2, - }, - ], - }, - inputsSelection: { - inputsToSpend: tx.data.inputs, - }, - }; + const fbSigner = this.getFbSigner(integration); + const payload = { + rawMessageData: { + messages: [ + { + 'content': tx.data.unsigned_tx_hash, + }, + { + 'content': tx.data.unsigned_tx_hash, + 'bip44change': 2, + }, + ], + }, + inputsSelection: { + inputsToSpend: tx.data.inputs, + }, + }; - const fbNote = note ? note : 'ADA tx from @kilnfi/sdk'; - const fbTx = await fbSigner.signWithFB(payload, this.testnet ? 'ADA_TEST' : 'ADA', fbNote); + const fbNote = note ? note : 'ADA tx from @kilnfi/sdk'; + const fbTx = await fbSigner.signWithFB(payload, this.testnet ? 'ADA_TEST' : 'ADA', fbNote); - if (!fbTx.signedMessages) { - throw new Error(`Could not sign the transaction.`); - } + if (!fbTx.signedMessages) { + throw new Error(`Could not sign the transaction.`); + } - const signedMessages: AdaSignedMessage[] = fbTx.signedMessages.map((message) => { - return { - pubkey: message.publicKey, - signature: message.signature.fullSig, - }; - }); + const signedMessages: AdaSignedMessage[] = fbTx.signedMessages.map((message) => { + return { + pubkey: message.publicKey, + signature: message.signature.fullSig, + }; + }); - const { data } = await api.post( - `/v1/ada/transaction/prepare`, - { - unsigned_tx_serialized: tx.data.unsigned_tx_serialized, - signed_messages: signedMessages, - }); + const { data } = await api.post( + `/v1/ada/transaction/prepare`, + { + unsigned_tx_serialized: tx.data.unsigned_tx_serialized, + signed_messages: signedMessages, + }); - data.data.fireblocks_tx = fbTx; - return data; - } catch (err: any) { - throw new Error(err); - } + data.data.fireblocks_tx = fbTx; + return data; } /** @@ -152,16 +136,12 @@ export class AdaService extends Service { * @param signedTx */ async broadcast(signedTx: AdaSignedTx): Promise { - try { - const { data } = await api.post( - `/v1/ada/transaction/broadcast`, - { - tx_serialized: signedTx.data.signed_tx_serialized, - }); - return data; - } catch (error: any) { - throw new Error(error); - } + const { data } = await api.post( + `/v1/ada/transaction/broadcast`, + { + tx_serialized: signedTx.data.signed_tx_serialized, + }); + return data; } /** @@ -169,13 +149,9 @@ export class AdaService extends Service { * @param txHash transaction hash */ async getTxStatus(txHash: string): Promise { - try { - const { data } = await api.get( - `/v1/ada/transaction/status?tx_hash=${txHash}`); - return data; - } catch (error: any) { - throw new Error(error); - } + const { data } = await api.get( + `/v1/ada/transaction/status?tx_hash=${txHash}`); + return data; } /** @@ -183,13 +159,9 @@ export class AdaService extends Service { * @param txSerialized transaction serialized */ async decodeTx(txSerialized: string): Promise { - try { - const { data } = await api.get( - `/v1/ada/transaction/decode?tx_serialized=${txSerialized}`); - return data; - } catch (error: any) { - throw new Error(error); - } + const { data } = await api.get( + `/v1/ada/transaction/decode?tx_serialized=${txSerialized}`); + return data; } /** @@ -200,13 +172,9 @@ export class AdaService extends Service { async getStakesByAccounts( accountIds: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/ada/stakes?accounts=${accountIds.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + const { data } = await api.get( + `/v1/ada/stakes?accounts=${accountIds.join(',')}`); + return data; } /** @@ -217,13 +185,9 @@ export class AdaService extends Service { async getStakesByStakeAddresses( stakeAddresses: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/ada/stakes?stake_addresses=${stakeAddresses.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + const { data } = await api.get( + `/v1/ada/stakes?stake_addresses=${stakeAddresses.join(',')}`); + return data; } /** @@ -234,13 +198,9 @@ export class AdaService extends Service { async getStakesByWallets( wallets: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/ada/stakes?wallets=${wallets.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + const { data } = await api.get( + `/v1/ada/stakes?wallets=${wallets.join(',')}`); + return data; } /** @@ -255,15 +215,13 @@ export class AdaService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/ada/rewards?accounts=${accountIds.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/ada/rewards?accounts=${accountIds.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } /** @@ -278,15 +236,13 @@ export class AdaService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/ada/rewards?stake_addresses=${stakeAddresses.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/ada/rewards?stake_addresses=${stakeAddresses.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } /** @@ -301,28 +257,24 @@ export class AdaService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/ada/rewards?wallets=${wallets.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/ada/rewards?wallets=${wallets.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } /** * Retrieve ADA network stats */ async getNetworkStats(): Promise { - try { - const { data } = await api.get( - `/v1/ada/network-stats`, - ); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/ada/network-stats`, + ); + return data; + } } diff --git a/src/services/atom.ts b/src/services/atom.ts index 595cd9f..a9ddd54 100644 --- a/src/services/atom.ts +++ b/src/services/atom.ts @@ -1,10 +1,11 @@ import { Service } from "./service"; -import { AtomRewards, AtomSignedTx, AtomStakes, AtomTx, AtomTxHash, AtomTxStatus } from "../types/atom"; +import { AtomRewards, AtomStakes } from "../types/atom"; 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"; export class AtomService extends Service { @@ -32,20 +33,18 @@ export class AtomService extends Service { pubkey: string, validatorAddress: string, amountAtom: number, - ): Promise { - try { - const { data } = await api.post( - `/v1/atom/transaction/stake`, - { - account_id: accountId, - pubkey: pubkey, - validator: validatorAddress, - amount_uatom: this.atomToUatom(amountAtom.toString()), - }); - return data; - } catch (err: any) { - throw new Error(err); - } + ): Promise { + + const { data } = await api.post( + `/v1/atom/transaction/stake`, + { + account_id: accountId, + pubkey: pubkey, + validator: validatorAddress, + amount_uatom: this.atomToUatom(amountAtom.toString()), + }); + return data; + } /** @@ -56,18 +55,16 @@ export class AtomService extends Service { async craftWithdrawRewardsTx( pubkey: string, validatorAddress: string, - ): Promise { - try { - const { data } = await api.post( - `/v1/atom/transaction/withdraw-rewards`, - { - pubkey: pubkey, - validator: validatorAddress, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + ): Promise { + + const { data } = await api.post( + `/v1/atom/transaction/withdraw-rewards`, + { + pubkey: pubkey, + validator: validatorAddress, + }); + return data; + } /** @@ -80,19 +77,17 @@ export class AtomService extends Service { pubkey: string, validatorAddress: string, amountAtom?: number, - ): Promise { - try { - const { data } = await api.post( - `/v1/atom/transaction/unstake`, - { - pubkey: pubkey, - validator: validatorAddress, - amount_uatom: amountAtom ? this.atomToUatom(amountAtom.toString()) : undefined, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + ): Promise { + + const { data } = await api.post( + `/v1/atom/transaction/unstake`, + { + pubkey: pubkey, + validator: validatorAddress, + amount_uatom: amountAtom ? this.atomToUatom(amountAtom.toString()) : undefined, + }); + return data; + } /** @@ -109,21 +104,19 @@ export class AtomService extends Service { validatorSourceAddress: string, validatorDestinationAddress: string, amountAtom?: number, - ): Promise { - try { - const { data } = await api.post( - `/v1/atom/transaction/redelegate`, - { - account_id: accountId, - pubkey: pubkey, - validator_source: validatorSourceAddress, - validator_destination: validatorDestinationAddress, - amount_uatom: amountAtom ? this.atomToUatom(amountAtom.toString()) : undefined, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + ): Promise { + + const { data } = await api.post( + `/v1/atom/transaction/redelegate`, + { + account_id: accountId, + pubkey: pubkey, + validator_source: validatorSourceAddress, + validator_destination: validatorDestinationAddress, + amount_uatom: amountAtom ? this.atomToUatom(amountAtom.toString()) : undefined, + }); + return data; + } /** @@ -132,7 +125,7 @@ export class AtomService extends Service { * @param tx raw transaction * @param note note to identify the transaction in your custody solution */ - async sign(integration: Integration, tx: AtomTx, note?: string): Promise { + async sign(integration: Integration, tx: CosmosTx, note?: string): Promise { const payload = { rawMessageData: { messages: [ @@ -146,7 +139,7 @@ export class AtomService extends Service { const signer = this.getFbSigner(integration); const fbTx = await signer.signWithFB(payload, this.testnet ? 'ATOM_COS_TEST' : 'ATOM_COS', fbNote); const signature: string = fbTx.signedMessages![0].signature.fullSig; - const { data } = await api.post( + const { data } = await api.post( `/v1/atom/transaction/prepare`, { pubkey: tx.data.pubkey, @@ -163,31 +156,27 @@ export class AtomService extends Service { * Broadcast transaction to the network * @param signedTx */ - async broadcast(signedTx: AtomSignedTx): Promise { - try { - const { data } = await api.post( - `/v1/atom/transaction/broadcast`, - { - tx_serialized: signedTx.data.signed_tx_serialized, - }); - return data; - } catch (e: any) { - throw new Error(e); - } + async broadcast(signedTx: CosmosSignedTx): Promise { + + const { data } = await api.post( + `/v1/atom/transaction/broadcast`, + { + tx_serialized: signedTx.data.signed_tx_serialized, + }); + return data; + } /** * Get transaction status * @param txHash */ - async getTxStatus(txHash: string): Promise { - try { - const { data } = await api.get( - `/v1/atom/transaction/status?tx_hash=${txHash}`); - return data; - } catch (e: any) { - throw new Error(e); - } + async getTxStatus(txHash: string): Promise { + + const { data } = await api.get( + `/v1/atom/transaction/status?tx_hash=${txHash}`); + return data; + } /** @@ -195,13 +184,11 @@ export class AtomService extends Service { * @param txSerialized transaction serialized */ async decodeTx(txSerialized: string): Promise { - try { - const { data } = await api.get( - `/v1/atom/transaction/decode?tx_serialized=${txSerialized}`); - return data; - } catch (error: any) { - throw new Error(error); - } + + const { data } = await api.get( + `/v1/atom/transaction/decode?tx_serialized=${txSerialized}`); + return data; + } /** @@ -212,13 +199,11 @@ export class AtomService extends Service { async getStakesByAccounts( accountIds: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/atom/stakes?accounts=${accountIds.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/atom/stakes?accounts=${accountIds.join(',')}`); + return data; + } /** @@ -231,13 +216,11 @@ export class AtomService extends Service { delegators: string[], validators: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/atom/stakes?delegators=${delegators.join(',')}&validators=${validators.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/atom/stakes?delegators=${delegators.join(',')}&validators=${validators.join(',')}`); + return data; + } /** @@ -252,15 +235,13 @@ export class AtomService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/atom/rewards?accounts=${accountIds.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/atom/rewards?accounts=${accountIds.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } /** @@ -277,14 +258,12 @@ export class AtomService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/atom/rewards?delegators=${delegators.join(',')}&validators=${validators.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/atom/rewards?delegators=${delegators.join(',')}&validators=${validators.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } } diff --git a/src/services/dot.ts b/src/services/dot.ts index 316e8e9..59d5a82 100644 --- a/src/services/dot.ts +++ b/src/services/dot.ts @@ -46,19 +46,17 @@ export class DotService extends Service { rewardDestination: DotRewardDestination, ): Promise { const amountPlanck = this.testnet ? this.wndToPlanck(amountDot.toString()) : this.dotToPlanck(amountDot.toString()); - try { - const { data } = await api.post( - `/v1/dot/transaction/bond`, - { - account_id: accountId, - stash_account: stashAccount, - amount_planck: amountPlanck, - reward_destination: rewardDestination, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/dot/transaction/bond`, + { + account_id: accountId, + stash_account: stashAccount, + amount_planck: amountPlanck, + reward_destination: rewardDestination, + }); + return data; + } /** @@ -71,17 +69,15 @@ export class DotService extends Service { amountDot: number, ): Promise { const amountPlanck = this.testnet ? this.wndToPlanck(amountDot.toString()) : this.dotToPlanck(amountDot.toString()); - try { - const { data } = await api.post( - `/v1/dot/transaction/bond-extra`, - { - stash_account: stashAccount, - amount_planck: amountPlanck, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/dot/transaction/bond-extra`, + { + stash_account: stashAccount, + amount_planck: amountPlanck, + }); + return data; + } /** @@ -94,17 +90,15 @@ export class DotService extends Service { amountDot: number, ): Promise { const amountPlanck = this.testnet ? this.wndToPlanck(amountDot.toString()) : this.dotToPlanck(amountDot.toString()); - try { - const { data } = await api.post( - `/v1/dot/transaction/rebond`, - { - stash_account: stashAccount, - amount_planck: amountPlanck, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/dot/transaction/rebond`, + { + stash_account: stashAccount, + amount_planck: amountPlanck, + }); + return data; + } /** @@ -116,17 +110,15 @@ export class DotService extends Service { stashAccount: string, validatorAddresses: string[], ): Promise { - try { - const { data } = await api.post( - `/v1/dot/transaction/nominate`, - { - stash_account: stashAccount, - validator_addresses: validatorAddresses, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/dot/transaction/nominate`, + { + stash_account: stashAccount, + validator_addresses: validatorAddresses, + }); + return data; + } /** @@ -139,17 +131,15 @@ export class DotService extends Service { amountDot: number, ): Promise { const amountPlanck = this.testnet ? this.wndToPlanck(amountDot.toString()) : this.dotToPlanck(amountDot.toString()); - try { - const { data } = await api.post( - `/v1/dot/transaction/unbond`, - { - stash_account: stashAccount, - amount_planck: amountPlanck, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/dot/transaction/unbond`, + { + stash_account: stashAccount, + amount_planck: amountPlanck, + }); + return data; + } /** @@ -159,16 +149,14 @@ export class DotService extends Service { async craftWithdrawUnbondedTx( stashAccount: string, ): Promise { - try { - const { data } = await api.post( - `/v1/dot/transaction/withdraw-unbonded`, - { - stash_account: stashAccount, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/dot/transaction/withdraw-unbonded`, + { + stash_account: stashAccount, + }); + return data; + } /** @@ -181,16 +169,14 @@ export class DotService extends Service { async craftChillTx( stashAccount: string, ): Promise { - try { - const { data } = await api.post( - `/v1/dot/transaction/chill`, - { - stash_account: stashAccount, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/dot/transaction/chill`, + { + stash_account: stashAccount, + }); + return data; + } /** @@ -206,17 +192,15 @@ export class DotService extends Service { stashAccount: string, rewardsDestination: DotRewardDestination, ): Promise { - try { - const { data } = await api.post( - `/v1/dot/transaction/set-payee`, - { - stash_account: stashAccount, - reward_destination: rewardsDestination, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/dot/transaction/set-payee`, + { + stash_account: stashAccount, + reward_destination: rewardsDestination, + }); + return data; + } /** @@ -233,20 +217,18 @@ export class DotService extends Service { amountDot: number, poolId: string, ): Promise { - try { - const amountPlanck = this.testnet ? this.wndToPlanck(amountDot.toString()) : this.dotToPlanck(amountDot.toString()); - const { data } = await api.post( - `/v1/dot/transaction/join-pool`, - { - account_id: accountId, - member_account: memberAccount, - amount_planck: amountPlanck, - pool_id: poolId, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const amountPlanck = this.testnet ? this.wndToPlanck(amountDot.toString()) : this.dotToPlanck(amountDot.toString()); + const { data } = await api.post( + `/v1/dot/transaction/join-pool`, + { + account_id: accountId, + member_account: memberAccount, + amount_planck: amountPlanck, + pool_id: poolId, + }); + return data; + } /** @@ -260,18 +242,16 @@ export class DotService extends Service { memberAccount: string, amountDot: number, ): Promise { - try { - const amountPlanck = this.testnet ? this.wndToPlanck(amountDot.toString()) : this.dotToPlanck(amountDot.toString()); - const { data } = await api.post( - `/v1/dot/transaction/bond-extra-pool`, - { - member_account: memberAccount, - amount_planck: amountPlanck, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const amountPlanck = this.testnet ? this.wndToPlanck(amountDot.toString()) : this.dotToPlanck(amountDot.toString()); + const { data } = await api.post( + `/v1/dot/transaction/bond-extra-pool`, + { + member_account: memberAccount, + amount_planck: amountPlanck, + }); + return data; + } /** @@ -281,16 +261,14 @@ export class DotService extends Service { async craftBondRewardsToPoolTx( memberAccount: string, ): Promise { - try { - const { data } = await api.post( - `/v1/dot/transaction/bond-rewards-pool`, - { - member_account: memberAccount, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/dot/transaction/bond-rewards-pool`, + { + member_account: memberAccount, + }); + return data; + } /** @@ -305,16 +283,14 @@ export class DotService extends Service { async craftClaimPayoutFromPoolTx( memberAccount: string, ): Promise { - try { - const { data } = await api.post( - `/v1/dot/transaction/claim-payout-pool`, - { - member_account: memberAccount, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/dot/transaction/claim-payout-pool`, + { + member_account: memberAccount, + }); + return data; + } /** @@ -329,18 +305,16 @@ export class DotService extends Service { memberAccount: string, amountDot: number, ): Promise { - try { - const amountPlanck = this.testnet ? this.wndToPlanck(amountDot.toString()) : this.dotToPlanck(amountDot.toString()); - const { data } = await api.post( - `/v1/dot/transaction/unbond-pool`, - { - member_account: memberAccount, - amount_planck: amountPlanck, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const amountPlanck = this.testnet ? this.wndToPlanck(amountDot.toString()) : this.dotToPlanck(amountDot.toString()); + const { data } = await api.post( + `/v1/dot/transaction/unbond-pool`, + { + member_account: memberAccount, + amount_planck: amountPlanck, + }); + return data; + } /** @@ -351,16 +325,14 @@ export class DotService extends Service { async craftWithdrawUnbondedFromPoolTx( memberAccount: string, ): Promise { - try { - const { data } = await api.post( - `/v1/dot/transaction/withdraw-unbonded-pool`, - { - member_account: memberAccount, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/dot/transaction/withdraw-unbonded-pool`, + { + member_account: memberAccount, + }); + return data; + } /** @@ -370,33 +342,31 @@ export class DotService extends Service { * @param note note to identify the transaction in your custody solution */ async sign(integration: Integration, tx: DotTx, note?: string): Promise { - try { - const payload = { - rawMessageData: { - messages: [ - { - content: tx.data.unsigned_tx_payload.substring(2), - }, - ], - }, - }; - - const fbSigner = this.getFbSigner(integration); - const fbNote = note ? note : 'DOT tx from @kilnfi/sdk'; - const fbTx = await fbSigner.signWithFB(payload, this.testnet ? 'WND' : 'DOT', fbNote); - const signature = `0x00${fbTx.signedMessages![0].signature.fullSig}`; - - const { data } = await api.post( - `/v1/dot/transaction/prepare`, - { - unsigned_tx_serialized: tx.data.unsigned_tx_serialized, - signature: signature, - }); - data.data.fireblocks_tx = fbTx; - return data; - } catch (err: any) { - throw new Error(err); - } + + const payload = { + rawMessageData: { + messages: [ + { + content: tx.data.unsigned_tx_payload.substring(2), + }, + ], + }, + }; + + const fbSigner = this.getFbSigner(integration); + const fbNote = note ? note : 'DOT tx from @kilnfi/sdk'; + const fbTx = await fbSigner.signWithFB(payload, this.testnet ? 'WND' : 'DOT', fbNote); + const signature = `0x00${fbTx.signedMessages![0].signature.fullSig}`; + + const { data } = await api.post( + `/v1/dot/transaction/prepare`, + { + unsigned_tx_serialized: tx.data.unsigned_tx_serialized, + signature: signature, + }); + data.data.fireblocks_tx = fbTx; + return data; + } /** @@ -404,16 +374,14 @@ export class DotService extends Service { * @param signedTx */ async broadcast(signedTx: DotSignedTx): Promise { - try { - const { data } = await api.post( - `/v1/dot/transaction/broadcast`, - { - tx_serialized: signedTx.data.signed_tx_serialized, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/dot/transaction/broadcast`, + { + tx_serialized: signedTx.data.signed_tx_serialized, + }); + return data; + } /** @@ -423,12 +391,10 @@ export class DotService extends Service { async getTxStatus( txHash: string, ): Promise { - try { - const { data } = await api.get(`/v1/dot/transaction/status?tx_hash=${txHash}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get(`/v1/dot/transaction/status?tx_hash=${txHash}`); + return data; + } /** @@ -436,12 +402,10 @@ export class DotService extends Service { * @param txSerialized transaction serialized */ async decodeTx(txSerialized: string): Promise { - try { - const { data } = await api.get( - `/v1/dot/transaction/decode?tx_serialized=${txSerialized}`); - return data; - } catch (error: any) { - throw new Error(error); - } + + const { data } = await api.get( + `/v1/dot/transaction/decode?tx_serialized=${txSerialized}`); + return data; + } } diff --git a/src/services/dydx.ts b/src/services/dydx.ts index 8209c99..560ca91 100644 --- a/src/services/dydx.ts +++ b/src/services/dydx.ts @@ -4,7 +4,7 @@ import { ServiceProps } from '../types/service'; import { Integration } from '../types/integrations'; import api from '../api'; import { DecodedTxRaw } from "@cosmjs/proto-signing"; -import { DydxSignedTx, DydxTxHash, DydxTxStatus, DydxTx } from "../types/dydx"; +import { CosmosSignedTx, CosmosTx, CosmosTxHash, CosmosTxStatus } from "../types/cosmos"; export class DydxService extends Service { @@ -32,20 +32,18 @@ export class DydxService extends Service { pubkey: string, validatorAddress: string, amountDydx: number, - ): Promise { - try { - const { data } = await api.post( - `/v1/dydx/transaction/stake`, - { - account_id: accountId, - pubkey: pubkey, - validator: validatorAddress, - amount_adydx: this.dydxToAdydx(amountDydx.toString()), - }); - return data; - } catch (err: any) { - throw new Error(err); - } + ): Promise { + + const { data } = await api.post( + `/v1/dydx/transaction/stake`, + { + account_id: accountId, + pubkey: pubkey, + validator: validatorAddress, + amount_adydx: this.dydxToAdydx(amountDydx.toString()), + }); + return data; + } /** @@ -56,18 +54,16 @@ export class DydxService extends Service { async craftWithdrawRewardsTx( pubkey: string, validatorAddress: string, - ): Promise { - try { - const { data } = await api.post( - `/v1/dydx/transaction/withdraw-rewards`, - { - pubkey: pubkey, - validator: validatorAddress, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + ): Promise { + + const { data } = await api.post( + `/v1/dydx/transaction/withdraw-rewards`, + { + pubkey: pubkey, + validator: validatorAddress, + }); + return data; + } /** @@ -80,19 +76,17 @@ export class DydxService extends Service { pubkey: string, validatorAddress: string, amountDydx?: number, - ): Promise { - try { - const { data } = await api.post( - `/v1/dydx/transaction/unstake`, - { - pubkey: pubkey, - validator: validatorAddress, - amount_adydx: amountDydx ? this.dydxToAdydx(amountDydx.toString()) : undefined, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + ): Promise { + + const { data } = await api.post( + `/v1/dydx/transaction/unstake`, + { + pubkey: pubkey, + validator: validatorAddress, + amount_adydx: amountDydx ? this.dydxToAdydx(amountDydx.toString()) : undefined, + }); + return data; + } /** @@ -109,21 +103,19 @@ export class DydxService extends Service { validatorSourceAddress: string, validatorDestinationAddress: string, amountDydx?: number, - ): Promise { - try { - const { data } = await api.post( - `/v1/dydx/transaction/redelegate`, - { - account_id: accountId, - pubkey: pubkey, - validator_source: validatorSourceAddress, - validator_destination: validatorDestinationAddress, - amount_adydx: amountDydx ? this.dydxToAdydx(amountDydx.toString()) : undefined, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + ): Promise { + + const { data } = await api.post( + `/v1/dydx/transaction/redelegate`, + { + account_id: accountId, + pubkey: pubkey, + validator_source: validatorSourceAddress, + validator_destination: validatorDestinationAddress, + amount_adydx: amountDydx ? this.dydxToAdydx(amountDydx.toString()) : undefined, + }); + return data; + } /** @@ -132,7 +124,7 @@ export class DydxService extends Service { * @param tx raw transaction * @param note note to identify the transaction in your custody solution */ - async sign(integration: Integration, tx: DydxTx, note?: string): Promise { + async sign(integration: Integration, tx: CosmosTx, note?: string): Promise { const payload = { rawMessageData: { messages: [ @@ -146,7 +138,7 @@ export class DydxService extends Service { const signer = this.getFbSigner(integration); const fbTx = await signer.signWithFB(payload, this.testnet ? 'DV4TNT_TEST' : 'DYDX_DYDX', fbNote); const signature: string = fbTx.signedMessages![0].signature.fullSig; - const { data } = await api.post( + const { data } = await api.post( `/v1/dydx/transaction/prepare`, { pubkey: tx.data.pubkey, @@ -163,31 +155,27 @@ export class DydxService extends Service { * Broadcast transaction to the network * @param signedTx */ - async broadcast(signedTx: DydxSignedTx): Promise { - try { - const { data } = await api.post( - `/v1/dydx/transaction/broadcast`, - { - tx_serialized: signedTx.data.signed_tx_serialized, - }); - return data; - } catch (e: any) { - throw new Error(e); - } + async broadcast(signedTx: CosmosSignedTx): Promise { + + const { data } = await api.post( + `/v1/dydx/transaction/broadcast`, + { + tx_serialized: signedTx.data.signed_tx_serialized, + }); + return data; + } /** * Get transaction status * @param txHash */ - async getTxStatus(txHash: string): Promise { - try { - const { data } = await api.get( - `/v1/dydx/transaction/status?tx_hash=${txHash}`); - return data; - } catch (e: any) { - throw new Error(e); - } + async getTxStatus(txHash: string): Promise { + + const { data } = await api.get( + `/v1/dydx/transaction/status?tx_hash=${txHash}`); + return data; + } /** @@ -195,12 +183,10 @@ export class DydxService extends Service { * @param txSerialized transaction serialized */ async decodeTx(txSerialized: string): Promise { - try { - const { data } = await api.get( - `/v1/dydx/transaction/decode?tx_serialized=${txSerialized}`); - return data; - } catch (error: any) { - throw new Error(error); - } + + const { data } = await api.get( + `/v1/dydx/transaction/decode?tx_serialized=${txSerialized}`); + return data; + } } diff --git a/src/services/eth.ts b/src/services/eth.ts index 66fffe3..ee3fc67 100644 --- a/src/services/eth.ts +++ b/src/services/eth.ts @@ -32,18 +32,16 @@ export class EthService extends Service { walletAddress: string, amountEth: number, ): Promise { - try { - const { data } = await api.post( - `/v1/eth/transaction/stake`, - { - account_id: accountId, - wallet: walletAddress, - amount_wei: this.ethToWei(amountEth.toString()), - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/eth/transaction/stake`, + { + account_id: accountId, + wallet: walletAddress, + amount_wei: this.ethToWei(amountEth.toString()), + }); + return data; + } /** @@ -55,17 +53,15 @@ export class EthService extends Service { walletAddress: string, validatorAddresses: string[], ): Promise { - try { - const { data } = await api.post( - `/v1/eth/transaction/exit-request`, - { - wallet: walletAddress, - validators: validatorAddresses, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/eth/transaction/exit-request`, + { + wallet: walletAddress, + validators: validatorAddresses, + }); + return data; + } /** @@ -75,38 +71,36 @@ export class EthService extends Service { * @param note note to identify the transaction in your custody solution */ async sign(integration: Integration, tx: EthTx, note?: string): Promise { - try { - const payload = { - rawMessageData: { - messages: [ - { - 'content': tx.data.unsigned_tx_hash, - }, - ], - }, - }; - - const fbSigner = this.getFbSigner(integration); - const assetId = this.testnet ? 'ETH_TEST6' : 'ETH'; - const fbNote = note ? note : 'ETH tx from @kilnfi/sdk'; - const fbTx = await fbSigner.signWithFB( - payload, - assetId, - fbNote, - ); - const { data } = await api.post( - `/v1/eth/transaction/prepare`, - { - unsigned_tx_serialized: tx.data.unsigned_tx_serialized, - r: `0x${fbTx?.signedMessages?.[0].signature.r}`, - s: `0x${fbTx?.signedMessages?.[0].signature.s}`, - v: fbTx?.signedMessages?.[0].signature.v ?? 0, - }); - data.data.fireblocks_tx = fbTx; - return data; - } catch (err: any) { - throw new Error(err); - } + + const payload = { + rawMessageData: { + messages: [ + { + 'content': tx.data.unsigned_tx_hash, + }, + ], + }, + }; + + const fbSigner = this.getFbSigner(integration); + const assetId = this.testnet ? 'ETH_TEST6' : 'ETH'; + const fbNote = note ? note : 'ETH tx from @kilnfi/sdk'; + const fbTx = await fbSigner.signWithFB( + payload, + assetId, + fbNote, + ); + const { data } = await api.post( + `/v1/eth/transaction/prepare`, + { + unsigned_tx_serialized: tx.data.unsigned_tx_serialized, + r: `0x${fbTx?.signedMessages?.[0].signature.r}`, + s: `0x${fbTx?.signedMessages?.[0].signature.s}`, + v: fbTx?.signedMessages?.[0].signature.v ?? 0, + }); + data.data.fireblocks_tx = fbTx; + return data; + } /** @@ -119,25 +113,23 @@ export class EthService extends Service { if(!integration.fireblocksDestinationId) { throw new Error('Fireblocks destination id is missing in integration'); } - try { - const payload = { - contractCallData: tx.data.contract_call_data, - }; - - const fbSigner = this.getFbSigner(integration); - const assetId = this.testnet ? 'ETH_TEST6' : 'ETH'; - const fbNote = note ? note : 'ETH tx from @kilnfi/sdk'; - return await fbSigner.signAndBroadcastWithFB( - payload, - assetId, - tx, - integration.fireblocksDestinationId, - true, - fbNote, - ); - } catch (err: any) { - throw new Error(err); - } + + const payload = { + contractCallData: tx.data.contract_call_data, + }; + + const fbSigner = this.getFbSigner(integration); + const assetId = this.testnet ? 'ETH_TEST6' : 'ETH'; + const fbNote = note ? note : 'ETH tx from @kilnfi/sdk'; + return await fbSigner.signAndBroadcastWithFB( + payload, + assetId, + tx, + integration.fireblocksDestinationId, + true, + fbNote, + ); + } /** @@ -145,16 +137,14 @@ export class EthService extends Service { * @param signedTx */ async broadcast(signedTx: EthSignedTx): Promise { - try { - const { data } = await api.post( - `/v1/eth/transaction/broadcast`, - { - tx_serialized: signedTx.data.signed_tx_serialized, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/eth/transaction/broadcast`, + { + tx_serialized: signedTx.data.signed_tx_serialized, + }); + return data; + } /** @@ -162,13 +152,11 @@ export class EthService extends Service { * @param txHash transaction hash */ async getTxStatus(txHash: string): Promise { - try { - const { data } = await api.get( - `/v1/eth/transaction/status?tx_hash=${txHash}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/eth/transaction/status?tx_hash=${txHash}`); + return data; + } /** @@ -176,13 +164,11 @@ export class EthService extends Service { * @param txSerialized transaction serialized */ async decodeTx(txSerialized: string): Promise { - try { - const { data } = await api.get( - `/v1/eth/transaction/decode?tx_serialized=${txSerialized}`); - return data; - } catch (error: any) { - throw new Error(error); - } + + const { data } = await api.get( + `/v1/eth/transaction/decode?tx_serialized=${txSerialized}`); + return data; + } /** @@ -193,13 +179,11 @@ export class EthService extends Service { async getStakesByAccounts( accountIds: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/eth/stakes?accounts=${accountIds.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/eth/stakes?accounts=${accountIds.join(',')}`); + return data; + } /** @@ -210,14 +194,12 @@ export class EthService extends Service { async getStakesByWallets( walletAddresses: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/eth/stakes?wallets=${walletAddresses.join(',')}`, - ); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/eth/stakes?wallets=${walletAddresses.join(',')}`, + ); + return data; + } /** @@ -226,14 +208,12 @@ export class EthService extends Service { * @returns {EthStakes} Ethereum Stakes */ async getStakesByValidators(validatorAddresses: string[]): Promise { - try { - const { data } = await api.get( - `/v1/eth/stakes?validators=${validatorAddresses.join(',')}`, - ); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/eth/stakes?validators=${validatorAddresses.join(',')}`, + ); + return data; + } /** @@ -248,15 +228,13 @@ export class EthService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/eth/rewards?accounts=${accountIds.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/eth/rewards?accounts=${accountIds.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } /** @@ -271,15 +249,13 @@ export class EthService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/eth/rewards?wallets=${walletAddresses.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/eth/rewards?wallets=${walletAddresses.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } /** @@ -294,43 +270,37 @@ export class EthService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/eth/rewards?validators=${validatorAddresses.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/eth/rewards?validators=${validatorAddresses.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } /** * Retrieve ETH network stats */ async getNetworkStats(): Promise { - try { - const { data } = await api.get( - `/v1/eth/network-stats`, - ); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/eth/network-stats`, + ); + return data; + } /** * Retrieve ETH kiln stats */ async getKilnStats(): Promise { - try { - const { data } = await api.get( - `/v1/eth/kiln-stats`, - ); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/eth/kiln-stats`, + ); + return data; + } /** diff --git a/src/services/fireblocks.ts b/src/services/fireblocks.ts index c3a9a3b..1019441 100644 --- a/src/services/fireblocks.ts +++ b/src/services/fireblocks.ts @@ -1,6 +1,6 @@ import { Integration } from "../types/integrations"; import { Service } from "./service"; -import { PublicKeyResponse } from "fireblocks-sdk"; +import { AssetTypeResponse, PublicKeyResponse } from "fireblocks-sdk"; export class FireblocksService extends Service { @@ -20,4 +20,9 @@ export class FireblocksService extends Service { }); return data; } + + async getAssets(integration: Integration): Promise { + const fbSdk = this.getFbSdk(integration); + return await fbSdk.getSupportedAssets(); + } } \ No newline at end of file diff --git a/src/services/matic.ts b/src/services/matic.ts index eacdddc..8be6c9d 100644 --- a/src/services/matic.ts +++ b/src/services/matic.ts @@ -29,18 +29,16 @@ export class MaticService extends Service { contractAddressToApprove: string, amountMatic?: number, ): Promise { - try { - const { data } = await api.post( - `/v1/matic/transaction/approve`, - { - wallet: walletAddress, - contract: contractAddressToApprove, - amount_wei: amountMatic ? this.maticToWei(amountMatic?.toString()) : undefined, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/matic/transaction/approve`, + { + wallet: walletAddress, + contract: contractAddressToApprove, + amount_wei: amountMatic ? this.maticToWei(amountMatic?.toString()) : undefined, + }); + return data; + } /** @@ -57,19 +55,17 @@ export class MaticService extends Service { validatorShareProxyAddress: string, amountMatic: number, ): Promise { - try { - const { data } = await api.post( - `/v1/matic/transaction/buy-voucher`, - { - account_id: accountId, - wallet: walletAddress, - amount_wei: this.maticToWei(amountMatic.toString()), - validator_share_proxy_address: validatorShareProxyAddress, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/matic/transaction/buy-voucher`, + { + account_id: accountId, + wallet: walletAddress, + amount_wei: this.maticToWei(amountMatic.toString()), + validator_share_proxy_address: validatorShareProxyAddress, + }); + return data; + } /** @@ -84,18 +80,16 @@ export class MaticService extends Service { validatorShareProxyAddress: string, amountMatic: number, ): Promise { - try { - const { data } = await api.post( - `/v1/matic/transaction/sell-voucher`, - { - wallet: walletAddress, - amount_wei: this.maticToWei(amountMatic.toString()), - validator_share_proxy_address: validatorShareProxyAddress, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/matic/transaction/sell-voucher`, + { + wallet: walletAddress, + amount_wei: this.maticToWei(amountMatic.toString()), + validator_share_proxy_address: validatorShareProxyAddress, + }); + return data; + } /** @@ -108,17 +102,15 @@ export class MaticService extends Service { walletAddress: string, validatorShareProxyAddress: string, ): Promise { - try { - const { data } = await api.post( - `/v1/matic/transaction/unstake-claim-tokens`, - { - wallet: walletAddress, - validator_share_proxy_address: validatorShareProxyAddress, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/matic/transaction/unstake-claim-tokens`, + { + wallet: walletAddress, + validator_share_proxy_address: validatorShareProxyAddress, + }); + return data; + } /** @@ -131,17 +123,15 @@ export class MaticService extends Service { walletAddress: string, validatorShareProxyAddress: string, ): Promise { - try { - const { data } = await api.post( - `/v1/matic/transaction/withdraw-rewards`, - { - wallet: walletAddress, - validator_share_proxy_address: validatorShareProxyAddress, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/matic/transaction/withdraw-rewards`, + { + wallet: walletAddress, + validator_share_proxy_address: validatorShareProxyAddress, + }); + return data; + } /** @@ -154,17 +144,15 @@ export class MaticService extends Service { walletAddress: string, validatorShareProxyAddress: string, ): Promise { - try { - const { data } = await api.post( - `/v1/matic/transaction/restake-rewards`, - { - wallet: walletAddress, - validator_share_proxy_address: validatorShareProxyAddress, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/matic/transaction/restake-rewards`, + { + wallet: walletAddress, + validator_share_proxy_address: validatorShareProxyAddress, + }); + return data; + } /** @@ -174,33 +162,31 @@ export class MaticService extends Service { * @param note note to identify the transaction in your custody solution */ async sign(integration: Integration, tx: MaticTx, note?: string): Promise { - try { - const payload = { - rawMessageData: { - messages: [ - { - 'content': tx.data.unsigned_tx_hash, - }, - ], - }, - }; - const fbSigner = this.getFbSigner(integration); - const fbNote = note ? note : 'MATIC tx from @kilnfi/sdk'; - const fbTx = await fbSigner.signWithFB(payload, this.testnet ? 'ETH_TEST3' : 'ETH', fbNote); - const { data } = await api.post( - `/v1/matic/transaction/prepare`, - { - unsigned_tx_serialized: tx.data.unsigned_tx_serialized, - r: `0x${fbTx?.signedMessages?.[0].signature.r}`, - s: `0x${fbTx?.signedMessages?.[0].signature.s}`, - v: fbTx?.signedMessages?.[0].signature.v ?? 0, - }); - data.data.fireblocks_tx = fbTx; - return data; - } catch (err: any) { - throw new Error(err); - } + const payload = { + rawMessageData: { + messages: [ + { + 'content': tx.data.unsigned_tx_hash, + }, + ], + }, + }; + + const fbSigner = this.getFbSigner(integration); + const fbNote = note ? note : 'MATIC tx from @kilnfi/sdk'; + const fbTx = await fbSigner.signWithFB(payload, this.testnet ? 'ETH_TEST3' : 'ETH', fbNote); + const { data } = await api.post( + `/v1/matic/transaction/prepare`, + { + unsigned_tx_serialized: tx.data.unsigned_tx_serialized, + r: `0x${fbTx?.signedMessages?.[0].signature.r}`, + s: `0x${fbTx?.signedMessages?.[0].signature.s}`, + v: fbTx?.signedMessages?.[0].signature.v ?? 0, + }); + data.data.fireblocks_tx = fbTx; + return data; + } /** @@ -213,25 +199,23 @@ export class MaticService extends Service { if(!integration.fireblocksDestinationId) { throw new Error('Fireblocks destination id is missing in integration'); } - try { - const payload = { - contractCallData: tx.data.contract_call_data, - }; - const fbSigner = this.getFbSigner(integration); - const fbNote = note ? note : 'MATIC tx from @kilnfi/sdk'; - const assetId = this.testnet ? 'ETH_TEST3' : 'ETH'; - return await fbSigner.signAndBroadcastWithFB( - payload, - assetId, - tx, - integration.fireblocksDestinationId, - false, - fbNote, - ); - } catch (err: any) { - throw new Error(err); - } + const payload = { + contractCallData: tx.data.contract_call_data, + }; + + const fbSigner = this.getFbSigner(integration); + const fbNote = note ? note : 'MATIC tx from @kilnfi/sdk'; + const assetId = this.testnet ? 'ETH_TEST3' : 'ETH'; + return await fbSigner.signAndBroadcastWithFB( + payload, + assetId, + tx, + integration.fireblocksDestinationId, + false, + fbNote, + ); + } @@ -240,16 +224,14 @@ export class MaticService extends Service { * @param signedTx */ async broadcast(signedTx: MaticSignedTx): Promise { - try { - const { data } = await api.post( - `/v1/matic/transaction/broadcast`, - { - tx_serialized: signedTx.data.signed_tx_serialized, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/matic/transaction/broadcast`, + { + tx_serialized: signedTx.data.signed_tx_serialized, + }); + return data; + } /** @@ -257,13 +239,11 @@ export class MaticService extends Service { * @param txHash transaction hash */ async getTxStatus(txHash: string): Promise { - try { - const { data } = await api.get( - `/v1/matic/transaction/status?tx_hash=${txHash}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/matic/transaction/status?tx_hash=${txHash}`); + return data; + } /** @@ -271,13 +251,11 @@ export class MaticService extends Service { * @param txSerialized transaction serialized */ async decodeTx(txSerialized: string): Promise { - try { - const { data } = await api.get( - `/v1/matic/transaction/decode?tx_serialized=${txSerialized}`); - return data; - } catch (error: any) { - throw new Error(error); - } + + const { data } = await api.get( + `/v1/matic/transaction/decode?tx_serialized=${txSerialized}`); + return data; + } /** diff --git a/src/services/near.ts b/src/services/near.ts index 8cc4b29..ca772c2 100644 --- a/src/services/near.ts +++ b/src/services/near.ts @@ -264,17 +264,15 @@ export class NearService extends Service { * @param signedTx */ async broadcast(signedTx: NearSignedTx): Promise { - try { - const connection = await this.getConnection(); - const res = await connection.connection.provider.sendTransaction(signedTx.data.tx); - return { - data: { - tx_hash: res.transaction.hash, - }, - }; - } catch (e: any) { - throw new Error(e); - } + + const connection = await this.getConnection(); + const res = await connection.connection.provider.sendTransaction(signedTx.data.tx); + return { + data: { + tx_hash: res.transaction.hash, + }, + }; + } /** @@ -283,19 +281,17 @@ export class NearService extends Service { * @param poolId pool id */ async getTxStatus(transactionHash: string, poolId: string): Promise { - try { - const connection = await this.getConnection(); - const receipt = await connection.connection.provider.txStatusReceipts(transactionHash, poolId); - const status = Object.keys(receipt.status).includes('SuccessValue') ? 'success' : 'error'; - return { - data: { - status: status, - receipt: receipt, - }, - }; - } catch (e: any) { - throw new Error(e); - } + + const connection = await this.getConnection(); + const receipt = await connection.connection.provider.txStatusReceipts(transactionHash, poolId); + const status = Object.keys(receipt.status).includes('SuccessValue') ? 'success' : 'error'; + return { + data: { + status: status, + receipt: receipt, + }, + }; + } /** @@ -303,13 +299,11 @@ export class NearService extends Service { * @param txSerialized transaction serialized */ async decodeTx(txSerialized: string): Promise { - try { - const { data } = await api.get( - `/v1/near/transaction/decode?tx_serialized=${txSerialized}`); - return data; - } catch (error: any) { - throw new Error(error); - } + + const { data } = await api.get( + `/v1/near/transaction/decode?tx_serialized=${txSerialized}`); + return data; + } /** @@ -320,13 +314,11 @@ export class NearService extends Service { async getStakesByAccounts( accountIds: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/near/stakes?accounts=${accountIds.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/near/stakes?accounts=${accountIds.join(',')}`); + return data; + } /** @@ -337,13 +329,11 @@ export class NearService extends Service { async getStakesByStakeAccounts( stakeAccounts: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/near/stakes?stake_accounts=${stakeAccounts.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/near/stakes?stake_accounts=${stakeAccounts.join(',')}`); + return data; + } /** @@ -354,13 +344,11 @@ export class NearService extends Service { async getStakesByWallets( wallets: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/near/stakes?wallets=${wallets.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/near/stakes?wallets=${wallets.join(',')}`); + return data; + } /** @@ -375,15 +363,13 @@ export class NearService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const { data } = await api.get( - `/v1/near/rewards?accounts=${accountIds.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/near/rewards?accounts=${accountIds.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`); + return data; + } /** @@ -398,15 +384,13 @@ export class NearService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const { data } = await api.get( - `/v1/near/rewards?stake_accounts=${stakeAccounts.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/near/rewards?stake_accounts=${stakeAccounts.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`); + return data; + } /** @@ -421,28 +405,24 @@ export class NearService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const { data } = await api.get( - `/v1/near/rewards?wallets=${wallets.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/near/rewards?wallets=${wallets.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`); + return data; + } /** * Retrieve NEAR network stats */ async getNetworkStats(): Promise { - try { - const { data } = await api.get( - `/v1/near/network-stats`, - ); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/near/network-stats`, + ); + return data; + } } diff --git a/src/services/osmo.ts b/src/services/osmo.ts index 6f5d038..00ae1ba 100644 --- a/src/services/osmo.ts +++ b/src/services/osmo.ts @@ -2,16 +2,13 @@ import { Service } from './service'; import { OsmoRewards, - OsmoSignedTx, OsmoStakes, - OsmoTx, - OsmoTxHash, - OsmoTxStatus, } from '../types/osmo'; 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"; export class OsmoService extends Service { @@ -39,20 +36,18 @@ export class OsmoService extends Service { pubkey: string, validatorAddress: string, amountOsmo: number, - ): Promise { - try { - const { data } = await api.post( - `/v1/osmo/transaction/stake`, - { - account_id: accountId, - pubkey: pubkey, - validator: validatorAddress, - amount_uosmo: this.osmoToUosmo(amountOsmo.toString()), - }); - return data; - } catch (err: any) { - throw new Error(err); - } + ): Promise { + + const { data } = await api.post( + `/v1/osmo/transaction/stake`, + { + account_id: accountId, + pubkey: pubkey, + validator: validatorAddress, + amount_uosmo: this.osmoToUosmo(amountOsmo.toString()), + }); + return data; + } /** @@ -63,18 +58,16 @@ export class OsmoService extends Service { async craftWithdrawRewardsTx( pubkey: string, validatorAddress: string, - ): Promise { - try { - const { data } = await api.post( - `/v1/osmo/transaction/withdraw-rewards`, - { - pubkey: pubkey, - validator: validatorAddress, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + ): Promise { + + const { data } = await api.post( + `/v1/osmo/transaction/withdraw-rewards`, + { + pubkey: pubkey, + validator: validatorAddress, + }); + return data; + } /** @@ -87,19 +80,17 @@ export class OsmoService extends Service { pubkey: string, validatorAddress: string, amountOsmo?: number, - ): Promise { - try { - const { data } = await api.post( - `/v1/osmo/transaction/unstake`, - { - pubkey: pubkey, - validator: validatorAddress, - amount_uosmo: amountOsmo ? this.osmoToUosmo(amountOsmo.toString()) : undefined, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + ): Promise { + + const { data } = await api.post( + `/v1/osmo/transaction/unstake`, + { + pubkey: pubkey, + validator: validatorAddress, + amount_uosmo: amountOsmo ? this.osmoToUosmo(amountOsmo.toString()) : undefined, + }); + return data; + } /** @@ -116,21 +107,19 @@ export class OsmoService extends Service { validatorSourceAddress: string, validatorDestinationAddress: string, amountOsmo?: number, - ): Promise { - try { - const { data } = await api.post( - `/v1/osmo/transaction/redelegate`, - { - account_id: accountId, - pubkey: pubkey, - validator_source: validatorSourceAddress, - validator_destination: validatorDestinationAddress, - amount_uosmo: amountOsmo ? this.osmoToUosmo(amountOsmo.toString()) : undefined, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + ): Promise { + + const { data } = await api.post( + `/v1/osmo/transaction/redelegate`, + { + account_id: accountId, + pubkey: pubkey, + validator_source: validatorSourceAddress, + validator_destination: validatorDestinationAddress, + amount_uosmo: amountOsmo ? this.osmoToUosmo(amountOsmo.toString()) : undefined, + }); + return data; + } /** @@ -139,7 +128,7 @@ export class OsmoService extends Service { * @param tx raw transaction * @param note note to identify the transaction in your custody solution */ - async sign(integration: Integration, tx: OsmoTx, note?: string): Promise { + async sign(integration: Integration, tx: CosmosTx, note?: string): Promise { const payload = { rawMessageData: { messages: [ @@ -153,7 +142,7 @@ export class OsmoService extends Service { const signer = this.getFbSigner(integration); const fbTx = await signer.signWithFB(payload, this.testnet ? 'OSMO_TEST' : 'OSMO', fbNote); const signature: string = fbTx.signedMessages![0].signature.fullSig; - const { data } = await api.post( + const { data } = await api.post( `/v1/osmo/transaction/prepare`, { pubkey: tx.data.pubkey, @@ -170,31 +159,27 @@ export class OsmoService extends Service { * Broadcast transaction to the network * @param signedTx */ - async broadcast(signedTx: OsmoSignedTx): Promise { - try { - const { data } = await api.post( - `/v1/osmo/transaction/broadcast`, - { - tx_serialized: signedTx.data.signed_tx_serialized, - }); - return data; - } catch (e: any) { - throw new Error(e); - } + async broadcast(signedTx: CosmosSignedTx): Promise { + + const { data } = await api.post( + `/v1/osmo/transaction/broadcast`, + { + tx_serialized: signedTx.data.signed_tx_serialized, + }); + return data; + } /** * Get transaction status * @param txHash */ - async getTxStatus(txHash: string): Promise { - try { - const { data } = await api.get( - `/v1/osmo/transaction/status?tx_hash=${txHash}`); - return data; - } catch (e: any) { - throw new Error(e); - } + async getTxStatus(txHash: string): Promise { + + const { data } = await api.get( + `/v1/osmo/transaction/status?tx_hash=${txHash}`); + return data; + } /** @@ -202,13 +187,11 @@ export class OsmoService extends Service { * @param txSerialized transaction serialized */ async decodeTx(txSerialized: string): Promise { - try { - const { data } = await api.get( - `/v1/osmo/transaction/decode?tx_serialized=${txSerialized}`); - return data; - } catch (error: any) { - throw new Error(error); - } + + const { data } = await api.get( + `/v1/osmo/transaction/decode?tx_serialized=${txSerialized}`); + return data; + } /** @@ -219,13 +202,11 @@ export class OsmoService extends Service { async getStakesByAccounts( accountIds: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/osmo/stakes?accounts=${accountIds.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/osmo/stakes?accounts=${accountIds.join(',')}`); + return data; + } /** @@ -238,13 +219,11 @@ export class OsmoService extends Service { delegators: string[], validators: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/osmo/stakes?delegators=${delegators.join(',')}&validators=${validators.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/osmo/stakes?delegators=${delegators.join(',')}&validators=${validators.join(',')}`); + return data; + } /** @@ -259,15 +238,13 @@ export class OsmoService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/osmo/rewards?accounts=${accountIds.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/osmo/rewards?accounts=${accountIds.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } /** @@ -284,14 +261,12 @@ export class OsmoService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/osmo/rewards?delegators=${delegators.join(',')}&validators=${validators.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/osmo/rewards?delegators=${delegators.join(',')}&validators=${validators.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } } diff --git a/src/services/sol.ts b/src/services/sol.ts index 640e1ec..7920048 100644 --- a/src/services/sol.ts +++ b/src/services/sol.ts @@ -34,20 +34,18 @@ export class SolService extends Service { amountSol: number, memo?: string, ): Promise { - try { - const { data } = await api.post( - `/v1/sol/transaction/stake`, - { - account_id: accountId, - wallet: walletAddress, - amount_lamports: this.solToLamports(amountSol.toString()), - vote_account_address: voteAccountAddress, - memo, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/sol/transaction/stake`, + { + account_id: accountId, + wallet: walletAddress, + amount_lamports: this.solToLamports(amountSol.toString()), + vote_account_address: voteAccountAddress, + memo, + }); + return data; + } /** @@ -59,17 +57,15 @@ export class SolService extends Service { stakeAccountAddress: string, walletAddress: string, ): Promise { - try { - const { data } = await api.post( - `/v1/sol/transaction/deactivate-stake`, - { - stake_account: stakeAccountAddress, - wallet: walletAddress, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/sol/transaction/deactivate-stake`, + { + stake_account: stakeAccountAddress, + wallet: walletAddress, + }); + return data; + } /** @@ -83,18 +79,16 @@ export class SolService extends Service { walletAddress: string, amountSol?: number, ): Promise { - try { - const { data } = await api.post( - `/v1/sol/transaction/withdraw-stake`, - { - stake_account: stakeAccountAddress, - wallet: walletAddress, - amount_lamports: amountSol ? this.solToLamports(amountSol.toString()) : undefined, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/sol/transaction/withdraw-stake`, + { + stake_account: stakeAccountAddress, + wallet: walletAddress, + amount_lamports: amountSol ? this.solToLamports(amountSol.toString()) : undefined, + }); + return data; + } /** @@ -109,18 +103,16 @@ export class SolService extends Service { stakeAccountDestinationAddress: string, walletAddress: string, ): Promise { - try { - const { data } = await api.post( - `/v1/sol/transaction/merge-stakes`, - { - stake_account_source: stakeAccountSourceAddress, - stake_account_destination: stakeAccountDestinationAddress, - wallet: walletAddress, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/sol/transaction/merge-stakes`, + { + stake_account_source: stakeAccountSourceAddress, + stake_account_destination: stakeAccountDestinationAddress, + wallet: walletAddress, + }); + return data; + } /** @@ -136,19 +128,17 @@ export class SolService extends Service { walletAddress: string, amountSol: number, ): Promise { - try { - const { data } = await api.post( - `/v1/sol/transaction/split-stake`, - { - account_id: accountId, - stake_account: stakeAccountAddress, - wallet: walletAddress, - amount_lamports: this.solToLamports(amountSol.toString()), - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/sol/transaction/split-stake`, + { + account_id: accountId, + stake_account: stakeAccountAddress, + wallet: walletAddress, + amount_lamports: this.solToLamports(amountSol.toString()), + }); + return data; + } /** @@ -158,38 +148,36 @@ export class SolService extends Service { * @param note note to identify the transaction in your custody solution */ async sign(integration: Integration, tx: SolTx, note?: string): Promise { - try { - const payload = { - rawMessageData: { - messages: [ - { - 'content': tx.data.unsigned_tx_hash, - }, - ], - }, - }; - - const fbSigner = this.getFbSigner(integration); - const fbNote = note ? note : 'SOL tx from @kilnfi/sdk'; - const fbTx = await fbSigner.signWithFB(payload, this.testnet ? 'SOL_TEST' : 'SOL', fbNote); - const signatures: string[] = []; - fbTx.signedMessages?.forEach((signedMessage: any) => { - if (signedMessage.derivationPath[3] == 0) { - signatures.push(signedMessage.signature.fullSig); - } - }); - const { data } = await api.post( - `/v1/sol/transaction/prepare`, - { - unsigned_tx_serialized: tx.data.unsigned_tx_serialized, - signatures: signatures, - }); - data.data.fireblocks_tx = fbTx; - return data; - } catch (err: any) { - throw new Error(err); - } + const payload = { + rawMessageData: { + messages: [ + { + 'content': tx.data.unsigned_tx_hash, + }, + ], + }, + }; + + const fbSigner = this.getFbSigner(integration); + const fbNote = note ? note : 'SOL tx from @kilnfi/sdk'; + const fbTx = await fbSigner.signWithFB(payload, this.testnet ? 'SOL_TEST' : 'SOL', fbNote); + const signatures: string[] = []; + fbTx.signedMessages?.forEach((signedMessage: any) => { + if (signedMessage.derivationPath[3] == 0) { + signatures.push(signedMessage.signature.fullSig); + } + }); + + const { data } = await api.post( + `/v1/sol/transaction/prepare`, + { + unsigned_tx_serialized: tx.data.unsigned_tx_serialized, + signatures: signatures, + }); + data.data.fireblocks_tx = fbTx; + return data; + } @@ -198,16 +186,14 @@ export class SolService extends Service { * @param signedTx serialized signed tx */ async broadcast(signedTx: SolSignedTx): Promise { - try { - const { data } = await api.post( - `/v1/sol/transaction/broadcast`, - { - tx_serialized: signedTx.data.signed_tx_serialized, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/sol/transaction/broadcast`, + { + tx_serialized: signedTx.data.signed_tx_serialized, + }); + return data; + } /** @@ -215,13 +201,11 @@ export class SolService extends Service { * @param txHash transaction hash */ async getTxStatus(txHash: string): Promise { - try { - const { data } = await api.get( - `/v1/sol/transaction/status?tx_hash=${txHash}`); - return data; - } catch (e: any) { - throw new Error(e); - } + + const { data } = await api.get( + `/v1/sol/transaction/status?tx_hash=${txHash}`); + return data; + } /** @@ -229,13 +213,11 @@ export class SolService extends Service { * @param txSerialized transaction serialized */ async decodeTx(txSerialized: string): Promise { - try { - const { data } = await api.get( - `/v1/sol/transaction/decode?tx_serialized=${txSerialized}`); - return data; - } catch (error: any) { - throw new Error(error); - } + + const { data } = await api.get( + `/v1/sol/transaction/decode?tx_serialized=${txSerialized}`); + return data; + } /** @@ -246,13 +228,11 @@ export class SolService extends Service { async getStakesByAccounts( accountIds: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/sol/stakes?accounts=${accountIds.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/sol/stakes?accounts=${accountIds.join(',')}`); + return data; + } /** @@ -263,13 +243,11 @@ export class SolService extends Service { async getStakesByStakeAccounts( stakeAccounts: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/sol/stakes?stake_accounts=${stakeAccounts.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/sol/stakes?stake_accounts=${stakeAccounts.join(',')}`); + return data; + } /** @@ -280,13 +258,11 @@ export class SolService extends Service { async getStakesByWallets( wallets: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/sol/stakes?wallets=${wallets.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/sol/stakes?wallets=${wallets.join(',')}`); + return data; + } /** @@ -301,15 +277,13 @@ export class SolService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/sol/rewards?accounts=${accountIds.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/sol/rewards?accounts=${accountIds.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } /** @@ -324,15 +298,13 @@ export class SolService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/sol/rewards?stake_accounts=${stakeAccounts.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/sol/rewards?stake_accounts=${stakeAccounts.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } /** @@ -347,29 +319,25 @@ export class SolService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/sol/rewards?wallets=${wallets.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/sol/rewards?wallets=${wallets.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } /** * Retrieve SOL network stats */ async getNetworkStats(): Promise { - try { - const { data } = await api.get( - `/v1/sol/network-stats`, - ); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/sol/network-stats`, + ); + return data; + } /** diff --git a/src/services/tia.ts b/src/services/tia.ts new file mode 100644 index 0000000..ad826e7 --- /dev/null +++ b/src/services/tia.ts @@ -0,0 +1,192 @@ +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"; + +export class TiaService extends Service { + + constructor({ testnet }: ServiceProps) { + super({ testnet }); + } + + /** + * Convert TIA to uTIA + * @param amountTia + */ + tiaToUtia(amountTia: string): string { + return (parseFloat(amountTia) * 10 ** 6).toFixed(); + } + + /** + * Craft tia 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 amountTia how many tokens to stake in TIA + */ + async craftStakeTx( + accountId: string, + pubkey: string, + validatorAddress: string, + amountTia: number, + ): Promise { + + const { data } = await api.post( + `/v1/tia/transaction/stake`, + { + account_id: accountId, + pubkey: pubkey, + validator: validatorAddress, + amount_utia: this.tiaToUtia(amountTia.toString()), + }); + return data; + + } + + /** + * Craft tia 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/tia/transaction/withdraw-rewards`, + { + pubkey: pubkey, + validator: validatorAddress, + }); + return data; + + } + + /** + * Craft tia 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 amountTia how many tokens to undelegate in TIA + */ + async craftUnstakeTx( + pubkey: string, + validatorAddress: string, + amountTia?: number, + ): Promise { + + const { data } = await api.post( + `/v1/tia/transaction/unstake`, + { + pubkey: pubkey, + validator: validatorAddress, + amount_utia: amountTia ? this.tiaToUtia(amountTia.toString()) : undefined, + }); + return data; + + } + + /** + * Craft tia 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 amountTia how many tokens to redelegate in TIA + */ + async craftRedelegateTx( + accountId: string, + pubkey: string, + validatorSourceAddress: string, + validatorDestinationAddress: string, + amountTia?: number, + ): Promise { + + const { data } = await api.post( + `/v1/tia/transaction/redelegate`, + { + account_id: accountId, + pubkey: pubkey, + validator_source: validatorSourceAddress, + validator_destination: validatorDestinationAddress, + amount_utia: amountTia ? this.tiaToUtia(amountTia.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 : 'TIA tx from @kilnfi/sdk'; + const signer = this.getFbSigner(integration); + const fbTx = await signer.signWithFB(payload, 'CELESTIA', fbNote); + const signature: string = fbTx.signedMessages![0].signature.fullSig; + const { data } = await api.post( + `/v1/tia/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/tia/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/tia/transaction/status?tx_hash=${txHash}`); + return data; + + } + + /** + * Decode transaction + * @param txSerialized transaction serialized + */ + async decodeTx(txSerialized: string): Promise { + + const { data } = await api.get( + `/v1/tia/transaction/decode?tx_serialized=${txSerialized}`); + return data; + + } +} diff --git a/src/services/xtz.ts b/src/services/xtz.ts index 0d37022..5d3393c 100644 --- a/src/services/xtz.ts +++ b/src/services/xtz.ts @@ -29,18 +29,16 @@ export class XtzService extends Service { walletAddress: string, bakerAddress: string, ): Promise { - try { - const { data } = await api.post( - `/v1/xtz/transaction/stake`, - { - account_id: accountId, - wallet: walletAddress, - baker_address: bakerAddress, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/xtz/transaction/stake`, + { + account_id: accountId, + wallet: walletAddress, + baker_address: bakerAddress, + }); + return data; + } /** @@ -50,16 +48,14 @@ export class XtzService extends Service { async craftUnStakeTx( walletAddress: string, ): Promise { - try { - const { data } = await api.post( - `/v1/xtz/transaction/unstake`, - { - wallet: walletAddress, - }); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.post( + `/v1/xtz/transaction/unstake`, + { + wallet: walletAddress, + }); + return data; + } /** @@ -99,16 +95,14 @@ export class XtzService extends Service { * @param signedTx serialized signed tx */ async broadcast(signedTx: XtzSignedTx): Promise { - try { - const { data } = await api.post( - `/v1/xtz/transaction/broadcast`, - { - tx_serialized: signedTx.data.signed_tx_serialized, - }); - return data; - } catch (e: any) { - throw new Error(e); - } + + const { data } = await api.post( + `/v1/xtz/transaction/broadcast`, + { + tx_serialized: signedTx.data.signed_tx_serialized, + }); + return data; + } /** @@ -117,13 +111,11 @@ export class XtzService extends Service { * @param txHash transaction hash */ async getTxStatus(blockNumber: number, txHash: string): Promise { - try { - const { data } = await api.get( - `/v1/xtz/transaction/status?block_number=${blockNumber}&tx_hash=${txHash}`); - return data; - } catch (e: any) { - throw new Error(e); - } + + const { data } = await api.get( + `/v1/xtz/transaction/status?block_number=${blockNumber}&tx_hash=${txHash}`); + return data; + } /** @@ -131,13 +123,11 @@ export class XtzService extends Service { * @param txSerialized transaction serialized */ async decodeTx(txSerialized: string): Promise { - try { - const { data } = await api.get( - `/v1/xtz/transaction/decode?tx_serialized=${txSerialized}`); - return data; - } catch (error: any) { - throw new Error(error); - } + + const { data } = await api.get( + `/v1/xtz/transaction/decode?tx_serialized=${txSerialized}`); + return data; + } /** @@ -148,13 +138,11 @@ export class XtzService extends Service { async getStakesByAccounts( accountIds: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/xtz/stakes?accounts=${accountIds.join(',')}`); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/xtz/stakes?accounts=${accountIds.join(',')}`); + return data; + } /** @@ -165,14 +153,12 @@ export class XtzService extends Service { async getStakesByWallets( walletAddresses: string[], ): Promise { - try { - const { data } = await api.get( - `/v1/xtz/stakes?wallets=${walletAddresses.join(',')}`, - ); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/xtz/stakes?wallets=${walletAddresses.join(',')}`, + ); + return data; + } /** @@ -187,15 +173,13 @@ export class XtzService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/xtz/rewards?accounts=${accountIds.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/xtz/rewards?accounts=${accountIds.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } /** @@ -210,29 +194,25 @@ export class XtzService extends Service { startDate?: string, endDate?: string, ): Promise { - try { - const query = `/v1/xtz/rewards?wallets=${walletAddresses.join(',')}${ - startDate ? `&start_date=${startDate}` : '' - }${endDate ? `&end_date=${endDate}` : ''}`; - const { data } = await api.get(query); - return data; - } catch (err: any) { - throw new Error(err); - } + + const query = `/v1/xtz/rewards?wallets=${walletAddresses.join(',')}${ + startDate ? `&start_date=${startDate}` : '' + }${endDate ? `&end_date=${endDate}` : ''}`; + const { data } = await api.get(query); + return data; + } /** * Retrieve XTZ network stats */ async getNetworkStats(): Promise { - try { - const { data } = await api.get( - `/v1/xtz/network-stats`, - ); - return data; - } catch (err: any) { - throw new Error(err); - } + + const { data } = await api.get( + `/v1/xtz/network-stats`, + ); + return data; + } /** diff --git a/src/types/atom.ts b/src/types/atom.ts index 73c7497..7838927 100644 --- a/src/types/atom.ts +++ b/src/types/atom.ts @@ -1,39 +1,3 @@ -import { IndexedTx, StdFee } from "@cosmjs/stargate"; -import { EncodeObject } from "@cosmjs/proto-signing"; -import { TransactionResponse } from "fireblocks-sdk"; - -export type AtomTx = { - data: { - unsigned_tx_hash: string; - unsigned_tx_serialized: string; - tx_body: string; - tx_auth_info: string; - pubkey: string; - message: EncodeObject; - fee: StdFee; - } -} - -export type AtomSignedTx = { - data: { - fireblocks_tx: TransactionResponse; - signed_tx_serialized: string; - }; -}; - -export type AtomTxHash = { - data: { - tx_hash: string; - }; -}; - -export type AtomTxStatus = { - data: { - status: 'success' | 'error', - receipt: IndexedTx | null, - } -} - export type AtomStake = { validator_address: string; delegator_address: string; diff --git a/src/types/dydx.ts b/src/types/cosmos.ts similarity index 83% rename from src/types/dydx.ts rename to src/types/cosmos.ts index 356958f..6611810 100644 --- a/src/types/dydx.ts +++ b/src/types/cosmos.ts @@ -2,7 +2,7 @@ import { IndexedTx, StdFee } from "@cosmjs/stargate"; import { EncodeObject } from "@cosmjs/proto-signing"; import { TransactionResponse } from "fireblocks-sdk"; -export type DydxTx = { +export type CosmosTx = { data: { unsigned_tx_hash: string; unsigned_tx_serialized: string; @@ -14,20 +14,20 @@ export type DydxTx = { } } -export type DydxSignedTx = { +export type CosmosSignedTx = { data: { fireblocks_tx: TransactionResponse; signed_tx_serialized: string; }; }; -export type DydxTxHash = { +export type CosmosTxHash = { data: { tx_hash: string; }; }; -export type DydxTxStatus = { +export type CosmosTxStatus = { data: { status: 'success' | 'error', receipt: IndexedTx | null, diff --git a/src/types/osmo.ts b/src/types/osmo.ts index 3d96e8d..3b78bde 100644 --- a/src/types/osmo.ts +++ b/src/types/osmo.ts @@ -1,39 +1,3 @@ -import { IndexedTx, StdFee } from "@cosmjs/stargate"; -import { EncodeObject } from "@cosmjs/proto-signing"; -import { TransactionResponse } from "fireblocks-sdk"; - -export type OsmoTx = { - data: { - unsigned_tx_hash: string; - unsigned_tx_serialized: string; - tx_body: string; - tx_auth_info: string; - pubkey: string; - message: EncodeObject; - fee: StdFee; - } -} - -export type OsmoSignedTx = { - data: { - fireblocks_tx: TransactionResponse; - signed_tx_serialized: string; - }; -}; - -export type OsmoTxHash = { - data: { - tx_hash: string; - }; -}; - -export type OsmoTxStatus = { - data: { - status: 'success' | 'error', - receipt: IndexedTx | null, - } -} - export type OsmoStake = { validator_address: string; delegator_address: string;