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.soldiffbeforeafterbothno changes
tests/src/eth/util/helpers.tsdiffbeforeafterboth17import config from '../../config';17import config from '../../config';18import privateKey from '../../substrate/privateKey';18import privateKey from '../../substrate/privateKey';19import contractHelpersAbi from './contractHelpersAbi.json';19import contractHelpersAbi from './contractHelpersAbi.json';20import fs from 'fs';212022export const GAS_ARGS = { gas: 0x1000000, gasPrice: '0x01' };21export const GAS_ARGS = { gas: 0x1000000, gasPrice: '0x01' };2322176 };175 };177}176}178177179export async function deployFungibleContract(web3: Web3 & Web3HttpMarker, deployer: string) {178// export async function deployFungibleContract(web3: Web3 & Web3HttpMarker, deployer: string) {180179181 const sol = fs.readFileSync(__dirname + '/ERC721.sol').toString();180// const sol = fs.readFileSync(__dirname + '/ERC721.sol').toString();182181183 const compiled = compileContract('ERC721', sol);182// const compiled = compileContract('ERC721', sol);184 const ERC721 = new web3.eth.Contract(compiled.abi, undefined, {183// const ERC721 = new web3.eth.Contract(compiled.abi, undefined, {185 data: compiled.object,184// data: compiled.object,186 from: deployer,185// from: deployer,187 ...GAS_ARGS,186// ...GAS_ARGS,188 });187// });189 const fungible = await ERC721.deploy({ data: compiled.object }).send({from: deployer});188// const fungible = await ERC721.deploy({ data: compiled.object }).send({from: deployer});190189191 return fungible;190// return fungible;192}191// }193192194export async function deployFlipper(web3: Web3 & Web3HttpMarker, deployer: string) {193export async function deployFlipper(web3: Web3 & Web3HttpMarker, deployer: string) {195 const compiled = compileContract('Flipper', `194 const compiled = compileContract('Flipper', `