Skip to content

Commit 5b510a7

Browse files
committed
feat: upgrade sdk modules
1 parent 899f026 commit 5b510a7

File tree

19 files changed

+1780
-449
lines changed

19 files changed

+1780
-449
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@changesets/cli": "^2.29.6",
3535
"@effect/docgen": "^0.5.2",
3636
"@effect/eslint-plugin": "^0.3.2",
37+
"@effect/language-service": "^0.39.0",
3738
"@effect/vitest": "^0.25.1",
3839
"@eslint/compat": "^1.3.2",
3940
"@eslint/eslintrc": "^3.3.1",
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Datum types and utilities for handling Cardano transaction data.
3+
*
4+
* This module provides types and functions for working with datum values
5+
* that can be attached to UTxOs in Cardano transactions.
6+
*/
7+
8+
export type Datum =
9+
| {
10+
type: "datumHash"
11+
hash: string
12+
}
13+
| {
14+
type: "inlineDatum"
15+
inline: string
16+
}
17+
18+
// Type guards
19+
export const isDatumHash = (datum?: Datum): datum is { type: "datumHash"; hash: string } =>
20+
datum !== undefined && datum.type === "datumHash"
21+
22+
export const isInlineDatum = (datum?: Datum): datum is { type: "inlineDatum"; inline: string } =>
23+
datum !== undefined && datum.type === "inlineDatum"
24+
25+
// Constructors
26+
export const makeDatumHash = (hash: string): Datum => ({
27+
type: "datumHash",
28+
hash
29+
})
30+
31+
export const makeInlineDatum = (inline: string): Datum => ({
32+
type: "inlineDatum",
33+
inline
34+
})
35+
36+
// Utility functions
37+
export const equals = (a: Datum, b: Datum): boolean => {
38+
if (a.type !== b.type) return false
39+
if (isDatumHash(a) && isDatumHash(b)) return a.hash === b.hash
40+
if (isInlineDatum(a) && isInlineDatum(b)) return a.inline === b.inline
41+
return false
42+
}
43+
44+
// Array utilities
45+
export const filterHashes = (datums: Array<Datum>): Array<{ type: "datumHash"; hash: string }> =>
46+
datums.filter(isDatumHash)
47+
48+
export const filterInline = (datums: Array<Datum>): Array<{ type: "inlineDatum"; inline: string }> =>
49+
datums.filter(isInlineDatum)
50+
51+
export const unique = (datums: Array<Datum>): Array<Datum> =>
52+
datums.filter((datum, index, self) => self.findIndex((d) => equals(datum, d)) === index)
53+
54+
export const groupByType = (
55+
datums: Array<Datum>
56+
): {
57+
hashes: Array<{ type: "datumHash"; hash: string }>
58+
inline: Array<{ type: "inlineDatum"; inline: string }>
59+
} => ({
60+
hashes: filterHashes(datums),
61+
inline: filterInline(datums)
62+
})
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Delegation types and utilities for handling Cardano stake delegation.
3+
*
4+
* This module provides types and functions for working with stake delegation
5+
* information, including pool assignments and reward balances.
6+
*/
7+
8+
export interface Delegation {
9+
readonly poolId: string | undefined
10+
readonly rewards: bigint
11+
}
12+
13+
// Constructors
14+
export const make = (poolId: string | undefined, rewards: bigint): Delegation => ({
15+
poolId,
16+
rewards
17+
})
18+
19+
export const empty = (): Delegation => make(undefined, 0n)
20+
21+
// Type guards
22+
export const isDelegated = (delegation: Delegation): boolean => delegation.poolId !== undefined
23+
24+
export const hasRewards = (delegation: Delegation): boolean => delegation.rewards > 0n
25+
26+
// Transformations
27+
export const addRewards = (delegation: Delegation, additionalRewards: bigint): Delegation =>
28+
make(delegation.poolId, delegation.rewards + additionalRewards)
29+
30+
export const subtractRewards = (delegation: Delegation, rewardsToSubtract: bigint): Delegation => {
31+
const newRewards = delegation.rewards - rewardsToSubtract
32+
return make(delegation.poolId, newRewards >= 0n ? newRewards : 0n)
33+
}
34+
35+
// Comparisons
36+
export const equals = (a: Delegation, b: Delegation): boolean =>
37+
a.poolId === b.poolId && a.rewards === b.rewards
38+
39+
export const hasSamePool = (a: Delegation, b: Delegation): boolean =>
40+
a.poolId === b.poolId
41+
42+
export const compareByRewards = (a: Delegation, b: Delegation): number => {
43+
const diff = a.rewards - b.rewards
44+
return diff > 0n ? 1 : diff < 0n ? -1 : 0
45+
}
46+
47+
export const compareByPoolId = (a: Delegation, b: Delegation): number => {
48+
if (a.poolId === b.poolId) return 0
49+
if (a.poolId === undefined) return -1
50+
if (b.poolId === undefined) return 1
51+
return a.poolId.localeCompare(b.poolId)
52+
}
53+
54+
// Array utilities
55+
export const sortByRewards = (delegations: Array<Delegation>, ascending = true): Array<Delegation> =>
56+
[...delegations].sort((a, b) => {
57+
const comparison = compareByRewards(a, b)
58+
return ascending ? comparison : -comparison
59+
})
60+
61+
export const sortByPoolId = (delegations: Array<Delegation>): Array<Delegation> =>
62+
[...delegations].sort(compareByPoolId)
63+
64+
export const filterDelegated = (delegations: Array<Delegation>): Array<Delegation> =>
65+
delegations.filter(isDelegated)
66+
67+
export const filterUndelegated = (delegations: Array<Delegation>): Array<Delegation> =>
68+
delegations.filter(d => !isDelegated(d))
69+
70+
export const filterWithRewards = (delegations: Array<Delegation>): Array<Delegation> =>
71+
delegations.filter(hasRewards)
72+
73+
export const filterByPool = (delegations: Array<Delegation>, poolId: string): Array<Delegation> =>
74+
delegations.filter(d => d.poolId === poolId)
75+
76+
export const getTotalRewards = (delegations: Array<Delegation>): bigint =>
77+
delegations.reduce((total, delegation) => total + delegation.rewards, 0n)
78+
79+
export const getUniquePoolIds = (delegations: Array<Delegation>): Array<string> =>
80+
[...new Set(delegations.map(d => d.poolId).filter((id): id is string => id !== undefined))]
81+
82+
export const groupByPool = (delegations: Array<Delegation>): Record<string, Array<Delegation>> =>
83+
delegations.reduce((groups, delegation) => {
84+
const poolId = delegation.poolId || "undelegated"
85+
if (!groups[poolId]) groups[poolId] = []
86+
groups[poolId].push(delegation)
87+
return groups
88+
}, {} as Record<string, Array<Delegation>>)
89+
90+
// Statistical utilities
91+
export const getAverageRewards = (delegations: Array<Delegation>): bigint => {
92+
if (delegations.length === 0) return 0n
93+
return getTotalRewards(delegations) / BigInt(delegations.length)
94+
}
95+
96+
export const getMaxRewards = (delegations: Array<Delegation>): bigint =>
97+
delegations.reduce((max, delegation) =>
98+
delegation.rewards > max ? delegation.rewards : max, 0n)
99+
100+
export const getMinRewards = (delegations: Array<Delegation>): bigint =>
101+
delegations.reduce((min, delegation) =>
102+
delegation.rewards < min ? delegation.rewards : min,
103+
delegations[0]?.rewards || 0n)
104+
105+
// Set operations
106+
export const unique = (delegations: Array<Delegation>): Array<Delegation> =>
107+
delegations.filter((delegation, index, self) =>
108+
self.findIndex(d => equals(delegation, d)) === index)
109+
110+
export const find = (delegations: Array<Delegation>, predicate: (delegation: Delegation) => boolean): Delegation | undefined =>
111+
delegations.find(predicate)
112+
113+
export const findByPool = (delegations: Array<Delegation>, poolId: string): Delegation | undefined =>
114+
find(delegations, d => d.poolId === poolId)
115+
116+
export const contains = (delegations: Array<Delegation>, target: Delegation): boolean =>
117+
delegations.some(delegation => equals(delegation, target))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// EvalRedeemer types and utilities for transaction evaluation
2+
3+
export type EvalRedeemer = {
4+
readonly ex_units: { readonly mem: number; readonly steps: number }
5+
readonly redeemer_index: number
6+
readonly redeemer_tag: "spend" | "mint" | "publish" | "withdraw" | "vote" | "propose"
7+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* OutRef types and utilities for handling Cardano transaction output references.
3+
*
4+
* This module provides types and functions for working with transaction output references,
5+
* which uniquely identify UTxOs by their transaction hash and output index.
6+
*/
7+
8+
export interface OutRef {
9+
txHash: string
10+
outputIndex: number
11+
}
12+
13+
// Constructors
14+
export const make = (txHash: string, outputIndex: number): OutRef => ({
15+
txHash,
16+
outputIndex
17+
})
18+
19+
export const fromTxHashAndIndex = (txHash: string, outputIndex: number): OutRef => make(txHash, outputIndex)
20+
21+
// Comparisons
22+
export const equals = (a: OutRef, b: OutRef): boolean => a.txHash === b.txHash && a.outputIndex === b.outputIndex
23+
24+
export const compare = (a: OutRef, b: OutRef): number => {
25+
const txHashComparison = a.txHash.localeCompare(b.txHash)
26+
if (txHashComparison !== 0) return txHashComparison
27+
return a.outputIndex - b.outputIndex
28+
}
29+
30+
// String operations
31+
export const toString = (outRef: OutRef): string => `${outRef.txHash}#${outRef.outputIndex}`
32+
33+
// Array utilities
34+
export const sort = (outRefs: Array<OutRef>): Array<OutRef> => [...outRefs].sort(compare)
35+
36+
export const sortByTxHash = (outRefs: Array<OutRef>): Array<OutRef> =>
37+
[...outRefs].sort((a, b) => a.txHash.localeCompare(b.txHash))
38+
39+
export const sortByIndex = (outRefs: Array<OutRef>): Array<OutRef> =>
40+
[...outRefs].sort((a, b) => a.outputIndex - b.outputIndex)
41+
42+
export const unique = (outRefs: Array<OutRef>): Array<OutRef> =>
43+
outRefs.filter((outRef, index, self) => self.findIndex((other) => equals(outRef, other)) === index)
44+
45+
export const contains = (outRefs: Array<OutRef>, target: OutRef): boolean =>
46+
outRefs.some((outRef) => equals(outRef, target))
47+
48+
export const remove = (outRefs: Array<OutRef>, target: OutRef): Array<OutRef> =>
49+
outRefs.filter((outRef) => !equals(outRef, target))
50+
51+
export const find = (outRefs: Array<OutRef>, predicate: (outRef: OutRef) => boolean): OutRef | undefined =>
52+
outRefs.find(predicate)
53+
54+
export const filter = (outRefs: Array<OutRef>, predicate: (outRef: OutRef) => boolean): Array<OutRef> =>
55+
outRefs.filter(predicate)
56+
57+
// Group operations
58+
export const groupByTxHash = (outRefs: Array<OutRef>): Record<string, Array<OutRef>> =>
59+
outRefs.reduce(
60+
(groups, outRef) => {
61+
const txHash = outRef.txHash
62+
if (!groups[txHash]) groups[txHash] = []
63+
groups[txHash].push(outRef)
64+
return groups
65+
},
66+
{} as Record<string, Array<OutRef>>
67+
)
68+
69+
export const getTxHashes = (outRefs: Array<OutRef>): Array<string> => [
70+
...new Set(outRefs.map((outRef) => outRef.txHash))
71+
]
72+
73+
export const getIndicesForTx = (outRefs: Array<OutRef>, txHash: string): Array<number> =>
74+
outRefs.filter((outRef) => outRef.txHash === txHash).map((outRef) => outRef.outputIndex)
75+
76+
// Set operations
77+
export const union = (setA: Array<OutRef>, setB: Array<OutRef>): Array<OutRef> => unique([...setA, ...setB])
78+
79+
export const intersection = (setA: Array<OutRef>, setB: Array<OutRef>): Array<OutRef> =>
80+
setA.filter((outRefA) => contains(setB, outRefA))
81+
82+
export const difference = (setA: Array<OutRef>, setB: Array<OutRef>): Array<OutRef> =>
83+
setA.filter((outRefA) => !contains(setB, outRefA))
84+
85+
// Convenience functions
86+
export const isEmpty = (outRefs: Array<OutRef>): boolean => outRefs.length === 0
87+
88+
export const size = (outRefs: Array<OutRef>): number => outRefs.length
89+
90+
export const first = (outRefs: Array<OutRef>): OutRef | undefined => outRefs[0]
91+
92+
export const last = (outRefs: Array<OutRef>): OutRef | undefined =>
93+
outRefs.length > 0 ? outRefs[outRefs.length - 1] : undefined
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Protocol Parameters types and utilities for Cardano network configuration.
3+
*
4+
* This module provides types and functions for working with Cardano protocol parameters,
5+
* which define the operational rules and limits of the network.
6+
*/
7+
8+
export type ProtocolParameters = {
9+
readonly minFeeA: number
10+
readonly minFeeB: number
11+
readonly maxTxSize: number
12+
readonly maxValSize: number
13+
readonly keyDeposit: bigint
14+
readonly poolDeposit: bigint
15+
readonly drepDeposit: bigint
16+
readonly govActionDeposit: bigint
17+
readonly priceMem: number
18+
readonly priceStep: number
19+
readonly maxTxExMem: bigint
20+
readonly maxTxExSteps: bigint
21+
readonly coinsPerUtxoByte: bigint
22+
readonly collateralPercentage: number
23+
readonly maxCollateralInputs: number
24+
readonly minFeeRefScriptCostPerByte: number
25+
readonly costModels: {
26+
readonly PlutusV1: Record<string, number>
27+
readonly PlutusV2: Record<string, number>
28+
readonly PlutusV3: Record<string, number>
29+
}
30+
}
31+
32+
/**
33+
* Calculate the minimum fee for a transaction based on protocol parameters.
34+
*
35+
* @param protocolParams - The current protocol parameters
36+
* @param txSize - The transaction size in bytes
37+
* @returns The minimum fee in lovelace
38+
*/
39+
export const calculateMinFee = (protocolParams: ProtocolParameters, txSize: number): bigint => {
40+
return BigInt(protocolParams.minFeeA * txSize + protocolParams.minFeeB)
41+
}
42+
43+
/**
44+
* Calculate the UTxO cost based on the protocol parameters.
45+
*
46+
* @param protocolParams - The current protocol parameters
47+
* @param utxoSize - The UTxO size in bytes
48+
* @returns The UTxO cost in lovelace
49+
*/
50+
export const calculateUtxoCost = (protocolParams: ProtocolParameters, utxoSize: number): bigint => {
51+
return protocolParams.coinsPerUtxoByte * BigInt(utxoSize)
52+
}
53+
54+
/**
55+
* Get the cost model for a specific Plutus version.
56+
*
57+
* @param protocolParams - The current protocol parameters
58+
* @param version - The Plutus version
59+
* @returns The cost model for the specified version
60+
*/
61+
export const getCostModel = (
62+
protocolParams: ProtocolParameters,
63+
version: "PlutusV1" | "PlutusV2" | "PlutusV3"
64+
): Record<string, number> => {
65+
return protocolParams.costModels[version]
66+
}
67+
68+
/**
69+
* Check if the protocol parameters support a specific Plutus version.
70+
*
71+
* @param protocolParams - The current protocol parameters
72+
* @param version - The Plutus version to check
73+
* @returns True if the version is supported
74+
*/
75+
export const supportsPlutusVersion = (
76+
protocolParams: ProtocolParameters,
77+
version: "PlutusV1" | "PlutusV2" | "PlutusV3"
78+
): boolean => {
79+
const costModel = protocolParams.costModels[version]
80+
return costModel && Object.keys(costModel).length > 0
81+
}

0 commit comments

Comments
 (0)