Skip to content

Commit b575f5b

Browse files
authored
fix amount conversions (#98)
1 parent 9e7f230 commit b575f5b

File tree

12 files changed

+48
-93
lines changed

12 files changed

+48
-93
lines changed

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.14.0",
3+
"version": "2.14.1",
44
"autor": "Kiln <[email protected]> (https://kiln.fi)",
55
"license": "BUSL-1.1",
66
"description": "JavaScript sdk for Kiln API",

src/services/ada.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class AdaService extends Service {
6363
* @param amountAda
6464
*/
6565
adaToLovelace(amountAda: string): string {
66-
return (parseFloat(amountAda) * 10 ** 6).toFixed();
66+
return (BigInt(amountAda) * BigInt(10 ** 6)).toString();
6767
}
6868

6969
/**

src/services/atom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class AtomService extends Service {
1717
* @param amountAtom
1818
*/
1919
atomToUatom(amountAtom: string): string {
20-
return (parseFloat(amountAtom) * 10 ** 6).toFixed();
20+
return (BigInt(amountAtom) * BigInt(10 ** 6)).toString();
2121
}
2222

2323
/**

src/services/dot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class DotService extends Service {
2020
* @param amountWnd
2121
*/
2222
wndToPlanck(amountWnd: string): string {
23-
return (parseFloat(amountWnd) * 10 ** 12).toFixed();
23+
return (BigInt(amountWnd) * BigInt(10 ** 12)).toString();
2424
}
2525

2626
/**
@@ -29,7 +29,7 @@ export class DotService extends Service {
2929
* @param amountDot
3030
*/
3131
dotToPlanck(amountDot: string): string {
32-
return (parseFloat(amountDot) * 10 ** 10).toFixed();
32+
return (BigInt(amountDot) * BigInt(10 ** 10)).toString();
3333
}
3434

3535
/**

src/services/dydx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ export class DydxService extends Service {
1616
* @param amountDydx
1717
*/
1818
dydxToAdydx(amountDydx: string): string {
19-
return (parseFloat(amountDydx) * 10 ** 18).toFixed();
19+
return (BigInt(amountDydx) * BigInt(10 ** 18)).toString();
2020
}
2121

2222
usdcToUusdc(amountUsdc: string): string {
23-
return (parseFloat(amountUsdc) * 10 ** 6).toFixed();
23+
return (BigInt(amountUsdc) * BigInt(10 ** 6)).toString();
2424
}
2525

2626
/**

src/services/fet.ts

Lines changed: 35 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import { ServiceProps } from "../types/service";
44
import { Integration } from "../types/integrations";
55
import api from "../api";
66
import { DecodedTxRaw } from "@cosmjs/proto-signing";
7-
import {
8-
CosmosSignedTx,
9-
CosmosTx,
10-
CosmosTxHash,
11-
CosmosTxStatus,
12-
} from "../types/cosmos";
7+
import { CosmosSignedTx, CosmosTx, CosmosTxHash, CosmosTxStatus } from "../types/cosmos";
138
import { SigningAlgorithm } from "fireblocks-sdk";
149

1510
export class FetService extends Service {
@@ -22,7 +17,7 @@ export class FetService extends Service {
2217
* @param amountFet
2318
*/
2419
fetToAfet(amountFet: string): string {
25-
return (parseFloat(amountFet) * 10 ** 18).toFixed();
20+
return (BigInt(amountFet) * BigInt(10 ** 18)).toString();
2621
}
2722

2823
/**
@@ -38,7 +33,7 @@ export class FetService extends Service {
3833
pubkey: string,
3934
validatorAddress: string,
4035
amountFet: number,
41-
restakeRewards: boolean = false
36+
restakeRewards: boolean = false,
4237
): Promise<CosmosTx> {
4338
const { data } = await api.post<CosmosTx>(`/v1/fet/transaction/stake`, {
4439
account_id: accountId,
@@ -55,17 +50,11 @@ export class FetService extends Service {
5550
* @param pubkey wallet pubkey, this is different from the wallet address
5651
* @param validatorAddress validator address to which the delegation has been made
5752
*/
58-
async craftWithdrawRewardsTx(
59-
pubkey: string,
60-
validatorAddress: string
61-
): Promise<CosmosTx> {
62-
const { data } = await api.post<CosmosTx>(
63-
`/v1/fet/transaction/withdraw-rewards`,
64-
{
65-
pubkey: pubkey,
66-
validator: validatorAddress,
67-
}
68-
);
53+
async craftWithdrawRewardsTx(pubkey: string, validatorAddress: string): Promise<CosmosTx> {
54+
const { data } = await api.post<CosmosTx>(`/v1/fet/transaction/withdraw-rewards`, {
55+
pubkey: pubkey,
56+
validator: validatorAddress,
57+
});
6958
return data;
7059
}
7160

@@ -75,17 +64,11 @@ export class FetService extends Service {
7564
* @param validatorAccount validator account address (wallet controlling the validator)
7665
* @param validatorAddress validator address to which the delegation has been made
7766
*/
78-
async craftRestakeRewardsTx(
79-
pubkey: string,
80-
validatorAddress: string
81-
): Promise<CosmosTx> {
82-
const { data } = await api.post<CosmosTx>(
83-
`/v1/fet/transaction/restake-rewards`,
84-
{
85-
pubkey: pubkey,
86-
validator_address: validatorAddress,
87-
}
88-
);
67+
async craftRestakeRewardsTx(pubkey: string, validatorAddress: string): Promise<CosmosTx> {
68+
const { data } = await api.post<CosmosTx>(`/v1/fet/transaction/restake-rewards`, {
69+
pubkey: pubkey,
70+
validator_address: validatorAddress,
71+
});
8972
return data;
9073
}
9174

@@ -95,11 +78,7 @@ export class FetService extends Service {
9578
* @param validatorAddress validator address to which the delegation has been made
9679
* @param amountFet how many tokens to undelegate in FET
9780
*/
98-
async craftUnstakeTx(
99-
pubkey: string,
100-
validatorAddress: string,
101-
amountFet?: number
102-
): Promise<CosmosTx> {
81+
async craftUnstakeTx(pubkey: string, validatorAddress: string, amountFet?: number): Promise<CosmosTx> {
10382
const { data } = await api.post<CosmosTx>(`/v1/fet/transaction/unstake`, {
10483
pubkey: pubkey,
10584
validator: validatorAddress,
@@ -121,20 +100,15 @@ export class FetService extends Service {
121100
pubkey: string,
122101
validatorSourceAddress: string,
123102
validatorDestinationAddress: string,
124-
amountFet?: number
103+
amountFet?: number,
125104
): Promise<CosmosTx> {
126-
const { data } = await api.post<CosmosTx>(
127-
`/v1/fet/transaction/redelegate`,
128-
{
129-
account_id: accountId,
130-
pubkey: pubkey,
131-
validator_source: validatorSourceAddress,
132-
validator_destination: validatorDestinationAddress,
133-
amount_afet: amountFet
134-
? this.fetToAfet(amountFet.toString())
135-
: undefined,
136-
}
137-
);
105+
const { data } = await api.post<CosmosTx>(`/v1/fet/transaction/redelegate`, {
106+
account_id: accountId,
107+
pubkey: pubkey,
108+
validator_source: validatorSourceAddress,
109+
validator_destination: validatorDestinationAddress,
110+
amount_afet: amountFet ? this.fetToAfet(amountFet.toString()) : undefined,
111+
});
138112
return data;
139113
}
140114

@@ -144,33 +118,21 @@ export class FetService extends Service {
144118
* @param tx raw transaction
145119
* @param note note to identify the transaction in your custody solution
146120
*/
147-
async sign(
148-
integration: Integration,
149-
tx: CosmosTx,
150-
note?: string
151-
): Promise<CosmosSignedTx> {
121+
async sign(integration: Integration, tx: CosmosTx, note?: string): Promise<CosmosSignedTx> {
152122
const payloadContent = tx.data.unsigned_tx_hash;
153123
const derivationPath = [44, 118, integration.vaultId, 0, 0];
154124
const signingAlgorithm = SigningAlgorithm.MPC_ECDSA_SECP256K1;
155125
const fbNote = note ? note : "FET tx from @kilnfi/sdk";
156126

157127
const signer = this.getFbSigner(integration);
158-
const fbTx = await signer.signGenericWithFB(
159-
payloadContent,
160-
derivationPath,
161-
signingAlgorithm,
162-
fbNote
163-
);
128+
const fbTx = await signer.signGenericWithFB(payloadContent, derivationPath, signingAlgorithm, fbNote);
164129
const signature: string = fbTx.signedMessages![0].signature.fullSig;
165-
const { data } = await api.post<CosmosSignedTx>(
166-
`/v1/fet/transaction/prepare`,
167-
{
168-
pubkey: tx.data.pubkey,
169-
tx_body: tx.data.tx_body,
170-
tx_auth_info: tx.data.tx_auth_info,
171-
signature: signature,
172-
}
173-
);
130+
const { data } = await api.post<CosmosSignedTx>(`/v1/fet/transaction/prepare`, {
131+
pubkey: tx.data.pubkey,
132+
tx_body: tx.data.tx_body,
133+
tx_auth_info: tx.data.tx_auth_info,
134+
signature: signature,
135+
});
174136
data.data.fireblocks_tx = fbTx;
175137
return data;
176138
}
@@ -180,12 +142,9 @@ export class FetService extends Service {
180142
* @param signedTx
181143
*/
182144
async broadcast(signedTx: CosmosSignedTx): Promise<CosmosTxHash> {
183-
const { data } = await api.post<CosmosTxHash>(
184-
`/v1/fet/transaction/broadcast`,
185-
{
186-
tx_serialized: signedTx.data.signed_tx_serialized,
187-
}
188-
);
145+
const { data } = await api.post<CosmosTxHash>(`/v1/fet/transaction/broadcast`, {
146+
tx_serialized: signedTx.data.signed_tx_serialized,
147+
});
189148
return data;
190149
}
191150

@@ -194,9 +153,7 @@ export class FetService extends Service {
194153
* @param txHash
195154
*/
196155
async getTxStatus(txHash: string): Promise<CosmosTxStatus> {
197-
const { data } = await api.get<CosmosTxStatus>(
198-
`/v1/fet/transaction/status?tx_hash=${txHash}`
199-
);
156+
const { data } = await api.get<CosmosTxStatus>(`/v1/fet/transaction/status?tx_hash=${txHash}`);
200157
return data;
201158
}
202159

@@ -205,9 +162,7 @@ export class FetService extends Service {
205162
* @param txSerialized transaction serialized
206163
*/
207164
async decodeTx(txSerialized: string): Promise<DecodedTxRaw> {
208-
const { data } = await api.get<DecodedTxRaw>(
209-
`/v1/fet/transaction/decode?tx_serialized=${txSerialized}`
210-
);
165+
const { data } = await api.get<DecodedTxRaw>(`/v1/fet/transaction/decode?tx_serialized=${txSerialized}`);
211166
return data;
212167
}
213168
}

src/services/inj.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class InjService extends Service {
1616
* @param amount
1717
*/
1818
injToAinj(amount: string): string {
19-
return (parseFloat(amount) * 10 ** 18).toFixed();
19+
return (BigInt(amount) * BigInt(10 ** 18)).toString();
2020
}
2121

2222
/**

src/services/noble.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class NobleService extends Service {
1212
}
1313

1414
usdcToUusdc(amountUsdc: string): string {
15-
return (parseFloat(amountUsdc) * 10 ** 6).toFixed();
15+
return (BigInt(amountUsdc) * BigInt(10 ** 6)).toString();
1616
}
1717

1818
/**

src/services/osmo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class OsmoService extends Service {
1717
* @param amountOsmo
1818
*/
1919
osmoToUosmo(amountOsmo: string): string {
20-
return (parseFloat(amountOsmo) * 10 ** 6).toFixed();
20+
return (BigInt(amountOsmo) * BigInt(10 ** 6)).toString();
2121
}
2222

2323
/**

src/services/sol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,6 @@ export class SolService extends Service {
257257
* @param sol
258258
*/
259259
solToLamports(sol: string): string {
260-
return (Number(sol) * LAMPORTS_PER_SOL).toString();
260+
return (BigInt(sol) * BigInt(LAMPORTS_PER_SOL)).toString();
261261
}
262262
}

0 commit comments

Comments
 (0)