Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: inject the csl instance in ogmiosToCsl
- Loading branch information
Showing
8 changed files
with
68 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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 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,34 +1,36 @@ | ||
import { CSL } from '@cardano-sdk/cardano-serialization-lib'; | ||
import { CardanoSerializationLib, CSL } from '@cardano-sdk/cardano-serialization-lib'; | ||
import OgmiosSchema from '@cardano-ogmios/schema'; | ||
import * as Asset from '../Asset'; | ||
|
||
export const ogmiosToCsl = { | ||
export const ogmiosToCsl = (csl: CardanoSerializationLib) => ({ | ||
txIn: (ogmios: OgmiosSchema.TxIn): CSL.TransactionInput => | ||
CSL.TransactionInput.new(CSL.TransactionHash.from_bytes(Buffer.from(ogmios.txId, 'hex')), ogmios.index), | ||
csl.TransactionInput.new(csl.TransactionHash.from_bytes(Buffer.from(ogmios.txId, 'hex')), ogmios.index), | ||
txOut: (ogmios: OgmiosSchema.TxOut): CSL.TransactionOutput => | ||
CSL.TransactionOutput.new(CSL.Address.from_bech32(ogmios.address), ogmiosToCsl.value(ogmios.value)), | ||
csl.TransactionOutput.new(csl.Address.from_bech32(ogmios.address), ogmiosToCsl(csl).value(ogmios.value)), | ||
utxo: (ogmios: OgmiosSchema.Utxo): CSL.TransactionUnspentOutput[] => | ||
ogmios.map((item) => CSL.TransactionUnspentOutput.new(ogmiosToCsl.txIn(item[0]), ogmiosToCsl.txOut(item[1]))), | ||
ogmios.map((item) => | ||
csl.TransactionUnspentOutput.new(ogmiosToCsl(csl).txIn(item[0]), ogmiosToCsl(csl).txOut(item[1])) | ||
), | ||
value: (ogmios: OgmiosSchema.Value): CSL.Value => { | ||
const value = CSL.Value.new(CSL.BigNum.from_str(ogmios.coins.toString())); | ||
const value = csl.Value.new(csl.BigNum.from_str(ogmios.coins.toString())); | ||
const assets = ogmios.assets !== undefined ? Object.entries(ogmios.assets) : []; | ||
if (assets.length === 0) { | ||
return value; | ||
} | ||
const multiAsset = CSL.MultiAsset.new(); | ||
const multiAsset = csl.MultiAsset.new(); | ||
const policies = [...new Set(assets.map(([assetId]) => Asset.util.policyIdFromAssetId(assetId)))]; | ||
for (const policy of policies) { | ||
const policyAssets = assets.filter(([assetId]) => Asset.util.policyIdFromAssetId(assetId) === policy); | ||
const wasmAssets = CSL.Assets.new(); | ||
const wasmAssets = csl.Assets.new(); | ||
for (const [assetId, assetQuantity] of policyAssets) { | ||
wasmAssets.insert( | ||
CSL.AssetName.new(Buffer.from(Asset.util.assetNameFromAssetId(assetId), 'hex')), | ||
CSL.BigNum.from_str(assetQuantity.toString()) | ||
csl.AssetName.new(Buffer.from(Asset.util.assetNameFromAssetId(assetId), 'hex')), | ||
csl.BigNum.from_str(assetQuantity.toString()) | ||
); | ||
} | ||
multiAsset.insert(CSL.ScriptHash.from_bytes(Buffer.from(policy, 'hex')), wasmAssets); | ||
multiAsset.insert(csl.ScriptHash.from_bytes(Buffer.from(policy, 'hex')), wasmAssets); | ||
} | ||
value.set_multiasset(multiAsset); | ||
return value; | ||
} | ||
}; | ||
}); |
This file contains 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 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 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 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 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 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