Skip to content

Commit

Permalink
refactor: rename OgmiosToCardanoWasm to ogmiosToCsl
Browse files Browse the repository at this point in the history
The next commit will refactor the object to a function, to inject the csl instance
  • Loading branch information
rhyslbw committed Sep 27, 2021
1 parent 7085f5d commit 474e7e8
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 21 deletions.
6 changes: 3 additions & 3 deletions 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';
Expand Down Expand Up @@ -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()));
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Ogmios/index.ts
@@ -1,2 +1,2 @@
export * from './OgmiosToCardanoWasm';
export * from './ogmiosToCsl';
export * as util from './util';
Expand Up @@ -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) : [];
Expand Down
@@ -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 };
Expand All @@ -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);
});
});
2 changes: 1 addition & 1 deletion packages/wallet/src/InMemoryUtxoRepository.ts
Expand Up @@ -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
});
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet/src/SingleAddressWallet.ts
Expand Up @@ -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 }) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/wallet/test/InMemoryUtxoRepository.test.ts
Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions packages/wallet/test/createTransactionInternals.test.ts
Expand Up @@ -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 }
Expand Down

0 comments on commit 474e7e8

Please sign in to comment.