Skip to content

Commit 9e7f230

Browse files
authored
add support for INJ (#97)
1 parent 4df0adb commit 9e7f230

File tree

5 files changed

+181
-2
lines changed

5 files changed

+181
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ Check out the [full documentation](https://docs.kiln.fi/v1/connect/overview).
1313
- DOT
1414
- DYDX
1515
- ETH
16+
- FET
17+
- INJ
1618
- MATIC
1719
- NEAR
20+
- NOBLE
1821
- OSMO
1922
- SOL
2023
- TIA

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kilnfi/sdk",
3-
"version": "2.13.0",
3+
"version": "2.14.0",
44
"autor": "Kiln <[email protected]> (https://kiln.fi)",
55
"license": "BUSL-1.1",
66
"description": "JavaScript sdk for Kiln API",

src/integrations/fb_signer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ type AssetId =
3333
| "DOT"
3434
| "DV4TNT_TEST"
3535
| "DYDX_DYDX"
36-
| "CELESTIA";
36+
| "CELESTIA"
37+
| "INJ_INJ";
3738

3839
export class FbSigner {
3940
protected fireblocks: FireblocksSDK;

src/kiln.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { SolService } from "./services/sol";
1515
import { TiaService } from "./services/tia";
1616
import { XtzService } from "./services/xtz";
1717
import { KILN_VALIDATORS as v } from "./validators";
18+
import { InjService } from "./services/inj";
1819

1920
type Config = {
2021
apiToken: string;
@@ -40,6 +41,7 @@ export class Kiln {
4041
tia: TiaService;
4142
noble: NobleService;
4243
fet: FetService;
44+
inj: InjService;
4345

4446
constructor({ testnet, apiToken, baseUrl }: Config) {
4547
api.defaults.headers.common.Authorization = `Bearer ${apiToken}`;
@@ -61,5 +63,6 @@ export class Kiln {
6163
this.tia = new TiaService({ testnet });
6264
this.noble = new NobleService({ testnet });
6365
this.fet = new FetService({ testnet });
66+
this.inj = new InjService({ testnet });
6467
}
6568
}

src/services/inj.ts

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import { Service } from "./service";
2+
3+
import { ServiceProps } from "../types/service";
4+
import { Integration } from "../types/integrations";
5+
import api from "../api";
6+
import { DecodedTxRaw } from "@cosmjs/proto-signing";
7+
import { CosmosSignedTx, CosmosTx, CosmosTxHash, CosmosTxStatus } from "../types/cosmos";
8+
9+
export class InjService extends Service {
10+
constructor({ testnet }: ServiceProps) {
11+
super({ testnet });
12+
}
13+
14+
/**
15+
* Convert INJ to inj
16+
* @param amount
17+
*/
18+
injToAinj(amount: string): string {
19+
return (parseFloat(amount) * 10 ** 18).toFixed();
20+
}
21+
22+
/**
23+
* Craft inj staking transaction
24+
* @param accountId id of the kiln account to use for the stake transaction
25+
* @param pubkey wallet pubkey, this is different from the wallet address
26+
* @param validatorAddress validator address to delegate to
27+
* @param amountInj how many tokens to stake in INJ
28+
* @param restakeRewards If enabled, the rewards will be automatically restaked
29+
*/
30+
async craftStakeTx(
31+
accountId: string,
32+
pubkey: string,
33+
validatorAddress: string,
34+
amountInj: number,
35+
restakeRewards: boolean = false,
36+
): Promise<CosmosTx> {
37+
const { data } = await api.post<CosmosTx>(`/v1/inj/transaction/stake`, {
38+
account_id: accountId,
39+
pubkey: pubkey,
40+
validator: validatorAddress,
41+
amount_inj: this.injToAinj(amountInj.toString()),
42+
restake_rewards: restakeRewards,
43+
});
44+
return data;
45+
}
46+
47+
/**
48+
* Craft inj withdraw rewards transaction
49+
* @param pubkey wallet pubkey, this is different from the wallet address
50+
* @param validatorAddress validator address to which the delegation has been made
51+
*/
52+
async craftWithdrawRewardsTx(pubkey: string, validatorAddress: string): Promise<CosmosTx> {
53+
const { data } = await api.post<CosmosTx>(`/v1/inj/transaction/withdraw-rewards`, {
54+
pubkey: pubkey,
55+
validator: validatorAddress,
56+
});
57+
return data;
58+
}
59+
60+
/**
61+
* Craft inj restake rewards transaction
62+
* @param pubkey wallet pubkey, this is different from the wallet address
63+
* @param validatorAccount validator account address (wallet controlling the validator)
64+
* @param validatorAddress validator address to which the delegation has been made
65+
*/
66+
async craftRestakeRewardsTx(pubkey: string, validatorAddress: string): Promise<CosmosTx> {
67+
const { data } = await api.post<CosmosTx>(`/v1/inj/transaction/restake-rewards`, {
68+
pubkey: pubkey,
69+
validator_address: validatorAddress,
70+
});
71+
return data;
72+
}
73+
74+
/**
75+
* Craft inj unstaking transaction
76+
* @param pubkey wallet pubkey, this is different from the wallet address
77+
* @param validatorAddress validator address to which the delegation has been made
78+
* @param amountInj how many tokens to undelegate in INJ
79+
*/
80+
async craftUnstakeTx(pubkey: string, validatorAddress: string, amountInj?: number): Promise<CosmosTx> {
81+
const { data } = await api.post<CosmosTx>(`/v1/inj/transaction/unstake`, {
82+
pubkey: pubkey,
83+
validator: validatorAddress,
84+
amount_inj: amountInj ? this.injToAinj(amountInj.toString()) : undefined,
85+
});
86+
return data;
87+
}
88+
89+
/**
90+
* Craft inj redelegate transaction
91+
* @param accountId id of the kiln account to use for the new stake
92+
* @param pubkey wallet pubkey, this is different from the wallet address
93+
* @param validatorSourceAddress validator address of the current delegation
94+
* @param validatorDestinationAddress validator address to which the delegation will be moved
95+
* @param amountInj how many tokens to redelegate in INJ
96+
*/
97+
async craftRedelegateTx(
98+
accountId: string,
99+
pubkey: string,
100+
validatorSourceAddress: string,
101+
validatorDestinationAddress: string,
102+
amountInj?: number,
103+
): Promise<CosmosTx> {
104+
const { data } = await api.post<CosmosTx>(`/v1/inj/transaction/redelegate`, {
105+
account_id: accountId,
106+
pubkey: pubkey,
107+
validator_source: validatorSourceAddress,
108+
validator_destination: validatorDestinationAddress,
109+
amount_inj: amountInj ? this.injToAinj(amountInj.toString()) : undefined,
110+
});
111+
return data;
112+
}
113+
114+
/**
115+
* Sign transaction with given integration
116+
* @param integration custody solution to sign with
117+
* @param tx raw transaction
118+
* @param note note to identify the transaction in your custody solution
119+
*/
120+
async sign(integration: Integration, tx: CosmosTx, note?: string): Promise<CosmosSignedTx> {
121+
const payload = {
122+
rawMessageData: {
123+
messages: [
124+
{
125+
content: tx.data.unsigned_tx_hash,
126+
},
127+
],
128+
},
129+
};
130+
const fbNote = note ? note : "INJ tx from @kilnfi/sdk";
131+
const signer = this.getFbSigner(integration);
132+
const fbTx = await signer.signWithFB(payload, "INJ_INJ", fbNote);
133+
const signature: string = fbTx.signedMessages![0].signature.fullSig;
134+
const { data } = await api.post<CosmosSignedTx>(`/v1/inj/transaction/prepare`, {
135+
pubkey: tx.data.pubkey,
136+
tx_body: tx.data.tx_body,
137+
tx_auth_info: tx.data.tx_auth_info,
138+
signature: signature,
139+
});
140+
data.data.fireblocks_tx = fbTx;
141+
return data;
142+
}
143+
144+
/**
145+
* Broadcast transaction to the network
146+
* @param signedTx
147+
*/
148+
async broadcast(signedTx: CosmosSignedTx): Promise<CosmosTxHash> {
149+
const { data } = await api.post<CosmosTxHash>(`/v1/inj/transaction/broadcast`, {
150+
tx_serialized: signedTx.data.signed_tx_serialized,
151+
});
152+
return data;
153+
}
154+
155+
/**
156+
* Get transaction status
157+
* @param txHash
158+
*/
159+
async getTxStatus(txHash: string): Promise<CosmosTxStatus> {
160+
const { data } = await api.get<CosmosTxStatus>(`/v1/inj/transaction/status?tx_hash=${txHash}`);
161+
return data;
162+
}
163+
164+
/**
165+
* Decode transaction
166+
* @param txSerialized transaction serialized
167+
*/
168+
async decodeTx(txSerialized: string): Promise<DecodedTxRaw> {
169+
const { data } = await api.get<DecodedTxRaw>(`/v1/inj/transaction/decode?tx_serialized=${txSerialized}`);
170+
return data;
171+
}
172+
}

0 commit comments

Comments
 (0)