git.delta.rocks / unique-network / refs/commits / 8b9e255fd2b8

difftreelog

feat Add `collection_admins` method to eth collection.

Trubnikov Sergey2022-09-16parent: #2ffba98.patch.diff
in: master

2 files changed

modifiedtests/src/eth/collectionAdmin.test.tsdiffbeforeafterboth
--- a/tests/src/eth/collectionAdmin.test.ts
+++ b/tests/src/eth/collectionAdmin.test.ts
@@ -14,7 +14,8 @@
 // along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
 
 import {IKeyringPair} from '@polkadot/types/types';
-import {usingEthPlaygrounds, itEth, expect, EthUniqueHelper} from './util';
+import {IEthCrossAccountId} from '../util/playgrounds/types';
+import {usingEthPlaygrounds, itEth, expect, EthUniqueHelper} from './util/playgrounds';
 
 async function recordEthFee(helper: EthUniqueHelper, userAddress: string, call: () => Promise<any>) {
   const before = await helper.balance.getSubstrate(helper.address.ethToSubstrate(userAddress));
@@ -93,25 +94,25 @@
     await collectionEvm.methods.addCollectionAdmin(newAdmin).send();
     expect(await collectionEvm.methods.isOwnerOrAdmin(newAdmin).call()).to.be.true;
   });
-
-  // itEth.skip('Check adminlist', async ({helper, privateKey}) => {
-  //   const owner = await helper.eth.createAccountWithBalance(donor);
+  
+  itEth('Check adminlist', async ({helper, privateKey}) => {
+    const owner = await helper.eth.createAccountWithBalance(donor);
         
-  //   const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');
-  //   const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
+    const {collectionAddress, collectionId} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');
+    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
 
-  //   const admin1 = helper.eth.createAccount();
-  //   const admin2 = await privateKey('admin');
-  //   await collectionEvm.methods.addCollectionAdmin(admin1).send();
-  //   await collectionEvm.methods.addCollectionAdminSubstrate(admin2.addressRaw).send();
+    const admin1 = helper.eth.createAccount();
+    const admin2 = privateKey('admin');
+    await collectionEvm.methods.addCollectionAdmin(admin1).send();
+    await collectionEvm.methods.addCollectionAdminSubstrate(admin2.addressRaw).send();
 
-  //   const adminListRpc = await helper.collection.getAdmins(collectionId);
-  //   let adminListEth = await collectionEvm.methods.collectionAdmins().call();
-  //   adminListEth = adminListEth.map((element: IEthCrossAccountId) => {
-  //     return helper.address.convertCrossAccountFromEthCrossAcoount(element);
-  //   });
-  //   expect(adminListRpc).to.be.like(adminListEth);
-  // });
+    const adminListRpc = await helper.collection.getAdmins(collectionId);
+    let adminListEth = await collectionEvm.methods.collectionAdmins().call();
+    adminListEth = adminListEth.map((element: IEthCrossAccountId) => {
+      return helper.address.convertCrossAccountFromEthCrossAcoount(element);
+    });
+    expect(adminListRpc).to.be.like(adminListEth);
+  });  
     
   itEth('(!negative tests!) Add admin by ADMIN is not allowed', async ({helper}) => {
     const owner = await helper.eth.createAccountWithBalance(donor);
modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
77
8import {ApiPromise, WsProvider, Keyring} from '@polkadot/api';8import {ApiPromise, WsProvider, Keyring} from '@polkadot/api';
9import {ApiInterfaceEvents, SignerOptions} from '@polkadot/api/types';9import {ApiInterfaceEvents, SignerOptions} from '@polkadot/api/types';
10import {encodeAddress, decodeAddress, keccakAsHex, evmToAddress, addressToEvm} from '@polkadot/util-crypto';10import {encodeAddress, decodeAddress, keccakAsHex, evmToAddress, addressToEvm, base58Encode, blake2AsU8a} from '@polkadot/util-crypto';
11import {IKeyringPair} from '@polkadot/types/types';11import {IKeyringPair} from '@polkadot/types/types';
12import {hexToU8a} from '@polkadot/util/hex';
13import {u8aConcat} from '@polkadot/util/u8a';
12import {14import {
13 IApiListeners,15 IApiListeners,
14 IBlock,16 IBlock,
2368 return CrossAccountId.translateSubToEth(subAddress);2370 return CrossAccountId.translateSubToEth(subAddress);
2369 }2371 }
2372
2373 /**
2374 * Encode key to substrate address
2375 * @param key key for encoding address
2376 * @param ss58Format prefix for encoding to the address of the corresponding network
2377 * @returns encoded substrate address
2378 */
2379 encodeSubstrateAddress (key: Uint8Array | string | bigint, ss58Format = 42): string {
2380 const u8a :Uint8Array = typeof key === 'string'
2381 ? hexToU8a(key)
2382 : typeof key === 'bigint'
2383 ? hexToU8a(key.toString(16))
2384 : key;
2385
2386 if (ss58Format < 0 || ss58Format > 16383 || [46, 47].includes(ss58Format)) {
2387 throw new Error(`ss58Format is not valid, received ${typeof ss58Format} "${ss58Format}"`);
2388 }
2389
2390 const allowedDecodedLengths = [1, 2, 4, 8, 32, 33];
2391 if (!allowedDecodedLengths.includes(u8a.length)) {
2392 throw new Error(`key length is not valid, received ${u8a.length}, valid values are ${allowedDecodedLengths.join(', ')}`);
2393 }
2394
2395 const u8aPrefix = ss58Format < 64
2396 ? new Uint8Array([ss58Format])
2397 : new Uint8Array([
2398 ((ss58Format & 0xfc) >> 2) | 0x40,
2399 (ss58Format >> 8) | ((ss58Format & 0x03) << 6),
2400 ]);
2401
2402 const input = u8aConcat(u8aPrefix, u8a);
2403
2404 return base58Encode(u8aConcat(
2405 input,
2406 blake2AsU8a(input).subarray(0, [32, 33].includes(u8a.length) ? 2 : 1),
2407 ));
2408 }
2409
2410 /**
2411 * Restore substrate address from bigint representation
2412 * @param number decimal representation of substrate address
2413 * @returns substrate address
2414 */
2415 restoreCrossAccountFromBigInt(number: bigint): TSubstrateAccount {
2416 if (this.helper.api === null) {
2417 throw 'Not connected';
2418 }
2419 const res = this.helper.api.registry.createType('AccountId', '0x' + number.toString(16).padStart(64, '0')).toJSON();
2420 if (res === undefined || res === null) {
2421 throw 'Restore address error';
2422 }
2423 return res.toString();
2424 }
2425
2426 /**
2427 * Convert etherium cross account id to substrate cross account id
2428 * @param ethCrossAccount etherium cross account
2429 * @returns substrate cross account id
2430 */
2431 convertCrossAccountFromEthCrossAcoount(ethCrossAccount: IEthCrossAccountId): ICrossAccountId {
2432 if (ethCrossAccount.field_1 === '0') {
2433 return {Ethereum: ethCrossAccount.field_0.toLocaleLowerCase()};
2434 }
2435
2436 const ss58 = this.restoreCrossAccountFromBigInt(BigInt(ethCrossAccount.field_1));
2437 return {Substrate: ss58};
2438 }
23702439
2371 paraSiblingSovereignAccount(paraid: number) {2440 paraSiblingSovereignAccount(paraid: number) {
2372 // We are getting a *sibling* parachain sovereign account,2441 // We are getting a *sibling* parachain sovereign account,