-
Notifications
You must be signed in to change notification settings - Fork 25
Add typescript declarations for cardano-wasm API and generate it automatically from ApiInfo
#901
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d502193
Created TypeScript definition for existing API
palas 7c95dcc
Generate TypeScript definitions from `ApiInfo`
palas 3c35371
Add test to ensure the generated TypeScript file matches
palas 4986b03
Make cardano-wasm-golden test build in nix
carbolymer d4a5e5b
Make test suite build minimally for `wasm`
palas 88979f1
Clarify explanation for adding `max-backjumps: 50000`
palas 57555f5
Move golden tests to `test/cardano-wasm-golden`
palas 8dc0d12
Use `OverloadedStrings`
palas 6a3f545
Improve haddocks for datatypes in `Info.hs`
palas bb53acb
Move wrapper files outside of the example folder (into `lib-wrapper` …
palas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| module Main (main) where | ||
| module Main where | ||
|
|
||
| import Cardano.Wasm.Internal.Api.Info (apiInfo) | ||
| import Cardano.Wasm.Internal.Api.InfoToTypeScript (apiInfoToTypeScriptFile) | ||
| import Cardano.Wasm.Internal.Api.TypeScriptDefs (printTypeScriptFile) | ||
|
|
||
| main :: IO () | ||
| main = pure () | ||
| main = printTypeScriptFile (apiInfoToTypeScriptFile apiInfo) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
palas marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // cardano-api.d.ts | ||
|
|
||
| export default initialise; | ||
|
|
||
| /** | ||
| * Initialises the Cardano API. | ||
| * @returns A promise that resolves to the main `CardanoAPI` object. | ||
| */ | ||
| declare function initialise(): Promise<CardanoAPI>; | ||
|
|
||
| /** | ||
| * Represents an unsigned transaction. | ||
| */ | ||
| declare interface UnsignedTx { | ||
| /** | ||
| * The type of the object, used for identification (the "UnsignedTx" string). | ||
| */ | ||
| objectType: string; | ||
|
|
||
| /** | ||
| * Adds a simple transaction input to the transaction. | ||
| * @param txId The transaction ID of the input UTxO. | ||
| * @param txIx The index of the input within the UTxO. | ||
| * @returns The `UnsignedTx` object with the added input. | ||
| */ | ||
| addTxInput(txId: string, txIx: number): UnsignedTx; | ||
|
|
||
| /** | ||
| * Adds a simple transaction output to the transaction. | ||
| * @param destAddr The destination address. | ||
| * @param lovelaceAmount The amount in lovelaces to output. | ||
| * @returns The `UnsignedTx` object with the added output. | ||
| */ | ||
| addSimpleTxOut(destAddr: string, lovelaceAmount: bigint): UnsignedTx; | ||
|
|
||
| /** | ||
| * Sets the fee for the transaction. | ||
| * @param lovelaceAmount The fee amount in lovelaces. | ||
| * @returns The `UnsignedTx` object with the set fee. | ||
| */ | ||
| setFee(lovelaceAmount: bigint): UnsignedTx; | ||
|
|
||
| /** | ||
| * Estimates the minimum fee for the transaction. | ||
palas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * @param protocolParams The protocol parameters. | ||
| * @param numKeyWitnesses The number of key witnesses. | ||
| * @param numByronKeyWitnesses The number of Byron key witnesses. | ||
| * @param totalRefScriptSize The total size of reference scripts in bytes. | ||
| * @returns A promise that resolves to the estimated minimum fee in lovelaces. | ||
| */ | ||
| estimateMinFee(protocolParams: any, numKeyWitnesses: number, numByronKeyWitnesses: number, totalRefScriptSize: number): Promise<BigInt>; | ||
|
|
||
| /** | ||
| * Signs the transaction with a payment key. | ||
| * @param signingKey The signing key to witness the transaction. | ||
| * @returns A promise that resolves to a `SignedTx` object. | ||
| */ | ||
| signWithPaymentKey(signingKey: string): Promise<SignedTx>; | ||
| } | ||
|
|
||
| /** | ||
| * Represents a signed transaction. | ||
| */ | ||
| declare interface SignedTx { | ||
| /** | ||
| * The type of the object, used for identification (the "SignedTx" string). | ||
| */ | ||
| objectType: string; | ||
|
|
||
| /** | ||
| * Adds an extra signature to the transaction with a payment key. | ||
| * @param signingKey The signing key to witness the transaction. | ||
| * @returns The `SignedTx` object with the additional signature. | ||
| */ | ||
| alsoSignWithPaymentKey(signingKey: string): SignedTx; | ||
|
|
||
| /** | ||
| * Converts the signed transaction to its CBOR representation. | ||
| * @returns A promise that resolves to the CBOR representation of the transaction as a hex string. | ||
| */ | ||
| txToCbor(): Promise<string>; | ||
| } | ||
|
|
||
| /** | ||
| * The main Cardano API object with static methods. | ||
| */ | ||
| declare interface CardanoAPI { | ||
| /** | ||
| * The type of the object, used for identification (the "CardanoAPI" string). | ||
| */ | ||
| objectType: string; | ||
|
|
||
| /** | ||
| * Creates a new Conway-era transaction. | ||
| * @returns A promise that resolves to a new `UnsignedTx` object. | ||
| */ | ||
| newConwayTx(): Promise<UnsignedTx>; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| /// <reference path="./cardano-api.d.ts" /> | ||
|
|
||
| import { WASI } from "https://unpkg.com/@bjorn3/[email protected]/dist/index.js"; | ||
| import ghc_wasm_jsffi from "./cardano-wasm.js"; | ||
| const __exports = {}; | ||
| const wasi = new WASI([], [], []); | ||
| async function initialize() { | ||
| async function initialise() { | ||
| let { instance } = await WebAssembly.instantiateStreaming(fetch("./cardano-wasm.wasm"), { | ||
| ghc_wasm_jsffi: ghc_wasm_jsffi(__exports), | ||
| wasi_snapshot_preview1: wasi.wasiImport, | ||
|
|
@@ -12,7 +14,7 @@ async function initialize() { | |
|
|
||
| // Wrap a function with variable arguments to make the parameters inspectable | ||
| function fixateArgs(params, func) { | ||
| const paramString = params.join(','); | ||
| const paramString = params.map(p => p.name).join(','); | ||
| // Dynamically create a function that captures 'func' from the closure. | ||
| // 'this' and 'arguments' are passed through from the wrapper to 'func'. | ||
| // Using eval allows the returned function to have named parameters for inspectability. | ||
|
|
@@ -26,7 +28,7 @@ async function initialize() { | |
|
|
||
| // Same as fixateArgs but for async functions | ||
| async function fixateArgsAsync(params, func) { | ||
| const paramString = params.join(','); | ||
| const paramString = params.map(p => p.name).join(','); | ||
| // Dynamically create an async function. | ||
| const wrapper = eval(` | ||
| (async function(${paramString}) { | ||
|
|
@@ -80,7 +82,7 @@ async function initialize() { | |
| }); | ||
|
|
||
| // Populate the main API object with static methods | ||
| apiInfo.staticMethods.forEach(method => { | ||
| apiInfo.mainObject.methods.forEach(method => { | ||
| cardanoAPI[method.name] = async function (...args) { | ||
| const resultPromise = instance.exports[method.name](...args); | ||
|
|
||
|
|
@@ -93,4 +95,4 @@ async function initialize() { | |
| }); | ||
| return cardanoAPI; | ||
| } | ||
| export default initialize; | ||
| export default initialise; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import cardano_api from "./cardano-api.js"; | ||
|
|
||
| let promise = cardano_api(); | ||
|
|
||
| async function get_protocol_params() { | ||
| const response = await fetch("./preview_pparams.json"); | ||
| return (await response.json()); | ||
| } | ||
|
|
||
| let protocolParams = await get_protocol_params(); | ||
|
|
||
| async function do_async_work() { | ||
| let api = await promise; | ||
| console.log("Api object:"); | ||
| console.log(api); | ||
|
|
||
| let emptyTx = await api.newConwayTx(); | ||
| console.log("UnsignedTx object:"); | ||
| console.log(emptyTx); | ||
|
|
||
| let tx = await emptyTx.addTxInput("be6efd42a3d7b9a00d09d77a5d41e55ceaf0bd093a8aa8a893ce70d9caafd978", 0) | ||
| .addSimpleTxOut("addr_test1vzpfxhjyjdlgk5c0xt8xw26avqxs52rtf69993j4tajehpcue4v2v", 10_000_000n) | ||
|
|
||
| let feeEstimate = await tx.estimateMinFee(protocolParams, 1, 0, 0); | ||
| console.log("Estimated fee:"); | ||
| console.log(feeEstimate); | ||
|
|
||
| let signedTx = await tx.setFee(feeEstimate) | ||
| .signWithPaymentKey("addr_sk1648253w4tf6fv5fk28dc7crsjsaw7d9ymhztd4favg3cwkhz7x8sl5u3ms"); | ||
| console.log("SignedTx object:"); | ||
| console.log(signedTx); | ||
|
|
||
| let txCbor = await signedTx.txToCbor(); | ||
| console.log("Tx CBOR:"); | ||
| console.log(txCbor); | ||
| } | ||
|
|
||
| do_async_work().then(() => { }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,7 @@ | ||
| <html> | ||
|
|
||
| <body> | ||
| <script type="module"> | ||
| import cardano_api from "./cardano-api.js"; | ||
| let promise = cardano_api(); | ||
| async function get_protocol_params() { | ||
| const response = await fetch("./preview_pparams.json"); | ||
| return (await response.json()); | ||
| } | ||
| let protocolParams = await get_protocol_params(); | ||
| async function do_async_work() { | ||
| let api = await promise; | ||
| console.log("Api object:"); | ||
| console.log(api); | ||
|
|
||
| let emptyTx = await api.newConwayTx(); | ||
| console.log("UnsignedTx object:"); | ||
| console.log(emptyTx); | ||
|
|
||
| let tx = await emptyTx.addTxInput("be6efd42a3d7b9a00d09d77a5d41e55ceaf0bd093a8aa8a893ce70d9caafd978", 0) | ||
| .addSimpleTxOut("addr_test1vzpfxhjyjdlgk5c0xt8xw26avqxs52rtf69993j4tajehpcue4v2v", 10_000_000n) | ||
|
|
||
| let feeEstimate = await tx.estimateMinFee(protocolParams, 1, 0, 0); | ||
| console.log("Estimated fee:"); | ||
| console.log(feeEstimate); | ||
|
|
||
| let signedTx = await tx.setFee(feeEstimate) | ||
| .signWithPaymentKey("addr_sk1648253w4tf6fv5fk28dc7crsjsaw7d9ymhztd4favg3cwkhz7x8sl5u3ms"); | ||
| console.log("SignedTx object:"); | ||
| console.log(signedTx); | ||
|
|
||
| let txCbor = await signedTx.txToCbor(); | ||
| console.log("Tx CBOR:"); | ||
| console.log(txCbor); | ||
| } | ||
| do_async_work().then(() => { }); | ||
| </script> | ||
| <script type="module" src="./example.js"></script> | ||
| </body> | ||
|
|
||
| </html> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.