mirror of openzeppelin-contracts
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.
openzeppelin-contracts/test/GSN/GSNRecipient.test.js

120 lines
4.4 KiB

const { accounts, contract, web3 } = require('@openzeppelin/test-environment');
const { balance, BN, constants, ether, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;
Initial GSN support (beta) (#1844) * Add base Context contract * Add GSNContext and tests * Add RelayHub deployment to tests * Add RelayProvider integration, complete GSNContext tests * Switch dependency to openzeppelin-gsn-provider * Add default txfee to provider * Add basic signing recipient * Sign more values * Add comment clarifying RelayHub's msg.data * Make context constructors internal * Rename SigningRecipient to GSNRecipientSignedData * Add ERC20Charge recipients * Harcode RelayHub address into GSNContext * Fix Solidity linter errors * Run server from binary, use gsn-helpers to fund it * Migrate to published @openzeppelin/gsn-helpers * Silence false-positive compiler warning * Use GSN helper assertions * Rename meta-tx to gsn, take out of drafts * Merge ERC20 charge recipients into a single one * Rename GSNRecipients to Bouncers * Add GSNBouncerUtils to decouple the bouncers from GSNRecipient * Add _upgradeRelayHub * Store RelayHub address using unstructored storage * Add IRelayHub * Add _withdrawDeposits to GSNRecipient * Add relayHub version to recipient * Make _acceptRelayedCall and _declineRelayedCall easier to use * Rename GSNBouncerUtils to GSNBouncerBase, make it IRelayRecipient * Improve GSNBouncerBase, make pre and post sender-protected and optional * Fix GSNBouncerERC20Fee, add tests * Add missing GSNBouncerSignature test * Override transferFrom in __unstable__ERC20PrimaryAdmin * Fix gsn dependencies in package.json * Rhub address slot reduced by 1 * Rename relay hub changed event * Use released gsn-provider * Run relayer with short sleep of 1s instead of 100ms * update package-lock.json * clear circle cache * use optimized gsn-provider * update to latest @openzeppelin/gsn-provider * replace with gsn dev provider * remove relay server * rename arguments in approveFunction * fix GSNBouncerSignature test * change gsn txfee * initialize development provider only once * update RelayHub interface * adapt to new IRelayHub.withdraw * update @openzeppelin/gsn-helpers * update relayhub singleton address * fix helper name * set up gsn provider for coverage too * lint * Revert "set up gsn provider for coverage too" This reverts commit 8a7b5be5f942002710cba148f249cb888ee8e373. * remove unused code * add gsn provider to coverage * move truffle contract options back out * increase gas limit for coverage * remove unreachable code * add more gas for GSNContext test * fix test suite name * rename GSNBouncerBase internal API * remove onlyRelayHub modifier * add explicit inheritance * remove redundant event * update name of bouncers error codes enums * add basic docs page for gsn contracts * make gsn directory all caps * add changelog entry * lint * enable test run to fail in coverage
6 years ago
const gsn = require('@openzeppelin/gsn-helpers');
const { expect } = require('chai');
const GSNRecipientMock = contract.fromArtifact('GSNRecipientMock');
const ContextMockCaller = contract.fromArtifact('ContextMockCaller');
const { shouldBehaveLikeRegularContext } = require('./Context.behavior');
Initial GSN support (beta) (#1844) * Add base Context contract * Add GSNContext and tests * Add RelayHub deployment to tests * Add RelayProvider integration, complete GSNContext tests * Switch dependency to openzeppelin-gsn-provider * Add default txfee to provider * Add basic signing recipient * Sign more values * Add comment clarifying RelayHub's msg.data * Make context constructors internal * Rename SigningRecipient to GSNRecipientSignedData * Add ERC20Charge recipients * Harcode RelayHub address into GSNContext * Fix Solidity linter errors * Run server from binary, use gsn-helpers to fund it * Migrate to published @openzeppelin/gsn-helpers * Silence false-positive compiler warning * Use GSN helper assertions * Rename meta-tx to gsn, take out of drafts * Merge ERC20 charge recipients into a single one * Rename GSNRecipients to Bouncers * Add GSNBouncerUtils to decouple the bouncers from GSNRecipient * Add _upgradeRelayHub * Store RelayHub address using unstructored storage * Add IRelayHub * Add _withdrawDeposits to GSNRecipient * Add relayHub version to recipient * Make _acceptRelayedCall and _declineRelayedCall easier to use * Rename GSNBouncerUtils to GSNBouncerBase, make it IRelayRecipient * Improve GSNBouncerBase, make pre and post sender-protected and optional * Fix GSNBouncerERC20Fee, add tests * Add missing GSNBouncerSignature test * Override transferFrom in __unstable__ERC20PrimaryAdmin * Fix gsn dependencies in package.json * Rhub address slot reduced by 1 * Rename relay hub changed event * Use released gsn-provider * Run relayer with short sleep of 1s instead of 100ms * update package-lock.json * clear circle cache * use optimized gsn-provider * update to latest @openzeppelin/gsn-provider * replace with gsn dev provider * remove relay server * rename arguments in approveFunction * fix GSNBouncerSignature test * change gsn txfee * initialize development provider only once * update RelayHub interface * adapt to new IRelayHub.withdraw * update @openzeppelin/gsn-helpers * update relayhub singleton address * fix helper name * set up gsn provider for coverage too * lint * Revert "set up gsn provider for coverage too" This reverts commit 8a7b5be5f942002710cba148f249cb888ee8e373. * remove unused code * add gsn provider to coverage * move truffle contract options back out * increase gas limit for coverage * remove unreachable code * add more gas for GSNContext test * fix test suite name * rename GSNBouncerBase internal API * remove onlyRelayHub modifier * add explicit inheritance * remove redundant event * update name of bouncers error codes enums * add basic docs page for gsn contracts * make gsn directory all caps * add changelog entry * lint * enable test run to fail in coverage
6 years ago
describe('GSNRecipient', function () {
const [ payee, sender, newRelayHub ] = accounts;
Initial GSN support (beta) (#1844) * Add base Context contract * Add GSNContext and tests * Add RelayHub deployment to tests * Add RelayProvider integration, complete GSNContext tests * Switch dependency to openzeppelin-gsn-provider * Add default txfee to provider * Add basic signing recipient * Sign more values * Add comment clarifying RelayHub's msg.data * Make context constructors internal * Rename SigningRecipient to GSNRecipientSignedData * Add ERC20Charge recipients * Harcode RelayHub address into GSNContext * Fix Solidity linter errors * Run server from binary, use gsn-helpers to fund it * Migrate to published @openzeppelin/gsn-helpers * Silence false-positive compiler warning * Use GSN helper assertions * Rename meta-tx to gsn, take out of drafts * Merge ERC20 charge recipients into a single one * Rename GSNRecipients to Bouncers * Add GSNBouncerUtils to decouple the bouncers from GSNRecipient * Add _upgradeRelayHub * Store RelayHub address using unstructored storage * Add IRelayHub * Add _withdrawDeposits to GSNRecipient * Add relayHub version to recipient * Make _acceptRelayedCall and _declineRelayedCall easier to use * Rename GSNBouncerUtils to GSNBouncerBase, make it IRelayRecipient * Improve GSNBouncerBase, make pre and post sender-protected and optional * Fix GSNBouncerERC20Fee, add tests * Add missing GSNBouncerSignature test * Override transferFrom in __unstable__ERC20PrimaryAdmin * Fix gsn dependencies in package.json * Rhub address slot reduced by 1 * Rename relay hub changed event * Use released gsn-provider * Run relayer with short sleep of 1s instead of 100ms * update package-lock.json * clear circle cache * use optimized gsn-provider * update to latest @openzeppelin/gsn-provider * replace with gsn dev provider * remove relay server * rename arguments in approveFunction * fix GSNBouncerSignature test * change gsn txfee * initialize development provider only once * update RelayHub interface * adapt to new IRelayHub.withdraw * update @openzeppelin/gsn-helpers * update relayhub singleton address * fix helper name * set up gsn provider for coverage too * lint * Revert "set up gsn provider for coverage too" This reverts commit 8a7b5be5f942002710cba148f249cb888ee8e373. * remove unused code * add gsn provider to coverage * move truffle contract options back out * increase gas limit for coverage * remove unreachable code * add more gas for GSNContext test * fix test suite name * rename GSNBouncerBase internal API * remove onlyRelayHub modifier * add explicit inheritance * remove redundant event * update name of bouncers error codes enums * add basic docs page for gsn contracts * make gsn directory all caps * add changelog entry * lint * enable test run to fail in coverage
6 years ago
beforeEach(async function () {
this.recipient = await GSNRecipientMock.new();
});
it('returns the compatible RelayHub version', async function () {
expect(await this.recipient.relayHubVersion()).to.equal('1.0.0');
});
describe('get/set RelayHub', function () {
const singletonRelayHub = '0xD216153c06E857cD7f72665E0aF1d7D82172F494';
it('initially returns the singleton instance address', async function () {
expect(await this.recipient.getHubAddr()).to.equal(singletonRelayHub);
});
it('can be upgraded to a new RelayHub', async function () {
const { logs } = await this.recipient.upgradeRelayHub(newRelayHub);
expectEvent.inLogs(logs, 'RelayHubChanged', { oldRelayHub: singletonRelayHub, newRelayHub });
});
it('cannot upgrade to the same RelayHub', async function () {
await expectRevert(
this.recipient.upgradeRelayHub(singletonRelayHub),
'GSNRecipient: new RelayHub is the current one'
);
});
it('cannot upgrade to the zero address', async function () {
await expectRevert(
this.recipient.upgradeRelayHub(ZERO_ADDRESS), 'GSNRecipient: new RelayHub is the zero address'
);
});
context('with new RelayHub', function () {
beforeEach(async function () {
await this.recipient.upgradeRelayHub(newRelayHub);
});
it('returns the new instance address', async function () {
expect(await this.recipient.getHubAddr()).to.equal(newRelayHub);
});
});
});
context('when called directly', function () {
beforeEach(async function () {
this.context = this.recipient; // The Context behavior expects the contract in this.context
this.caller = await ContextMockCaller.new();
});
shouldBehaveLikeRegularContext(sender);
});
context('when receiving a relayed call', function () {
beforeEach(async function () {
await gsn.fundRecipient(web3, { recipient: this.recipient.address });
});
describe('msgSender', function () {
it('returns the relayed transaction original sender', async function () {
const { tx } = await this.recipient.msgSender({ from: sender, useGSN: true });
await expectEvent.inTransaction(tx, GSNRecipientMock, 'Sender', { sender });
});
});
describe('msgData', function () {
it('returns the relayed transaction original data', async function () {
const integerValue = new BN('42');
const stringValue = 'OpenZeppelin';
const callData = this.recipient.contract.methods.msgData(integerValue.toString(), stringValue).encodeABI();
// The provider doesn't properly estimate gas for a relayed call, so we need to manually set a higher value
const { tx } = await this.recipient.msgData(integerValue, stringValue, { gas: 1000000, useGSN: true });
await expectEvent.inTransaction(tx, GSNRecipientMock, 'Data', { data: callData, integerValue, stringValue });
});
});
});
Initial GSN support (beta) (#1844) * Add base Context contract * Add GSNContext and tests * Add RelayHub deployment to tests * Add RelayProvider integration, complete GSNContext tests * Switch dependency to openzeppelin-gsn-provider * Add default txfee to provider * Add basic signing recipient * Sign more values * Add comment clarifying RelayHub's msg.data * Make context constructors internal * Rename SigningRecipient to GSNRecipientSignedData * Add ERC20Charge recipients * Harcode RelayHub address into GSNContext * Fix Solidity linter errors * Run server from binary, use gsn-helpers to fund it * Migrate to published @openzeppelin/gsn-helpers * Silence false-positive compiler warning * Use GSN helper assertions * Rename meta-tx to gsn, take out of drafts * Merge ERC20 charge recipients into a single one * Rename GSNRecipients to Bouncers * Add GSNBouncerUtils to decouple the bouncers from GSNRecipient * Add _upgradeRelayHub * Store RelayHub address using unstructored storage * Add IRelayHub * Add _withdrawDeposits to GSNRecipient * Add relayHub version to recipient * Make _acceptRelayedCall and _declineRelayedCall easier to use * Rename GSNBouncerUtils to GSNBouncerBase, make it IRelayRecipient * Improve GSNBouncerBase, make pre and post sender-protected and optional * Fix GSNBouncerERC20Fee, add tests * Add missing GSNBouncerSignature test * Override transferFrom in __unstable__ERC20PrimaryAdmin * Fix gsn dependencies in package.json * Rhub address slot reduced by 1 * Rename relay hub changed event * Use released gsn-provider * Run relayer with short sleep of 1s instead of 100ms * update package-lock.json * clear circle cache * use optimized gsn-provider * update to latest @openzeppelin/gsn-provider * replace with gsn dev provider * remove relay server * rename arguments in approveFunction * fix GSNBouncerSignature test * change gsn txfee * initialize development provider only once * update RelayHub interface * adapt to new IRelayHub.withdraw * update @openzeppelin/gsn-helpers * update relayhub singleton address * fix helper name * set up gsn provider for coverage too * lint * Revert "set up gsn provider for coverage too" This reverts commit 8a7b5be5f942002710cba148f249cb888ee8e373. * remove unused code * add gsn provider to coverage * move truffle contract options back out * increase gas limit for coverage * remove unreachable code * add more gas for GSNContext test * fix test suite name * rename GSNBouncerBase internal API * remove onlyRelayHub modifier * add explicit inheritance * remove redundant event * update name of bouncers error codes enums * add basic docs page for gsn contracts * make gsn directory all caps * add changelog entry * lint * enable test run to fail in coverage
6 years ago
context('with deposited funds', async function () {
const amount = ether('1');
beforeEach(async function () {
await gsn.fundRecipient(web3, { recipient: this.recipient.address, amount });
});
it('funds can be withdrawn', async function () {
const balanceTracker = await balance.tracker(payee);
await this.recipient.withdrawDeposits(amount, payee);
expect(await balanceTracker.delta()).to.be.bignumber.equal(amount);
});
it('partial funds can be withdrawn', async function () {
const balanceTracker = await balance.tracker(payee);
await this.recipient.withdrawDeposits(amount.divn(2), payee);
expect(await balanceTracker.delta()).to.be.bignumber.equal(amount.divn(2));
});
it('reverts on overwithdrawals', async function () {
await expectRevert(this.recipient.withdrawDeposits(amount.addn(1), payee), 'insufficient funds');
});
});
});