-
Notifications
You must be signed in to change notification settings - Fork 217
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
Multi-asset API extensions #2447
Conversation
e3a685a
to
3611a2d
Compare
lib/core/src/Cardano/Wallet.hs
Outdated
@@ -1905,7 +1905,10 @@ mkTxMeta ti' blockHeader wState tx cs expiry = | |||
, direction = if amtInps > amtOuts then Outgoing else Incoming | |||
, slotNo = blockHeader ^. #slotNo | |||
, blockHeight = blockHeader ^. #blockHeight | |||
, amount = Quantity $ distance amtInps amtOuts | |||
-- fixme: ADP-347 calculate asset balance too |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The amount
should remain unchanged and ada-only. We will not be providing this information for assets. It is less useful for assets since fees can only be paid in Ada. What assets are being sent can be figured out from the resolved inputs and outputs fully.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK then. Nonetheless I thought it would be good to calculate a total of assets moved in or out of the wallet by the transaction. In mkTxMeta
it's easier to know which tx outputs are "ours". Resolved inputs and outputs show the address only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feature is tracked by ADP-683.
@@ -376,7 +378,9 @@ prefilterBlock b u0 = runState $ do | |||
, direction = dir | |||
, slotNo = b ^. #header . #slotNo | |||
, blockHeight = b ^. #header . #blockHeight | |||
, amount = Quantity amt | |||
-- fixme: ADP-347 | |||
-- fixme: why on earth do we have both Coin and Quantity "lovelace" Natural? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the very beginning, the Quantity
type was only used in the Api as a way to generate pretty JSON. It has one day escaped its cage and spread over the rest of the codebase as a quick replacement for newtypes over numeric types.. So in some places we have Coin
, in some Word64
, in some Quantity "lovelace" Natural
or Quantity "lovelace" Word64
or also Fee
which is very much the same thing as Coin
.
instance FromJSON (ApiT W.TokenMap) where | ||
parseJSON = fmap (ApiT . W.getFlat) . parseJSON | ||
instance ToJSON (ApiT W.TokenMap) where | ||
toJSON = toJSON . W.Flat . getApiT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @rvl
I'm just curious about the reason for choosing Flat
here. Is there a reason to not use Nested
?
(From recollection, the Daedalus team indicated that they preferred a nested representation, where assets were grouped by token policy.)
Apologies if I'm missing some context here which would make the reason for this choice obvious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nesting makes it more complicated to process. The fact that there's a hierarchy policy_id -> asset_name is actually an implementation detail. End-users care about tokens, and tokens are uniquely identified by a pair (policy_id, asset_name). Tokens with different names are different which is why in the end, a flat views of assets makes makes more sense in the interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best place to comment on the API would be #2446. But it's one line of javascript to convert between flat and nested. EIther way would probably be fine.
@@ -81,3 +83,11 @@ instance ToText TokenName where | |||
|
|||
instance FromText TokenName where | |||
fromText = pure . UnsafeTokenName |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related question: are we planning to change the type of TokenName
from Text
to ByteString
(or perhaps ShortByteString
), given that it can be any sequence of bytes between 0 bytes and 32 bytes in length (and therefore might not be representable by Text
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course, because Text is the wrong type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
@@ -2044,6 +2084,7 @@ mkApiTransaction ti txid mfee ins outs ws (meta, timestamp) txMeta setTimeRefere | |||
, inputs = [ApiTxInput (fmap toAddressAmount o) (ApiT i) | (i, o) <- ins] | |||
, outputs = toAddressAmount <$> outs | |||
, withdrawals = mkApiWithdrawal @n <$> Map.toList ws | |||
, forge = mempty -- fixme: ADP-604 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Should this be mint
? And do we need to support this particular field as part of ADP-604?
, forge = mempty -- fixme: ADP-604 | |
, mint = mempty -- fixme: ADP-604 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok thanks - i'll use "mint" to match the terminology from Cardano.Api.
The API field is a placeholder to help Daedalus team with integration.
The correct place to review these names is in #2446 - because ApiTransaction just mirrors what's in swagger.yaml
.
f4c5489
to
fc15718
Compare
-> Handler [ApiAsset] | ||
listAssets ctx (ApiT wid) = | ||
Handler $ ExceptT $ withDatabase df wid $ \db -> runHandler $ do | ||
let wrk = hoistResource db (MsgFromWorker wid) ctx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use withWorkerCtx
instead:
withWorkerCtx @_ @s @k ctx wid liftE liftE $ \wrk -> liftHandler $
W.readWallet @_ @s @k wrk wid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
withWorkerCtx
comes with some error handling that is common to all wallets accesses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that's better.
Nothing -> case T.decodeUtf8' assetNameBytes of | ||
Right "" -> hexText $ getHash $ W.unTokenPolicyId pid | ||
Right text -> text | ||
Left _ -> hexText assetNameBytes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure to get the rationale for the Nothing
branch in this function. The assetName
isn't something that is supposed to be user-facing or displayed to user.
I'd suggest to leave it null
if there's no metadata (that is, simply leave it as metadata.name
) or, to go with your first idea in the cryptpad spec: a bech32 encoded string comprised of the policyId | assetName, possibly hashed. Though, it's not really anymore displayable than the policyId and assetName.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I have removed displayName for the time being.
AddressAmount | ||
<$> v .: "address" | ||
<*> (v .: "amount" >>= validateCoin) | ||
<*> v .:? "assets" .!= mempty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
{ status = InLedger | ||
, direction = dir | ||
, slotNo = b ^. #header . #slotNo | ||
, blockHeight = b ^. #header . #blockHeight | ||
, amount = Quantity amt | ||
, amount = amount |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be okay-ish for Ada / Coin because we know that the max supply is 42B. But we should generally be careful when summing token quantities. Even if each token individually fit in a Word64, their sum might not. So for anything that regards folds or sums, we should stick to Natural
IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the very least, if changing this to Coin
, I'd like to leave a note in the code about exactly this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added a note to Coin.hs
.
In Model.hs
, any sums are limited to a single transaction, so it's all fine for ada amounts, and token amounts use Natural.
I would not be against changing newtype Coin to use Natural or Integer, just to remove all doubt about overflow.
f3d8bef
to
fc978b5
Compare
This should be removed once PR #2447 is merged.
fc978b5
to
2fbfaab
Compare
Rebased and squashed... bors r+ |
2447: Multi-asset API extensions r=rvl a=rvl ### Issue Number ADP-604 ### Overview Starting implementation of API spec in #2446. - [x] listAssets endpoint - [x] getAsset endpoint - [x] Added `ApiWalletAssetsBalance` which provides the balance of non-ade assets. - [x] Removed WalletBalance from Primitive.Types and replaced `ApiT WalletBalance` with `ApiWalletBalance`. - [ ] Some kind of integration test for these endpoints. - [x] Remove `Quantity "lovelace" a` where possible, except for the API. - [x] TokenName inner type is now ByteString, add length validation. ### Comments - This branch is based on top of the PR #2446 branch. Co-authored-by: KtorZ <[email protected]> Co-authored-by: Rodney Lorrimar <[email protected]>
Build failed:
|
bors r+ |
Merge conflict. |
Some reordering required to please the openapi-spec-validator.
2fbfaab
to
af2ce54
Compare
rebased bors r+ |
2447: Multi-asset API extensions r=Anviking a=rvl ### Issue Number ADP-604 ### Overview Starting implementation of API spec in #2446. - [x] listAssets endpoint - [x] getAsset endpoint - [x] Added `ApiWalletAssetsBalance` which provides the balance of non-ade assets. - [x] Removed WalletBalance from Primitive.Types and replaced `ApiT WalletBalance` with `ApiWalletBalance`. - [ ] Some kind of integration test for these endpoints. - [x] Remove `Quantity "lovelace" a` where possible, except for the API. - [x] TokenName inner type is now ByteString, add length validation. ### Comments - This branch is based on top of the PR #2446 branch. Co-authored-by: KtorZ <[email protected]> Co-authored-by: Rodney Lorrimar <[email protected]>
This should be removed once PR #2447 is merged.
Build failed:
Think this is reward-related: |
bors r+ |
2447: Multi-asset API extensions r=Anviking a=rvl ### Issue Number ADP-604 ### Overview Starting implementation of API spec in #2446. - [x] listAssets endpoint - [x] getAsset endpoint - [x] Added `ApiWalletAssetsBalance` which provides the balance of non-ade assets. - [x] Removed WalletBalance from Primitive.Types and replaced `ApiT WalletBalance` with `ApiWalletBalance`. - [ ] Some kind of integration test for these endpoints. - [x] Remove `Quantity "lovelace" a` where possible, except for the API. - [x] TokenName inner type is now ByteString, add length validation. ### Comments - This branch is based on top of the PR #2446 branch. Co-authored-by: KtorZ <[email protected]> Co-authored-by: Rodney Lorrimar <[email protected]>
Build failed:
|
bors retry |
Build succeeded: |
Issue Number
ADP-604
Overview
Starting implementation of API spec in #2446.
ApiWalletAssetsBalance
which provides the balance of non-ade assets.ApiT WalletBalance
withApiWalletBalance
.Quantity "lovelace" a
where possible, except for the API.Comments