Skip to content

Commit f16c468

Browse files
authored
Merge pull request #931 from IntersectMBO/fix-coin-serialisation
Fix conversion from Coin to BigInt (it was using Number)
2 parents cc529e1 + 2dff94a commit f16c468

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

cardano-wasm/example/example.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ function log(out) {
2727
output.innerText += "    " + key + ": " + text + "\n";
2828
}
2929
output.innerText += "  }\n";
30+
} else if (typeof (out) == "bigint") {
31+
output.innerText += "> " + out.toString() + "n\n";
3032
} else {
3133
output.innerText += "> " + JSON.stringify(out) + "\n";
3234
}

cardano-wasm/grpc-example/example.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ function log(out) {
1818
output.innerText += "    " + key + ": " + text + "\n";
1919
}
2020
output.innerText += "  }\n";
21+
} else if (typeof (out) == "bigint") {
22+
output.innerText += "> " + out.toString() + "n\n";
2123
} else {
2224
output.innerText += "> " + JSON.stringify(out) + "\n";
2325
}

cardano-wasm/js-test/basic-test.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ test('test output matches', async ({ page }) => {
88
// Wait for the test to finish running (we signal this by creating a tag with id "finish-tag" and text "Finished test!")
99
await expect(page.locator('#finish-tag')).toHaveText("Finished test!");
1010
// Check the output of the test (from the example folder), which is displayed in the code element with id "test-output". The output contains information about the various objects and results of trying some of the functions.
11-
await expect(page.locator('#test-output')).toHaveText("> \"Api object:\"> [object] { objectType: cardano-api newConwayTx: async function(...args) newGrpcConnection: async function(...args) generatePaymentWallet: async function(...args) restorePaymentWalletFromSigningKeyBech32: async function(...args) generateTestnetPaymentWallet: async function(...args) restoreTestnetPaymentWalletFromSigningKeyBech32: async function(...args) }> \"Bech32 of address:\"> \"addr_test1vp93p9em3regvgylxuvet6fgr3e9sn259pcejgrk4ykystcs7v8j6\"> \"UnsignedTx object:\"> [object] { objectType: UnsignedTx addTxInput: function(txId,txIx) addSimpleTxOut: function(destAddr,lovelaceAmount) setFee: function(lovelaceAmount) estimateMinFee: function(protocolParams,numKeyWitnesses,numByronKeyWitnesses,totalRefScriptSize) signWithPaymentKey: function(signingKey) }> \"Estimated fee:\"> 164005> \"SignedTx object:\"> [object] { objectType: SignedTx alsoSignWithPaymentKey: function(signingKey) txToCbor: function() }> \"Tx CBOR:\"> \"84a300d9010281825820be6efd42a3d7b9a00d09d77a5d41e55ceaf0bd093a8aa8a893ce70d9caafd97800018182581d6082935e44937e8b530f32ce672b5d600d0a286b4e8a52c6555f659b871a00989680021a000280a5a100d9010281825820adfc1c30385916da87db1ba3328f0690a57ebb2a6ac9f6f86b2d97f943adae005840a49259b5977aea523b46f01261fbff93e0899e8700319e11f5ab96b67eb628fca1a233ce2d50ee3227b591b84f27237d920d63974d65728362382f751c4d9400f5f6\"");
11+
await expect(page.locator('#test-output')).toHaveText("> \"Api object:\"> [object] { objectType: cardano-api newConwayTx: async function(...args) newGrpcConnection: async function(...args) generatePaymentWallet: async function(...args) restorePaymentWalletFromSigningKeyBech32: async function(...args) generateTestnetPaymentWallet: async function(...args) restoreTestnetPaymentWalletFromSigningKeyBech32: async function(...args) }> \"Bech32 of address:\"> \"addr_test1vp93p9em3regvgylxuvet6fgr3e9sn259pcejgrk4ykystcs7v8j6\"> \"UnsignedTx object:\"> [object] { objectType: UnsignedTx addTxInput: function(txId,txIx) addSimpleTxOut: function(destAddr,lovelaceAmount) setFee: function(lovelaceAmount) estimateMinFee: function(protocolParams,numKeyWitnesses,numByronKeyWitnesses,totalRefScriptSize) signWithPaymentKey: function(signingKey) }> \"Estimated fee:\"> 164005n> \"SignedTx object:\"> [object] { objectType: SignedTx alsoSignWithPaymentKey: function(signingKey) txToCbor: function() }> \"Tx CBOR:\"> \"84a300d9010281825820be6efd42a3d7b9a00d09d77a5d41e55ceaf0bd093a8aa8a893ce70d9caafd97800018182581d6082935e44937e8b530f32ce672b5d600d0a286b4e8a52c6555f659b871a00989680021a000280a5a100d9010281825820adfc1c30385916da87db1ba3328f0690a57ebb2a6ac9f6f86b2d97f943adae005840a49259b5977aea523b46f01261fbff93e0899e8700319e11f5ab96b67eb628fca1a233ce2d50ee3227b591b84f27237d920d63974d65728362382f751c4d9400f5f6\"");
1212
});

cardano-wasm/src/Cardano/Wasm/Internal/JavaScript/Bridge.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ fromJSBigInt val = do
6868
[(n, "")] -> return n
6969
_ -> error ("Wrong format for argument when deserialising, expected integer but got: " ++ show str)
7070

71+
-- | Convert a Haskell @String@ to a JavaScript @BigInt@ (@JSVal@).
72+
foreign import javascript unsafe "BigInt($1)"
73+
js_toBigInt :: JSString -> IO JSVal
74+
7175
-- | Convert a Haskell value with @ToJSON@ instance to a JavaScript object (@JSVal)
7276
jsonToJSVal :: Api.ToJSON a => a -> IO JSVal
7377
jsonToJSVal =
@@ -147,6 +151,10 @@ instance ToJSVal Wasm.GrpcObject JSGrpc where
147151
toJSVal :: Wasm.GrpcObject -> IO JSGrpc
148152
toJSVal (Wasm.GrpcObject client) = return client
149153

154+
instance ToJSVal Ledger.Coin JSCoin where
155+
toJSVal :: Ledger.Coin -> IO JSCoin
156+
toJSVal (Ledger.Coin n) = js_toBigInt $ toJSString $ show n
157+
150158
-- |  Type class that provides functions to convert values from JavaScript to Haskell.
151159
class FromJSVal jsType haskellType where
152160
fromJSVal :: HasCallStack => jsType -> IO haskellType

0 commit comments

Comments
 (0)