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

[SOF-3705] add support for INJ #97

Merged
merged 1 commit into from
Apr 17, 2024
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ Check out the [full documentation](https://docs.kiln.fi/v1/connect/overview).
- DOT
- DYDX
- ETH
- FET
- INJ
- MATIC
- NEAR
- NOBLE
- OSMO
- SOL
- TIA
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.13.0",
"version": "2.14.0",
"autor": "Kiln <[email protected]> (https://kiln.fi)",
"license": "BUSL-1.1",
"description": "JavaScript sdk for Kiln API",
Expand Down
3 changes: 2 additions & 1 deletion src/integrations/fb_signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type AssetId =
| "DOT"
| "DV4TNT_TEST"
| "DYDX_DYDX"
| "CELESTIA";
| "CELESTIA"
| "INJ_INJ";

export class FbSigner {
protected fireblocks: FireblocksSDK;
Expand Down
3 changes: 3 additions & 0 deletions src/kiln.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { SolService } from "./services/sol";
import { TiaService } from "./services/tia";
import { XtzService } from "./services/xtz";
import { KILN_VALIDATORS as v } from "./validators";
import { InjService } from "./services/inj";

type Config = {
apiToken: string;
Expand All @@ -40,6 +41,7 @@ export class Kiln {
tia: TiaService;
noble: NobleService;
fet: FetService;
inj: InjService;

constructor({ testnet, apiToken, baseUrl }: Config) {
api.defaults.headers.common.Authorization = `Bearer ${apiToken}`;
Expand All @@ -61,5 +63,6 @@ export class Kiln {
this.tia = new TiaService({ testnet });
this.noble = new NobleService({ testnet });
this.fet = new FetService({ testnet });
this.inj = new InjService({ testnet });
}
}
172 changes: 172 additions & 0 deletions src/services/inj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
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 InjService extends Service {
constructor({ testnet }: ServiceProps) {
super({ testnet });
}

/**
* Convert INJ to inj
* @param amount
*/
injToAinj(amount: string): string {
return (parseFloat(amount) * 10 ** 18).toFixed();
}

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

/**
* Craft inj 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/inj/transaction/withdraw-rewards`, {
pubkey: pubkey,
validator: validatorAddress,
});
return data;
}

/**
* Craft inj restake rewards transaction
* @param pubkey wallet pubkey, this is different from the wallet address
* @param validatorAccount validator account address (wallet controlling the validator)
* @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/inj/transaction/restake-rewards`, {
pubkey: pubkey,
validator_address: validatorAddress,
});
return data;
}

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

/**
* Craft inj 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 amountInj how many tokens to redelegate in INJ
*/
async craftRedelegateTx(
accountId: string,
pubkey: string,
validatorSourceAddress: string,
validatorDestinationAddress: string,
amountInj?: number,
): Promise<CosmosTx> {
const { data } = await api.post<CosmosTx>(`/v1/inj/transaction/redelegate`, {
account_id: accountId,
pubkey: pubkey,
validator_source: validatorSourceAddress,
validator_destination: validatorDestinationAddress,
amount_inj: amountInj ? this.injToAinj(amountInj.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 payload = {
rawMessageData: {
messages: [
{
content: tx.data.unsigned_tx_hash,
},
],
},
};
const fbNote = note ? note : "INJ tx from @kilnfi/sdk";
const signer = this.getFbSigner(integration);
const fbTx = await signer.signWithFB(payload, "INJ_INJ", fbNote);
const signature: string = fbTx.signedMessages![0].signature.fullSig;
const { data } = await api.post<CosmosSignedTx>(`/v1/inj/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/inj/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/inj/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/inj/transaction/decode?tx_serialized=${txSerialized}`);
return data;
}
}
Loading