Skip to content

Commit

Permalink
[rooch-portal] Fix client error: Add null checks and input validation (
Browse files Browse the repository at this point in the history
#2591)

* 🔨 fix: typescript-sdk zh-CN link error

* 🔨fix: portal list PageLimit adapts theme colors & typescript sdk tutorial error

* 🔨fix: shell error

* Add null checks and input validation for safer data handling
  • Loading branch information
nlyrthiia authored Sep 17, 2024
1 parent 7895b40 commit 1defd75
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
20 changes: 14 additions & 6 deletions infra/rooch-portal-v2/src/sections/mint/utils/get-token-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export type TokenInfo = {

function extractCoinInfoContent(input: string): string | null {
const regex = /CoinInfo<([^>]+)>/;
const match = input.match(regex);
let match;

if (input) {
match = input.match(regex);
}

if (match && match[1]) {
return match[1];
Expand All @@ -38,11 +42,15 @@ export async function getTokenInfo(
showDisplay: true,
},
});
const decode = (((data[0].decoded_value as any).value as any).value as any).value as any;
const coinInfo = decode.coin_info as AnnotatedMoveStructView;
const coinId = coinInfo.value.id as string;
const decode = (((data?.[0]?.decoded_value as any)?.value as any)?.value as any)?.value as any;
const coinInfo = decode?.coin_info as AnnotatedMoveStructView;
const coinId = coinInfo?.value?.id as string;

const coinType = extractCoinInfoContent(coinInfo.type)!;
const coinType = extractCoinInfoContent(coinInfo?.type)!;

if (!coinId) {
return undefined;
}

return client
.getStates({
Expand All @@ -53,7 +61,7 @@ export async function getTokenInfo(
},
})
.then((sv) => {
const coinView = (sv[0].decoded_value as any).value as any;
const coinView = (sv?.[0]?.decoded_value as any)?.value as any;
const starTime = decode.start_time as number;
const endTime = decode.end_time as number;
const now = Date.now() / 1000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function RedEnvelopeDetail({ redEnvelopeId }: { redEnvelopeId: st
console.log('🚀 ~ file: view.tsx:21 ~ RedEnvelopeDetail ~ data:', redEnvelopeObject);

const redEnvelopeInfo = useMemo(() => {
const info = redEnvelopeObject?.data[0].decoded_value?.value as unknown as
const info = redEnvelopeObject?.data?.[0]?.decoded_value?.value as unknown as
| RedEnvelopeItem
| undefined;
return info;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export class RoochDataSource implements IDatasource {
throw new Error(`Transaction with id ${txId} not found`)
}

const btcTxOption = txResult.return_values[0].decoded_value.value.vec[0]
const btcTxOption = txResult.return_values?.[0]?.decoded_value?.value?.vec?.[0]
if (!btcTxOption) {
throw new Error(`Transaction with id ${txId} not found`)
}
Expand Down
10 changes: 5 additions & 5 deletions sdk/typescript/rooch-sdk/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export class RoochClient {
})

if (resp && resp.return_values) {
return BigInt(resp.return_values[0].decoded_value as number)
return BigInt(resp.return_values?.[0]?.decoded_value as number)
}

return BigInt(0)
Expand Down Expand Up @@ -335,7 +335,7 @@ export class RoochClient {
})

if (result.vm_status === 'Executed' && result.return_values) {
const value = (result.return_values[0].decoded_value as AnnotatedMoveStructView).value
const value = (result.return_values?.[0]?.decoded_value as AnnotatedMoveStructView).value

const address = (((value as any).vec[0] as AnnotatedMoveStructView).value as any).bytes

Expand Down Expand Up @@ -386,7 +386,7 @@ export class RoochClient {
throw new Error('view 0x3::session_key::is_expired_session_key fail')
}

return result.return_values![0].decoded_value as boolean
return result.return_values![0]?.decoded_value as boolean
}

async getSessionKeys({
Expand Down Expand Up @@ -416,7 +416,7 @@ export class RoochClient {
const tableId = (
(
(
(states[0].decoded_value as AnnotatedMoveStructView).value[
(states?.[0]?.decoded_value as AnnotatedMoveStructView).value[
'value'
] as AnnotatedMoveStructView
).value['keys'] as AnnotatedMoveStructView
Expand Down Expand Up @@ -450,7 +450,7 @@ export class RoochClient {
const result = new Array<SessionInfoView>()

for (const state of statePage.data as any) {
const moveValue = state?.state.decoded_value as any
const moveValue = state?.state?.decoded_value as any

if (moveValue) {
const val = moveValue.value.value.value
Expand Down

0 comments on commit 1defd75

Please sign in to comment.