Skip to content

Commit 35ecd34

Browse files
committed
break out utils file
1 parent ec7dfbf commit 35ecd34

File tree

2 files changed

+99
-84
lines changed

2 files changed

+99
-84
lines changed

packages/mobile-app/app/send/index.tsx

Lines changed: 10 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -24,72 +24,13 @@ import {
2424
import SendConfirmed from "../../svgs/SendConfirmed";
2525
import Rubics from "../../svgs/Rubics";
2626
import { useRouter, Stack } from "expo-router";
27-
28-
const isValidBigInt = (num: string) => {
29-
if (num.length === 0) return false;
30-
try {
31-
const bi = BigInt(num);
32-
return bi > 0;
33-
} catch {
34-
return false;
35-
}
36-
};
37-
38-
const convertAmountToMinor = (
39-
amount: string,
40-
assetId: string,
41-
assetMap: Map<string, Asset>,
42-
): [bigint, null] | [null, Error] => {
43-
const asset =
44-
assetId === IRON_ASSET_ID_HEX ? undefined : assetMap.get(assetId);
45-
return CurrencyUtils.tryMajorToMinor(amount, assetId, {
46-
decimals: getAssetDecimals(asset),
47-
});
48-
};
49-
50-
const isValidAmount = (
51-
value: string,
52-
assetId: string,
53-
assetMap: Map<string, Asset>,
54-
) => {
55-
if (value.length === 0) return true;
56-
57-
const asset =
58-
assetId === IRON_ASSET_ID_HEX ? undefined : assetMap.get(assetId);
59-
60-
// For unverified assets, don't allow any decimals
61-
if (asset && asset.verification.status !== "verified") {
62-
return !value.includes(".");
63-
}
64-
65-
const decimals = getAssetDecimals(asset) ?? 8; // $IRON has 8 decimals by default
66-
const parts = value.split(".");
67-
return parts.length <= 2 && (parts[1]?.length ?? 0) <= decimals;
68-
};
69-
70-
const enforceDecimals = (
71-
value: string,
72-
assetId: string,
73-
assetMap: Map<string, Asset>,
74-
): string => {
75-
if (value.length === 0) return value;
76-
77-
const asset =
78-
assetId === IRON_ASSET_ID_HEX ? undefined : assetMap.get(assetId);
79-
80-
// For unverified assets, remove any decimal points
81-
if (asset && asset.verification.status !== "verified") {
82-
return value.replace(/\./g, "");
83-
}
84-
85-
const decimals = getAssetDecimals(asset) ?? 8;
86-
const parts = value.split(".");
87-
if (parts.length === 2 && parts[1].length > decimals) {
88-
return `${parts[0]}.${parts[1].slice(0, decimals)}`;
89-
}
90-
91-
return value;
92-
};
27+
import {
28+
isValidBigInt,
29+
convertAmountToMinor,
30+
isValidAmount,
31+
enforceDecimals,
32+
getAssetDecimals,
33+
} from "../../utils/send.utils";
9334

9435
const CheckIcon = (props: IconProps) => (
9536
<Icon {...props} name="checkmark-outline" />
@@ -98,18 +39,6 @@ const CheckIcon = (props: IconProps) => (
9839
// First add a new type for the transaction state
9940
type TransactionState = "sending" | "sent" | "idle";
10041

101-
// Add this helper at the top with other utility functions
102-
const getAssetDecimals = (asset: Asset | undefined): number | undefined => {
103-
if (!asset) return undefined;
104-
try {
105-
return asset.verification.status === "verified"
106-
? asset.verification.decimals
107-
: JSON.parse(asset.metadata).decimals;
108-
} catch {
109-
return undefined;
110-
}
111-
};
112-
11342
export default function Send() {
11443
const facade = useFacade();
11544
const router = useRouter();
@@ -154,6 +83,9 @@ export default function Send() {
15483
}) ?? [],
15584
});
15685

86+
// Add the mutation
87+
const sendTransactionMutation = facade.sendTransaction.useMutation();
88+
15789
const assetMap = useMemo(() => {
15890
const map = new Map<string, Asset>();
15991
for (const asset of getCustomAssets) {
@@ -200,9 +132,6 @@ export default function Send() {
200132
);
201133
}, [selectedAssetId, assetOptions]);
202134

203-
// Add the mutation
204-
const sendTransactionMutation = facade.sendTransaction.useMutation();
205-
206135
// Add amount validation
207136
const amountError = useMemo(() => {
208137
if (!amount) return undefined;
@@ -216,9 +145,6 @@ export default function Send() {
216145
return `Maximum ${decimals} decimal places allowed`;
217146
}
218147

219-
const [amountInMinorUnits] =
220-
convertAmountToMinor(amount, selectedAssetId, assetMap) ?? [];
221-
222148
return undefined;
223149
}, [amount, selectedAssetId, assetMap]);
224150

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { Asset } from "@/data/facades/chain/types";
2+
import { IRON_ASSET_ID_HEX } from "../data/constants";
3+
import { CurrencyUtils } from "@ironfish/sdk";
4+
5+
type AmountValidationResult = [bigint, null] | [null, Error];
6+
7+
interface AssetWithVerification extends Asset {
8+
verification: {
9+
status: "verified" | "unverified" | "unknown";
10+
decimals?: number;
11+
};
12+
}
13+
14+
export const isValidBigInt = (num: string) => {
15+
if (num.length === 0) return false;
16+
try {
17+
const bi = BigInt(num);
18+
return bi > 0;
19+
} catch {
20+
return false;
21+
}
22+
};
23+
24+
export const getAssetDecimals = (
25+
asset: AssetWithVerification | undefined,
26+
): number | undefined => {
27+
if (!asset) return undefined;
28+
try {
29+
return asset.verification.status === "verified"
30+
? asset.verification.decimals
31+
: JSON.parse(asset.metadata).decimals;
32+
} catch {
33+
return undefined;
34+
}
35+
};
36+
37+
export const convertAmountToMinor = (
38+
amount: string,
39+
assetId: string,
40+
assetMap: Map<string, AssetWithVerification>,
41+
): AmountValidationResult => {
42+
const asset =
43+
assetId === IRON_ASSET_ID_HEX ? undefined : assetMap.get(assetId);
44+
return CurrencyUtils.tryMajorToMinor(amount, assetId, {
45+
decimals: getAssetDecimals(asset),
46+
});
47+
};
48+
49+
export const isValidAmount = (
50+
value: string,
51+
assetId: string,
52+
assetMap: Map<string, AssetWithVerification>,
53+
) => {
54+
if (value.length === 0) return true;
55+
56+
const asset =
57+
assetId === IRON_ASSET_ID_HEX ? undefined : assetMap.get(assetId);
58+
59+
if (asset && asset.verification.status !== "verified") {
60+
return !value.includes(".");
61+
}
62+
63+
const decimals = getAssetDecimals(asset) ?? 8;
64+
const parts = value.split(".");
65+
return parts.length <= 2 && (parts[1]?.length ?? 0) <= decimals;
66+
};
67+
68+
export const enforceDecimals = (
69+
value: string,
70+
assetId: string,
71+
assetMap: Map<string, AssetWithVerification>,
72+
): string => {
73+
if (value.length === 0) return value;
74+
75+
const asset =
76+
assetId === IRON_ASSET_ID_HEX ? undefined : assetMap.get(assetId);
77+
78+
if (asset && asset.verification.status !== "verified") {
79+
return value.replace(/\./g, "");
80+
}
81+
82+
const decimals = getAssetDecimals(asset) ?? 8;
83+
const parts = value.split(".");
84+
if (parts.length === 2 && parts[1].length > decimals) {
85+
return `${parts[0]}.${parts[1].slice(0, decimals)}`;
86+
}
87+
88+
return value;
89+
};

0 commit comments

Comments
 (0)