From 474e7e89702d7f48c26c8cf55344a51c6e8d68a6 Mon Sep 17 00:00:00 2001 From: Rhys Bartels-Waller Date: Fri, 24 Sep 2021 20:59:40 +1000 Subject: [PATCH] refactor: rename OgmiosToCardanoWasm to ogmiosToCsl The next commit will refactor the object to a function, to inject the csl instance --- packages/cip2/src/util.ts | 6 +++--- packages/core/src/Ogmios/index.ts | 2 +- .../{OgmiosToCardanoWasm.ts => ogmiosToCsl.ts} | 8 +++----- ...miosToCardanoWasm.test.ts => ogmiosToCsl.test.ts} | 12 ++++++------ packages/wallet/src/InMemoryUtxoRepository.ts | 2 +- packages/wallet/src/SingleAddressWallet.ts | 2 +- packages/wallet/test/InMemoryUtxoRepository.test.ts | 4 ++-- .../wallet/test/createTransactionInternals.test.ts | 4 ++-- 8 files changed, 19 insertions(+), 21 deletions(-) rename packages/core/src/Ogmios/{OgmiosToCardanoWasm.ts => ogmiosToCsl.ts} (87%) rename packages/core/test/Ogmios/{OgmiosToCardanoWasm.test.ts => ogmiosToCsl.test.ts} (58%) diff --git a/packages/cip2/src/util.ts b/packages/cip2/src/util.ts index 94433c0eede..e08261cf61b 100644 --- a/packages/cip2/src/util.ts +++ b/packages/cip2/src/util.ts @@ -1,5 +1,5 @@ // TODO: move these utils to either core package. -// Current impelmentation of OgmiosToCardanoWasm.value() uses number instead of bigint for lovelace. +// Current impelmentation of ogmiosToCsl.value() uses number instead of bigint for lovelace. // These utils should be moved after CSL is updated to use bigint. import { CardanoSerializationLib, CSL } from '@cardano-sdk/cardano-serialization-lib'; import { Asset, Ogmios } from '@cardano-sdk/core'; @@ -41,8 +41,8 @@ export const valueToValueQuantities = (value: CSL.Value): ValueQuantities => { return result; }; -// This is equivalent to OgmiosToCardanoWasm.value(), so they should be merged. -// Note that current implementation of OgmiosToCardanoWasm +// This is equivalent to ogmiosToCsl.value(), so they should be merged. +// Note that current implementation of ogmiosToCsl // imports nodejs version of CSL, so it cannot be used in browser. export const valueQuantitiesToValue = ({ coins, assets }: ValueQuantities, csl: CardanoSerializationLib): CSL.Value => { const value = csl.Value.new(csl.BigNum.from_str(coins.toString())); diff --git a/packages/core/src/Ogmios/index.ts b/packages/core/src/Ogmios/index.ts index 20fbdac77f4..653b770c5a7 100644 --- a/packages/core/src/Ogmios/index.ts +++ b/packages/core/src/Ogmios/index.ts @@ -1,2 +1,2 @@ -export * from './OgmiosToCardanoWasm'; +export * from './ogmiosToCsl'; export * as util from './util'; diff --git a/packages/core/src/Ogmios/OgmiosToCardanoWasm.ts b/packages/core/src/Ogmios/ogmiosToCsl.ts similarity index 87% rename from packages/core/src/Ogmios/OgmiosToCardanoWasm.ts rename to packages/core/src/Ogmios/ogmiosToCsl.ts index 6fc992082fa..5b13f33fbbb 100644 --- a/packages/core/src/Ogmios/OgmiosToCardanoWasm.ts +++ b/packages/core/src/Ogmios/ogmiosToCsl.ts @@ -2,15 +2,13 @@ import { CSL } from '@cardano-sdk/cardano-serialization-lib'; import OgmiosSchema from '@cardano-ogmios/schema'; import * as Asset from '../Asset'; -export const OgmiosToCardanoWasm = { +export const ogmiosToCsl = { 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)), + CSL.TransactionOutput.new(CSL.Address.from_bech32(ogmios.address), ogmiosToCsl.value(ogmios.value)), utxo: (ogmios: OgmiosSchema.Utxo): CSL.TransactionUnspentOutput[] => - ogmios.map((item) => - CSL.TransactionUnspentOutput.new(OgmiosToCardanoWasm.txIn(item[0]), OgmiosToCardanoWasm.txOut(item[1])) - ), + ogmios.map((item) => CSL.TransactionUnspentOutput.new(ogmiosToCsl.txIn(item[0]), ogmiosToCsl.txOut(item[1]))), 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) : []; diff --git a/packages/core/test/Ogmios/OgmiosToCardanoWasm.test.ts b/packages/core/test/Ogmios/ogmiosToCsl.test.ts similarity index 58% rename from packages/core/test/Ogmios/OgmiosToCardanoWasm.test.ts rename to packages/core/test/Ogmios/ogmiosToCsl.test.ts index 44f69012bd1..b7d9c081019 100644 --- a/packages/core/test/Ogmios/OgmiosToCardanoWasm.test.ts +++ b/packages/core/test/Ogmios/ogmiosToCsl.test.ts @@ -1,5 +1,5 @@ import { CSL } from '@cardano-sdk/cardano-serialization-lib'; -import { OgmiosToCardanoWasm } from '../../src/Ogmios'; +import { ogmiosToCsl } from '../../src/Ogmios'; import * as OgmiosSchema from '@cardano-ogmios/schema'; const txIn: OgmiosSchema.TxIn = { txId: '0f3abbc8fc19c2e61bab6059bf8a466e6e754833a08a62a6c56fe0e78f19d9d5', index: 0 }; @@ -9,17 +9,17 @@ const txOut: OgmiosSchema.TxOut = { value: { coins: 10, assets: { '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740': 20n } } }; -describe('OgmiosToCardanoWasm', () => { +describe('ogmiosToCsl', () => { test('txIn', () => { - expect(OgmiosToCardanoWasm.txIn(txIn)).toBeInstanceOf(CSL.TransactionInput); + expect(ogmiosToCsl.txIn(txIn)).toBeInstanceOf(CSL.TransactionInput); }); test('txOut', () => { - expect(OgmiosToCardanoWasm.txOut(txOut)).toBeInstanceOf(CSL.TransactionOutput); + expect(ogmiosToCsl.txOut(txOut)).toBeInstanceOf(CSL.TransactionOutput); }); test('utxo', () => { - expect(OgmiosToCardanoWasm.utxo([[txIn, txOut]])[0]).toBeInstanceOf(CSL.TransactionUnspentOutput); + expect(ogmiosToCsl.utxo([[txIn, txOut]])[0]).toBeInstanceOf(CSL.TransactionUnspentOutput); }); test('value', () => { - expect(OgmiosToCardanoWasm.value(txOut.value)).toBeInstanceOf(CSL.Value); + expect(ogmiosToCsl.value(txOut.value)).toBeInstanceOf(CSL.Value); }); }); diff --git a/packages/wallet/src/InMemoryUtxoRepository.ts b/packages/wallet/src/InMemoryUtxoRepository.ts index d1c533c55af..76e8c07d0ba 100644 --- a/packages/wallet/src/InMemoryUtxoRepository.ts +++ b/packages/wallet/src/InMemoryUtxoRepository.ts @@ -55,7 +55,7 @@ export class InMemoryUtxoRepository implements UtxoRepository { await this.sync(); } return this.#inputSelector.select({ - utxo: Ogmios.OgmiosToCardanoWasm.utxo([...this.#utxoSet.values()]), + utxo: Ogmios.ogmiosToCsl.utxo([...this.#utxoSet.values()]), outputs, constraints }); diff --git a/packages/wallet/src/SingleAddressWallet.ts b/packages/wallet/src/SingleAddressWallet.ts index 5395a156389..570a74c868e 100644 --- a/packages/wallet/src/SingleAddressWallet.ts +++ b/packages/wallet/src/SingleAddressWallet.ts @@ -41,7 +41,7 @@ export const createSingleAddressWallet = async ( const validityInterval = ensureValidityInterval(tip.slot, props.options?.validityInterval); const txOutputs = csl.TransactionOutputs.new(); for (const output of props.outputs) { - txOutputs.add(Ogmios.OgmiosToCardanoWasm.txOut(output)); + txOutputs.add(Ogmios.ogmiosToCsl.txOut(output)); } const inputSelectionResult = await utxoRepository.selectInputs(txOutputs, { computeMinimumCost: async ({ utxo, outputs, change }) => { diff --git a/packages/wallet/test/InMemoryUtxoRepository.test.ts b/packages/wallet/test/InMemoryUtxoRepository.test.ts index 4182c503729..8f81e68dcdd 100644 --- a/packages/wallet/test/InMemoryUtxoRepository.test.ts +++ b/packages/wallet/test/InMemoryUtxoRepository.test.ts @@ -20,14 +20,14 @@ const addresses = [ const outputs = CSL.TransactionOutputs.new(); outputs.add( - Ogmios.OgmiosToCardanoWasm.txOut({ + Ogmios.ogmiosToCsl.txOut({ address: addresses[0], // value: { coins: 4_000_000, assets: { '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740': 20n } } value: { coins: 4_000_000 } }) ); outputs.add( - Ogmios.OgmiosToCardanoWasm.txOut({ + Ogmios.ogmiosToCsl.txOut({ address: addresses[0], // value: { coins: 2_000_000, assets: { '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740': 20n } } value: { coins: 2_000_000 } diff --git a/packages/wallet/test/createTransactionInternals.test.ts b/packages/wallet/test/createTransactionInternals.test.ts index f8fea3d7628..5b1fb193ac9 100644 --- a/packages/wallet/test/createTransactionInternals.test.ts +++ b/packages/wallet/test/createTransactionInternals.test.ts @@ -14,14 +14,14 @@ const address = const outputs = CSL.TransactionOutputs.new(); outputs.add( - Ogmios.OgmiosToCardanoWasm.txOut({ + Ogmios.ogmiosToCsl.txOut({ address, // value: { coins: 4_000_000, assets: { '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740': 20n } } value: { coins: 4_000_000 } }) ); outputs.add( - Ogmios.OgmiosToCardanoWasm.txOut({ + Ogmios.ogmiosToCsl.txOut({ address, // value: { coins: 2_000_000, assets: { '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740': 20n } } value: { coins: 2_000_000 }