web3-utils#randomHex TypeScript Examples

The following examples show how to use web3-utils#randomHex. 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: utils.test.ts    From solidity-utils with MIT License 5 votes vote down vote up
contract('', function ([wallet1, wallet2]) {
    const initContext = async () => {
        const USDT = await TokenMock.new('USDT', 'USDT');
        const USDC = await TokenMock.new('USDC', 'USDC');
        return { USDT, USDC };
    };

    let context: Awaited<ReturnType<typeof initContext>> = undefined!;

    before(async () => {
        context = await initContext();
    });

    beforeEach(async function () {
        for (const addr of [wallet1, wallet2]) {
            for (const token of [context.USDT, context.USDC]) {
                await token.mint(addr, ether('1000'));
            }
        }
    });

    describe('signMessage', async function () {
        it('should be signed test1', async function () {
            expect(await web3.eth.sign('0x', wallet1)).equal(await signMessage(wallet1));
        });

        it('should be signed test2', async function () {
            const message = randomHex(32);
            expect(await web3.eth.sign(message, wallet1)).equal(await signMessage(wallet1, message));
        });

        it('should be signed test3', async function () {
            const message = toHex('Test message');
            expect(await web3.eth.sign(message, wallet1)).equal(await signMessage(wallet1, message));
        });
    });

    describe('trackReceivedTokenAndTx', async function () {
        it('should be tracked ERC20 Transfer', async function () {
            const [received, tx] = await trackReceivedTokenAndTx(
                context.USDT,
                wallet2,
                () => context.USDT.transfer(wallet2, ether('1'), { from: wallet1 }),
            );
            expect(received).to.be.bignumber.equal(ether('1'));
            expect(tx.tx.length).equal(66);
            expect(tx.receipt.from).equal(wallet1.toLowerCase());
            expect(tx.receipt.to).equal(context.USDT.address.toLowerCase());
            expect(tx.logs.length).equal(1);
            expect(tx.logs[0].event).equal('Transfer');
        });

        it('should be tracked ERC20 Approve', async function () {
            const [received, tx] = await trackReceivedTokenAndTx(
                context.USDT,
                wallet2,
                () => context.USDT.approve(wallet2, ether('1'), { from: wallet1 }),
            );
            expect(received).to.be.bignumber.equal('0');
            expect(tx.tx.length).equal(66);
            expect(tx.receipt.from).equal(wallet1.toLowerCase());
            expect(tx.receipt.to).equal(context.USDT.address.toLowerCase());
            expect(tx.logs.length).equal(1);
            expect(tx.logs[0].event).equal('Approval');
        });
    });

    describe('trackReceivedToken', async function () {
        it('should be tracked ERC20 Transfer', async function () {
            const [received] = await trackReceivedTokenAndTx(
                context.USDT,
                wallet2,
                () => context.USDT.transfer(wallet2, ether('1'), { from: wallet1 }),
            );
            expect(received).to.be.bignumber.equal(ether('1'));
        });

        it('should be tracked ERC20 Approve', async function () {
            const [received] = await trackReceivedTokenAndTx(
                context.USDT,
                wallet2,
                () => context.USDT.approve(wallet2, ether('1'), { from: wallet1 }),
            );
            expect(received).to.be.bignumber.equal('0');
        });
    });

    describe('countInstructions', async function () {
        it('should be counted ERC20 Transfer', async function () {
            const [, tx] = await trackReceivedTokenAndTx(
                context.USDT,
                wallet2,
                () => context.USDT.transfer(wallet2, ether('1'), { from: wallet1 }),
            );
            expect(await countInstructions(tx.receipt.transactionHash, ['STATICCALL', 'CALL', 'SSTORE', 'SLOAD']))
                .to.be.deep.equal([0, 0, 2, 2]);
        });

        it('should be counted ERC20 Approve', async function () {
            const [, tx] = await trackReceivedTokenAndTx(
                context.USDT,
                wallet2,
                () => context.USDT.approve(wallet2, ether('1'), { from: wallet1 }),
            );
            expect(await countInstructions(tx.receipt.transactionHash, ['STATICCALL', 'CALL', 'SSTORE', 'SLOAD']))
                .to.be.deep.equal([0, 0, 1, 0]);
        });
    });
});