Skip to content

Commit

Permalink
Merge #3104
Browse files Browse the repository at this point in the history
3104: Handle reclaims in get transaction r=paweljakubas a=paweljakubas

<!--
Detail in a few bullet points the work accomplished in this PR.

Before you submit, don't forget to:

* Make sure the GitHub PR fields are correct:
   ✓ Set a good Title for your PR.
   ✓ Assign yourself to the PR.
   ✓ Assign one or more reviewer(s).
   ✓ Link to a Jira issue, and/or other GitHub issues or PRs.
   ✓ In the PR description delete any empty sections
     and all text commented in <!--, so that this text does not appear
     in merge commit messages.

* Don't waste reviewers' time:
   ✓ If it's a draft, select the Create Draft PR option.
   ✓ Self-review your changes to make sure nothing unexpected slipped through.

* Try to make your intent clear:
   ✓ Write a good Description that explains what this PR is meant to do.
   ✓ Jira will detect and link to this PR once created, but you can also
     link this PR in the description of the corresponding Jira ticket.
   ✓ Highlight what Testing you have done.
   ✓ Acknowledge any changes required to the Documentation.
-->


- [x] Update of `ApiTransaction` to include `depositTaken` and `depositReturned`
- [x] adding of integration tests showing the case for joining/rejoining and quitting
- [x] update core unit tests and swagger

### Comments

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number

<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Pawel Jakubas <[email protected]>
Co-authored-by: Piotr Stachyra <[email protected]>
  • Loading branch information
3 people authored Feb 9, 2022
2 parents 2423a01 + 994c5a5 commit 927aa19
Show file tree
Hide file tree
Showing 8 changed files with 622 additions and 383 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
[ expectResponseCode HTTP.status202
, expectField (#status . #getApiT) (`shouldBe` Pending)
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectField #deposit (`shouldBe` Quantity 1000000)
, expectField #depositTaken (`shouldBe` Quantity 1000000)
, expectField #inputs $ \inputs' -> do
inputs' `shouldSatisfy` all (isJust . source)
]
Expand All @@ -239,7 +239,8 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
[ expectResponseCode HTTP.status200
, expectField (#status . #getApiT) (`shouldBe` InLedger)
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectField #deposit (`shouldBe` Quantity 1000000)
, expectField #depositTaken (`shouldBe` Quantity 1000000)
, expectField #depositReturned (`shouldBe` Quantity 0)
]

let txId = getFromResponse #id rJoin
Expand Down Expand Up @@ -414,6 +415,8 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
#amount (`shouldBe` quitFeeAmt)
, expectField
(#status . #getApiT) (`shouldBe` InLedger)
, expectField #depositTaken (`shouldBe` Quantity 0)
, expectField #depositReturned (`shouldBe` Quantity 1000000)
]

let epl = Link.listTransactions @'Shelley src
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ import qualified Data.Text.Encoding as T
import qualified Network.HTTP.Types.Status as HTTP
import qualified Test.Integration.Plutus as PlutusScenario


spec :: forall n.
( DecodeAddress n
, DecodeStakeAddress n
Expand Down Expand Up @@ -841,9 +842,6 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
rGetTx <- request @(ApiTransaction n) ctx queryTx Default Empty
verify rGetTx
[ expectResponseCode HTTP.status200
, expectField
(#amount . #getQuantity)
(`shouldBe` initialAmt - (amt + fromIntegral expectedFee))
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectField (#status . #getApiT) (`shouldBe` Pending)
]
Expand Down Expand Up @@ -2175,6 +2173,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
let initialAmt = 10 * minUTxOValue (_mainEra ctx)
src <- fixtureWalletWith @n ctx [initialAmt]
dest <- emptyWallet ctx
let depositAmt = Quantity 1000000

pool1:pool2:_ <- map (view #id) . snd <$> unsafeRequest
@[ApiStakePool]
Expand All @@ -2192,7 +2191,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
(Link.createUnsignedTransaction @'Shelley src) Default delegationJoin
verify rTx1
[ expectResponseCode HTTP.status202
, expectField (#coinSelection . #depositsTaken) (`shouldBe` [Quantity 1000000])
, expectField (#coinSelection . #depositsTaken) (`shouldBe` [depositAmt])
, expectField (#coinSelection . #depositsReturned) (`shouldBe` [])
]

Expand All @@ -2219,7 +2218,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
verify rDecodedTx1
[ expectResponseCode HTTP.status202
, expectField #certificates (`shouldBe` [registerStakeKeyCert, delegatingCert])
, expectField #depositsTaken (`shouldBe` [Quantity 1000000])
, expectField #depositsTaken (`shouldBe` [depositAmt])
, expectField #depositsReturned (`shouldBe` [])
]

Expand All @@ -2239,7 +2238,8 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
[ expectResponseCode HTTP.status200
, expectField (#status . #getApiT) (`shouldBe` InLedger)
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectField #deposit (`shouldBe` Quantity 1000000)
, expectField #depositTaken (`shouldBe` depositAmt)
, expectField #depositReturned (`shouldBe` Quantity 0)
]

let txId1 = getFromResponse #id submittedTx1
Expand Down Expand Up @@ -2308,6 +2308,15 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
, expectResponseCode HTTP.status202
]

let txid2 = getFromResponse (#id) submittedTx2
let queryTx2 = Link.getTransaction @'Shelley src (ApiTxId txid2)
rGetTx2 <- request @(ApiTransaction n) ctx queryTx2 Default Empty
verify rGetTx2
[ expectResponseCode HTTP.status200
, expectField #depositTaken (`shouldBe` Quantity 0)
, expectField #depositReturned (`shouldBe` Quantity 0)
]

-- Wait for the certificate to be inserted
eventually "Certificates are inserted" $ do
let ep = Link.listTransactions @'Shelley src
Expand Down Expand Up @@ -2385,7 +2394,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
verify rTx4
[ expectResponseCode HTTP.status202
, expectField (#coinSelection . #depositsTaken) (`shouldBe` [])
, expectField (#coinSelection . #depositsReturned) (`shouldBe` [Quantity 1000000])
, expectField (#coinSelection . #depositsReturned) (`shouldBe` [depositAmt])
]
let apiTx4 = getFromResponse #transaction rTx4
signedTx4 <- signTx ctx src apiTx4 [ expectResponseCode HTTP.status202 ]
Expand All @@ -2399,20 +2408,38 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
verify rDecodedTx4
[ expectResponseCode HTTP.status202
, expectField #certificates (`shouldBe` [quittingCert])
, expectField #depositsReturned (`shouldBe` [Quantity 1000000])
, expectField #depositsReturned (`shouldBe` [depositAmt])
, expectField #depositsTaken (`shouldBe` [])
]
submittedTx4 <- submitTxWithWid ctx src signedTx4
verify submittedTx4
[ expectSuccess
, expectResponseCode HTTP.status202
]

let txid3 = getFromResponse (#id) submittedTx4
let queryTx3 = Link.getTransaction @'Shelley src (ApiTxId txid3)
rGetTx3 <- request @(ApiTransaction n) ctx queryTx3 Default Empty
verify rGetTx3
[ expectResponseCode HTTP.status200
, expectField #depositTaken (`shouldBe` Quantity 0)
, expectField #depositReturned (`shouldBe` depositAmt)
]

eventually "Wallet is not delegating" $ do
request @ApiWallet ctx (Link.getWallet @'Shelley src) Default Empty
>>= flip verify
[ expectField #delegation (`shouldBe` notDelegating [])
]

-- transaction history shows deposit returned
rGetTx4 <- request @(ApiTransaction n) ctx queryTx3 Default Empty
verify rGetTx4
[ expectResponseCode HTTP.status200
, expectField #depositTaken (`shouldBe` Quantity 0)
, expectField #depositReturned (`shouldBe` depositAmt)
]

it "TRANS_NEW_JOIN_01b - Invalid pool id" $ \ctx -> runResourceT $ do

wa <- fixtureWallet ctx
Expand Down
19 changes: 9 additions & 10 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,8 @@ import Safe
( lastMay )
import Statistics.Quantile
( medianUnbiased, quantiles )
import Text.Pretty.Simple
( pShow )
import Type.Reflection
( Typeable, typeRep )
import UnliftIO.Exception
Expand Down Expand Up @@ -568,8 +570,7 @@ import qualified Data.List.NonEmpty as NE
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Vector as V
import Text.Pretty.Simple
( pShow )


-- $Development
-- __Naming Conventions__
Expand Down Expand Up @@ -2106,7 +2107,6 @@ getTxExpiry ti maybeTTL = do
constructTxMeta
:: forall ctx s k.
( HasDBLayer IO s k ctx
, IsOwned s k
)
=> ctx
-> WalletId
Expand All @@ -2120,21 +2120,19 @@ constructTxMeta ctx wid txCtx inps outs = db & \DBLayer{..} -> do
$ withNoSuchWallet wid
$ readCheckpoint wid
liftIO $
mkTxMetaWithoutSel (currentTip cp) (getState cp) txCtx inps outs
mkTxMetaWithoutSel (currentTip cp) txCtx inps outs
where
db = ctx ^. dbLayer @IO @s @k

mkTxMetaWithoutSel
:: IsOurs s Address
=> BlockHeader
-> s
:: BlockHeader
-> TransactionCtx
-> [(TxIn, Coin)]
-> [TxOut]
-> IO TxMeta
mkTxMetaWithoutSel blockHeader wState txCtx inps outs =
mkTxMetaWithoutSel blockHeader txCtx inps outs =
let
amtOuts = F.fold $ mapMaybe (`ourCoin` wState) outs
amtOuts = F.fold $ map txOutCoin outs

amtInps
= F.fold (map snd inps)
Expand All @@ -2144,7 +2142,7 @@ mkTxMetaWithoutSel blockHeader wState txCtx inps outs =
NoWithdrawal -> Prelude.id
in return TxMeta
{ status = Pending
, direction = Outgoing
, direction = if amtInps > amtOuts then Outgoing else Incoming
, slotNo = blockHeader ^. #slotNo
, blockHeight = blockHeader ^. #blockHeight
, amount = Coin.distance amtInps amtOuts
Expand Down Expand Up @@ -3082,6 +3080,7 @@ data ErrSubmitTransaction
= ErrSubmitTransactionNoSuchWallet ErrNoSuchWallet
| ErrSubmitTransactionForeignWallet
| ErrSubmitTransactionPartiallySignedOrNoSignedTx Int Int
| ErrSubmitTransactionMultidelegationNotSupported
deriving (Show, Eq)

-- | Errors that can occur when constructing an unsigned transaction.
Expand Down
Loading

0 comments on commit 927aa19

Please sign in to comment.