@ethersproject/abi#LogDescription TypeScript Examples

The following examples show how to use @ethersproject/abi#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: contract.ts    From solana-solidity.js with Apache License 2.0 6 votes vote down vote up
/** @internal */
    protected parseLogsEvents(logs: string[]): LogDescription[] {
        const events: LogDescription[] = [];

        for (const log of logs) {
            const eventData = parseLogTopic(log);
            if (eventData) {
                const event = this.interface.parseLog(eventData);
                events.push(event);
            }
        }

        return events;
    }
Example #2
Source File: logs.ts    From solana-solidity.js with Apache License 2.0 6 votes vote down vote up
protected setupSubscription(): void {
        this._subscriptionId ||= this._contract.connection.onLogs(this._contract.program, (logs) => {
            if (logs.err) return;
            for (const log of logs.logs) {
                const eventData = parseLogTopic(log);
                const message = parseLogLog(log);

                if (eventData) {
                    for (const listener of this._eventListeners.values()) {
                        let event: LogDescription | null = null;
                        try {
                            event = this._contract.interface.parseLog(eventData);
                        } catch (error) {
                            console.error(error);
                        }
                        if (event) {
                            listener(event);
                        }
                    }
                }

                if (message) {
                    for (const listener of this._logListeners.values()) {
                        listener(message);
                    }
                }
            }
        });
    }
Example #3
Source File: erc20.test.ts    From solana-solidity.js with Apache License 2.0 5 votes vote down vote up
describe('ERC20', () => {
    let contract: Contract;
    let payerETHAddress: string;

    it('deploys new contract', async function () {
        this.timeout(150000);
        ({ contract, payerETHAddress } = await loadContract(__dirname, [NAME, SYMBOL, TOTAL_SUPPLY]));

        const name = await contract.name();
        expect(name).toEqual(NAME);

        const symbol = await contract.symbol();
        expect(symbol).toEqual(SYMBOL);

        const decimals = await contract.decimals();
        expect(decimals).toEqual(18);

        const supply = await contract.totalSupply();
        expect(supply.toString()).toEqual(TOTAL_SUPPLY.toString());

        const balance = await contract.balanceOf(payerETHAddress);
        expect(balance.toString()).toEqual(TOTAL_SUPPLY.toString());
    });

    it('works with existing contract', async function () {
        contract = new Contract(contract.connection, contract.program, contract.storage, contract.abi, contract.payer);
        const name = await contract.name();
        expect(name).toEqual('Solana');
    });

    it('mutates contract state', async function () {
        const otherAccount = publicKeyToHex(Keypair.generate().publicKey);
        const transferAmount = 9;

        await contract.transfer(otherAccount, transferAmount);

        const otherAccountBalance = await contract.balanceOf(otherAccount);
        expect(otherAccountBalance.toString()).toEqual(transferAmount.toString());

        const payerBalance = await contract.balanceOf(payerETHAddress);
        expect(payerBalance.toString()).toEqual((TOTAL_SUPPLY - transferAmount).toString());
    });

    it('emits events', async function () {
        const spenderAccount = publicKeyToHex(Keypair.generate().publicKey);
        const spendAmount = 9;

        const event = await new Promise<LogDescription>((resolve, reject) => {
            const listenerId = contract.addEventListener(async (event) => {
                await contract.removeEventListener(listenerId);
                resolve(event);
            });
            contract.approve(spenderAccount, spendAmount).catch(reject);
        });

        expect(event.name).toEqual('Approval');
        const [owner, spender, value] = event.args;
        expect(owner).toEqual(payerETHAddress);
        expect(spender.toString()).toEqual(spenderAccount.toString());
        expect(value.toString()).toEqual(spendAmount.toString());
    });
});
Example #4
Source File: events.test.ts    From solana-solidity.js with Apache License 2.0 5 votes vote down vote up
describe('Events', () => {
    let contract: Contract;

    it('get returned in contract deploy results', async function () {
        this.timeout(150000);

        const result = await loadContract(__dirname);
        contract = result.contract;
        const events = result.events;
        expect(events.length).toEqual(1);

        const [{ args }] = events;
        expect(args.length).toEqual(3);
        expect(args[0].toString()).toEqual('102');
        expect(args[1]).toEqual(true);
        expect(args[2]).toEqual('foobar');
    });

    it('can be subscribed', async function () {
        const event = await new Promise<LogDescription>((resolve, reject) => {
            const listenerId = contract.addEventListener(async (event) => {
                await contract.removeEventListener(listenerId);
                resolve(event);
            });
            contract.functions.first().catch(reject);
        });

        expect(event.name).toEqual('First');

        const { args } = event;
        expect(args.length).toEqual(3);
        expect(args[0].toString()).toEqual('102');
        expect(args[1]).toEqual(true);
        expect(args[2]).toEqual('foobar');
    });

    it('get returned in contract call results', async function () {
        const { events } = await contract.functions.second();

        expect(events.length).toEqual(1);

        const [{ args }] = events;
        expect(args.length).toEqual(3);
        expect(args[0].toString()).toEqual('500332');
        expect(args[1]).toEqual('0x41424344');
        expect(args[2]).toEqual('0xcafe0123');
    });
});