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

ton support #101

Merged
merged 2 commits into from
May 23, 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Check out the [full documentation](https://docs.kiln.fi/v1/connect/overview).
- OSMO
- SOL
- TIA
- TON
- XTZ
- More protocol to come, don't hesitate to contact us ([email protected])

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.14.2",
"version": "2.15.0",
"autor": "Kiln <[email protected]> (https://kiln.fi)",
"license": "BUSL-1.1",
"description": "JavaScript sdk for Kiln API",
Expand Down
4 changes: 3 additions & 1 deletion src/integrations/fb_signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ type AssetId =
| "DV4TNT_TEST"
| "DYDX_DYDX"
| "CELESTIA"
| "INJ_INJ";
| "INJ_INJ"
| "TON_TEST"
| "TON";

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 @@ -16,6 +16,7 @@ import { TiaService } from "./services/tia";
import { XtzService } from "./services/xtz";
import { KILN_VALIDATORS as v } from "./validators";
import { InjService } from "./services/inj";
import { TonService } from "./services/ton";

type Config = {
apiToken: string;
Expand All @@ -42,6 +43,7 @@ export class Kiln {
noble: NobleService;
fet: FetService;
inj: InjService;
ton: TonService;

constructor({ testnet, apiToken, baseUrl }: Config) {
api.defaults.headers.common.Authorization = `Bearer ${apiToken}`;
Expand All @@ -64,5 +66,6 @@ export class Kiln {
this.noble = new NobleService({ testnet });
this.fet = new FetService({ testnet });
this.inj = new InjService({ testnet });
this.ton = new TonService({ testnet });
}
}
148 changes: 148 additions & 0 deletions src/services/ton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import api from "../api";
import { Service } from "./service";
import { ServiceProps } from "../types/service";
import { Integration } from "../types/integrations";
import { parseUnits } from "viem";
import { TonSignedTx, TonTx, TonTxHash, TonTxStatus } from "../types/ton";

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

/**
* Craft TON staking transaction to a single nomination pool
* @param accountId id of the kiln account to use for the stake transaction
* @param walletAddress used to create the stake account and retrieve rewards in the future
* @param poolAddress vote account address of the validator that you wish to delegate to
* @param amountTon how much to stake in TON
*/
async craftStakeSingleNominationPoolTx(
accountId: string,
walletAddress: string,
poolAddress: string,
amountTon: number,
): Promise<TonTx> {
const { data } = await api.post<TonTx>(`/v1/ton/transaction/stake-single-nomination-pool`, {
account_id: accountId,
wallet: walletAddress,
amount_nanoton: this.tonToNanoTon(amountTon.toString()),
pool_address: poolAddress,
});
return data;
}

/**
* Craft TON staking transaction to a nomination pool
* @param accountId id of the kiln account to use for the stake transaction
* @param walletAddress used to create the stake account and retrieve rewards in the future
* @param poolAddress vote account address of the validator that you wish to delegate to
* @param amountTon how much to stake in TON
*/
async craftStakeNominationPoolTx(
accountId: string,
walletAddress: string,
poolAddress: string,
amountTon: number,
): Promise<TonTx> {
const { data } = await api.post<TonTx>(`/v1/ton/transaction/stake-nomination-pool`, {
account_id: accountId,
wallet: walletAddress,
amount_nanoton: this.tonToNanoTon(amountTon.toString()),
pool_address: poolAddress,
});
return data;
}

/**
* Craft TON unstake transaction from a single nomination pool
* @param walletAddress used to create the stake account and retrieve rewards in the future
* @param poolAddress vote account address of the validator that you wish to delegate to
* @param amountTon how much to stake in TON
*/
async craftUnstakeSingleNominationPoolTx(
walletAddress: string,
poolAddress: string,
amountTon?: number,
): Promise<TonTx> {
const { data } = await api.post<TonTx>(`/v1/ton/transaction/unstake-single-nomination-pool`, {
wallet: walletAddress,
amount_nanoton: amountTon ? this.tonToNanoTon(amountTon.toString()) : undefined,
pool_address: poolAddress,
});
return data;
}

/**
* Craft TON unstake transaction from a nomination pool
* @param walletAddress used to create the stake account and retrieve rewards in the future
* @param poolAddress vote account address of the validator that you wish to delegate to
*/
async craftUnstakeNominationPoolTx(walletAddress: string, poolAddress: string): Promise<TonTx> {
const { data } = await api.post<TonTx>(`/v1/ton/transaction/unstake-nomination-pool`, {
wallet: walletAddress,
pool_address: poolAddress,
});
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: TonTx, note?: string): Promise<TonSignedTx> {
const payload = {
rawMessageData: {
messages: [
{
content: tx.data.unsigned_tx_hash,
},
],
},
};

const fbSigner = this.getFbSigner(integration);
const fbNote = note ? note : "TON tx from @kilnfi/sdk";
const fbTx = await fbSigner.sign(payload, this.testnet ? "TON_TEST" : "TON", fbNote);
const signature =
fbTx.signedMessages && fbTx.signedMessages.length > 0 ? fbTx.signedMessages[0].signature.fullSig : undefined;

const { data } = await api.post<TonSignedTx>(`/v1/ton/transaction/prepare`, {
unsigned_tx_serialized: tx.data.unsigned_tx_serialized,
signature: signature,
from: tx.data.from,
});
data.data.fireblocks_tx = fbTx;
return data;
}

/**
* Broadcast transaction to the network
* @param signedTx serialized signed tx
*/
async broadcast(signedTx: TonSignedTx): Promise<TonTxHash> {
const { data } = await api.post<TonTxHash>(`/v1/ton/transaction/broadcast`, {
tx_serialized: signedTx.data.signed_tx_serialized,
});
return data;
}

/**
* Get transaction status by message hash
* @param msgHash transaction hash
*/
async getTxStatus(msgHash: string): Promise<TonTxStatus> {
const { data } = await api.get<TonTxStatus>(`/v1/ton/transaction/status?msg_hash=${msgHash}`);
return data;
}

/**
* Convert TON to nanoTON
* @param ton
*/
tonToNanoTon(ton: string): string {
return parseUnits(ton, 9).toString();
}
}
28 changes: 28 additions & 0 deletions src/types/ton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { TransactionResponse } from "fireblocks-sdk";

export type TonTx = {
data: {
unsigned_tx_hash: string;
unsigned_tx_serialized: string;
from: string;
};
};

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

export type TonTxHash = {
data: {
tx_hash: string;
};
};
export type TonTxStatus = {
data: {
status: "success" | "error";
receipt: TransactionResponse | null;
};
};
Loading