You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
4.1 KiB
143 lines
4.1 KiB
3 years ago
|
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
|
||
3 years ago
|
import { expect } from 'chai';
|
||
2 years ago
|
import { BigNumber } from 'ethers';
|
||
3 years ago
|
import { ethers } from 'hardhat';
|
||
3 years ago
|
|
||
|
import {
|
||
|
InterchainGasPaymaster,
|
||
|
InterchainGasPaymaster__factory,
|
||
2 years ago
|
} from '../../types';
|
||
3 years ago
|
|
||
2 years ago
|
const MESSAGE_ID =
|
||
|
'0x6ae9a99190641b9ed0c07143340612dde0e9cb7deaa5fe07597858ae9ba5fd7f';
|
||
2 years ago
|
const DESTINATION_DOMAIN = 1234;
|
||
2 years ago
|
const GAS_PAYMENT_AMOUNT = 123456789;
|
||
|
const GAS_AMOUNT = 4444;
|
||
|
const REFUND_ADDRESS = '0xc0ffee0000000000000000000000000000000000';
|
||
3 years ago
|
const OWNER = '0xdeadbeef00000000000000000000000000000000';
|
||
|
|
||
|
describe('InterchainGasPaymaster', async () => {
|
||
|
let paymaster: InterchainGasPaymaster, signer: SignerWithAddress;
|
||
|
|
||
|
before(async () => {
|
||
|
[signer] = await ethers.getSigners();
|
||
|
});
|
||
|
|
||
|
beforeEach(async () => {
|
||
|
const paymasterFactory = new InterchainGasPaymaster__factory(signer);
|
||
|
paymaster = await paymasterFactory.deploy();
|
||
|
});
|
||
|
|
||
2 years ago
|
describe('#initialize', () => {
|
||
2 years ago
|
it('should not be callable twice', async () => {
|
||
|
await expect(paymaster.initialize()).to.be.reverted;
|
||
|
});
|
||
|
});
|
||
|
|
||
2 years ago
|
describe('#payForGas', () => {
|
||
|
it('deposits the required amount into the contract', async () => {
|
||
|
const refundAddressBalanceBefore = await signer.provider!.getBalance(
|
||
|
REFUND_ADDRESS,
|
||
|
);
|
||
3 years ago
|
const paymasterBalanceBefore = await signer.provider!.getBalance(
|
||
|
paymaster.address,
|
||
|
);
|
||
|
|
||
2 years ago
|
await paymaster.payForGas(
|
||
|
MESSAGE_ID,
|
||
|
DESTINATION_DOMAIN,
|
||
|
GAS_AMOUNT,
|
||
|
REFUND_ADDRESS,
|
||
|
{
|
||
|
value: GAS_PAYMENT_AMOUNT,
|
||
|
},
|
||
|
);
|
||
3 years ago
|
|
||
2 years ago
|
const requiredPayment = await paymaster.quoteGasPayment(
|
||
|
DESTINATION_DOMAIN,
|
||
|
GAS_AMOUNT,
|
||
|
);
|
||
|
|
||
3 years ago
|
const paymasterBalanceAfter = await signer.provider!.getBalance(
|
||
|
paymaster.address,
|
||
|
);
|
||
|
expect(paymasterBalanceAfter.sub(paymasterBalanceBefore)).equals(
|
||
2 years ago
|
requiredPayment,
|
||
|
);
|
||
|
|
||
|
// Ensure that any overpayment was refunded to the refund address
|
||
|
const refundAddressBalanceAfter = await signer.provider!.getBalance(
|
||
|
REFUND_ADDRESS,
|
||
|
);
|
||
|
expect(refundAddressBalanceAfter.sub(refundAddressBalanceBefore)).equals(
|
||
|
BigNumber.from(GAS_PAYMENT_AMOUNT).sub(requiredPayment),
|
||
3 years ago
|
);
|
||
|
});
|
||
|
|
||
|
it('emits the GasPayment event', async () => {
|
||
3 years ago
|
await expect(
|
||
2 years ago
|
paymaster.payForGas(
|
||
|
MESSAGE_ID,
|
||
|
DESTINATION_DOMAIN,
|
||
|
GAS_AMOUNT,
|
||
|
REFUND_ADDRESS,
|
||
|
{
|
||
|
value: GAS_PAYMENT_AMOUNT,
|
||
|
},
|
||
|
),
|
||
3 years ago
|
)
|
||
3 years ago
|
.to.emit(paymaster, 'GasPayment')
|
||
2 years ago
|
.withArgs(MESSAGE_ID, GAS_AMOUNT, GAS_PAYMENT_AMOUNT);
|
||
3 years ago
|
});
|
||
|
});
|
||
|
|
||
2 years ago
|
describe('#quoteGasPayment', () => {
|
||
|
it('returns 1 wei', async () => {
|
||
|
const quotedPayment = await paymaster.quoteGasPayment(
|
||
|
DESTINATION_DOMAIN,
|
||
|
GAS_AMOUNT,
|
||
|
);
|
||
|
expect(quotedPayment).equals(1);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
describe('#claim', () => {
|
||
3 years ago
|
it('sends the entire balance of the contract to the owner', async () => {
|
||
|
// First pay some ether into the contract
|
||
2 years ago
|
await paymaster.payForGas(
|
||
|
MESSAGE_ID,
|
||
|
DESTINATION_DOMAIN,
|
||
|
GAS_AMOUNT,
|
||
|
REFUND_ADDRESS,
|
||
|
{
|
||
|
value: GAS_PAYMENT_AMOUNT,
|
||
|
},
|
||
|
);
|
||
2 years ago
|
const requiredPayment = await paymaster.quoteGasPayment(
|
||
|
DESTINATION_DOMAIN,
|
||
|
GAS_AMOUNT,
|
||
|
);
|
||
3 years ago
|
|
||
|
// Set the owner to a different address so we aren't paying gas with the same
|
||
|
// address we want to observe the balance of
|
||
|
await paymaster.transferOwnership(OWNER);
|
||
|
|
||
|
const ownerBalanceBefore = await signer.provider!.getBalance(OWNER);
|
||
|
expect(ownerBalanceBefore).equals(0);
|
||
|
const paymasterBalanceBefore = await signer.provider!.getBalance(
|
||
|
paymaster.address,
|
||
|
);
|
||
2 years ago
|
expect(paymasterBalanceBefore).equals(requiredPayment);
|
||
3 years ago
|
|
||
|
await paymaster.claim();
|
||
|
|
||
|
const ownerBalanceAfter = await signer.provider!.getBalance(OWNER);
|
||
2 years ago
|
expect(ownerBalanceAfter).equals(requiredPayment);
|
||
3 years ago
|
const paymasterBalanceAfter = await signer.provider!.getBalance(
|
||
|
paymaster.address,
|
||
|
);
|
||
|
expect(paymasterBalanceAfter).equals(0);
|
||
|
});
|
||
|
});
|
||
|
});
|