Skip to content
Merged
98 changes: 98 additions & 0 deletions cardano-wasm/example/cardano-api.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// cardano-api.d.ts

export default initialize;

/**
* Initializes the Cardano API.
* @returns A promise that resolves to the main `CardanoAPI` object.
*/
declare function initialize(): 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 lovelace 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 lovelace.
* @returns The `UnsignedTx` object with the set fee.
*/
setFee(lovelaceAmount: bigint): UnsignedTx;

/**
* Estimates the minimum fee for the transaction.
* @param protocolParams The protocol parameters.
* @param numExtraKeyWitnesses The number of extra key witnesses (in addition to the ones already added).
* @param numExtraByronKeyWitnesses The number of extra Byron key witnesses.
* @param totalRefScriptSize The total size of reference scripts in bytes.
* @returns A promise that resolves to the estimated minimum fee in lovelace.
*/
estimateMinFee(protocolParams: any, numExtraKeyWitnesses: number, numExtraByronKeyWitnesses: 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>;
}
2 changes: 2 additions & 0 deletions cardano-wasm/example/cardano-api.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <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 = {};
Expand Down
38 changes: 38 additions & 0 deletions cardano-wasm/example/example.js
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(() => { });
36 changes: 1 addition & 35 deletions cardano-wasm/example/index.html
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>
2 changes: 1 addition & 1 deletion cardano-wasm/src/Cardano/Wasm/Internal/Api/Info.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ apiInfo =
, MethodInfo
{ methodName = "estimateMinFee"
, methodParams =
["protocolParams", "numExtraKeyWitnesses", "numExtraByronKeyWitnesses", "totalRefScriptSize"]
["protocolParams", "numKeyWitnesses", "numByronKeyWitnesses", "totalRefScriptSize"]
, methodReturnType = OtherType "BigInt"
}
]
Expand Down