difftreelog
feat Add `collection_admins` method to eth collection.
in: master
2 files changed
tests/src/eth/collectionAdmin.test.tsdiffbeforeafterboth14// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.14// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.151516import {IKeyringPair} from '@polkadot/types/types';16import {IKeyringPair} from '@polkadot/types/types';17import {IEthCrossAccountId} from '../util/playgrounds/types';17import {usingEthPlaygrounds, itEth, expect, EthUniqueHelper} from './util';18import {usingEthPlaygrounds, itEth, expect, EthUniqueHelper} from './util/playgrounds';181919async function recordEthFee(helper: EthUniqueHelper, userAddress: string, call: () => Promise<any>) {20async function recordEthFee(helper: EthUniqueHelper, userAddress: string, call: () => Promise<any>) {20 const before = await helper.balance.getSubstrate(helper.address.ethToSubstrate(userAddress));21 const before = await helper.balance.getSubstrate(helper.address.ethToSubstrate(userAddress));94 expect(await collectionEvm.methods.isOwnerOrAdmin(newAdmin).call()).to.be.true;95 expect(await collectionEvm.methods.isOwnerOrAdmin(newAdmin).call()).to.be.true;95 });96 });9697 97 // itEth.skip('Check adminlist', async ({helper, privateKey}) => {98 itEth('Check adminlist', async ({helper, privateKey}) => {98 // const owner = await helper.eth.createAccountWithBalance(donor);99 const owner = await helper.eth.createAccountWithBalance(donor);99 100 100 // const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');101 const {collectionAddress, collectionId} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');101 // const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);102 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);102103103 // const admin1 = helper.eth.createAccount();104 const admin1 = helper.eth.createAccount();104 // const admin2 = await privateKey('admin');105 const admin2 = privateKey('admin');105 // await collectionEvm.methods.addCollectionAdmin(admin1).send();106 await collectionEvm.methods.addCollectionAdmin(admin1).send();106 // await collectionEvm.methods.addCollectionAdminSubstrate(admin2.addressRaw).send();107 await collectionEvm.methods.addCollectionAdminSubstrate(admin2.addressRaw).send();107108108 // const adminListRpc = await helper.collection.getAdmins(collectionId);109 const adminListRpc = await helper.collection.getAdmins(collectionId);109 // let adminListEth = await collectionEvm.methods.collectionAdmins().call();110 let adminListEth = await collectionEvm.methods.collectionAdmins().call();110 // adminListEth = adminListEth.map((element: IEthCrossAccountId) => {111 adminListEth = adminListEth.map((element: IEthCrossAccountId) => {111 // return helper.address.convertCrossAccountFromEthCrossAcoount(element);112 return helper.address.convertCrossAccountFromEthCrossAcoount(element);112 // });113 });113 // expect(adminListRpc).to.be.like(adminListEth);114 expect(adminListRpc).to.be.like(adminListEth);114 // });115 }); 115 116 116 itEth('(!negative tests!) Add admin by ADMIN is not allowed', async ({helper}) => {117 itEth('(!negative tests!) Add admin by ADMIN is not allowed', async ({helper}) => {117 const owner = await helper.eth.createAccountWithBalance(donor);118 const owner = await helper.eth.createAccountWithBalance(donor);tests/src/util/playgrounds/unique.tsdiffbeforeafterboth778import {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 }23722373 /**2374 * Encode key to substrate address2375 * @param key key for encoding address2376 * @param ss58Format prefix for encoding to the address of the corresponding network2377 * @returns encoded substrate address2378 */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 < 642396 ? new Uint8Array([ss58Format])2397 : new Uint8Array([2398 ((ss58Format & 0xfc) >> 2) | 0x40,2399 (ss58Format >> 8) | ((ss58Format & 0x03) << 6),2400 ]);24012402 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 }24092410 /**2411 * Restore substrate address from bigint representation2412 * @param number decimal representation of substrate address2413 * @returns substrate address2414 */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 }24252426 /**2427 * Convert etherium cross account id to substrate cross account id2428 * @param ethCrossAccount etherium cross account2429 * @returns substrate cross account id2430 */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 }237024392371 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,