Skip to content

Commit

Permalink
Merge pull request #102 from kilnfi/zeta-support
Browse files Browse the repository at this point in the history
sdk-js: ZETA support
  • Loading branch information
goulinkh authored Jun 12, 2024
2 parents 62dd194 + c399fdb commit 8fd62df
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Check out the [full documentation](https://docs.kiln.fi/v1/connect/overview).
- TIA
- TON
- XTZ
- ZETA
- More protocol to come, don't hesitate to contact us ([email protected])

### ⚠️️WARNING:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kilnfi/sdk",
"version": "2.15.0",
"version": "2.16.0",
"autor": "Kiln <[email protected]> (https://kiln.fi)",
"license": "BUSL-1.1",
"description": "JavaScript sdk for Kiln API",
Expand Down
7 changes: 5 additions & 2 deletions src/kiln.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import { DydxService } from "./services/dydx";
import { EthService } from "./services/eth";
import { FetService } from "./services/fet";
import { FireblocksService } from "./services/fireblocks";
import { InjService } from "./services/inj";
import { MaticService } from "./services/matic";
import { NearService } from "./services/near";
import { NobleService } from "./services/noble";
import { OsmoService } from "./services/osmo";
import { SolService } from "./services/sol";
import { TiaService } from "./services/tia";
import { TonService } from "./services/ton";
import { XtzService } from "./services/xtz";
import { ZetaService } from "./services/zeta";
import { KILN_VALIDATORS as v } from "./validators";
import { InjService } from "./services/inj";
import { TonService } from "./services/ton";

type Config = {
apiToken: string;
Expand Down Expand Up @@ -44,6 +45,7 @@ export class Kiln {
fet: FetService;
inj: InjService;
ton: TonService;
zeta: ZetaService;

constructor({ testnet, apiToken, baseUrl }: Config) {
api.defaults.headers.common.Authorization = `Bearer ${apiToken}`;
Expand All @@ -67,5 +69,6 @@ export class Kiln {
this.fet = new FetService({ testnet });
this.inj = new InjService({ testnet });
this.ton = new TonService({ testnet });
this.zeta = new ZetaService({ testnet });
}
}
175 changes: 175 additions & 0 deletions src/services/zeta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import { Service } from "./service";

import { ServiceProps } from "../types/service";
import { Integration } from "../types/integrations";
import api from "../api";
import { DecodedTxRaw } from "@cosmjs/proto-signing";
import { CosmosSignedTx, CosmosTx, CosmosTxHash, CosmosTxStatus } from "../types/cosmos";
import { SigningAlgorithm } from "fireblocks-sdk";
import { parseUnits } from "viem";

export class ZetaService extends Service {
constructor({ testnet }: ServiceProps) {
super({ testnet });
}

/**
* Convert ZETA to aZETA
* @param amountZeta
*/
zetaToAZeta(amountZeta: string): string {
return parseUnits(amountZeta, 18).toString();
}

/**
* Craft a Zetachain 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 amountZeta how many tokens to stake in ZETA
* @param restakeRewards If enabled, the rewards will be automatically restaked
*/
async craftStakeTx(
accountId: string,
pubkey: string,
validatorAddress: string,
amountZeta: number,
restakeRewards: boolean = false,
): Promise<CosmosTx> {
const { data } = await api.post<CosmosTx>(`/v1/zeta/transaction/stake`, {
account_id: accountId,
pubkey: pubkey,
validator: validatorAddress,
amount_azeta: this.zetaToAZeta(amountZeta.toString()),
restake_rewards: restakeRewards,
});
return data;
}

/**
* Craft a Zetachain 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<CosmosTx> {
const { data } = await api.post<CosmosTx>(`/v1/zeta/transaction/withdraw-rewards`, {
pubkey: pubkey,
validator: validatorAddress,
});
return data;
}

/**
* Craft a Zetachain restake rewards transaction
* @param pubkey wallet pubkey, this is different from the wallet address
* @param validatorAddress validator address to which the delegation has been made
*/
async craftRestakeRewardsTx(pubkey: string, validatorAddress: string): Promise<CosmosTx> {
const { data } = await api.post<CosmosTx>(`/v1/zeta/transaction/restake-rewards`, {
pubkey: pubkey,
validator_address: validatorAddress,
});
return data;
}

/**
* Craft a Zetachain 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 amountZeta how many tokens to undelegate in ZETA
*/
async craftUnstakeTx(pubkey: string, validatorAddress: string, amountZeta?: number): Promise<CosmosTx> {
const { data } = await api.post<CosmosTx>(`/v1/zeta/transaction/unstake`, {
pubkey: pubkey,
validator: validatorAddress,
amount_azeta: amountZeta ? this.zetaToAZeta(amountZeta.toString()) : undefined,
});
return data;
}

/**
* Craft a Zetachain 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 amountZeta how many tokens to redelegate in ZETA
*/
async craftRedelegateTx(
accountId: string,
pubkey: string,
validatorSourceAddress: string,
validatorDestinationAddress: string,
amountZeta?: number,
): Promise<CosmosTx> {
const { data } = await api.post<CosmosTx>(`/v1/zeta/transaction/redelegate`, {
account_id: accountId,
pubkey: pubkey,
validator_source: validatorSourceAddress,
validator_destination: validatorDestinationAddress,
amount_azeta: amountZeta ? this.zetaToAZeta(amountZeta.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<CosmosSignedTx> {
const fbNote = note ? note : "ZETA tx from @kilnfi/sdk";
const signer = this.getFbSigner(integration);
const payload = {
rawMessageData: {
messages: [
{
content: tx.data.unsigned_tx_hash,
derivationPath: [44, 118, integration.vaultId, 0, 0],
},
],
algorithm: SigningAlgorithm.MPC_ECDSA_SECP256K1,
},
};
const fbTx = await signer.sign(payload, undefined, fbNote);
const signature: string = fbTx.signedMessages![0].signature.fullSig;
const { data } = await api.post<CosmosSignedTx>(`/v1/zeta/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<CosmosTxHash> {
const { data } = await api.post<CosmosTxHash>(`/v1/zeta/transaction/broadcast`, {
tx_serialized: signedTx.data.signed_tx_serialized,
});
return data;
}

/**
* Get transaction status
* @param txHash
*/
async getTxStatus(txHash: string): Promise<CosmosTxStatus> {
const { data } = await api.get<CosmosTxStatus>(`/v1/zeta/transaction/status?tx_hash=${txHash}`);
return data;
}

/**
* Decode transaction
* @param txSerialized transaction serialized
*/
async decodeTx(txSerialized: string): Promise<DecodedTxRaw> {
const { data } = await api.get<DecodedTxRaw>(`/v1/zeta/transaction/decode?tx_serialized=${txSerialized}`);
return data;
}
}

0 comments on commit 8fd62df

Please sign in to comment.