Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getlogs and internal timing polish #902

Merged
merged 13 commits into from
Dec 15, 2023
73 changes: 41 additions & 32 deletions examples/waffle/scheduler/test/Scheduler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createTestPairs } from '@polkadot/keyring/testingPairs';
import RecurringPayment from '../build/RecurringPayment.json';
import Subscription from '../build/Subscription.json';
import ADDRESS from '@acala-network/contracts/utils/MandalaAddress';
import { sleep } from '@acala-network/eth-providers';

use(evmChai);

Expand All @@ -16,7 +17,13 @@ const formatAmount = (amount: String) => {
const dollar = BigNumber.from(formatAmount('1_000_000_000_000'));

const nextBlock = async (provider: BodhiProvider): Promise<void> => {
const nextBlockNumber = await provider.getBlockNumber() + 1;
await provider.api.rpc.engine.createBlock(true /* create empty */, true);

while ((await provider.getBlockNumber()) < nextBlockNumber) {
// provider internal head is slightly slower than node head
await sleep(200);
}
};

const SCHEDULE_CALL_ABI = require('@acala-network/contracts/build/contracts/Schedule.json').abi;
Expand All @@ -28,12 +35,14 @@ describe('Schedule', () => {
let subscriber: BodhiSigner;
let provider: BodhiProvider;
let schedule: Contract;
let subscriberAddr: string;

before(async () => {
const endpoint = process.env.ENDPOINT_URL ?? 'ws://localhost:9944';
const testUtils = await getTestUtils(endpoint);
[wallet, walletTo, subscriber] = testUtils.wallets;
provider = testUtils.provider; // this is the same as wallet.provider
subscriberAddr = await subscriber.getAddress();
schedule = new ethers.Contract(ADDRESS.SCHEDULE, SCHEDULE_CALL_ABI, wallet);
});

Expand All @@ -42,19 +51,19 @@ describe('Schedule', () => {
});

it('ScheduleCall works', async () => {
const target_block_number = Number(await provider.api.query.system.number()) + 4;
const target_block_number = await provider.getBlockNumber() + 4;

const erc20 = new ethers.Contract(ADDRESS.DOT, ERC20_ABI, walletTo);
const tx = await erc20.populateTransaction.transfer(walletTo.getAddress(), 1_000_000);
// console.log(tx, ethers.utils.hexlify(tx.data as string));

await schedule.scheduleCall(ADDRESS.DOT, 0, 300000, 10000, 1, ethers.utils.hexlify(tx.data as string));

let current_block_number = Number(await provider.api.query.system.number());
let current_block_number = await provider.getBlockNumber();
let balance = await erc20.balanceOf(await walletTo.getAddress());
while (current_block_number < target_block_number) {
await nextBlock(provider);
current_block_number = Number(await provider.api.query.system.number());
current_block_number = await provider.getBlockNumber();
}

let new_balance = await erc20.balanceOf(await walletTo.getAddress());
Expand All @@ -68,7 +77,7 @@ describe('Schedule', () => {

let iface = new ethers.utils.Interface(SCHEDULE_CALL_ABI);

let current_block_number = Number(await provider.api.query.system.number());
let current_block_number = await provider.getBlockNumber();
await schedule.scheduleCall(ADDRESS.DOT, 0, 300000, 10000, 2, ethers.utils.hexlify(tx.data as string));

let block_hash = await provider.api.rpc.chain.getBlockHash(current_block_number + 1);
Expand All @@ -90,7 +99,7 @@ describe('Schedule', () => {

let iface = new ethers.utils.Interface(SCHEDULE_CALL_ABI);

let current_block_number = Number(await provider.api.query.system.number());
let current_block_number = await provider.getBlockNumber();
await schedule.scheduleCall(ADDRESS.DOT, 0, 300000, 10000, 4, ethers.utils.hexlify(tx.data as string));

let block_hash = await provider.api.rpc.chain.getBlockHash(current_block_number + 1);
Expand All @@ -117,36 +126,36 @@ describe('Schedule', () => {
);
// ACA as erc20 decimals is 12
await erc20.transfer(recurringPayment.address, dollar.mul(5000));
const inital_block_number = Number(await provider.api.query.system.number());
const inital_block_number = await provider.getBlockNumber();
await recurringPayment.initialize();

expect((await provider.getBalance(transferTo)).toString()).to.equal('0');
expect((await erc20.balanceOf(transferTo)).toString()).to.equal('0');

let current_block_number = Number(await provider.api.query.system.number());
let current_block_number = await provider.getBlockNumber();

while (current_block_number < inital_block_number + 5) {
await nextBlock(provider);
current_block_number = Number(await provider.api.query.system.number());
current_block_number = await provider.getBlockNumber();
}

expect((await provider.getBalance(transferTo)).toString()).to.equal(dollar.mul(1000000000).toString());
expect((await erc20.balanceOf(transferTo)).toString()).to.equal(dollar.mul(1000).toString());

current_block_number = Number(await provider.api.query.system.number());
current_block_number = await provider.getBlockNumber();
while (current_block_number < inital_block_number + 14) {
await nextBlock(provider);
current_block_number = Number(await provider.api.query.system.number());
current_block_number = await provider.getBlockNumber();
}

expect((await provider.getBalance(transferTo)).toString()).to.equal(dollar.mul(3000000000).toString());
expect((await erc20.balanceOf(transferTo)).toString()).to.equal(dollar.mul(3000).toString());

current_block_number = Number(await provider.api.query.system.number());
current_block_number = await provider.getBlockNumber();
// ISchedule task needs one more block
while (current_block_number < inital_block_number + 17 + 1) {
await nextBlock(provider);
current_block_number = Number(await provider.api.query.system.number());
current_block_number = await provider.getBlockNumber();
}

expect((await provider.getBalance(recurringPayment.address)).toString()).to.equal('0');
Expand Down Expand Up @@ -176,53 +185,53 @@ describe('Schedule', () => {
await provider.api.tx.evm.publishContract(subscription.address).signAndSend(testPairs.alice.address);
}

expect((await subscription.balanceOf(subscriber.getAddress())).toString()).to.equal('0');
expect((await subscription.subTokensOf(subscriber.getAddress())).toString()).to.equal('0');
expect((await subscription.monthsSubscribed(subscriber.getAddress())).toString()).to.equal('0');
expect((await subscription.balanceOf(subscriberAddr)).toString()).to.equal('0');
expect((await subscription.subTokensOf(subscriberAddr)).toString()).to.equal('0');
expect((await subscription.monthsSubscribed(subscriberAddr)).toString()).to.equal('0');

const subscriberContract = subscription.connect(subscriber as any);
await subscriberContract.subscribe({
value: ethers.utils.parseEther(formatAmount('10_000')).toString(),
gasLimit: 2_000_000
});

expect((await subscription.balanceOf(subscriber.getAddress())).toString()).to.equal(
expect((await subscription.balanceOf(subscriberAddr)).toString()).to.equal(
ethers.utils.parseEther(formatAmount('10_000')).sub(subPrice).toString()
);
expect((await subscription.subTokensOf(subscriber.getAddress())).toString()).to.equal('1');
expect((await subscription.monthsSubscribed(subscriber.getAddress())).toString()).to.equal('1');
expect((await subscription.subTokensOf(subscriberAddr)).toString()).to.equal('1');
expect((await subscription.monthsSubscribed(subscriberAddr)).toString()).to.equal('1');

let current_block_number = Number(await provider.api.query.system.number());
let current_block_number = await provider.getBlockNumber();
for (let i = 0; i < period + 1; i++) {
await nextBlock(provider);
current_block_number = Number(await provider.api.query.system.number());
current_block_number = await provider.getBlockNumber();
}

expect((await subscription.balanceOf(subscriber.getAddress())).toString()).to.equal(
expect((await subscription.balanceOf(subscriberAddr)).toString()).to.equal(
ethers.utils.parseEther(formatAmount('10_000')).sub(subPrice.mul(2)).toString()
);
expect((await subscription.subTokensOf(subscriber.getAddress())).toString()).to.equal('3');
expect((await subscription.monthsSubscribed(subscriber.getAddress())).toString()).to.equal('2');
expect((await subscription.subTokensOf(subscriberAddr)).toString()).to.equal('3');
expect((await subscription.monthsSubscribed(subscriberAddr)).toString()).to.equal('2');

current_block_number = Number(await provider.api.query.system.number());
current_block_number = await provider.getBlockNumber();
for (let i = 0; i < period + 1; i++) {
await nextBlock(provider);
current_block_number = Number(await provider.api.query.system.number());
current_block_number = await provider.getBlockNumber();
}

expect((await subscription.balanceOf(subscriber.getAddress())).toString()).to.equal(
expect((await subscription.balanceOf(subscriberAddr)).toString()).to.equal(
ethers.utils.parseEther(formatAmount('10_000')).sub(subPrice.mul(3)).toString()
);
expect((await subscription.subTokensOf(subscriber.getAddress())).toString()).to.equal('6');
expect((await subscription.monthsSubscribed(subscriber.getAddress())).toString()).to.equal('3');
expect((await subscription.subTokensOf(subscriberAddr)).toString()).to.equal('6');
expect((await subscription.monthsSubscribed(subscriberAddr)).toString()).to.equal('3');

await subscriberContract.unsubscribe({ gasLimit: 2_000_000 });

current_block_number = Number(await provider.api.query.system.number());
current_block_number = await provider.getBlockNumber();
await nextBlock(provider);

expect((await subscription.balanceOf(subscriber.getAddress())).toString()).to.equal('0');
expect((await subscription.subTokensOf(subscriber.getAddress())).toString()).to.equal('6');
expect((await subscription.monthsSubscribed(subscriber.getAddress())).toString()).to.equal('0');
expect((await subscription.balanceOf(subscriberAddr)).toString()).to.equal('0');
expect((await subscription.subTokensOf(subscriberAddr)).toString()).to.equal('6');
expect((await subscription.monthsSubscribed(subscriberAddr)).toString()).to.equal('0');
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"start:chain": "docker compose up --build -- chain-ready",
"start:eth-rpc-adapter": "docker compose up --build -- eth-rpc-adapter-ready",
"start:eth-rpc-adapter-subql": "docker compose up --build -- eth-rpc-adapter-with-subql-ready",
"feed-tx": "yarn e2e:feed-tx && yarn e2e:feed-tx-2",
"e2e:feed-tx": "yarn workspace evm-waffle-example-dex run test",
"e2e:feed-tx-2": "yarn workspace evm-waffle-example-e2e run test",
"e2e:eth-providers": "yarn start:chain; yarn e2e:feed-tx; yarn start:eth-rpc-adapter; yarn workspace @acala-network/eth-providers run test:e2e",
Expand Down
3 changes: 2 additions & 1 deletion packages/eth-providers/src/base-provider-dd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ const TRACE_METHODS = [
'getTransactionByHash',
'getReceipt',
'_getReceipt',
'_checkSubqlHeight',
'_sanitizeRawFilter',
'_getSubqlMissedLogs',
'getLogs',
'_waitForSubql',
'getIndexerMetadata',
'healthCheck',
'addEventListener',
Expand Down
Loading
Loading