difftreelog
Sponsoring tests
in: master
3 files changed
tests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth556import privateKey from '../substrate/privateKey';6import privateKey from '../substrate/privateKey';7import { expect } from 'chai';7import { expect } from 'chai';8import { createCollectionExpectSuccess, 8import { 9 createFungibleItemExpectSuccess, 10 transferExpectSuccess, 11 transferFromExpectSuccess, 12 createItemExpectSuccess, 13 setContractSponsoringRateLimitExpectSuccess,14 enableContractSponsoringExpectSuccess,15 setCollectionSponsorExpectSuccess,16 confirmSponsorshipExpectSuccess,17 enableContractSponsoringExpectFailure} from '../util/helpers';18import { collectionIdToAddress, 19 contractHelpers,9 contractHelpers,20 createEthAccountWithBalance,10 createEthAccountWithBalance,21 createEthAccount,11 createEthAccount,22 transferBalanceToEth,12 transferBalanceToEth,23 subToEth,24 deployFlipper,13 deployFlipper,25 usingWeb3Http,14 usingWeb3Http,26 deployFungibleContract,15 itWeb3 } from './util/helpers';27 GAS_ARGS, itWeb3 } from './util/helpers';28import waitNewBlocks from '../substrate/wait-new-blocks';16import waitNewBlocks from '../substrate/wait-new-blocks';29import fungibleAbi from './fungibleAbi.json';30import nonFungibleAbi from './nonFungibleAbi.json';311732describe.only('Sponsoring EVM contracts', () => {18describe.only('Sponsoring EVM contracts', () => {33 itWeb3.skip('Sponsoring can be set by the address that has deployed the contract', async ({api, web3}) => {19 itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api}) => {34 await usingWeb3Http(async web3Http => {20 await usingWeb3Http(async web3Http => {35 const owner = await createEthAccountWithBalance(api, web3Http);21 const owner = await createEthAccountWithBalance(api, web3Http);36 const fungible = await deployFungibleContract(web3Http, owner);22 const flipper = await deployFlipper(web3Http, owner);37 await waitNewBlocks(api, 1);23 await waitNewBlocks(api, 1);38 const helpers = contractHelpers(web3Http, owner);24 const helpers = contractHelpers(web3Http, owner);39 await waitNewBlocks(api, 1);25 await waitNewBlocks(api, 1);40 expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.false;26 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;41 await waitNewBlocks(api, 1);27 await waitNewBlocks(api, 1);42 await helpers.methods.toggleSponsoring(fungible.options.address, true).send({from: owner});28 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});43 await waitNewBlocks(api, 1);29 await waitNewBlocks(api, 1);44 expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.true;30 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;45 });31 });46 });32 });473348 itWeb3.skip('Sponsoring cannot be set by the address that did not deployed the contract', async ({api, web3}) => {34 itWeb3('Sponsoring cannot be set by the address that did not deployed the contract', async ({api}) => {49 await usingWeb3Http(async web3Http => {35 await usingWeb3Http(async web3Http => {50 const owner = await createEthAccountWithBalance(api, web3Http);36 const owner = await createEthAccountWithBalance(api, web3Http);51 const notOwner = await createEthAccountWithBalance(api, web3Http);37 const notOwner = await createEthAccountWithBalance(api, web3Http);52 const fungible = await deployFungibleContract(web3Http, owner);38 const flipper = await deployFlipper(web3Http, owner);53 await waitNewBlocks(api, 1);39 await waitNewBlocks(api, 1);54 const helpers = contractHelpers(web3Http, owner);40 const helpers = contractHelpers(web3Http, owner);55 await waitNewBlocks(api, 1);41 await waitNewBlocks(api, 1);56 expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.false;42 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;57 await waitNewBlocks(api, 1);43 await waitNewBlocks(api, 1);58 await expect(helpers.methods.toggleSponsoring(notOwner, true).send({from: notOwner})).to.rejected;44 await expect(helpers.methods.toggleSponsoring(notOwner, true).send({from: notOwner})).to.rejected;59 await waitNewBlocks(api, 1);45 await waitNewBlocks(api, 1);60 expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.false;46 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;61 });47 });62 });48 });634964 itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works', async ({api, web3}) => {50 itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease', async ({api, web3}) => {65 await usingWeb3Http(async web3Http => {51 await usingWeb3Http(async web3Http => {66 const alice = privateKey('//Alice');52 const alice = privateKey('//Alice');67 const bob = privateKey('//Bob');5368 const collection = await createCollectionExpectSuccess({69 mode: { type: 'Fungible', decimalPoints: 0 },70 });71 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, alice.address);72 const owner = await createEthAccountWithBalance(api, web3Http);54 const owner = await createEthAccountWithBalance(api, web3Http);73 const notOwner = await createEthAccountWithBalance(api, web3Http);55 const caller = createEthAccount(web3Http);74 await transferExpectSuccess(collection, 0, alice, { ethereum: notOwner } , 200, 'Fungible');56 const originalCallerBalance = await web3.eth.getBalance(caller);75 const address = collectionIdToAddress(collection);57 expect(originalCallerBalance).to.be.equal('0');5876 const fungible = await deployFungibleContract(web3Http, address);59 const flipper = await deployFlipper(web3Http, owner);77 await transferBalanceToEth(api, alice, fungible.options.address);78 await waitNewBlocks(api, 1);60 await waitNewBlocks(api, 1);79 const balanceBefore = await web3.eth.getBalance(fungible.options.address);61 62 const helpers = contractHelpers(web3Http, owner);63 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });64 await waitNewBlocks(api, 1);65 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });66 await waitNewBlocks(api, 1);806781 const helpers = contractHelpers(web3Http, address);68 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;69 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});82 await waitNewBlocks(api, 1);70 await waitNewBlocks(api, 1);83 expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.false;71 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});84 await waitNewBlocks(api, 1);72 await waitNewBlocks(api, 1);85 await helpers.methods.toggleSponsoring(fungible.options.address, true).send({from: owner});73 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;7475 await transferBalanceToEth(api, alice, flipper.options.address);76 await waitNewBlocks(api, 2);7778 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);79 expect(originalFlipperBalance).to.be.not.equal('0');8081 await flipper.methods.flip().send({ from: caller });86 await waitNewBlocks(api, 1);82 await waitNewBlocks(api, 1);87 expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.true;83 expect(await flipper.methods.getValue().call()).to.be.true;888489// await fungible.methods.approve(owner, 50).send({ from: owner });85 // Balance should be taken from flipper instead of caller90 await fungible.methods.transferFrom(notOwner, owner, 50).send({ from: notOwner });91 const balanceAfter = await web3.eth.getBalance(fungible.options.address);86 const balanceAfter = await web3.eth.getBalance(flipper.options.address);92 expect(+balanceAfter).to.lessThan(+balanceBefore);87 expect(+balanceAfter).to.be.lessThan(+originalFlipperBalance);93 });88 });94 });89 });959091 itWeb3('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({api, web3}) => {92 await usingWeb3Http(async web3Http => {93 const alice = privateKey('//Alice');9495 const owner = await createEthAccountWithBalance(api, web3Http);96 const caller = createEthAccount(web3Http);97 const originalCallerBalance = await web3.eth.getBalance(caller);98 expect(originalCallerBalance).to.be.equal('0');99100 const flipper = await deployFlipper(web3Http, owner);101 await waitNewBlocks(api, 1);102 103 const helpers = contractHelpers(web3Http, owner);104 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });105 await waitNewBlocks(api, 1);106 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });107 await waitNewBlocks(api, 1);108109 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;110 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});111 await waitNewBlocks(api, 1);112 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});113 await waitNewBlocks(api, 1);114 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;115116 await transferBalanceToEth(api, alice, flipper.options.address);117 await waitNewBlocks(api, 2);118119 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);120 expect(originalFlipperBalance).to.be.not.equal('0');121122 await flipper.methods.flip().send({ from: caller });123 await waitNewBlocks(api, 1);124 expect(await flipper.methods.getValue().call()).to.be.true;125126 expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);127 });128 });129130 itWeb3('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({api, web3}) => {131 await usingWeb3Http(async web3Http => {132 const alice = privateKey('//Alice');133134 const owner = await createEthAccountWithBalance(api, web3Http);135 const caller = await createEthAccountWithBalance(api, web3Http);136 const originalCallerBalance = await web3.eth.getBalance(caller);137138 const flipper = await deployFlipper(web3Http, owner);139 await waitNewBlocks(api, 1);140 141 const helpers = contractHelpers(web3Http, owner);142 await helpers.methods.toggleAllowlist(flipper.options.address, true).send({ from: owner });143 await waitNewBlocks(api, 1);144 await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({ from: owner });145 await waitNewBlocks(api, 1);146147 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;148 await helpers.methods.toggleSponsoring(flipper.options.address, true).send({from: owner});149 await waitNewBlocks(api, 1);150 await helpers.methods.setSponsoringRateLimit(flipper.options.address, 10).send({from: owner});151 await waitNewBlocks(api, 1);152 expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;153154 await transferBalanceToEth(api, alice, flipper.options.address);155 await waitNewBlocks(api, 2);156157 const originalFlipperBalance = await web3.eth.getBalance(flipper.options.address);158 expect(originalFlipperBalance).to.be.not.equal('0');159160 await flipper.methods.flip().send({ from: caller });161 await waitNewBlocks(api, 1);162 expect(await flipper.methods.getValue().call()).to.be.true;163 expect(await web3.eth.getBalance(caller)).to.be.equals(originalCallerBalance);164165 await flipper.methods.flip().send({ from: caller });166 await waitNewBlocks(api, 1);167 expect(await web3.eth.getBalance(caller)).to.be.not.equals(originalCallerBalance);168 });169 });96});170});97171tests/src/eth/util/ERC721.soldiffbeforeafterboth--- a/tests/src/eth/util/ERC721.sol
+++ /dev/null
@@ -1,159 +0,0 @@
-contract ERC721 {
- uint8 _dummy = 0;
- address _dummy_addr = 0x0000000000000000000000000000000000000000;
- string _dummy_string = "";
- string stub_error =
- "this contract does not exists, code for collections is implemented at pallet side";
-
- event Transfer(
- address indexed from,
- address indexed to,
- uint256 indexed tokenId
- );
-
- event Approval(
- address indexed owner,
- address indexed approved,
- uint256 indexed tokenId
- );
-
- event ApprovalForAll(
- address indexed owner,
- address indexed operator,
- bool approved
- );
-
- // 0x18160ddd
- function totalSupply() external view returns (uint256) {
- require(false, stub_error);
- return 0;
- }
-
- function name() external view returns (string memory res_name) {
- require(false, stub_error);
- res_name = _dummy_string;
- }
-
- function symbol() external view returns (string memory res_symbol) {
- require(false, stub_error);
- res_symbol = _dummy_string;
- }
-
- function tokenURI(uint256 tokenId) external view returns (string memory) {
- require(false, stub_error);
- tokenId;
- return _dummy_string;
- }
-
- function tokenByIndex(uint256 index) external view returns (uint256) {
- require(false, stub_error);
- index;
- return 0;
- }
-
- function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256) {
- require(false, stub_error);
- owner;
- index;
- return 0;
- }
-
- // 0x70a08231
- function balanceOf(address owner) external view returns (uint256) {
- require(false, stub_error);
- owner;
- return 0;
- }
-
- // 0x6352211e
- function ownerOf(uint256 tokenId) external view returns (address) {
- require(false, stub_error);
- tokenId;
- return _dummy_addr;
- }
-
- // 0xb88d4fde
- function safeTransferFrom(
- address from,
- address to,
- uint256 tokenId,
- bytes calldata data
- ) external payable {
- require(false, stub_error);
- from;
- to;
- tokenId;
- data;
- }
-
- // 0x42842e0e
- function safeTransferFrom(
- address from,
- address to,
- uint256 tokenId
- ) external payable {
- require(false, stub_error);
- from;
- to;
- tokenId;
- }
-
- // 0x23b872dd
- function transferFrom(
- address from,
- address to,
- uint256 tokenId
- ) external payable {
- require(false, stub_error);
- from;
- to;
- tokenId;
- }
-
- // 0x095ea7b3
- function approve(address approved, uint256 tokenId) external payable {
- require(false, stub_error);
- approved;
- tokenId;
- }
-
- // 0xa22cb465
- function setApprovalForAll(address operator, bool approved) external {
- require(false, stub_error);
- operator;
- approved;
- _dummy = 0;
- }
-
- // 0x081812fc
- function getApproved(uint256 tokenId) external view returns (address) {
- require(false, stub_error);
- tokenId;
- return _dummy_addr;
- }
-
- // 0xe985e9c5
- function isApprovedForAll(address owner, address operator)
- external
- view
- returns (bool)
- {
- require(false, stub_error);
- owner;
- operator;
- return false;
- }
-
- // 0x01ffc9a7
- function supportsInterface(bytes4 interfaceID) public pure returns (bool) {
- return
- // ERC721
- interfaceID == 0x80ac58cd ||
- // ERC721Metadata
- interfaceID == 0x5b5e139f ||
- // ERC721Enumerable
- interfaceID == 0x780e9d63 ||
- // ERC165
- interfaceID == 0x01ffc9a7;
- }
-}
tests/src/eth/util/helpers.tsdiffbeforeafterboth--- a/tests/src/eth/util/helpers.ts
+++ b/tests/src/eth/util/helpers.ts
@@ -17,7 +17,6 @@
import config from '../../config';
import privateKey from '../../substrate/privateKey';
import contractHelpersAbi from './contractHelpersAbi.json';
-import fs from 'fs';
export const GAS_ARGS = { gas: 0x1000000, gasPrice: '0x01' };
@@ -176,20 +175,20 @@
};
}
-export async function deployFungibleContract(web3: Web3 & Web3HttpMarker, deployer: string) {
+// export async function deployFungibleContract(web3: Web3 & Web3HttpMarker, deployer: string) {
- const sol = fs.readFileSync(__dirname + '/ERC721.sol').toString();
+// const sol = fs.readFileSync(__dirname + '/ERC721.sol').toString();
- const compiled = compileContract('ERC721', sol);
- const ERC721 = new web3.eth.Contract(compiled.abi, undefined, {
- data: compiled.object,
- from: deployer,
- ...GAS_ARGS,
- });
- const fungible = await ERC721.deploy({ data: compiled.object }).send({from: deployer});
+// const compiled = compileContract('ERC721', sol);
+// const ERC721 = new web3.eth.Contract(compiled.abi, undefined, {
+// data: compiled.object,
+// from: deployer,
+// ...GAS_ARGS,
+// });
+// const fungible = await ERC721.deploy({ data: compiled.object }).send({from: deployer});
- return fungible;
-}
+// return fungible;
+// }
export async function deployFlipper(web3: Web3 & Web3HttpMarker, deployer: string) {
const compiled = compileContract('Flipper', `