git.delta.rocks / unique-network / refs/commits / 54811ce8b19d

difftreelog

Contract sponsoring

str-mv2021-08-29parent: #70da175.patch.diff
in: master

4 files changed

addedtests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/eth/contractSponsoring.test.ts
@@ -0,0 +1,96 @@
+//
+// This file is subject to the terms and conditions defined in
+// file 'LICENSE', which is part of this source code package.
+//
+
+import privateKey from '../substrate/privateKey';
+import { expect } from 'chai';
+import { createCollectionExpectSuccess, 
+  createFungibleItemExpectSuccess, 
+  transferExpectSuccess, 
+  transferFromExpectSuccess, 
+  createItemExpectSuccess, 
+  setContractSponsoringRateLimitExpectSuccess,
+  enableContractSponsoringExpectSuccess,
+  setCollectionSponsorExpectSuccess,
+  confirmSponsorshipExpectSuccess,
+  enableContractSponsoringExpectFailure} from '../util/helpers';
+import { collectionIdToAddress, 
+  contractHelpers,
+  createEthAccountWithBalance,
+  createEthAccount,
+  transferBalanceToEth,
+  subToEth,
+  deployFlipper,
+  usingWeb3Http,
+  deployFungibleContract,
+  GAS_ARGS, itWeb3 } from './util/helpers';
+import waitNewBlocks from '../substrate/wait-new-blocks';
+import fungibleAbi from './fungibleAbi.json';
+import nonFungibleAbi from './nonFungibleAbi.json';
+
+describe.only('Sponsoring EVM contracts', () => {
+  itWeb3.skip('Sponsoring can be set by the address that has deployed the contract', async ({api, web3}) => {
+    await usingWeb3Http(async web3Http => {
+      const owner = await createEthAccountWithBalance(api, web3Http);
+      const fungible = await deployFungibleContract(web3Http, owner);
+      await waitNewBlocks(api, 1);
+      const helpers = contractHelpers(web3Http, owner);
+      await waitNewBlocks(api, 1);
+      expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.false;
+      await waitNewBlocks(api, 1);
+      await helpers.methods.toggleSponsoring(fungible.options.address, true).send({from: owner});
+      await waitNewBlocks(api, 1);
+      expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.true;
+    });
+  });
+
+  itWeb3.skip('Sponsoring cannot be set by the address that did not deployed the contract', async ({api, web3}) => {
+    await usingWeb3Http(async web3Http => {
+      const owner = await createEthAccountWithBalance(api, web3Http);
+      const notOwner = await createEthAccountWithBalance(api, web3Http);
+      const fungible = await deployFungibleContract(web3Http, owner);
+      await waitNewBlocks(api, 1);
+      const helpers = contractHelpers(web3Http, owner);
+      await waitNewBlocks(api, 1);
+      expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.false;
+      await waitNewBlocks(api, 1);
+      await expect(helpers.methods.toggleSponsoring(notOwner, true).send({from: notOwner})).to.rejected;
+      await waitNewBlocks(api, 1);
+      expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.false;
+    });
+  });
+
+  itWeb3('Sponsoring is set, an address that has no UNQ can send a transaction and it works', async ({api, web3}) => {
+    await usingWeb3Http(async web3Http => {
+      const alice = privateKey('//Alice');
+      const bob = privateKey('//Bob');
+      const collection = await createCollectionExpectSuccess({
+        mode: { type: 'Fungible', decimalPoints: 0 },
+      });
+      await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, alice.address);
+      const owner = await createEthAccountWithBalance(api, web3Http);
+      const notOwner = await createEthAccountWithBalance(api, web3Http);
+      await transferExpectSuccess(collection, 0, alice, { ethereum: notOwner } , 200, 'Fungible');
+      const address = collectionIdToAddress(collection);
+      const fungible = await deployFungibleContract(web3Http, address);
+      await transferBalanceToEth(api, alice, fungible.options.address);
+      await waitNewBlocks(api, 1);
+      const balanceBefore = await web3.eth.getBalance(fungible.options.address);
+
+      const helpers = contractHelpers(web3Http, address);
+      await waitNewBlocks(api, 1);
+      expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.false;
+      await waitNewBlocks(api, 1);
+      await helpers.methods.toggleSponsoring(fungible.options.address, true).send({from: owner});
+      await waitNewBlocks(api, 1);
+      expect(await helpers.methods.sponsoringEnabled(fungible.options.address).call()).to.be.true;
+
+//      await fungible.methods.approve(owner, 50).send({ from: owner });
+      await fungible.methods.transferFrom(notOwner, owner, 50).send({ from: notOwner });
+      const balanceAfter = await web3.eth.getBalance(fungible.options.address);
+      expect(+balanceAfter).to.lessThan(+balanceBefore);
+    });
+  });
+
+});
addedtests/src/eth/util/ERC721.soldiffbeforeafterboth
after · tests/src/eth/util/ERC721.sol
1contract ERC721 {2    uint8 _dummy = 0;3    address _dummy_addr = 0x0000000000000000000000000000000000000000;4    string _dummy_string = "";5    string stub_error =6        "this contract does not exists, code for collections is implemented at pallet side";78    event Transfer(9        address indexed from,10        address indexed to,11        uint256 indexed tokenId12    );1314    event Approval(15        address indexed owner,16        address indexed approved,17        uint256 indexed tokenId18    );1920    event ApprovalForAll(21        address indexed owner,22        address indexed operator,23        bool approved24    );2526	// 0x18160ddd27    function totalSupply() external view returns (uint256) {28        require(false, stub_error);29        return 0;30    }3132    function name() external view returns (string memory res_name) {33        require(false, stub_error);34        res_name = _dummy_string;35    }3637    function symbol() external view returns (string memory res_symbol) {38        require(false, stub_error);39        res_symbol = _dummy_string;40    }4142    function tokenURI(uint256 tokenId) external view returns (string memory) {43        require(false, stub_error);44        tokenId;45        return _dummy_string;46    }4748    function tokenByIndex(uint256 index) external view returns (uint256) {49        require(false, stub_error);50        index;51		return 0;52    }5354    function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256) {55        require(false, stub_error);56		owner;57		index;58		return 0;59    }6061    // 0x70a0823162    function balanceOf(address owner) external view returns (uint256) {63        require(false, stub_error);64        owner;65        return 0;66    }6768    // 0x6352211e69    function ownerOf(uint256 tokenId) external view returns (address) {70        require(false, stub_error);71        tokenId;72        return _dummy_addr;73    }7475    // 0xb88d4fde76    function safeTransferFrom(77        address from,78        address to,79        uint256 tokenId,80        bytes calldata data81    ) external payable {82        require(false, stub_error);83        from;84        to;85        tokenId;86        data;87    }8889    // 0x42842e0e90    function safeTransferFrom(91        address from,92        address to,93        uint256 tokenId94    ) external payable {95        require(false, stub_error);96        from;97        to;98        tokenId;99    }100101    // 0x23b872dd102    function transferFrom(103        address from,104        address to,105        uint256 tokenId106    ) external payable {107        require(false, stub_error);108        from;109        to;110        tokenId;111    }112113    // 0x095ea7b3114    function approve(address approved, uint256 tokenId) external payable {115        require(false, stub_error);116        approved;117        tokenId;118    }119120    // 0xa22cb465121    function setApprovalForAll(address operator, bool approved) external {122        require(false, stub_error);123        operator;124        approved;125        _dummy = 0;126    }127128    // 0x081812fc129    function getApproved(uint256 tokenId) external view returns (address) {130        require(false, stub_error);131        tokenId;132        return _dummy_addr;133    }134135    // 0xe985e9c5136    function isApprovedForAll(address owner, address operator)137        external138        view139        returns (bool)140    {141        require(false, stub_error);142        owner;143        operator;144        return false;145    }146147    // 0x01ffc9a7148    function supportsInterface(bytes4 interfaceID) public pure returns (bool) {149        return150            // ERC721151            interfaceID == 0x80ac58cd ||152            // ERC721Metadata153            interfaceID == 0x5b5e139f ||154            // ERC721Enumerable155            interfaceID == 0x780e9d63 ||156            // ERC165157            interfaceID == 0x01ffc9a7;158    }159}
modifiedtests/src/eth/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/eth/util/helpers.ts
+++ b/tests/src/eth/util/helpers.ts
@@ -17,6 +17,7 @@
 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' };
 
@@ -175,6 +176,21 @@
   };
 }
 
+export async function deployFungibleContract(web3: Web3 & Web3HttpMarker, deployer: string) {
+
+  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});
+
+  return fungible;
+}
+
 export async function deployFlipper(web3: Web3 & Web3HttpMarker, deployer: string) {
   const compiled = compileContract('Flipper', `
     contract Flipper {
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -95,23 +95,6 @@
   checkMsgSysMethod: string;
 }
 
-export interface IFungibleTokenDataType {
-  Value: number;
-}
-
-export interface IChainLimits {
-  CollectionNumbersLimit: number;
-	AccountTokenOwnershipLimit: number;
-	CollectionsAdminsLimit: number;
-	CustomDataLimit: number;
-	NftSponsorTransferTimeout: number;
-	FungibleSponsorTransferTimeout: number;
-	RefungibleSponsorTransferTimeout: number;
-	OffchainSchemaLimit: number;
-	VariableOnChainSchemaLimit: number;
-	ConstOnChainSchemaLimit: number;
-}
-
 export interface IReFungibleTokenDataType {
   Owner: IReFungibleOwner[];
   ConstData: number[];
@@ -469,11 +452,11 @@
   });
 }
 
-export async function removeCollectionSponsorExpectFailure(collectionId: number, senderSeed = '//Alice') {
+export async function removeCollectionSponsorExpectFailure(collectionId: number) {
   await usingApi(async (api) => {
 
     // Run the transaction
-    const alicePrivateKey = privateKey(senderSeed);
+    const alicePrivateKey = privateKey('//Alice');
     const tx = api.tx.nft.removeCollectionSponsor(collectionId);
     await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;
   });
@@ -782,15 +765,6 @@
   });
 }
 
-export async function addCollectionAdminExpectSuccess(sender: IKeyringPair, collectionId: number, address: IKeyringPair) {
-  await usingApi(async (api) => {
-    const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(address.address));
-    const events = await submitTransactionAsync(sender, changeAdminTx);
-    const result = getCreateCollectionResult(events);
-    expect(result.success).to.be.true;
-  });
-}
-
 export async function
 scheduleTransferExpectSuccess(
   collectionId: number,
@@ -1040,17 +1014,6 @@
   await usingApi(async (api) => {
     // Run the transaction
     const tx = api.tx.nft.setMintPermission(collectionId, enabled);
-    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
-    const result = getCreateCollectionResult(events);
-    // tslint:disable-next-line:no-unused-expression
-    expect(result.success).to.be.false;
-  });
-}
-
-export async function setChainLimitsExpectFailure(sender: IKeyringPair, limits: IChainLimits) {
-  await usingApi(async (api) => {
-    // Run the transaction
-    const tx = api.tx.nft.setChainLimits(limits);
     const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
     const result = getCreateCollectionResult(events);
     // tslint:disable-next-line:no-unused-expression