Skip to content

Commit

Permalink
Expose contract name in the CIS2Tokens query.
Browse files Browse the repository at this point in the history
  • Loading branch information
abizjak committed Nov 28, 2022
1 parent 5b4b6eb commit 13bdcb1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased changes

## 0.23.0

- Expose "contract name" in the CIS2TokenMetadata response.

## 0.22.2

- Make the closing of connection to the node more robust when reconnecting. In
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: wallet-proxy
version: 0.22.2
version: 0.23.0
github: "Concordium/concordium-wallet-proxy"
author: "Concordium"
maintainer: "developers@concordium.com"
Expand Down
33 changes: 22 additions & 11 deletions src/Proxy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ getCIS2TokenMetadata index subindex = do
let serializedParam = Wasm.Parameter . BSS.toShort . S.runPut $ do
S.putWord16le (fromIntegral (length tids))
mapM_ S.put tids
cis2InvokeHelper contractAddr (Wasm.EntrypointName "tokenMetadata") serializedParam nrg $ \rv -> do
cis2InvokeHelper contractAddr (Wasm.EntrypointName "tokenMetadata") serializedParam nrg $ \name rv -> do
let getURLs = do
len <- S.getWord16le
replicateM (fromIntegral len) getMetadataUrl
Expand All @@ -1062,7 +1062,9 @@ getCIS2TokenMetadata index subindex = do
respond400Error EMInvokeFailed RequestInvalid
Right urls ->
sendResponse $
AE.toJSON
object [
"contractName" .= name,
"metadata" .= AE.toJSON
( zipWith
( \tid md ->
object
Expand All @@ -1074,6 +1076,7 @@ getCIS2TokenMetadata index subindex = do
tids
urls
)
]

getCIS2TokenBalance :: Word64 -> Word64 -> Text -> Handler TypedContent
getCIS2TokenBalance index subindex addrText = do
Expand All @@ -1086,7 +1089,7 @@ getCIS2TokenBalance index subindex addrText = do
S.putWord16le (fromIntegral (length tids))
mapM_ S.put (zip tids (repeat (AddressAccount addr)))
let contractAddr = ContractAddress (ContractIndex index) (ContractSubindex subindex)
cis2InvokeHelper contractAddr (Wasm.EntrypointName "balanceOf") serializedParam nrg $ \rv -> do
cis2InvokeHelper contractAddr (Wasm.EntrypointName "balanceOf") serializedParam nrg $ \_ rv -> do
let getBalances = do
len <- S.getWord16le
replicateM (fromIntegral len) getTokenBalance
Expand Down Expand Up @@ -1121,8 +1124,8 @@ cis2InvokeHelper ::
Wasm.Parameter ->
-- |Energy to allow for the invoke.
Energy ->
-- |Continuation applied to a return value produced by a successful result.
(BS8.ByteString -> Handler TypedContent) ->
-- |Continuation applied to the name of the contract (without @_init@) and the return value produced by a successful result.
(Text -> BS8.ByteString -> Handler TypedContent) ->
HandlerFor Proxy TypedContent
cis2InvokeHelper contractAddr entrypoint serializedParam nrg k = do
let invokeContext contractName =
Expand All @@ -1134,34 +1137,42 @@ cis2InvokeHelper contractAddr entrypoint serializedParam nrg k = do
, ccParameter = serializedParam
, ccEnergy = nrg
}
let parseInstance Null = return Nothing
parseInstance v = flip (AE.withObject "ContractInfo") v $ \obj -> do
n <- obj .: "name"
return (Just n)
-- Query the name of a contract at the given block hash.
let queryContractName block = do
ci <- getInstanceInfo (Text.decodeUtf8 . BSL.toStrict . AE.encode $ contractAddr) block
case ci of
Left err -> return (Left err)
Right v -> return (parseEither (AE.withObject "ContractInfo" (.: "name")) (grpcResponseVal v))
Right v -> case parseEither parseInstance (grpcResponseVal v) of
Left err -> return (Left err)
Right mci -> return (Right (mci <$ v))
let query = do
withLastFinalBlockHash Nothing $ \bh -> do
name <- queryContractName bh
case name of
Left err -> return (Left err)
Right n -> do
Right v@GRPCResponse{grpcResponseVal = Nothing} -> return (Right (Nothing <$ v))
Right (GRPCResponse{grpcResponseVal = Just n}) -> do
let ctx = invokeContext n
let invokeContextArg = Text.decodeUtf8 . BSL.toStrict . AE.encode $ ctx
res <- invokeContract invokeContextArg bh
case res of
Left err -> return (Left err)
Right v -> case AE.fromJSON (grpcResponseVal v) of
AE.Error jsonErr -> return (Left jsonErr)
AE.Success ir -> return (Right (ir <$ v))
AE.Success ir -> return (Right (Just (Wasm.initContractName n, ir) <$ v))
runGRPC query $ \case
InvokeContract.Failure{..} -> do
Nothing -> respond404Error $ EMErrorResponse NotFound
Just (_, InvokeContract.Failure{..}) -> do
$logDebug $ "Invoke failed: " <> Text.pack (show rcrReason)
respond400Error EMInvokeFailed RequestInvalid
InvokeContract.Success{..} -> do
Just (name, InvokeContract.Success{..}) -> do
case rcrReturnValue of
Nothing -> respond400Error EMV0Contract RequestInvalid
Just rv -> k rv
Just rv -> k name rv

-- |Balance of a CIS2 token.
newtype TokenBalance = TokenBalance Integer
Expand Down

0 comments on commit 13bdcb1

Please sign in to comment.