Skip to content

Commit 62f629c

Browse files
committed
append and resolve ipfs uri in calldata
1 parent 0169660 commit 62f629c

File tree

6 files changed

+120
-14
lines changed

6 files changed

+120
-14
lines changed

packages/thirdweb/src/cli/commands/publish-stylus/publish-stylus.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ export async function publishStylus(secretKey?: string) {
102102
devdoc: {},
103103
userdoc: {},
104104
},
105-
settings: {},
105+
settings: {
106+
compilationTarget: {
107+
"src/main.rs": contractName,
108+
},
109+
},
106110
sources: {},
107111
};
108112
writeFileSync(abiPath, JSON.stringify(metadata), "utf8");

packages/thirdweb/src/contract/actions/compiler-metadata.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ export function formatCompilerMetadata(
4141
meta = metadata.source_metadata;
4242
}
4343

44-
// TODO: fix
45-
// const compilationTarget = meta.settings.compilationTarget;
46-
// const targets = Object.keys(compilationTarget);
47-
const name = "";
44+
const compilationTarget = meta.settings.compilationTarget;
45+
const targets = Object.keys(compilationTarget);
46+
const name = compilationTarget[targets[0] as keyof typeof compilationTarget];
4847
const info = {
4948
title: meta.output.devdoc.title,
5049
author: meta.output.devdoc.author,

packages/thirdweb/src/contract/actions/get-compiler-metadata.ts

+38
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { eth_getTransactionByHash } from "../../rpc/actions/eth_getTransactionByHash.js";
2+
import { getRpcClient } from "../../rpc/rpc.js";
3+
import { download } from "../../storage/download.js";
4+
import { hexToString } from "../../utils/encoding/hex.js";
15
import type { ThirdwebContract } from "../contract.js";
26
import { formatCompilerMetadata } from "./compiler-metadata.js";
37

@@ -21,6 +25,40 @@ import { formatCompilerMetadata } from "./compiler-metadata.js";
2125
*/
2226
export async function getCompilerMetadata(contract: ThirdwebContract) {
2327
const { address, chain } = contract;
28+
29+
try {
30+
const res = await fetch(
31+
`https://contract.thirdweb-dev.com/creation/${contract.chain.id}/${contract.address}`,
32+
);
33+
const creationData = await res.json();
34+
35+
if (creationData.status === "1" && creationData.result[0]?.txHash) {
36+
const rpcClient = getRpcClient({
37+
client: contract.client,
38+
chain: contract.chain,
39+
});
40+
const creationTx = await eth_getTransactionByHash(rpcClient, {
41+
hash: creationData.result[0]?.txHash,
42+
});
43+
44+
const initCode = creationTx.input;
45+
const lengthHex = initCode.slice(-2);
46+
const dataLength = Number.parseInt(lengthHex, 16) * 2;
47+
const encodedIpfsHex = initCode.slice(-dataLength - 2, -2);
48+
const uri = hexToString(`0x${encodedIpfsHex}`);
49+
50+
const res = await download({
51+
client: contract.client,
52+
uri,
53+
});
54+
const metadata = await res.json();
55+
56+
return formatCompilerMetadata(metadata);
57+
}
58+
} catch (e) {
59+
console.debug(e);
60+
}
61+
2462
const response = await fetch(
2563
`https://contract.thirdweb.com/metadata/${chain.id}/${address}`,
2664
{

packages/thirdweb/src/contract/actions/resolve-abi.ts

+48-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { type Abi, formatAbi, parseAbi } from "abitype";
2+
import { eth_getTransactionByHash } from "../../rpc/actions/eth_getTransactionByHash.js";
3+
import { getRpcClient } from "../../rpc/rpc.js";
24
import { download } from "../../storage/download.js";
5+
import { hexToString } from "../../utils/encoding/hex.js";
36
import { getClientFetch } from "../../utils/fetch.js";
47
import { withCache } from "../../utils/promise/withCache.js";
58
import { type ThirdwebContract, getContract } from "../contract.js";
@@ -43,15 +46,53 @@ export function resolveContractAbi<abi extends Abi>(
4346
return (await resolveCompositeAbi(contract as ThirdwebContract)) as abi;
4447
}
4548

46-
// try to get it from the api
4749
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+
}
5282
} 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+
}
5596
}
5697
},
5798
{

packages/thirdweb/src/contract/deployment/deploy-with-abi.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type PrepareDirectDeployTransactionOptions = Prettify<
2424
abi: Abi;
2525
bytecode: Hex;
2626
constructorParams?: Record<string, unknown>;
27+
extraData?: string;
2728
}
2829
>;
2930

@@ -70,6 +71,7 @@ export function prepareDirectDeployTransaction(
7071
constructorAbi?.inputs || [], // Leave an empty array if there's no constructor
7172
normalizeFunctionParams(constructorAbi, options.constructorParams),
7273
),
74+
`0x${options.extraData}`,
7375
]),
7476
});
7577
}
@@ -121,6 +123,7 @@ export async function deployContract(
121123
options: PrepareDirectDeployTransactionOptions & {
122124
account: Account;
123125
salt?: string;
126+
extraData?: string;
124127
},
125128
) {
126129
if (await isZkSyncChain(options.chain)) {
@@ -160,7 +163,11 @@ export async function deployContract(
160163
chain: options.chain,
161164
client: options.client,
162165
to: info.create2FactoryAddress,
163-
data: info.initBytecodeWithsalt,
166+
data: options.extraData
167+
? (info.initBytecodeWithsalt.concat(
168+
options.extraData,
169+
) as `0x${string}`)
170+
: info.initBytecodeWithsalt,
164171
}),
165172
});
166173
return address;

packages/thirdweb/src/extensions/prebuilts/deploy-published.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import {
1616
fetchBytecodeFromCompilerMetadata,
1717
} from "../../utils/any-evm/deploy-metadata.js";
1818
import type { FetchDeployMetadataResult } from "../../utils/any-evm/deploy-metadata.js";
19-
import type { Hex } from "../../utils/encoding/hex.js";
19+
import {
20+
type Hex,
21+
numberToHex,
22+
stringToHex,
23+
} from "../../utils/encoding/hex.js";
2024
import type { Account } from "../../wallets/interfaces/wallet.js";
2125
import { getAllDefaultConstructorParamsForImplementation } from "./get-required-transactions.js";
2226
import {
@@ -194,6 +198,7 @@ export async function deployContractfromDeployMetadata(
194198
compilerMetadata: deployMetadata,
195199
contractParams: processedInitializeParams,
196200
salt,
201+
metadataUri: deployMetadata.metadataUri,
197202
});
198203
}
199204
case "autoFactory": {
@@ -311,10 +316,21 @@ async function directDeploy(options: {
311316
compilerMetadata: CompilerMetadata;
312317
contractParams?: Record<string, unknown>;
313318
salt?: string;
319+
metadataUri?: string;
314320
}): Promise<string> {
315321
const { account, client, chain, compilerMetadata, contractParams, salt } =
316322
options;
317323

324+
let extraData: string | undefined;
325+
if (options.metadataUri) {
326+
const uriHex = stringToHex(options.metadataUri).replace("0x", "");
327+
const lengthHex = numberToHex(uriHex.length / 2, { size: 1 }).replace(
328+
"0x",
329+
"",
330+
);
331+
extraData = uriHex.concat(lengthHex);
332+
}
333+
318334
const { deployContract } = await import(
319335
"../../contract/deployment/deploy-with-abi.js"
320336
);
@@ -330,6 +346,7 @@ async function directDeploy(options: {
330346
abi: compilerMetadata.abi,
331347
constructorParams: contractParams,
332348
salt,
349+
extraData,
333350
});
334351
}
335352

0 commit comments

Comments
 (0)