-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TW-834: NFT page. 'Sell' button. Refactor
- Loading branch information
Showing
5 changed files
with
76 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/app/pages/Collectibles/use-collectible-selling.hook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useCallback, useState } from 'react'; | ||
|
||
import BigNumber from 'bignumber.js'; | ||
|
||
import type { CollectibleDetails } from 'app/store/collectibles/state'; | ||
import { getObjktMarketplaceContract } from 'lib/apis/objkt'; | ||
import { fromFa2TokenSlug } from 'lib/assets/utils'; | ||
import { useAccount, useTezos } from 'lib/temple/front'; | ||
import { getTransferPermissions } from 'lib/utils/get-transfer-permissions'; | ||
import { parseTransferParamsToParamsWithKind } from 'lib/utils/parse-transfer-params'; | ||
|
||
const DEFAULT_OBJKT_STORAGE_LIMIT = 350; | ||
|
||
export const useCollectibleSelling = (assetSlug: string, details?: CollectibleDetails) => { | ||
const tezos = useTezos(); | ||
const { publicKeyHash } = useAccount(); | ||
const [isSelling, setIsSelling] = useState(false); | ||
|
||
const initiateSelling = useCallback(async () => { | ||
const offer = details?.highestOffer; | ||
if (!offer || isSelling) return; | ||
setIsSelling(true); | ||
|
||
const { contract: tokenAddress, id } = fromFa2TokenSlug(assetSlug); | ||
const tokenId = Number(id.toString()); | ||
|
||
const contract = await getObjktMarketplaceContract(tezos, offer.marketplace_contract); | ||
|
||
const transferParams = (() => { | ||
if ('fulfill_offer' in contract.methods) { | ||
return contract.methods.fulfill_offer(offer.bigmap_key, tokenId).toTransferParams(); | ||
} else { | ||
return contract.methods.offer_accept(offer.bigmap_key).toTransferParams(); | ||
} | ||
})(); | ||
|
||
const tokenToSpend = { | ||
standard: 'fa2' as const, | ||
contract: tokenAddress, | ||
tokenId | ||
}; | ||
|
||
const { approve, revoke } = await getTransferPermissions( | ||
tezos, | ||
offer.marketplace_contract, | ||
publicKeyHash, | ||
tokenToSpend, | ||
new BigNumber('0') | ||
); | ||
|
||
const operationParams = approve | ||
.concat(transferParams) | ||
.concat(revoke) | ||
.map(params => parseTransferParamsToParamsWithKind({ ...params, storageLimit: DEFAULT_OBJKT_STORAGE_LIMIT })); | ||
|
||
await tezos.wallet | ||
.batch(operationParams) | ||
.send() | ||
.catch(error => { | ||
console.error('Operation send error:', error); | ||
}); | ||
|
||
setIsSelling(false); | ||
}, [tezos, isSelling, details, assetSlug, publicKeyHash]); | ||
|
||
return { isSelling, initiateSelling }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters