Skip to content

Commit 8b20f6d

Browse files
committed
estimate data fee for activation
1 parent 244ba4a commit 8b20f6d

File tree

5 files changed

+90
-21
lines changed

5 files changed

+90
-21
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[
2-
"function activateProgram(address program)"
2+
"function activateProgram(address program) returns (uint16,uint256)"
33
]

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,20 @@ export async function deployContract(
186186
}
187187

188188
if (options.isStylus) {
189-
const activationTransaction = activateStylusContract({
190-
chain: options.chain,
191-
client: options.client,
192-
contractAddress: address,
193-
});
189+
try {
190+
const activationTransaction = await activateStylusContract({
191+
chain: options.chain,
192+
client: options.client,
193+
contractAddress: address,
194+
});
194195

195-
await sendTransaction({
196-
transaction: activationTransaction,
197-
account: options.account,
198-
});
196+
await sendTransaction({
197+
transaction: activationTransaction,
198+
account: options.account,
199+
});
200+
} catch {
201+
console.error("Error: Contract could not be activated.");
202+
}
199203
}
200204

201205
return address;

packages/thirdweb/src/contract/deployment/utils/bootstrap.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,20 @@ export async function getOrDeployInfraContractFromMetadata(
260260

261261
const isStylus = options.contractMetadata.metadata.language === "rust";
262262
if (isStylus) {
263-
const activationTransaction = activateStylusContract({
264-
chain: options.chain,
265-
client: options.client,
266-
contractAddress: deployedInfraContract.address,
267-
});
263+
try {
264+
const activationTransaction = await activateStylusContract({
265+
chain: options.chain,
266+
client: options.client,
267+
contractAddress: deployedInfraContract.address,
268+
});
268269

269-
await sendTransaction({
270-
transaction: activationTransaction,
271-
account: options.account,
272-
});
270+
await sendTransaction({
271+
transaction: activationTransaction,
272+
account: options.account,
273+
});
274+
} catch {
275+
console.error("Error: Contract could not be activated.");
276+
}
273277
}
274278

275279
return deployedInfraContract;

packages/thirdweb/src/extensions/stylus/__generated__/IArbWasm/write/activateProgram.ts

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/thirdweb/src/extensions/stylus/write/activateStylusContract.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
import {
2+
decodeAbiParameters,
3+
formatTransactionRequest,
4+
parseEther,
5+
} from "viem";
16
import type { Chain } from "../../../chains/types.js";
27
import type { ThirdwebClient } from "../../../client/client.js";
38
import { getContract } from "../../../contract/contract.js";
9+
import { eth_call } from "../../../rpc/actions/eth_call.js";
10+
import { getRpcClient } from "../../../rpc/rpc.js";
11+
import { encode } from "../../../transaction/actions/encode.js";
12+
import type { PreparedTransaction } from "../../../transaction/prepare-transaction.js";
413
import { activateProgram } from "../__generated__/IArbWasm/write/activateProgram.js";
514

615
const ARB_WASM_ADDRESS = "0x0000000000000000000000000000000000000071";
@@ -26,16 +35,61 @@ export type ActivateStylusContractOptions = {
2635
* await sendTransaction({ transaction, account });
2736
* ```
2837
*/
29-
export function activateStylusContract(options: ActivateStylusContractOptions) {
38+
export async function activateStylusContract(
39+
options: ActivateStylusContractOptions,
40+
) {
3041
const { chain, client, contractAddress } = options;
3142
const arbWasmPrecompile = getContract({
3243
client,
3344
chain,
3445
address: ARB_WASM_ADDRESS,
3546
});
3647

48+
const dataFee = await estimateDataFee({
49+
transaction: activateProgram({
50+
program: contractAddress,
51+
contract: arbWasmPrecompile,
52+
}),
53+
});
54+
3755
return activateProgram({
3856
program: contractAddress,
3957
contract: arbWasmPrecompile,
58+
overrides: {
59+
value: dataFee,
60+
},
61+
});
62+
}
63+
64+
async function estimateDataFee(options: {
65+
// biome-ignore lint/suspicious/noExplicitAny:
66+
transaction: PreparedTransaction<any>;
67+
}) {
68+
const data = await encode(options.transaction);
69+
70+
const serializedTx = formatTransactionRequest({
71+
data,
72+
to: ARB_WASM_ADDRESS,
73+
value: parseEther("1"), // only for simulation. it will be replaced with estimated data fee.
4074
});
75+
76+
const rpcRequest = getRpcClient(options.transaction);
77+
try {
78+
const result = await eth_call(rpcRequest, serializedTx);
79+
const [, dataFee] = decodeAbiParameters(
80+
[
81+
{
82+
type: "uint16",
83+
},
84+
{
85+
type: "uint256",
86+
},
87+
],
88+
result,
89+
);
90+
91+
return (dataFee * BigInt(100 + 10)) / BigInt(100); // bump 10%
92+
} catch {
93+
return 0n;
94+
}
4195
}

0 commit comments

Comments
 (0)