git.delta.rocks / unique-network / refs/commits / 99e7555e5ee6

difftreelog

Merge pull request #104 from usetech-llc/feature/NFTPAR-301_transfer_event

Greg Zaitsev2021-02-18parents: #dcb255f #04b203b.patch.diff
in: master
Add Transfer event

2 files changed

modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -507,6 +507,19 @@
         /// 
         /// item_id: Identifier of burned NFT.
         ItemDestroyed(CollectionId, TokenId),
+
+        /// Item was transferred
+        ///
+        /// * collection_id: Id of collection to which item is belong
+        ///
+        /// * item_id: Id of an item
+        ///
+        /// * sender: Original owner of item
+        ///
+        /// * recipient: New owner of item
+        ///
+        /// * amount: Always 1 for NFT
+        Transfer(CollectionId, TokenId, AccountId, AccountId, u128),
     }
 );
 
@@ -1558,12 +1571,14 @@
 
         match target_collection.mode
         {
-            CollectionMode::NFT => Self::transfer_nft(collection_id, item_id, sender.clone(), recipient)?,
+            CollectionMode::NFT => Self::transfer_nft(collection_id, item_id, sender.clone(), recipient.clone())?,
             CollectionMode::Fungible(_)  => Self::transfer_fungible(collection_id, value, &sender, &recipient)?,
-            CollectionMode::ReFungible  => Self::transfer_refungible(collection_id, item_id, value, sender.clone(), recipient)?,
+            CollectionMode::ReFungible  => Self::transfer_refungible(collection_id, item_id, value, sender.clone(), recipient.clone())?,
             _ => ()
         };
 
+        Self::deposit_event(RawEvent::Transfer(collection_id, item_id, sender, recipient, value));
+
         Ok(())
     }
 
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
39 recipient: string;39 recipient: string;
40}40}
41
42interface TransferResult {
43 success: boolean;
44 collectionId: number;
45 itemId: number;
46 sender: string;
47 recipient: string;
48 value: bigint;
49}
4150
42interface IReFungibleOwner {51interface IReFungibleOwner {
43 Fraction: BN;52 Fraction: BN;
115 return result;124 return result;
116}125}
126
127export function getTransferResult(events: EventRecord[]): TransferResult {
128 const result: TransferResult = {
129 success: false,
130 collectionId: 0,
131 itemId: 0,
132 sender: '',
133 recipient: '',
134 value: 0n,
135 };
136
137 events.forEach(({event: {data, method, section}}) => {
138 if (method === 'ExtrinsicSuccess') {
139 result.success = true;
140 } else if (section === 'nft' && method === 'Transfer') {
141 result.collectionId = +data[0].toString();
142 result.itemId = +data[1].toString();
143 result.sender = data[2].toString();
144 result.recipient = data[3].toString();
145 result.value = BigInt(data[4].toString());
146 }
147 });
148
149 return result;
150}
117151
118interface Invalid {152interface Invalid {
119 type: 'Invalid';153 type: 'Invalid';
624 }658 }
625 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);659 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);
626 const events = await submitTransactionAsync(sender, transferTx);660 const events = await submitTransactionAsync(sender, transferTx);
627 const result = getCreateItemResult(events);661 const result = getTransferResult(events);
628 // tslint:disable-next-line:no-unused-expression662 // tslint:disable-next-line:no-unused-expression
629 expect(result.success).to.be.true;663 expect(result.success).to.be.true;
664 expect(result.collectionId).to.be.equal(collectionId);
665 expect(result.itemId).to.be.equal(tokenId);
666 expect(result.sender).to.be.equal(sender.address);
667 expect(result.recipient).to.be.equal(recipient.address);
668 expect(result.value.toString()).to.be.equal(value.toString());
630 if (type === 'NFT') {669 if (type === 'NFT') {
631 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;670 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;
632 expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);671 expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);