Skip to content

Commit 1c7e8a8

Browse files
committed
Created TypeScript definition for existing API
1 parent 7c9eb4c commit 1c7e8a8

File tree

5 files changed

+140
-36
lines changed

5 files changed

+140
-36
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// cardano-api.d.ts
2+
3+
export default initialize;
4+
5+
/**
6+
* Initializes the Cardano API.
7+
* @returns A promise that resolves to the main `CardanoAPI` object.
8+
*/
9+
declare function initialize(): Promise<CardanoAPI>;
10+
11+
/**
12+
* Represents an unsigned transaction.
13+
*/
14+
declare interface UnsignedTx {
15+
/**
16+
* The type of the object, used for identification (the "UnsignedTx" string).
17+
*/
18+
objectType: string;
19+
20+
/**
21+
* Adds a simple transaction input to the transaction.
22+
* @param txId The transaction ID of the input UTxO.
23+
* @param txIx The index of the input within the UTxO.
24+
* @returns The `UnsignedTx` object with the added input.
25+
*/
26+
addTxInput(txId: string, txIx: number): UnsignedTx;
27+
28+
/**
29+
* Adds a simple transaction output to the transaction.
30+
* @param destAddr The destination address.
31+
* @param lovelaceAmount The amount in lovelace to output.
32+
* @returns The `UnsignedTx` object with the added output.
33+
*/
34+
addSimpleTxOut(destAddr: string, lovelaceAmount: bigint): UnsignedTx;
35+
36+
/**
37+
* Sets the fee for the transaction.
38+
* @param lovelaceAmount The fee amount in lovelace.
39+
* @returns The `UnsignedTx` object with the set fee.
40+
*/
41+
setFee(lovelaceAmount: bigint): UnsignedTx;
42+
43+
/**
44+
* Estimates the minimum fee for the transaction.
45+
* @param protocolParams The protocol parameters.
46+
* @param numExtraKeyWitnesses The number of extra key witnesses (in addition to the ones already added).
47+
* @param numExtraByronKeyWitnesses The number of extra Byron key witnesses.
48+
* @param totalRefScriptSize The total size of reference scripts in bytes.
49+
* @returns A promise that resolves to the estimated minimum fee in lovelace.
50+
*/
51+
estimateMinFee(protocolParams: any, numExtraKeyWitnesses: number, numExtraByronKeyWitnesses: number, totalRefScriptSize: number): Promise<bigint>;
52+
53+
/**
54+
* Signs the transaction with a payment key.
55+
* @param signingKey The signing key to witness the transaction.
56+
* @returns A promise that resolves to a `SignedTx` object.
57+
*/
58+
signWithPaymentKey(signingKey: string): Promise<SignedTx>;
59+
}
60+
61+
/**
62+
* Represents a signed transaction.
63+
*/
64+
declare interface SignedTx {
65+
/**
66+
* The type of the object, used for identification (the "SignedTx" string).
67+
*/
68+
objectType: string;
69+
70+
/**
71+
* Adds an extra signature to the transaction with a payment key.
72+
* @param signingKey The signing key to witness the transaction.
73+
* @returns The `SignedTx` object with the additional signature.
74+
*/
75+
alsoSignWithPaymentKey(signingKey: string): SignedTx;
76+
77+
/**
78+
* Converts the signed transaction to its CBOR representation.
79+
* @returns A promise that resolves to the CBOR representation of the transaction as a hex string.
80+
*/
81+
txToCbor(): Promise<string>;
82+
}
83+
84+
/**
85+
* The main Cardano API object with static methods.
86+
*/
87+
declare interface CardanoAPI {
88+
/**
89+
* The type of the object, used for identification (the "CardanoAPI" string).
90+
*/
91+
objectType: string;
92+
93+
/**
94+
* Creates a new Conway-era transaction.
95+
* @returns A promise that resolves to a new `UnsignedTx` object.
96+
*/
97+
newConwayTx(): Promise<UnsignedTx>;
98+
}

cardano-wasm/example/cardano-api.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference path="./cardano-api.d.ts" />
2+
13
import { WASI } from "https://unpkg.com/@bjorn3/[email protected]/dist/index.js";
24
import ghc_wasm_jsffi from "./cardano-wasm.js";
35
const __exports = {};

cardano-wasm/example/example.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import cardano_api from "./cardano-api.js";
2+
3+
let promise = cardano_api();
4+
5+
async function get_protocol_params() {
6+
const response = await fetch("./preview_pparams.json");
7+
return (await response.json());
8+
}
9+
10+
let protocolParams = await get_protocol_params();
11+
12+
async function do_async_work() {
13+
let api = await promise;
14+
console.log("Api object:");
15+
console.log(api);
16+
17+
let emptyTx = await api.newConwayTx();
18+
console.log("UnsignedTx object:");
19+
console.log(emptyTx);
20+
21+
let tx = await emptyTx.addTxInput("be6efd42a3d7b9a00d09d77a5d41e55ceaf0bd093a8aa8a893ce70d9caafd978", 0)
22+
.addSimpleTxOut("addr_test1vzpfxhjyjdlgk5c0xt8xw26avqxs52rtf69993j4tajehpcue4v2v", 10_000_000n)
23+
24+
let feeEstimate = await tx.estimateMinFee(protocolParams, 1, 0, 0);
25+
console.log("Estimated fee:");
26+
console.log(feeEstimate);
27+
28+
let signedTx = await tx.setFee(feeEstimate)
29+
.signWithPaymentKey("addr_sk1648253w4tf6fv5fk28dc7crsjsaw7d9ymhztd4favg3cwkhz7x8sl5u3ms");
30+
console.log("SignedTx object:");
31+
console.log(signedTx);
32+
33+
let txCbor = await signedTx.txToCbor();
34+
console.log("Tx CBOR:");
35+
console.log(txCbor);
36+
}
37+
38+
do_async_work().then(() => { });

cardano-wasm/example/index.html

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,7 @@
11
<html>
22

33
<body>
4-
<script type="module">
5-
import cardano_api from "./cardano-api.js";
6-
let promise = cardano_api();
7-
async function get_protocol_params() {
8-
const response = await fetch("./preview_pparams.json");
9-
return (await response.json());
10-
}
11-
let protocolParams = await get_protocol_params();
12-
async function do_async_work() {
13-
let api = await promise;
14-
console.log("Api object:");
15-
console.log(api);
16-
17-
let emptyTx = await api.newConwayTx();
18-
console.log("UnsignedTx object:");
19-
console.log(emptyTx);
20-
21-
let tx = await emptyTx.addTxInput("be6efd42a3d7b9a00d09d77a5d41e55ceaf0bd093a8aa8a893ce70d9caafd978", 0)
22-
.addSimpleTxOut("addr_test1vzpfxhjyjdlgk5c0xt8xw26avqxs52rtf69993j4tajehpcue4v2v", 10_000_000n)
23-
24-
let feeEstimate = await tx.estimateMinFee(protocolParams, 1, 0, 0);
25-
console.log("Estimated fee:");
26-
console.log(feeEstimate);
27-
28-
let signedTx = await tx.setFee(feeEstimate)
29-
.signWithPaymentKey("addr_sk1648253w4tf6fv5fk28dc7crsjsaw7d9ymhztd4favg3cwkhz7x8sl5u3ms");
30-
console.log("SignedTx object:");
31-
console.log(signedTx);
32-
33-
let txCbor = await signedTx.txToCbor();
34-
console.log("Tx CBOR:");
35-
console.log(txCbor);
36-
}
37-
do_async_work().then(() => { });
38-
</script>
4+
<script type="module" src="./example.js"></script>
395
</body>
406

417
</html>

cardano-wasm/src/Cardano/Wasm/Internal/Api/Info.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ apiInfo =
112112
, MethodInfo
113113
{ methodName = "estimateMinFee"
114114
, methodParams =
115-
["protocolParams", "numExtraKeyWitnesses", "numExtraByronKeyWitnesses", "totalRefScriptSize"]
115+
["protocolParams", "numKeyWitnesses", "numByronKeyWitnesses", "totalRefScriptSize"]
116116
, methodReturnType = OtherType "BigInt"
117117
}
118118
]

0 commit comments

Comments
 (0)