git.delta.rocks / unique-network / refs/commits / 74c60c798ed6

difftreelog

CORE-300 Add tests

Trubnikov Sergey2022-03-18parent: #d030d8a.patch.diff
in: master

3 files changed

modifiedpallets/unique/src/tests.rsdiffbeforeafterboth
--- a/pallets/unique/src/tests.rs
+++ b/pallets/unique/src/tests.rs
@@ -18,6 +18,7 @@
 use super::*;
 use crate::mock::*;
 use crate::{AccessMode, CollectionMode};
+use sp_runtime::AccountId32;
 use up_data_structs::{
 	COLLECTION_NUMBER_LIMIT, CollectionId, CreateItemData, CreateFungibleData, CreateNftData,
 	CreateReFungibleData, MAX_DECIMAL_POINTS, COLLECTION_ADMINS_LIMIT, MetaUpdatePermission,
@@ -2854,3 +2855,34 @@
 		);
 	});
 }
+
+#[test]
+fn collection_sponsoring() {
+	new_test_ext().execute_with(|| {
+		// default_limits();
+		let user1 = 1_u64;
+		let user2 = 777_u64;
+		let origin1 = Origin::signed(user1);
+		let origin2 = Origin::signed(user2);
+		let account2 = account(user2);
+		
+		let collection_id =
+		create_test_collection_for_owner(&CollectionMode::NFT, user1, CollectionId(1));
+		assert_ok!(TemplateModule::set_collection_sponsor(origin1.clone(), collection_id, user1));
+		assert_ok!(TemplateModule::confirm_sponsorship(origin1.clone(), collection_id));
+
+		// Expect error while have no permissions
+		assert!(TemplateModule::create_item(origin2.clone(), collection_id, account2.clone(), default_nft_data().into()).is_err());
+
+		assert_ok!(TemplateModule::set_public_access_mode(origin1.clone(), collection_id, AccessMode::AllowList));
+		assert_ok!(TemplateModule::add_to_allow_list(origin1.clone(), collection_id, account2.clone()));
+		assert_ok!(TemplateModule::set_mint_permission(origin1.clone(), collection_id, true));
+
+		assert_eq!(<pallet_balances::Pallet<Test>>::free_balance(user2), 0);
+		let balance_before = <pallet_balances::Pallet<Test>>::free_balance(user1);
+		
+		assert_ok!(TemplateModule::create_item(origin2, collection_id, account2, default_nft_data().into()));
+		let balance_after = <pallet_balances::Pallet<Test>>::free_balance(user1);
+		assert_ne!(balance_before, balance_after);
+	});
+}
\ No newline at end of file
modifiedtests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth
--- a/tests/src/eth/contractSponsoring.test.ts
+++ b/tests/src/eth/contractSponsoring.test.ts
@@ -24,7 +24,22 @@
   itWeb3,
   SponsoringMode,
   createEthAccount,
+  collectionIdToAddress,
+  GAS_ARGS,
+  normalizeEvents,
 } from './util/helpers';
+import {
+  createCollectionExpectSuccess,
+  createItemExpectSuccess,
+  getCreateCollectionResult,
+  normalizeAccountId,
+  toSubstrateAddress,
+} from '../util/helpers';
+import nonFungibleAbi from './nonFungibleAbi.json';
+import {
+  submitTransactionAsync,
+} from '../substrate/substrate-api';
+import { evmToAddress } from '@polkadot/util-crypto';
 
 describe('Sponsoring EVM contracts', () => {
   itWeb3('Sponsoring can be set by the address that has deployed the contract', async ({api, web3}) => {
@@ -199,4 +214,104 @@
     const helpers = contractHelpers(web3, owner);
     expect(await helpers.methods.getSponsoringRateLimit(flipper.options.address).call()).to.be.equals('7200');
   });
+
+
+
+
+
+
+
+  
+  itWeb3.only('Sponsoring evm address from substrate collection', async ({api, web3}) => {
+    const owner = privateKey('//Alice');
+    const userEth = createEthAccount(web3);
+    const userSub = evmToAddress(userEth);
+    const collectionId = await createCollectionExpectSuccess();
+
+    {
+      const tx = api.tx.unique.setCollectionSponsor(collectionId, owner.address);
+      const events = await submitTransactionAsync(owner, tx);
+      const result = getCreateCollectionResult(events);
+      expect(result.success).to.be.true;
+    }
+    {
+      const tx = api.tx.unique.confirmSponsorship(collectionId);
+      const events = await submitTransactionAsync(owner, tx);
+      const result = getCreateCollectionResult(events);
+      expect(result.success).to.be.true;
+    }
+
+    const address = collectionIdToAddress(collectionId);
+    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: userEth, ...GAS_ARGS});
+    const receiver = createEthAccount(web3);
+
+    {
+      const nextTokenId = await contract.methods.nextTokenId().call();
+      expect(nextTokenId).to.be.equal('1');
+      // const result = await contract.methods.mintWithTokenURI(
+      //   receiver,
+      //   nextTokenId,
+      //   'Test URI',
+      // ).send({from: userEth});
+      // const events = normalizeEvents(result.events);
+
+      // expect(events).to.be.deep.equal([
+      //   {
+      //     address,
+      //     event: 'Transfer',
+      //     args: {
+      //       from: '0x0000000000000000000000000000000000000000',
+      //       to: receiver,
+      //       tokenId: nextTokenId,
+      //     },
+      //   },
+      // ]);
+
+      // expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');
+    }
+
+    {
+      const tx = api.tx.unique.setPublicAccessMode(collectionId, 'AllowList');
+      const events = await submitTransactionAsync(owner, tx);
+      const result = getCreateCollectionResult(events);
+      expect(result.success).to.be.true;
+    }
+    {
+      const tx = api.tx.unique.addToAllowList(collectionId, {Ethereum: userEth});
+      const events = await submitTransactionAsync(owner, tx);
+      const result = getCreateCollectionResult(events);
+      expect(result.success).to.be.true;
+    }
+    {
+      const tx = api.tx.unique.setMintPermission(collectionId, true);
+      const events = await submitTransactionAsync(owner, tx);
+      const result = getCreateCollectionResult(events);
+      expect(result.success).to.be.true;
+    }
+    {
+      const nextTokenId = await contract.methods.nextTokenId().call();
+      expect(nextTokenId).to.be.equal('1');
+      // const result = await contract.methods.mintWithTokenURI(
+      //   receiver,
+      //   nextTokenId,
+      //   'Test URI',
+      // ).send({from: userEth});
+      // const events = normalizeEvents(result.events);
+
+      // expect(events).to.be.deep.equal([
+      //   {
+      //     address,
+      //     event: 'Transfer',
+      //     args: {
+      //       from: '0x0000000000000000000000000000000000000000',
+      //       to: receiver,
+      //       tokenId: nextTokenId,
+      //     },
+      //   },
+      // ]);
+
+      // expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');
+    }
+
+  });
 });
modifiedtests/src/eth/util/helpers.tsdiffbeforeafterboth
241 return collector;241 return collector;
242}242}
243243
244/**
245 * pallet evm_contract_helpers
246 * @param web3
247 * @param caller - eth address
248 * @returns
249 */
244export function contractHelpers(web3: Web3, caller: string) {250export function contractHelpers(web3: Web3, caller: string) {
245 return new web3.eth.Contract(contractHelpersAbi as any, '0x842899ECF380553E8a4de75bF534cdf6fBF64049', {from: caller, ...GAS_ARGS});251 return new web3.eth.Contract(contractHelpersAbi as any, '0x842899ECF380553E8a4de75bF534cdf6fBF64049', {from: caller, ...GAS_ARGS});
246}252}