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
--- /dev/null
+++ b/tests/src/eth/util/ERC721.sol
@@ -0,0 +1,159 @@
+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;
+    }
+}
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
95 checkMsgSysMethod: string;95 checkMsgSysMethod: string;
96}96}
97
98export interface IFungibleTokenDataType {
99 Value: number;
100}
101
102export interface IChainLimits {
103 CollectionNumbersLimit: number;
104 AccountTokenOwnershipLimit: number;
105 CollectionsAdminsLimit: number;
106 CustomDataLimit: number;
107 NftSponsorTransferTimeout: number;
108 FungibleSponsorTransferTimeout: number;
109 RefungibleSponsorTransferTimeout: number;
110 OffchainSchemaLimit: number;
111 VariableOnChainSchemaLimit: number;
112 ConstOnChainSchemaLimit: number;
113}
11497
115export interface IReFungibleTokenDataType {98export interface IReFungibleTokenDataType {
116 Owner: IReFungibleOwner[];99 Owner: IReFungibleOwner[];
469 });452 });
470}453}
471454
472export async function removeCollectionSponsorExpectFailure(collectionId: number, senderSeed = '//Alice') {455export async function removeCollectionSponsorExpectFailure(collectionId: number) {
473 await usingApi(async (api) => {456 await usingApi(async (api) => {
474457
475 // Run the transaction458 // Run the transaction
476 const alicePrivateKey = privateKey(senderSeed);459 const alicePrivateKey = privateKey('//Alice');
477 const tx = api.tx.nft.removeCollectionSponsor(collectionId);460 const tx = api.tx.nft.removeCollectionSponsor(collectionId);
478 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;461 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;
479 });462 });
782 });765 });
783}766}
784
785export async function addCollectionAdminExpectSuccess(sender: IKeyringPair, collectionId: number, address: IKeyringPair) {
786 await usingApi(async (api) => {
787 const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, normalizeAccountId(address.address));
788 const events = await submitTransactionAsync(sender, changeAdminTx);
789 const result = getCreateCollectionResult(events);
790 expect(result.success).to.be.true;
791 });
792}
793767
794export async function768export async function
795scheduleTransferExpectSuccess(769scheduleTransferExpectSuccess(
1047 });1021 });
1048}1022}
1049
1050export async function setChainLimitsExpectFailure(sender: IKeyringPair, limits: IChainLimits) {
1051 await usingApi(async (api) => {
1052 // Run the transaction
1053 const tx = api.tx.nft.setChainLimits(limits);
1054 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
1055 const result = getCreateCollectionResult(events);
1056 // tslint:disable-next-line:no-unused-expression
1057 expect(result.success).to.be.false;
1058 });
1059}
10601023
1061export async function isWhitelisted(collectionId: number, address: string) {1024export async function isWhitelisted(collectionId: number, address: string) {
1062 let whitelisted = false;1025 let whitelisted = false;