11module Seabug.CallContract
22 ( callMarketPlaceBuy
3- , callMarketPlaceListNft
43 , callMarketPlaceBuyTest
4+ , callMarketPlaceFetchNft
5+ , callMarketPlaceListNft
56 ) where
67
78import Contract.Prelude
@@ -16,12 +17,9 @@ import Contract.Monad
1617 , runContract_
1718 )
1819import Contract.Numeric.Natural (toBigInt )
19- import Contract.Prim.ByteArray
20- ( byteArrayToHex
21- , hexToByteArray
22- )
20+ import Contract.Prim.ByteArray (byteArrayToHex , hexToByteArray )
2321import Contract.Transaction
24- ( TransactionInput (TransactionInput )
22+ ( TransactionInput (.. )
2523 , TransactionOutput (TransactionOutput)
2624 )
2725import Contract.Value
@@ -38,19 +36,22 @@ import Control.Promise (Promise)
3836import Control.Promise as Promise
3937import Data.BigInt (BigInt )
4038import Data.BigInt as BigInt
39+ import Data.Log.Level (LogLevel (..))
40+ import Data.Nullable (Nullable , notNull , null )
4141import Data.Tuple.Nested ((/\))
4242import Data.UInt as UInt
4343import Effect (Effect )
4444import Effect.Aff (error )
4545import Effect.Class (liftEffect )
46- import Data.Log.Level (LogLevel (..))
4746import Effect.Exception (Error )
48- import Seabug.Metadata.Types (SeabugMetadata (SeabugMetadata))
49- import Seabug.Metadata.Share (unShare )
5047import Partial.Unsafe (unsafePartial )
5148import Plutus.Conversion (fromPlutusAddress )
49+ import Seabug.Contract.Common (NftResult )
5250import Seabug.Contract.MarketPlaceBuy (marketplaceBuy )
53- import Seabug.Contract.MarketPlaceListNft (ListNftResult , marketPlaceListNft )
51+ import Seabug.Contract.MarketPlaceFetchNft (marketPlaceFetchNft )
52+ import Seabug.Contract.MarketPlaceListNft (marketPlaceListNft )
53+ import Seabug.Metadata.Share (unShare )
54+ import Seabug.Metadata.Types (SeabugMetadata (SeabugMetadata))
5455import Seabug.Types
5556 ( NftCollection (NftCollection)
5657 , NftData (NftData)
@@ -63,14 +64,26 @@ import Serialization.Hash
6364 , scriptHashFromBech32
6465 , scriptHashToBech32Unsafe
6566 )
67+ import Types.BigNum as BigNum
6668import Types.Natural as Nat
6769import Wallet (mkNamiWalletAff )
68- import Types.BigNum as BigNum
6970
7071-- | Exists temporarily for testing purposes
7172callMarketPlaceBuyTest :: String -> Effect (Promise String )
7273callMarketPlaceBuyTest = Promise .fromAff <<< pure
7374
75+ callMarketPlaceFetchNft
76+ :: ContractConfiguration
77+ -> TransactionInputOut
78+ -> Effect (Promise (Nullable ListNftResultOut ))
79+ callMarketPlaceFetchNft cfg args = Promise .fromAff do
80+ contractConfig <- buildContractConfig cfg
81+ txInput <- liftEffect $ liftEither $ buildTransactionInput args
82+ runContract contractConfig (marketPlaceFetchNft txInput) >>= case _ of
83+ Nothing -> pure null
84+ Just nftResult -> pure $ notNull $
85+ buildNftList (unwrap contractConfig).networkId nftResult
86+
7487-- | Calls Seabugs marketplaceBuy and takes care of converting data types.
7588-- Returns a JS promise holding no data.
7689callMarketPlaceBuy
@@ -123,12 +136,14 @@ type BuyNftArgs =
123136 }
124137 }
125138
139+ type TransactionInputOut = { transactionId :: String , inputIndex :: Int }
140+
126141-- Placeholder for types I'm not sure how should we represent on frontend.
127142type ValueOut = Array
128143 { currencySymbol :: String , tokenName :: String , amount :: BigInt }
129144
130145type ListNftResultOut =
131- { input :: { transactionId :: String , inputIndex :: Int }
146+ { input :: TransactionInputOut
132147 , output :: { address :: String , value :: ValueOut , dataHash :: String }
133148 , metadata ::
134149 { seabugMetadata ::
@@ -193,7 +208,7 @@ stringToLogLevel "Warn" = Just Warn
193208stringToLogLevel " Error" = Just Error
194209stringToLogLevel _ = Nothing
195210
196- buildNftList :: NetworkId -> ListNftResult -> ListNftResultOut
211+ buildNftList :: NetworkId -> NftResult -> ListNftResultOut
197212buildNftList
198213 network
199214 { input: TransactionInput input, output: TransactionOutput output, metadata } =
@@ -298,3 +313,13 @@ buildNftData { nftCollectionArgs, nftIdArgs } = do
298313 , daoScript
299314 , daoShare
300315 }
316+
317+ buildTransactionInput :: TransactionInputOut -> Either Error TransactionInput
318+ buildTransactionInput input = do
319+ transactionId <-
320+ note (error $ " Invalid transaction id: " <> input.transactionId)
321+ $ wrap
322+ <$> hexToByteArray input.transactionId
323+ index <- note (error $ " Invalid input index: " <> show input.inputIndex) $
324+ UInt .fromInt' input.inputIndex
325+ pure $ wrap { transactionId, index }
0 commit comments