Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Reuse unused tokens for the next offer #11204

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

hassnian
Copy link
Contributor

@hassnian hassnian commented Dec 2, 2024

PR Type

  • Bugfix
  • Feature
  • Refactoring

Context

That has to be burned by user, or you can reuse it for the next offer

let's save this cost to the user, by reusing unsued tokens from old offers

Screenshot 📸

  • My fix has changed something on UI;

Copy link

netlify bot commented Dec 2, 2024

Deploy Preview for koda-canary ready!

Name Link
🔨 Latest commit c3996ea
🔍 Latest deploy log https://app.netlify.com/sites/koda-canary/deploys/674d3def9202060008c5c24e
😎 Deploy Preview https://deploy-preview-11204--koda-canary.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@hassnian hassnian marked this pull request as ready for review December 2, 2024 04:54
@hassnian hassnian requested a review from a team as a code owner December 2, 2024 04:54
Copy link

sonarcloud bot commented Dec 2, 2024

Copy link
Contributor

@Jarsen136 Jarsen136 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely a good idea to reuse the unused offer token. However, it need a better way to get the unused nft id.


Let me provide an example showing how it may cause chaotic situations.

  1. In the beginning you have an expired offer
[ { offerId: 1, nftSn: 1, status: EXPIRED }]
  1. Then you try to create a new offer and reuse the unused token
[ { offerId: 1, nftSn: 1, status: EXPIRED },
{ offerId: 2, nftSn: 1, status: ACTIVE }]
  1. If you continue to create a new offer
[ { offerId: 1, nftSn: 1, status: EXPIRED },
{ offerId: 2, nftSn: 1, status: ACTIVE },
{ offerId: 3, nftSn: 1, status: ACTIVE }]

In this step, there is already a problem occurred. You may create infinite offer with one nft.

  1. More chaos is coming If the offer 2 has been accepted.
[ { offerId: 1, nftSn: 1, status: EXPIRED },
{ offerId: 2, nftSn: 1, status: ACCEPT },
{ offerId: 3, nftSn: 1, status: ACTIVE }]

If you continue to creating a new offer, the current implement will still using nftSn: 1.
However, the nftSn: 1 is not available anymore.
This step would cause problems again.

[ { offerId: 1, nftSn: 1, status: EXPIRED },
{ offerId: 2, nftSn: 1, status: ACCEPT },
{ offerId: 3, nftSn: 1, status: ACTIVE }
{ offerId: 4, nftSn: 1, status: ACTIVE }]

Let me know if this example is not clear or it's not possible to happen with the current implement.

components/trade/makeOffer/MakeOfferModal.vue Show resolved Hide resolved
@hassnian
Copy link
Contributor Author

hassnian commented Dec 3, 2024

Let me know if this example is not clear or it's not possible to happen with the current implement.

the example you provided would defiantly be an issue if each new atomic swap mapped to an offer in our indexer, which it does not

an offer's id is made of {offeredCollectionId}-${offeredItem}

so when we create a new offer, with a reused token it will override the old offer code

const id = createTokenId(event.collectionId, event.sn)
const final = offer ? await getOrCreate(context.store, Offer, id, {}) : await getOrCreate(context.store, Swap, id, {})

so basically atm you can't have multiple swaps/offers with the same offered item

this is either a bug or by design, cc @vikiival

if it's the former I will make the required changes , otherwise the current implementation should pass the provided case.

@vikiival
Copy link
Member

vikiival commented Dec 3, 2024

so when we create a new offer, with a reused token it will override the old offer

Yup, that's how it is implemented

https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/nfts/src/features/atomic_swap.rs#L82-L91

Swap is stored inside a map by key - (offeredCollection, offeredItem) - aka same as in our indexer (note: except that that key in rust is tuple and in our case is dash-separated string).

so basically atm you can't have multiple swaps/offers with the same offered item

So yes each, one offered item per desired item

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatic cancel expired offer and return deposit
3 participants