• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cartesi / rollups-explorer / 10946891476

19 Sep 2024 06:35PM UTC coverage: 86.213% (+0.005%) from 86.208%
10946891476

push

github

brunomenezes
feat: Add support for decoding Voucher payload when available to remotely retrieve the destination contract ABI.

1133 of 1373 branches covered (82.52%)

Branch coverage included in aggregate %.

95 of 162 new or added lines in 6 files covered. (58.64%)

32 existing lines in 2 files now uncovered.

8347 of 9623 relevant lines covered (86.74%)

45.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

37.8
/apps/web/src/components/specification/hooks/useVoucherDecoder.tsx
1
"use client";
1✔
2
import { whatsabi } from "@shazow/whatsabi";
1✔
3
import { any, isNil } from "ramda";
1✔
4
import { isNilOrEmpty, isNotNilOrEmpty } from "ramda-adjunct";
1✔
5
import { useEffect, useState } from "react";
1✔
6
import { Abi, Hex, createPublicClient } from "viem";
1✔
7
import getSupportedChainInfo, {
8
    SupportedChainId,
9
} from "../../../lib/supportedChains";
1✔
10
import buildTransport from "../../../lib/transport";
1✔
11
import { decodePayload } from "../decoder";
1✔
12
import { Specification } from "../types";
13
import { stringifyContent } from "../utils";
1✔
14

15
type UseVoucherDecoderProps = {
16
    destination: Hex;
17
    payload: Hex;
18
    chainId: number;
19
};
20
const cache = new Map<string, Abi>();
1✔
21

22
const buildClient = (chainId: number) => {
1✔
NEW
23
    const chain = getSupportedChainInfo(chainId as SupportedChainId);
×
24

NEW
25
    if (isNil(chain)) throw new Error(`ChainId ${chainId} is not supported!`);
×
26

NEW
27
    return createPublicClient({
×
NEW
28
        transport: buildTransport(chain.id),
×
NEW
29
        chain,
×
NEW
30
    });
×
NEW
31
};
×
32

33
const fetchAbiFor = async (destination: Hex, chainId: number) => {
1✔
NEW
34
    const cacheKey = `${chainId}:${destination}`;
×
NEW
35
    const abi = cache.get(cacheKey);
×
36

NEW
37
    if (abi && isNotNilOrEmpty(abi)) return abi;
×
38

NEW
39
    const result = await whatsabi.autoload(destination, {
×
NEW
40
        provider: buildClient(chainId),
×
NEW
41
        followProxies: true,
×
NEW
42
        onError: () => false,
×
NEW
43
    });
×
44

NEW
45
    cache.set(cacheKey, result.abi as Abi);
×
46

NEW
47
    return result.abi as Abi;
×
NEW
48
};
×
49

50
const buildSpecification = (
1✔
NEW
51
    destination: Hex,
×
NEW
52
    chainId: number,
×
NEW
53
    abi: Abi,
×
NEW
54
): Specification => {
×
NEW
55
    return {
×
NEW
56
        mode: "json_abi",
×
NEW
57
        abi,
×
NEW
58
        name: `Abi for ${destination} on chain ${chainId}`,
×
NEW
59
        timestamp: Date.now(),
×
NEW
60
        version: 1,
×
NEW
61
    };
×
NEW
62
};
×
63

64
interface Result {
65
    status: "loading" | "idle";
66
    data: string | null;
67
}
68

69
const useVoucherDecoder = ({
1✔
70
    destination,
33✔
71
    payload,
33✔
72
    chainId,
33✔
73
}: UseVoucherDecoderProps) => {
33✔
74
    const [result, setResult] = useState<Result>({
33✔
75
        status: "idle",
33✔
76
        data: null,
33✔
77
    });
33✔
78

79
    useEffect(() => {
33✔
80
        if (any(isNilOrEmpty)([destination, chainId, payload])) return;
13!
81

NEW
82
        setResult((old) => ({ ...old, status: "loading" }));
×
83

NEW
84
        (async () => {
×
NEW
85
            let result: string;
×
86

NEW
87
            const baseMessage = "Skipping voucher decoding. Reason:";
×
NEW
88
            try {
×
NEW
89
                const abi = await fetchAbiFor(destination, chainId);
×
NEW
90
                const spec = buildSpecification(destination, chainId, abi);
×
NEW
91
                const envelope = decodePayload(spec, payload);
×
92

NEW
93
                if (!envelope.error) result = stringifyContent(envelope.result);
×
NEW
94
                else {
×
NEW
95
                    console.info(`${baseMessage} ${envelope.error.message}`);
×
NEW
96
                    result = payload;
×
NEW
97
                }
×
NEW
98
            } catch (error: any) {
×
NEW
99
                console.info(`${baseMessage} ${error.message}`);
×
NEW
100
                result = payload;
×
NEW
101
            }
×
102

NEW
103
            setResult(() => ({ status: "idle", data: result }));
×
NEW
104
        })();
×
105
    }, [destination, chainId, payload]);
33✔
106

107
    return result;
33✔
108
};
33✔
109

110
export default useVoucherDecoder;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc