Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dydx and fireblocks service #84

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Check out the [full documentation](https://docs.kiln.fi/v1/connect/overview).
- SOL
- XTZ
- OSMO
- DYDX
- More protocol to come, don't hesitate to contact us ([email protected])

## Installation
Expand Down
5 changes: 4 additions & 1 deletion src/integrations/fb_signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ type AssetId =
| 'XTZ_TEST'
| 'XTZ'
| 'WND'
| 'DOT';
| 'DOT'
| 'DV4TNT_TEST'
| 'DYDX_DYDX'
;

export class FbSigner {
protected fireblocks: FireblocksSDK;
Expand Down
8 changes: 7 additions & 1 deletion src/kiln.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { DotService } from './services/dot';
import { XtzService } from './services/xtz';
import { MaticService } from './services/matic';
import { OsmoService } from './services/osmo';
import { FireblocksService } from "./services/fireblocks";
import { DydxService } from "./services/dydx";

type Config = {
apiToken: string;
Expand All @@ -20,16 +22,18 @@ type Config = {
export const KILN_VALIDATORS = v;

export class Kiln {
fireblocks: FireblocksService;
accounts: AccountService;
eth: EthService;
sol: SolService;
accounts: AccountService;
atom: AtomService;
ada: AdaService;
near: NearService;
dot: DotService;
xtz: XtzService;
matic: MaticService;
osmo: OsmoService;
dydx: DydxService;

constructor({ testnet, apiToken, baseUrl }: Config) {
api.defaults.headers.common.Authorization = `Bearer ${apiToken}`;
Expand All @@ -39,6 +43,7 @@ export class Kiln {
? 'https://api.testnet.kiln.fi'
: 'https://api.kiln.fi';

this.fireblocks = new FireblocksService({ testnet });
this.accounts = new AccountService({ testnet });
this.eth = new EthService({ testnet });
this.sol = new SolService({ testnet });
Expand All @@ -49,5 +54,6 @@ export class Kiln {
this.xtz = new XtzService({ testnet });
this.matic = new MaticService({ testnet });
this.osmo = new OsmoService({ testnet });
this.dydx = new DydxService({ testnet });
}
}
206 changes: 206 additions & 0 deletions src/services/dydx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
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 { DydxSignedTx, DydxTxHash, DydxTxStatus, DydxTx } from "../types/dydx";

export class DydxService extends Service {

constructor({ testnet }: ServiceProps) {
super({ testnet });
}

/**
* Convert DYDX to ADYDX
* @param amountDydx
*/
dydxToAdydx(amountDydx: string): string {
return (parseFloat(amountDydx) * 10 ** 18).toFixed();
}

/**
* Craft dydx 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 amountDydx how many tokens to stake in DYDX
*/
async craftStakeTx(
accountId: string,
pubkey: string,
validatorAddress: string,
amountDydx: number,
): Promise<DydxTx> {
try {
const { data } = await api.post<DydxTx>(
`/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);
}
}

/**
* Craft dydx 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<DydxTx> {
try {
const { data } = await api.post<DydxTx>(
`/v1/dydx/transaction/withdraw-rewards`,
{
pubkey: pubkey,
validator: validatorAddress,
});
return data;
} catch (err: any) {
throw new Error(err);
}
}

/**
* Craft dydx 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 amountDydx how many tokens to undelegate in DYDX
*/
async craftUnstakeTx(
pubkey: string,
validatorAddress: string,
amountDydx?: number,
): Promise<DydxTx> {
try {
const { data } = await api.post<DydxTx>(
`/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);
}
}

/**
* Craft dydx 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 amountDydx how many tokens to redelegate in DYDX
*/
async craftRedelegateTx(
accountId: string,
pubkey: string,
validatorSourceAddress: string,
validatorDestinationAddress: string,
amountDydx?: number,
): Promise<DydxTx> {
try {
const { data } = await api.post<DydxTx>(
`/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);
}
}

/**
* 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: DydxTx, note?: string): Promise<DydxSignedTx> {
const payload = {
rawMessageData: {
messages: [
{
'content': tx.data.unsigned_tx_hash,
},
],
},
};
const fbNote = note ? note : 'DYDX tx from @kilnfi/sdk';
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<DydxSignedTx>(
`/v1/dydx/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: DydxSignedTx): Promise<DydxTxHash> {
try {
const { data } = await api.post<DydxTxHash>(
`/v1/dydx/transaction/broadcast`,
{
tx_serialized: signedTx.data.signed_tx_serialized,
});
return data;
} catch (e: any) {
throw new Error(e);
}
}

/**
* Get transaction status
* @param txHash
*/
async getTxStatus(txHash: string): Promise<DydxTxStatus> {
try {
const { data } = await api.get<DydxTxStatus>(
`/v1/dydx/transaction/status?tx_hash=${txHash}`);
return data;
} catch (e: any) {
throw new Error(e);
}
}

/**
* Decode transaction
* @param txSerialized transaction serialized
*/
async decodeTx(txSerialized: string): Promise<DecodedTxRaw> {
try {
const { data } = await api.get<DecodedTxRaw>(
`/v1/dydx/transaction/decode?tx_serialized=${txSerialized}`);
return data;
} catch (error: any) {
throw new Error(error);
}
}
}
23 changes: 23 additions & 0 deletions src/services/fireblocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Integration } from "../types/integrations";
import { Service } from "./service";
import { PublicKeyResponse } from "fireblocks-sdk";

export class FireblocksService extends Service {

/**
* Get fireblocks wallet pubkey compressed
* @param integration
* @param assetId
*/
async getPubkey(integration: Integration, assetId: string): Promise<PublicKeyResponse> {
const fbSdk = this.getFbSdk(integration);
const data = await fbSdk.getPublicKeyInfoForVaultAccount({
assetId: assetId,
vaultAccountId: integration.vaultId,
change: 0,
addressIndex: 0,
compressed: true,
});
return data;
}
}
35 changes: 35 additions & 0 deletions src/types/dydx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { IndexedTx, StdFee } from "@cosmjs/stargate";
import { EncodeObject } from "@cosmjs/proto-signing";
import { TransactionResponse } from "fireblocks-sdk";

export type DydxTx = {
data: {
unsigned_tx_hash: string;
unsigned_tx_serialized: string;
tx_body: string;
tx_auth_info: string;
pubkey: string;
message: EncodeObject;
fee: StdFee;
}
}

export type DydxSignedTx = {
data: {
fireblocks_tx: TransactionResponse;
signed_tx_serialized: string;
};
};

export type DydxTxHash = {
data: {
tx_hash: string;
};
};

export type DydxTxStatus = {
data: {
status: 'success' | 'error',
receipt: IndexedTx | null,
}
}