|
1 | 1 | import { type Abi, formatAbi, parseAbi } from "abitype";
|
| 2 | +import { eth_getTransactionByHash } from "../../rpc/actions/eth_getTransactionByHash.js"; |
| 3 | +import { getRpcClient } from "../../rpc/rpc.js"; |
2 | 4 | import { download } from "../../storage/download.js";
|
| 5 | +import { hexToString } from "../../utils/encoding/hex.js"; |
3 | 6 | import { getClientFetch } from "../../utils/fetch.js";
|
4 | 7 | import { withCache } from "../../utils/promise/withCache.js";
|
5 | 8 | import { type ThirdwebContract, getContract } from "../contract.js";
|
@@ -43,15 +46,53 @@ export function resolveContractAbi<abi extends Abi>(
|
43 | 46 | return (await resolveCompositeAbi(contract as ThirdwebContract)) as abi;
|
44 | 47 | }
|
45 | 48 |
|
46 |
| - // try to get it from the api |
47 | 49 | try {
|
48 |
| - return (await resolveAbiFromContractApi( |
49 |
| - contract, |
50 |
| - contractApiBaseUrl, |
51 |
| - )) as abi; |
| 50 | + const res = await fetch( |
| 51 | + `https://contract.thirdweb-dev.com/creation/${contract.chain.id}/${contract.address}`, |
| 52 | + ); |
| 53 | + const creationData = await res.json(); |
| 54 | + |
| 55 | + if (creationData.status === "1" && creationData.result[0]?.txHash) { |
| 56 | + const rpcClient = getRpcClient({ |
| 57 | + client: contract.client, |
| 58 | + chain: contract.chain, |
| 59 | + }); |
| 60 | + const creationTx = await eth_getTransactionByHash(rpcClient, { |
| 61 | + hash: creationData.result[0]?.txHash, |
| 62 | + }); |
| 63 | + |
| 64 | + const initCode = creationTx.input; |
| 65 | + const lengthHex = initCode.slice(-2); |
| 66 | + const dataLength = Number.parseInt(lengthHex, 16) * 2; |
| 67 | + const encodedIpfsHex = initCode.slice(-dataLength - 2, -2); |
| 68 | + const uri = hexToString(`0x${encodedIpfsHex}`); |
| 69 | + |
| 70 | + const res = await download({ |
| 71 | + client: contract.client, |
| 72 | + uri, |
| 73 | + }); |
| 74 | + const metadata = await res.json(); |
| 75 | + |
| 76 | + return metadata.output.abi as abi; |
| 77 | + } else { |
| 78 | + return (await resolveCompositeAbi( |
| 79 | + contract as ThirdwebContract, |
| 80 | + )) as abi; |
| 81 | + } |
52 | 82 | } catch {
|
53 |
| - // if that fails, try to resolve it from the bytecode |
54 |
| - return (await resolveCompositeAbi(contract as ThirdwebContract)) as abi; |
| 83 | + // try to get it from the api |
| 84 | + try { |
| 85 | + return (await resolveAbiFromContractApi( |
| 86 | + contract, |
| 87 | + contractApiBaseUrl, |
| 88 | + )) as abi; |
| 89 | + } catch { |
| 90 | + // console.debug(e); |
| 91 | + // if that fails, try to resolve it from the bytecode |
| 92 | + return (await resolveCompositeAbi( |
| 93 | + contract as ThirdwebContract, |
| 94 | + )) as abi; |
| 95 | + } |
55 | 96 | }
|
56 | 97 | },
|
57 | 98 | {
|
|
0 commit comments