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

difftreelog

feat(tests) handling of generic data from events

Fahrrader2022-06-09parent: #52ec766.patch.diff
in: master

1 file changed

modifiedtests/src/util/helpers.tsdiffbeforeafterboth
172 return event.event as T;172 return event.event as T;
173}173}
174
175export function getGenericResult<T>(events: EventRecord[]): GenericResult<T>;
176export function getGenericResult<T>(
177 events: EventRecord[],
178 expectSection: string,
179 expectMethod: string,
180 extractAction: (data: GenericEventData) => T
181): GenericResult<T>;
174182
175export function getGenericResult<T>(183export function getGenericResult<T>(
176 events: EventRecord[],184 events: EventRecord[],
182 let successData = null;190 let successData = null;
191
183 events.forEach(({event: {data, method, section}}) => {192 events.forEach(({event: {data, method, section}}) => {
193 // console.log(` ${phase}: ${section}.${method}:: ${data}`);
184 if (method === 'ExtrinsicSuccess') {194 if (method === 'ExtrinsicSuccess') {
185 success = true;195 success = true;
186 } else if ((expectSection == section) && (expectMethod == method)) {196 } else if ((expectSection == section) && (expectMethod == method)) {
195}206}
196207
197export function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {208export function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {
198 let success = false;
199 let collectionId = 0;
200 events.forEach(({event: {data, method, section}}) => {209 const genericResult = getGenericResult(events, 'common', 'CollectionCreated', (data) => parseInt(data[0].toString(), 10));
201 // console.log(` ${phase}: ${section}.${method}:: ${data}`);
202 if (method == 'ExtrinsicSuccess') {
203 success = true;
204 } else if ((section == 'common') && (method == 'CollectionCreated')) {
205 collectionId = parseInt(data[0].toString(), 10);
206 }
207 });
208 const result: CreateCollectionResult = {210 const result: CreateCollectionResult = {
209 success,211 success: genericResult.success,
210 collectionId,212 collectionId: genericResult.data ?? 0,
211 };213 };
212 return result;214 return result;
213}215}
214216
215export function getCreateItemsResult(events: EventRecord[]): CreateItemResult[] {217export function getCreateItemsResult(events: EventRecord[]): CreateItemResult[] {
216 let success = false;
217 let collectionId = 0;
218 let itemId = 0;
219 let recipient;
220
221 const results : CreateItemResult[] = [];218 const results: CreateItemResult[] = [];
222219
223 events.forEach(({event: {data, method, section}}) => {220 const genericResult = getGenericResult<CreateItemResult[]>(events, 'common', 'ItemCreated', (data) => {
224 // console.log(` ${phase}: ${section}.${method}:: ${data}`);
225 if (method == 'ExtrinsicSuccess') {
226 success = true;
227 } else if ((section == 'common') && (method == 'ItemCreated')) {
228 collectionId = parseInt(data[0].toString(), 10);221 const collectionId = parseInt(data[0].toString(), 10);
229 itemId = parseInt(data[1].toString(), 10);222 const itemId = parseInt(data[1].toString(), 10);
230 recipient = normalizeAccountId(data[2].toJSON() as any);223 const recipient = normalizeAccountId(data[2].toJSON() as any);
231224
232 const itemRes: CreateItemResult = {225 const itemRes: CreateItemResult = {
233 success,226 success: true,
234 collectionId,227 collectionId,
235 itemId,228 itemId,
236 recipient,229 recipient,
237 };230 };
238231
239 results.push(itemRes);232 results.push(itemRes);
240 }233 return results;
241 });234 });
242235
236 if (!genericResult.success) return [];
243 return results;237 return results;
244}238}
245239
246export function getCreateItemResult(events: EventRecord[]): CreateItemResult {240export function getCreateItemResult(events: EventRecord[]): CreateItemResult {
247 let success = false;
248 let collectionId = 0;
249 let itemId = 0;
250 let recipient;
251 events.forEach(({event: {data, method, section}}) => {241 const genericResult = getGenericResult<[number, number, CrossAccountId?]>(events, 'common', 'ItemCreated', (data) => [
252 // console.log(` ${phase}: ${section}.${method}:: ${data}`);
253 if (method == 'ExtrinsicSuccess') {
254 success = true;
255 } else if ((section == 'common') && (method == 'ItemCreated')) {
256 collectionId = parseInt(data[0].toString(), 10);242 parseInt(data[0].toString(), 10),
257 itemId = parseInt(data[1].toString(), 10);243 parseInt(data[1].toString(), 10),
258 recipient = normalizeAccountId(data[2].toJSON() as any);244 normalizeAccountId(data[2].toJSON() as any),
259 }
260 });245 ]);
246
247 if (genericResult.data == null) genericResult.data = [0, 0];
248
261 const result: CreateItemResult = {249 const result: CreateItemResult = {
262 success,250 success: genericResult.success,
263 collectionId,251 collectionId: genericResult.data[0],
264 itemId,252 itemId: genericResult.data[1],
265 recipient,253 recipient: genericResult.data![2],
266 };254 };
255
267 return result;256 return result;
877 }866 }
878 const transferFromTx = api.tx.unique.transferFrom(normalizeAccountId(accountFrom), to, collectionId, tokenId, value);867 const transferFromTx = api.tx.unique.transferFrom(normalizeAccountId(accountFrom), to, collectionId, tokenId, value);
879 const events = await submitTransactionAsync(accountApproved, transferFromTx);868 const events = await submitTransactionAsync(accountApproved, transferFromTx);
880 const result = getCreateItemResult(events);869 const result = getGenericResult(events);
881 // tslint:disable-next-line:no-unused-expression870 // tslint:disable-next-line:no-unused-expression
882 expect(result.success).to.be.true;871 expect(result.success).to.be.true;
883 if (type === 'NFT') {872 if (type === 'NFT') {