ethers/lib/utils#LogDescription TypeScript Examples
The following examples show how to use
ethers/lib/utils#LogDescription.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: transaction.ts From tx2uml with MIT License | 6 votes |
parseEvent = (contract: Contract, log: LogDescription): Event => {
const params: Param[] = []
// For each event param
log.eventFragment.inputs.forEach((param, i) => {
const components = addValuesToComponents(
log.eventFragment.inputs[i],
param
)
params.push({
name: log.eventFragment.inputs[i].name,
type: log.eventFragment.inputs[i].type,
value: log.args[i],
components,
})
})
return {
name: log.name,
params,
}
}
Example #2
Source File: useContractFunction.ts From useDApp with MIT License | 5 votes |
/**
* Hook returns an object with four variables: ``state`` , ``send``, ``events`` , and ``resetState``.
*
* The `state` represents the status of transaction. See {@link TransactionStatus}.
*
* `resetState` can be used to reset the state to `None` after a transaction attempt has either succeeded or failed.
*
* The `events` is a array of parsed transaction events of type [LogDescription](https://docs.ethers.io/v5/api/utils/abi/interface/#LogDescription).
*
* To send a transaction use `send` function returned by `useContractFunction`.
* The function forwards arguments to ethers.js contract object, so that arguments map 1 to 1 with Solidity function arguments.
* Additionally, there can be one extra argument - [TransactionOverrides](https://docs.ethers.io/v5/api/contract/contract/#contract-functionsSend), which can be used to manipulate transaction parameters like gasPrice, nonce, etc
*
* If typechain contract is supplied as contract parameter then function name and send arguments will be type checked.
* More on type checking [here](https://usedapp-docs.netlify.app/docs/Guides/Reading/Typechain).
* @public
* @param contract contract which function is to be called , also see [Contract](https://docs.ethers.io/v5/api/contract/contract/)
* @param functionName name of function to call
* @param options additional options of type {@link TransactionOptions}
* @returns {} object with variables: `send` , `state` , `events`: `{ send: (...args: any[]) => void, state: TransactionStatus, events: LogDescription[] }`.
*
* @example
* const { state, send } = useContractFunction(contract, 'deposit', { transactionName: 'Wrap' })
*
* const depositEther = (etherAmount: string) => {
* send({ value: utils.parseEther(etherAmount) })
* }
* @example
* const { state, send } = useContractFunction(contract, 'withdraw', { transactionName: 'Unwrap' })
*
* const withdrawEther = (wethAmount: string) => {
* send(utils.parseEther(wethAmount))
* }
*/
export function useContractFunction<T extends TypedContract, FN extends ContractFunctionNames<T>>(
contract: T | Falsy,
functionName: FN,
options?: TransactionOptions
) {
const { library, chainId } = useEthers()
const { promiseTransaction, state, resetState } = usePromiseTransaction(chainId, options)
const [events, setEvents] = useState<LogDescription[] | undefined>(undefined)
const { bufferGasLimitPercentage = 0 } = useConfig()
const send = useCallback(
async (...args: Params<T, FN>): Promise<void> => {
if (contract) {
const hasOpts = args.length > (contract.interface?.getFunction(functionName).inputs.length ?? 0)
const contractWithSigner = connectContractToSigner(contract, options, library)
const opts = hasOpts ? args[args.length - 1] : undefined
const gasLimit = await estimateGasLimit(opts, library?.getSigner(), bufferGasLimitPercentage)
const modifiedOpts = {
...opts,
gasLimit,
}
const modifiedArgs = hasOpts ? args.slice(0, args.length - 1) : args
modifiedArgs.push(modifiedOpts)
const receipt = await promiseTransaction(contractWithSigner[functionName](...modifiedArgs))
if (receipt?.logs) {
const events = receipt.logs.reduce((accumulatedLogs, log) => {
try {
return log.address.toLowerCase() === contract.address.toLowerCase()
? [...accumulatedLogs, contract.interface.parseLog(log)]
: accumulatedLogs
} catch (_err) {
return accumulatedLogs
}
}, [] as LogDescription[])
setEvents(events)
}
}
},
[contract, functionName, options, library]
)
return { send, state, events, resetState }
}
Example #3
Source File: expectEvent.ts From balancer-v2-monorepo with GNU General Public License v3.0 | 5 votes |
export function inIndirectReceipt(
receipt: ContractReceipt,
emitter: Interface,
eventName: string,
eventArgs = {},
address?: string
): any {
const decodedEvents = receipt.logs
.filter((log) => (address ? log.address.toLowerCase() === address.toLowerCase() : true))
.map((log) => {
try {
return emitter.parseLog(log);
} catch {
return undefined;
}
})
.filter((e): e is LogDescription => e !== undefined);
const expectedEvents = decodedEvents.filter((event) => event.name === eventName);
expect(expectedEvents.length > 0).to.equal(true, `No '${eventName}' events found`);
const exceptions: Array<string> = [];
const event = expectedEvents.find(function (e) {
for (const [k, v] of Object.entries(eventArgs)) {
try {
if (e.args == undefined) {
throw new Error('Event has no arguments');
}
contains(e.args, k, v);
} catch (error) {
exceptions.push(error);
return false;
}
}
return true;
});
if (event === undefined) {
// Each event entry may have failed to match for different reasons,
// throw the first one
throw exceptions[0];
}
return event;
}