Skip to content

Commit

Permalink
Add expiresAt field to api transaction responses
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Oct 2, 2020
1 parent a489f6d commit 3deebc5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import Cardano.Wallet.Api.Types
, ApiAddress
, ApiByronWallet
, ApiFee (..)
, ApiSlotReference (..)
, ApiT (..)
, ApiTimeReference (..)
, ApiTransaction
Expand Down Expand Up @@ -609,16 +608,21 @@ spec = describe "SHELLEY_TRANSACTIONS" $ do
, expectResponseCode HTTP.status202
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectField (#status . #getApiT) (`shouldBe` Pending)
, expectField #expiresAt (`shouldSatisfy` isJust)
]

-- Get insertion slot and expiry slot out of response.
-- This would be easier with Control.Lens.
-- This stuff would be easier with Control.Lens...

-- Get insertion slot and out of response.
let (_, Right apiTx) = r
let (Just (ApiTimeReference _ sinceBlock)) = apiTx ^. #pendingSince
let sl = sinceBlock ^. #absoluteSlotNumber . #getApiT
let (Just (ApiSlotReference _ (ApiT ttl))) = apiTx ^. #expiresAt

ttl - sl `shouldBe` SlotNo 7200 -- the hardcoded default
-- The expected expiry slot (adds the hardcoded default ttl)
let ttl = sl + SlotNo 7200

(view #absoluteSlotNumber <$> (apiTx ^. #expiresAt))
`shouldBe` Just (ApiT ttl)

it "TRANSMETA_CREATE_01 - Transaction with metadata" $ \ctx -> do
(wa, wb) <- (,) <$> fixtureWallet ctx <*> emptyWallet ctx
Expand Down
14 changes: 13 additions & 1 deletion lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ import Cardano.Wallet.Api.Types
, ApiPostRandomAddressData (..)
, ApiPutAddressesData (..)
, ApiSelectCoinsData (..)
, ApiSlotReference (..)
, ApiT (..)
, ApiTimeReference (..)
, ApiTransaction (..)
Expand Down Expand Up @@ -1803,7 +1804,8 @@ mkApiTransaction
-> m (ApiTransaction n)
mkApiTransaction ti txid ins outs ws (meta, timestamp) txMeta setTimeReference = do
timeRef <- timeReference
return $ tx & setTimeReference .~ Just timeRef
expRef <- expirySlotReference
return $ tx & setTimeReference .~ Just timeRef & #expiresAt .~ expRef
where
tx :: ApiTransaction n
tx = ApiTransaction
Expand Down Expand Up @@ -1839,6 +1841,16 @@ mkApiTransaction ti txid ins outs ws (meta, timestamp) txMeta setTimeReference =
, absoluteSlotNumber = ApiT $ meta ^. #slotNo
}

expirySlotReference :: m (Maybe ApiSlotReference)
expirySlotReference = case meta ^. #expiry of
Just expirySlot -> do
expiryTimestamp <- ti (startTime expirySlot)
pure $ Just $ ApiSlotReference
{ time = expiryTimestamp
, absoluteSlotNumber = ApiT expirySlot
}
Nothing -> pure Nothing

toAddressAmount :: TxOut -> AddressAmount (ApiT Address, Proxy n)
toAddressAmount (TxOut addr c) =
AddressAmount (ApiT addr, Proxy @n) (mkApiCoin c)
Expand Down

0 comments on commit 3deebc5

Please sign in to comment.