git.delta.rocks / unique-network / refs/commits / adb854cc5dc6

difftreelog

tests(util): introduce CrossAccountId class to take on some util functionality

Fahrrader2022-09-28parent: #a9c2603.patch.diff
in: master

4 files changed

modifiedtests/src/nesting/nest.test.tsdiffbeforeafterboth
35 // Create an immediately nested token35 // Create an immediately nested token
36 const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());36 const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());
37 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});37 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
38 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());38 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
39 39
40 // Create a token to be nested40 // Create a token to be nested
41 const newToken = await collection.mintToken(alice);41 const newToken = await collection.mintToken(alice);
4242
43 // Nest43 // Nest
44 await newToken.nest(alice, targetToken);44 await newToken.nest(alice, targetToken);
45 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});45 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
46 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());46 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
4747
48 // Move bundle to different user48 // Move bundle to different user
49 await targetToken.transfer(alice, {Substrate: bob.address});49 await targetToken.transfer(alice, {Substrate: bob.address});
50 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});50 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});
51 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());51 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
52 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});52 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});
53 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());53 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
5454
55 // Unnest55 // Unnest
56 await newToken.unnest(bob, targetToken, {Substrate: bob.address});56 await newToken.unnest(bob, targetToken, {Substrate: bob.address});
6565
66 // Create a nested token66 // Create a nested token
67 const tokenC = await collection.mintToken(alice, tokenA.nestingAccount());67 const tokenC = await collection.mintToken(alice, tokenA.nestingAccount());
68 expect(await tokenC.getOwner()).to.be.deep.equal(tokenA.nestingAccountInLowerCase());68 expect(await tokenC.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());
69 69
70 // Transfer the nested token to another token70 // Transfer the nested token to another token
71 await expect(tokenC.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount())).to.be.fulfilled;71 await expect(tokenC.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount())).to.be.fulfilled;
72 expect(await tokenC.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});72 expect(await tokenC.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
73 expect(await tokenC.getOwner()).to.be.deep.equal(tokenB.nestingAccountInLowerCase());73 expect(await tokenC.getOwner()).to.be.deep.equal(tokenB.nestingAccount().toLowerCase());
74 });74 });
7575
76 itSub('Checks token children', async ({helper}) => {76 itSub('Checks token children', async ({helper}) => {
150 // Create an immediately nested token150 // Create an immediately nested token
151 const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());151 const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());
152 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});152 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
153 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());153 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
154154
155 // Create a token to be nested and nest155 // Create a token to be nested and nest
156 const newToken = await collection.mintToken(bob);156 const newToken = await collection.mintToken(bob);
157 await newToken.nest(bob, targetToken);157 await newToken.nest(bob, targetToken);
158 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});158 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
159 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());159 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
160 });160 });
161161
162 itSub('Admin (NFT): Admin and Token Owner can operate together', async ({helper}) => {162 itSub('Admin (NFT): Admin and Token Owner can operate together', async ({helper}) => {
167 // Create an immediately nested token by an administrator167 // Create an immediately nested token by an administrator
168 const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());168 const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());
169 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});169 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
170 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());170 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
171171
172 // Create a token to be nested and nest172 // Create a token to be nested and nest
173 const newToken = await collection.mintToken(alice, {Substrate: charlie.address});173 const newToken = await collection.mintToken(alice, {Substrate: charlie.address});
174 await newToken.nest(charlie, targetToken);174 await newToken.nest(charlie, targetToken);
175 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});175 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
176 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());176 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
177 });177 });
178178
179 itSub('Admin (NFT): allows an Admin to nest a token (Restricted nesting)', async ({helper}) => {179 itSub('Admin (NFT): allows an Admin to nest a token (Restricted nesting)', async ({helper}) => {
187 // Create an immediately nested token187 // Create an immediately nested token
188 const nestedToken = await collectionB.mintToken(bob, targetToken.nestingAccount());188 const nestedToken = await collectionB.mintToken(bob, targetToken.nestingAccount());
189 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});189 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
190 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());190 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
191191
192 // Create a token to be nested and nest192 // Create a token to be nested and nest
193 const newToken = await collectionB.mintToken(bob);193 const newToken = await collectionB.mintToken(bob);
194 await newToken.nest(bob, targetToken);194 await newToken.nest(bob, targetToken);
195 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});195 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
196 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());196 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
197 });197 });
198198
199 // ---------- Non-Fungible ----------199 // ---------- Non-Fungible ----------
207 // Create an immediately nested token207 // Create an immediately nested token
208 const nestedToken = await collection.mintToken(charlie, targetToken.nestingAccount());208 const nestedToken = await collection.mintToken(charlie, targetToken.nestingAccount());
209 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});209 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
210 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());210 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
211211
212 // Create a token to be nested and nest212 // Create a token to be nested and nest
213 const newToken = await collection.mintToken(charlie);213 const newToken = await collection.mintToken(charlie);
214 await newToken.nest(charlie, targetToken);214 await newToken.nest(charlie, targetToken);
215 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});215 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
216 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());216 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
217 });217 });
218218
219 itSub('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async ({helper}) => {219 itSub('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async ({helper}) => {
233 // Create an immediately nested token233 // Create an immediately nested token
234 const nestedToken = await collectionB.mintToken(charlie, targetToken.nestingAccount());234 const nestedToken = await collectionB.mintToken(charlie, targetToken.nestingAccount());
235 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});235 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
236 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());236 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
237237
238 // Create a token to be nested and nest238 // Create a token to be nested and nest
239 const newToken = await collectionB.mintToken(charlie);239 const newToken = await collectionB.mintToken(charlie);
240 await newToken.nest(charlie, targetToken);240 await newToken.nest(charlie, targetToken);
241 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});241 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});
242 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());242 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
243 });243 });
244244
245 // ---------- Fungible ----------245 // ---------- Fungible ----------
424424
425 expect(await targetToken.getChildren()).to.be.length(1);425 expect(await targetToken.getChildren()).to.be.length(1);
426 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});426 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});
427 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());427 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
428 });428 });
429429
430 itSub('Admin (NFT): disallows an Admin to nest a token from an unlisted collection (Restricted nesting)', async ({helper}) => {430 itSub('Admin (NFT): disallows an Admin to nest a token from an unlisted collection (Restricted nesting)', async ({helper}) => {
modifiedtests/src/nesting/unnest.test.tsdiffbeforeafterboth
105105
106 // Try to unnest106 // Try to unnest
107 await expect(nestedToken.unnest(bob, targetToken, {Substrate: alice.address})).to.be.rejectedWith(/common\.ApprovedValueTooLow/);107 await expect(nestedToken.unnest(bob, targetToken, {Substrate: alice.address})).to.be.rejectedWith(/common\.ApprovedValueTooLow/);
108 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());108 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
109109
110 // Try to burn110 // Try to burn
111 await expect(nestedToken.burnFrom(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.ApprovedValueTooLow/);111 await expect(nestedToken.burnFrom(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.ApprovedValueTooLow/);
112 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccountInLowerCase());112 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
113 });113 });
114114
115 // todo another test for creating excessive depth matryoshka with Ethereum?115 // todo another test for creating excessive depth matryoshka with Ethereum?
modifiedtests/src/rpc.test.tsdiffbeforeafterboth
1616
17import {IKeyringPair} from '@polkadot/types/types';17import {IKeyringPair} from '@polkadot/types/types';
18import {usingPlaygrounds, itSub, expect} from './util/playgrounds';18import {usingPlaygrounds, itSub, expect} from './util/playgrounds';
19import {crossAccountIdFromLower} from './util/playgrounds/unique';19import {CrossAccountId} from './util/playgrounds/unique';
2020
21describe('integration test: RPC methods', () => {21describe('integration test: RPC methods', () => {
22 let donor: IKeyringPair;22 let donor: IKeyringPair;
55 // Set-up over55 // Set-up over
5656
57 const owners = await helper.callRpc('api.rpc.unique.tokenOwners', [collection.collectionId, 0]);57 const owners = await helper.callRpc('api.rpc.unique.tokenOwners', [collection.collectionId, 0]);
58 const ids = (owners.toJSON() as any[]).map(crossAccountIdFromLower);58 const ids = (owners.toJSON() as any[]).map(CrossAccountId.fromLowerCaseKeys);
5959
60 expect(ids).to.deep.include.members([{Substrate: alice.address}, ethAcc, {Substrate: bob.address}, ...facelessCrowd]);60 expect(ids).to.deep.include.members([{Substrate: alice.address}, ethAcc, {Substrate: bob.address}, ...facelessCrowd]);
61 expect(owners.length == 10).to.be.true;61 expect(owners.length == 10).to.be.true;
modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
11import {IKeyringPair} from '@polkadot/types/types';11import {IKeyringPair} from '@polkadot/types/types';
12import {IApiListeners, IBlock, IEvent, IChainProperties, ICollectionCreationOptions, ICollectionLimits, ICollectionPermissions, ICrossAccountId, ICrossAccountIdLower, ILogger, INestingPermissions, IProperty, IStakingInfo, ISubstrateBalance, IToken, ITokenPropertyPermission, ITransactionResult, IUniqueHelperLog, TApiAllowedListeners, TEthereumAccount, TSigner, TSubstrateAccount, TUniqueNetworks} from './types';12import {IApiListeners, IBlock, IEvent, IChainProperties, ICollectionCreationOptions, ICollectionLimits, ICollectionPermissions, ICrossAccountId, ICrossAccountIdLower, ILogger, INestingPermissions, IProperty, IStakingInfo, ISubstrateBalance, IToken, ITokenPropertyPermission, ITransactionResult, IUniqueHelperLog, TApiAllowedListeners, TEthereumAccount, TSigner, TSubstrateAccount, TUniqueNetworks} from './types';
1313
14export const crossAccountIdFromLower = (lowerAddress: ICrossAccountIdLower): ICrossAccountId => {14export class CrossAccountId implements ICrossAccountId {
15 Substrate?: TSubstrateAccount;
16 Ethereum?: TEthereumAccount;
17
18 constructor(account: ICrossAccountId) {
19 if (account.Substrate) this.Substrate = account.Substrate;
20 if (account.Ethereum) this.Ethereum = account.Ethereum;
21 }
22
23 static fromKeyring(account: IKeyringPair) {
24 return new CrossAccountId({Substrate: account.address});
25 }
26
27 static fromLowerCaseKeys(address: ICrossAccountIdLower): CrossAccountId {
28 return new CrossAccountId({Substrate: address.substrate, Ethereum: address.ethereum});
29 }
30
31 static normalizeSubstrateAddress(address: TSubstrateAccount, ss58Format = 42): TSubstrateAccount {
32 return encodeAddress(decodeAddress(address), ss58Format);
33 }
34
35 static withNormalizedSubstrate(address: TSubstrateAccount, ss58Format = 42): CrossAccountId {
36 return new CrossAccountId({Substrate: CrossAccountId.normalizeSubstrateAddress(address, ss58Format)});
37 }
38
15 const address = {} as ICrossAccountId;39 withNormalizedSubstrate(ss58Format = 42): CrossAccountId {
40 if (this.Substrate) return CrossAccountId.withNormalizedSubstrate(this.Substrate, ss58Format);
41 return this;
42 }
43
44 toLowerCase(): CrossAccountId {
16 if(lowerAddress.substrate) address.Substrate = lowerAddress.substrate;45 if (this.Substrate) this.Substrate = this.Substrate.toLowerCase();
17 if(lowerAddress.ethereum) address.Ethereum = lowerAddress.ethereum;46 if (this.Ethereum) this.Ethereum = this.Ethereum.toLowerCase();
18 return address;47 return this;
19};48 }
49}
2050
21const nesting = {51const nesting = {
22 toChecksumAddress(address: string): string {52 toChecksumAddress(address: string): string {
55 RPC: 'rpc',85 RPC: 'rpc',
56 };86 };
5787
58 static getTokenAccount(token: IToken): ICrossAccountId {88 static getTokenAccount(token: IToken): CrossAccountId {
59 return {Ethereum: this.getTokenAddress(token)};89 return new CrossAccountId({Ethereum: this.getTokenAddress(token)});
60 }90 }
61
62 static getTokenAccountInLowerCase(token: IToken): ICrossAccountId {
63 return {Ethereum: this.getTokenAddress(token).toLowerCase()};
64 }
6591
66 static getTokenAddress(token: IToken): string {92 static getTokenAddress(token: IToken): string {
67 return nesting.tokenIdToAddress(token.collectionId, token.tokenId);93 return nesting.tokenIdToAddress(token.collectionId, token.tokenId);
94 return keyring.addFromUri(seed);120 return keyring.addFromUri(seed);
95 }121 }
96
97 static normalizeSubstrateAddress(address: string, ss58Format = 42) {
98 return encodeAddress(decodeAddress(address), ss58Format);
99 }
100122
101 static extractCollectionIdFromCreationResult(creationResult: ITransactionResult) {123 static extractCollectionIdFromCreationResult(creationResult: ITransactionResult) {
102 if (creationResult.status !== this.transactionStatus.SUCCESS) {124 if (creationResult.status !== this.transactionStatus.SUCCESS) {
178 Object.keys(address).forEach(k => {200 Object.keys(address).forEach(k => {
179 obj[k.toLocaleLowerCase()] = address[k as 'Substrate' | 'Ethereum'];201 obj[k.toLocaleLowerCase()] = address[k as 'Substrate' | 'Ethereum'];
180 });202 });
181 if(obj.substrate) return {Substrate: this.normalizeSubstrateAddress(obj.substrate)};203 if(obj.substrate) return CrossAccountId.withNormalizedSubstrate(obj.substrate);
182 if(obj.ethereum) return {Ethereum: obj.ethereum.toLocaleLowerCase()};204 if(obj.ethereum) return CrossAccountId.fromLowerCaseKeys(obj).toLowerCase();
183 return address;205 return address;
184 };206 };
185 let transfer = {collectionId: null, tokenId: null, from: null, to: null, amount: 1} as any;207 let transfer = {collectionId: null, tokenId: null, from: null, to: null, amount: 1} as any;
563 name: string;585 name: string;
564 description: string;586 description: string;
565 tokensCount: number;587 tokensCount: number;
566 admins: ICrossAccountId[];588 admins: CrossAccountId[];
567 normalizedOwner: TSubstrateAccount;589 normalizedOwner: TSubstrateAccount;
568 raw: any590 raw: any
569 } | null> {591 } | null> {
596 * @example await getAdmins(1)618 * @example await getAdmins(1)
597 * @returns array of administrators619 * @returns array of administrators
598 */620 */
599 async getAdmins(collectionId: number, normalize = false): Promise<ICrossAccountId[]> {621 async getAdmins(collectionId: number, normalize = false): Promise<CrossAccountId[]> {
600 const admins = (await this.helper.callRpc('api.rpc.unique.adminlist', [collectionId])).toHuman();622 const admins = (await this.helper.callRpc('api.rpc.unique.adminlist', [collectionId])).toHuman();
601623
602 return normalize624 return normalize
603 ? admins.map((address: any) => this.helper.address.normalizeCrossAccountIfSubstrate(address))625 ? admins.map((address: CrossAccountId) => address.withNormalizedSubstrate())
604 : admins;626 : admins;
605 }627 }
606628
611 * @example await getAllowList(1)633 * @example await getAllowList(1)
612 * @returns array of allow-listed addresses634 * @returns array of allow-listed addresses
613 */635 */
614 async getAllowList(collectionId: number, normalize = false): Promise<ICrossAccountId[]> {636 async getAllowList(collectionId: number, normalize = false): Promise<CrossAccountId[]> {
615 const allowListed = (await this.helper.callRpc('api.rpc.unique.allowlist', [collectionId])).toHuman();637 const allowListed = (await this.helper.callRpc('api.rpc.unique.allowlist', [collectionId])).toHuman();
616 return normalize638 return normalize
617 ? allowListed.map((address: any) => this.helper.address.normalizeCrossAccountIfSubstrate(address))639 ? allowListed.map((address: CrossAccountId) => address.withNormalizedSubstrate())
618 : allowListed;640 : allowListed;
619 }641 }
620642
1099 */1121 */
1100 async getToken(collectionId: number, tokenId: number, propertyKeys: string[] = [], blockHashAt?: string): Promise<{1122 async getToken(collectionId: number, tokenId: number, propertyKeys: string[] = [], blockHashAt?: string): Promise<{
1101 properties: IProperty[];1123 properties: IProperty[];
1102 owner: ICrossAccountId;1124 owner: CrossAccountId;
1103 normalizedOwner: ICrossAccountId;1125 normalizedOwner: CrossAccountId;
1104 }| null> {1126 }| null> {
1105 let tokenData;1127 let tokenData;
1106 if(typeof blockHashAt === 'undefined') {1128 if(typeof blockHashAt === 'undefined') {
1118 if (tokenData === null || tokenData.owner === null) return null;1140 if (tokenData === null || tokenData.owner === null) return null;
1119 const owner = {} as any;1141 const owner = {} as any;
1120 for (const key of Object.keys(tokenData.owner)) {1142 for (const key of Object.keys(tokenData.owner)) {
1121 owner[key.toLocaleLowerCase()] = this.helper.address.normalizeCrossAccountIfSubstrate(tokenData.owner[key]);1143 owner[key.toLocaleLowerCase()] = new CrossAccountId(tokenData.owner[key]).withNormalizedSubstrate();
1122 }1144 }
1123 tokenData.normalizedOwner = crossAccountIdFromLower(owner);1145 tokenData.normalizedOwner = CrossAccountId.fromLowerCaseKeys(owner);
1124 return tokenData;1146 return tokenData;
1125 }1147 }
11261148
1272 * @example getTokenOwner(10, 5);1294 * @example getTokenOwner(10, 5);
1273 * @returns Address in CrossAccountId format, e.g. {Substrate: "5DnSF6RRjwteE3BrCj..."}1295 * @returns Address in CrossAccountId format, e.g. {Substrate: "5DnSF6RRjwteE3BrCj..."}
1274 */1296 */
1275 async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<ICrossAccountId> {1297 async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId> {
1276 let owner;1298 let owner;
1277 if (typeof blockHashAt === 'undefined') {1299 if (typeof blockHashAt === 'undefined') {
1278 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId]);1300 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId]);
1279 } else {1301 } else {
1280 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId, blockHashAt]);1302 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId, blockHashAt]);
1281 }1303 }
1282 return crossAccountIdFromLower(owner.toJSON());1304 return CrossAccountId.fromLowerCaseKeys(owner.toJSON());
1283 }1305 }
12841306
1285 /**1307 /**
1331 * @example getTokenTopmostOwner(10, 5);1353 * @example getTokenTopmostOwner(10, 5);
1332 * @returns address in CrossAccountId format, e.g. {Substrate: "5DyN4Y92vZCjv38fg..."}1354 * @returns address in CrossAccountId format, e.g. {Substrate: "5DyN4Y92vZCjv38fg..."}
1333 */1355 */
1334 async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<ICrossAccountId | null> {1356 async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId | null> {
1335 let owner;1357 let owner;
1336 if (typeof blockHashAt === 'undefined') {1358 if (typeof blockHashAt === 'undefined') {
1337 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId]);1359 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId]);
1541 * @example getTokenTop10Owners(10, 5);1563 * @example getTokenTop10Owners(10, 5);
1542 * @returns array of top 10 owners1564 * @returns array of top 10 owners
1543 */1565 */
1544 async getTokenTop10Owners(collectionId: number, tokenId: number): Promise<ICrossAccountId[]> {1566 async getTokenTop10Owners(collectionId: number, tokenId: number): Promise<CrossAccountId[]> {
1545 return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, tokenId])).toJSON().map(crossAccountIdFromLower);1567 return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, tokenId])).toJSON().map(CrossAccountId.fromLowerCaseKeys);
1546 }1568 }
15471569
1548 /**1570 /**
1825 * @example getTop10Owners(10);1847 * @example getTop10Owners(10);
1826 * @returns array of ```ICrossAccountId```1848 * @returns array of ```ICrossAccountId```
1827 */1849 */
1828 async getTop10Owners(collectionId: number): Promise<ICrossAccountId[]> {1850 async getTop10Owners(collectionId: number): Promise<CrossAccountId[]> {
1829 return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, 0])).toJSON().map(crossAccountIdFromLower);1851 return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, 0])).toJSON().map(CrossAccountId.fromLowerCaseKeys);
1830 }1852 }
18311853
1832 /**1854 /**
2045 };2067 };
2046 }2068 }
2047 });2069 });
2048 let isSuccess = this.helper.address.normalizeSubstrate(typeof signer === 'string' ? signer : signer.address) === transfer.from;2070 const isSuccess = this.helper.address.normalizeSubstrate(typeof signer === 'string' ? signer : signer.address) === transfer.from
2049 isSuccess = isSuccess && this.helper.address.normalizeSubstrate(address) === transfer.to;2071 && this.helper.address.normalizeSubstrate(address) === transfer.to
2050 isSuccess = isSuccess && BigInt(amount) === transfer.amount;2072 && BigInt(amount) === transfer.amount;
2051 return isSuccess;2073 return isSuccess;
2052 }2074 }
2053}2075}
2062 * @returns substrate address converted to normalized (i.e., starting with 5) or specified explicitly representation2084 * @returns substrate address converted to normalized (i.e., starting with 5) or specified explicitly representation
2063 */2085 */
2064 normalizeSubstrate(address: TSubstrateAccount, ss58Format = 42): TSubstrateAccount {2086 normalizeSubstrate(address: TSubstrateAccount, ss58Format = 42): TSubstrateAccount {
2065 return this.helper.util.normalizeSubstrateAddress(address, ss58Format);2087 return CrossAccountId.normalizeSubstrateAddress(address, ss58Format);
2066 }2088 }
2067
2068 /**
2069 * Normalizes the address of an account ONLY if it's Substrate to the specified ss58 format, by default ```42```.
2070 * @param account account of either Substrate type or Ethereum, but only Substrate will be changed
2071 * @param ss58Format format for address conversion, by default ```42```
2072 * @example normalizeCrossAccountIfSubstrate({Substrate: "unjKJQJrRd238pkUZZvzDQrfKuM39zBSnQ5zjAGAGcdRhaJTx"}) // returns 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
2073 * @returns untouched ethereum account or substrate account converted to normalized (i.e., starting with 5) or specified explicitly representation
2074 */
2075 normalizeCrossAccountIfSubstrate(account: ICrossAccountId, ss58Format = 42): ICrossAccountId {
2076 return account.Substrate
2077 ? {Substrate: this.normalizeSubstrate(account.Substrate, ss58Format)}
2078 : account;
2079 }
20802089
2081 /**2090 /**
2082 * Get address in the connected chain format2091 * Get address in the connected chain format
2574 return this.collection.helper.util.getTokenAccount(this);2583 return this.collection.helper.util.getTokenAccount(this);
2575 }2584 }
2576
2577 nestingAccountInLowerCase() {
2578 return this.collection.helper.util.getTokenAccountInLowerCase(this);
2579 }
2580}2585}
25812586
25822587