Replies: 1 comment
-
That is a general issue with Proxies, but can be solved as described on MDN. You will want your "get trap" to look like this (I've updated it so it only logs values when called, not when the property is accessed; if that is what you want, the logging can be moved outside the function declaration): const ProviderRecorder = (provider, path = '/dev/stdout') => new Proxy(provider, {
get(target, prop, receiver) {
const value = target[prop];
if (value instanceof Function) {
return function (...args) {
// Log things...
switch (prop) {
case 'broadcastTransaction':
// do the logging here
}
// Make sure the function targets the correct object that has access to the private members
return value.apply(this === receiver ? target : this, args);
};
return value;
}
}
}); Another option, if you are using a JsonRpcProvider, is to use the Another option, if you like sub-classing is to use something like: class RecorderProvider extends JsonRpcProvider {
async perform(method, params) {
const result = await super.perform(method, params);
if (method === "broadcastTransaction") { console.log({ method, params, result }); }
return result;
}
} Those are the first ideas that come to mind, anyways. If something else pops into my head, I'll add it. :) (moving this to discussions) |
Beta Was this translation helpful? Give feedback.
-
Suggestion
When working with some "exotic" networks, I find it reassuring the have a copy of all the transactions I've broadcasted to the network (in particular the hashes) so I can later review them if anything unexpected happens.
I'm currently doing it using this piece of code
I believe a better option would be to use a Proxy like this:
but that doesn't work because reading private members is failling:
Beta Was this translation helpful? Give feedback.
All reactions