MCPcopy Create free account
hub / github.com/PageLeay/celestial-runtime / buildWrappedMethod

Function buildWrappedMethod

lib.esm/contract/contract.js:185–280  ·  view source on GitHub ↗
(contract, key)

Source from the content-addressed store, hash-verified

183 return method;
184}
185function buildWrappedMethod(contract, key) {
186 const getFragment = function (...args) {
187 const fragment = contract.interface.getFunction(key, args);
188 assert(fragment, "no matching fragment", "UNSUPPORTED_OPERATION", {
189 operation: "fragment",
190 info: { key, args }
191 });
192 return fragment;
193 };
194 const populateTransaction = async function (...args) {
195 const fragment = getFragment(...args);
196 // If an overrides was passed in, copy it and normalize the values
197 let overrides = {};
198 if (fragment.inputs.length + 1 === args.length) {
199 overrides = await copyOverrides(args.pop());
200 if (overrides.from) {
201 overrides.from = await resolveAddress(overrides.from, getResolver(contract.runner));
202 }
203 }
204 if (fragment.inputs.length !== args.length) {
205 throw new Error("internal error: fragment inputs doesn't match arguments; should not happen");
206 }
207 const resolvedArgs = await resolveArgs(contract.runner, fragment.inputs, args);
208 return Object.assign({}, overrides, await resolveProperties({
209 to: contract.getAddress(),
210 data: contract.interface.encodeFunctionData(fragment, resolvedArgs)
211 }));
212 };
213 const staticCall = async function (...args) {
214 const result = await staticCallResult(...args);
215 if (result.length === 1) {
216 return result[0];
217 }
218 return result;
219 };
220 const send = async function (...args) {
221 const runner = contract.runner;
222 assert(canSend(runner), "contract runner does not support sending transactions", "UNSUPPORTED_OPERATION", { operation: "sendTransaction" });
223 const tx = await runner.sendTransaction(await populateTransaction(...args));
224 const provider = getProvider(contract.runner);
225 // @TODO: the provider can be null; make a custom dummy provider that will throw a
226 // meaningful error
227 return new ContractTransactionResponse(contract.interface, provider, tx);
228 };
229 const estimateGas = async function (...args) {
230 const runner = getRunner(contract.runner, "estimateGas");
231 assert(canEstimate(runner), "contract runner does not support gas estimation", "UNSUPPORTED_OPERATION", { operation: "estimateGas" });
232 return await runner.estimateGas(await populateTransaction(...args));
233 };
234 const staticCallResult = async function (...args) {
235 const runner = getRunner(contract.runner, "call");
236 assert(canCall(runner), "contract runner does not support calling", "UNSUPPORTED_OPERATION", { operation: "call" });
237 const tx = await populateTransaction(...args);
238 let result = "0x";
239 try {
240 result = await runner.call(tx);
241 }
242 catch (error) {

Callers 1

getFunctionMethod · 0.70

Calls 4

definePropertiesFunction · 0.90
assertFunction · 0.90
getFunctionNameMethod · 0.45
getFunctionMethod · 0.45

Tested by

no test coverage detected