Skip to content

Commit

Permalink
refactor(core): remove direct dependency on @emurgo/cardano-serializa…
Browse files Browse the repository at this point in the history
…tion-lib-nodejs
  • Loading branch information
rhyslbw committed Sep 24, 2021
1 parent 6c4cef6 commit cc74620
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
1 change: 0 additions & 1 deletion packages/core/package.json
Expand Up @@ -22,7 +22,6 @@
},
"dependencies": {
"@cardano-ogmios/schema": "^4.0.0-beta.6",
"@emurgo/cardano-serialization-lib-nodejs": "^8.0.0",
"buffer": "^6.0.3"
}
}
5 changes: 2 additions & 3 deletions packages/core/src/Asset/util.ts
@@ -1,5 +1,4 @@
import { CardanoSerializationLib } from '@cardano-sdk/cardano-serialization-lib';
import { AssetName, ScriptHash } from '@emurgo/cardano-serialization-lib-nodejs';
import { CardanoSerializationLib, CSL } from '@cardano-sdk/cardano-serialization-lib';
import { Buffer } from 'buffer';

export const policyIdFromAssetId = (assetId: string): string => assetId.slice(0, 56);
Expand All @@ -8,7 +7,7 @@ export const assetNameFromAssetId = (assetId: string): string => assetId.slice(5
/**
* @returns {string} concatenated hex-encoded policy id and asset name
*/
export const createAssetId = (scriptHash: ScriptHash, assetName: AssetName): string =>
export const createAssetId = (scriptHash: CSL.ScriptHash, assetName: CSL.AssetName): string =>
Buffer.from(scriptHash.to_bytes()).toString('hex') + Buffer.from(assetName.name()).toString('hex');

export const parseAssetId = (assetId: string, csl: CardanoSerializationLib) => {
Expand Down
34 changes: 14 additions & 20 deletions packages/core/src/Ogmios/OgmiosToCardanoWasm.ts
@@ -1,40 +1,34 @@
import CardanoWasm from '@emurgo/cardano-serialization-lib-nodejs';
import { CSL } from '@cardano-sdk/cardano-serialization-lib';
import OgmiosSchema from '@cardano-ogmios/schema';
import * as Asset from '../Asset';

export const OgmiosToCardanoWasm = {
txIn: (ogmios: OgmiosSchema.TxIn): CardanoWasm.TransactionInput =>
CardanoWasm.TransactionInput.new(
CardanoWasm.TransactionHash.from_bytes(Buffer.from(ogmios.txId, 'hex')),
ogmios.index
),
txOut: (ogmios: OgmiosSchema.TxOut): CardanoWasm.TransactionOutput =>
CardanoWasm.TransactionOutput.new(
CardanoWasm.Address.from_bech32(ogmios.address),
OgmiosToCardanoWasm.value(ogmios.value)
),
utxo: (ogmios: OgmiosSchema.Utxo): CardanoWasm.TransactionUnspentOutput[] =>
txIn: (ogmios: OgmiosSchema.TxIn): CSL.TransactionInput =>
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), OgmiosToCardanoWasm.value(ogmios.value)),
utxo: (ogmios: OgmiosSchema.Utxo): CSL.TransactionUnspentOutput[] =>
ogmios.map((item) =>
CardanoWasm.TransactionUnspentOutput.new(OgmiosToCardanoWasm.txIn(item[0]), OgmiosToCardanoWasm.txOut(item[1]))
CSL.TransactionUnspentOutput.new(OgmiosToCardanoWasm.txIn(item[0]), OgmiosToCardanoWasm.txOut(item[1]))
),
value: (ogmios: OgmiosSchema.Value): CardanoWasm.Value => {
const value = CardanoWasm.Value.new(CardanoWasm.BigNum.from_str(ogmios.coins.toString()));
value: (ogmios: OgmiosSchema.Value): CSL.Value => {
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 = CardanoWasm.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 = CardanoWasm.Assets.new();
const wasmAssets = CSL.Assets.new();
for (const [assetId, assetQuantity] of policyAssets) {
wasmAssets.insert(
CardanoWasm.AssetName.new(Buffer.from(Asset.util.assetNameFromAssetId(assetId), 'hex')),
CardanoWasm.BigNum.from_str(assetQuantity.toString())
CSL.AssetName.new(Buffer.from(Asset.util.assetNameFromAssetId(assetId), 'hex')),
CSL.BigNum.from_str(assetQuantity.toString())
);
}
multiAsset.insert(CardanoWasm.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;
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/Provider/CardanoProvider.ts
@@ -1,5 +1,4 @@
// Importing types from cardano-serialization-lib-browser will cause TypeScript errors.
import CardanoSerializationLib from '@emurgo/cardano-serialization-lib-nodejs';
import { CSL } from '@cardano-sdk/cardano-serialization-lib';
import Cardano, { ProtocolParametersAlonzo } from '@cardano-ogmios/schema';
import { Transaction } from '../';

Expand All @@ -20,7 +19,7 @@ export type ProtocolParametersRequiredByWallet = Pick<
export interface CardanoProvider {
ledgerTip: () => Promise<Cardano.Tip>;
/** @param signedTransaction signed and serialized cbor */
submitTx: (tx: CardanoSerializationLib.Transaction) => Promise<boolean>;
submitTx: (tx: CSL.Transaction) => Promise<boolean>;
utxoDelegationAndRewards: (
addresses: Cardano.Address[],
stakeKeyHash: Cardano.Hash16
Expand Down
10 changes: 5 additions & 5 deletions packages/core/test/Ogmios/OgmiosToCardanoWasm.test.ts
@@ -1,4 +1,4 @@
import CardanoWasm from '@emurgo/cardano-serialization-lib-nodejs';
import { CSL } from '@cardano-sdk/cardano-serialization-lib';
import { OgmiosToCardanoWasm } from '../../src/Ogmios';
import * as OgmiosSchema from '@cardano-ogmios/schema';

Expand All @@ -11,15 +11,15 @@ const txOut: OgmiosSchema.TxOut = {

describe('OgmiosToCardanoWasm', () => {
test('txIn', () => {
expect(OgmiosToCardanoWasm.txIn(txIn)).toBeInstanceOf(CardanoWasm.TransactionInput);
expect(OgmiosToCardanoWasm.txIn(txIn)).toBeInstanceOf(CSL.TransactionInput);
});
test('txOut', () => {
expect(OgmiosToCardanoWasm.txOut(txOut)).toBeInstanceOf(CardanoWasm.TransactionOutput);
expect(OgmiosToCardanoWasm.txOut(txOut)).toBeInstanceOf(CSL.TransactionOutput);
});
test('utxo', () => {
expect(OgmiosToCardanoWasm.utxo([[txIn, txOut]])[0]).toBeInstanceOf(CardanoWasm.TransactionUnspentOutput);
expect(OgmiosToCardanoWasm.utxo([[txIn, txOut]])[0]).toBeInstanceOf(CSL.TransactionUnspentOutput);
});
test('value', () => {
expect(OgmiosToCardanoWasm.value(txOut.value)).toBeInstanceOf(CardanoWasm.Value);
expect(OgmiosToCardanoWasm.value(txOut.value)).toBeInstanceOf(CSL.Value);
});
});

0 comments on commit cc74620

Please sign in to comment.