Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/configs/typescript/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"sourceMap": true,
"allowJs": true,
"allowSyntheticDefaultImports": true,
"noUnusedLocals": false
"noUnusedLocals": false,
"types": ["node", "jest"]
}
}
38 changes: 38 additions & 0 deletions packages/mesh-core-cst/src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,41 @@ export const toPlutusLanguageVersion = (
return PlutusLanguageVersion.V3;
}
};

export const utxosToCborMap = (utxos: UTxO[]): string => {
const cborWriter = new Serialization.CborWriter();
cborWriter.writeStartMap(utxos.length);
for (const utxo of utxos) {
const cardanoUtxo = toTxUnspentOutput(utxo);
cborWriter.writeEncodedValue(
Buffer.from(cardanoUtxo.input().toCbor(), "hex"),
);
cborWriter.writeEncodedValue(
Buffer.from(cardanoUtxo.output().toCbor(), "hex"),
);
}
return cborWriter.encodeAsHex();
};

export const cborMapToUtxos = (cborMaps: string[]): UTxO[] => {
const utxos: UTxO[] = [];
for (const cborMap of cborMaps) {
const cborReader = new Serialization.CborReader(
Buffer.from(cborMap, "hex"),
);
const mapLength = cborReader.readStartMap();
if (!mapLength) {
throw new Error("Invalid CBOR map: expected a map of UTxOs");
}
for (let i = 0; i < mapLength; i++) {
const inputCbor = cborReader.readEncodedValue();
const outputCbor = cborReader.readEncodedValue();
const utxo = Serialization.TransactionUnspentOutput.fromCore([
Serialization.TransactionInput.fromCbor(inputCbor).toCore(),
Serialization.TransactionOutput.fromCbor(outputCbor).toCore(),
]);
utxos.push(fromTxUnspentOutput(utxo));
}
}
return utxos;
};
3 changes: 2 additions & 1 deletion packages/mesh-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"@meshsdk/core-cst": "1.9.0-beta.102",
"@meshsdk/provider": "1.9.0-beta.100",
"@meshsdk/transaction": "1.9.0-beta.102",
"@meshsdk/wallet": "1.9.0-beta.102"
"@meshsdk/wallet": "1.9.0-beta.102",
"scalus": "^0.15.0"
},
"prettier": "@meshsdk/configs/prettier",
"publishConfig": {
Expand Down
96 changes: 96 additions & 0 deletions packages/mesh-core/src/utils/emulator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Emulator, SlotConfig as ScalusSlotConfig, SubmitResult } from "scalus";

import {
AccountInfo,
Asset,
AssetMetadata,
BlockInfo,
GovernanceProposalInfo,
IFetcher,
IFetcherOptions,
Protocol,
SlotConfig,
TransactionInfo,
UTxO,
} from "@meshsdk/common";
import { cborMapToUtxos, utxosToCborMap } from "@meshsdk/core-cst";
import { OfflineFetcher } from "@meshsdk/provider";

export class MeshEmulator extends Emulator implements IFetcher {
fetcher: OfflineFetcher;

constructor(initialUtxos: UTxO[], slotConfig: SlotConfig) {
super(
Buffer.from(utxosToCborMap(initialUtxos), "hex"),
new ScalusSlotConfig(
slotConfig.zeroTime,
slotConfig.zeroSlot,
slotConfig.slotLength,
),
);
this.fetcher = new OfflineFetcher();
this.fetcher.addUTxOs(initialUtxos);
}
fetchAccountInfo(address: string): Promise<AccountInfo> {
return this.fetcher.fetchAccountInfo(address);
}
fetchAddressUTxOs(address: string, asset?: string): Promise<UTxO[]> {
return this.fetcher.fetchAddressUTxOs(address, asset);
}
fetchAddressTxs(
address: string,
options?: IFetcherOptions,
): Promise<TransactionInfo[]> {
return this.fetcher.fetchAddressTxs(address, options);
}
fetchAssetAddresses(
asset: string,
): Promise<{ address: string; quantity: string }[]> {
return this.fetcher.fetchAssetAddresses(asset);
}
fetchAssetMetadata(asset: string): Promise<AssetMetadata> {
return this.fetcher.fetchAssetMetadata(asset);
}
fetchBlockInfo(hash: string): Promise<BlockInfo> {
return this.fetcher.fetchBlockInfo(hash);
}
fetchCollectionAssets(
policyId: string,
cursor?: number | string,
): Promise<{ assets: Asset[]; next?: string | number | null }> {
return this.fetcher.fetchCollectionAssets(policyId, cursor);
}
fetchProtocolParameters(epoch: number): Promise<Protocol> {
return this.fetcher.fetchProtocolParameters(epoch);
}
fetchTxInfo(hash: string): Promise<TransactionInfo> {
return this.fetcher.fetchTxInfo(hash);
}
fetchUTxOs(hash: string, index?: number): Promise<UTxO[]> {
return this.fetcher.fetchUTxOs(hash);
}
fetchGovernanceProposal(
txHash: string,
certIndex: number,
): Promise<GovernanceProposalInfo> {
return this.fetcher.fetchGovernanceProposal(txHash, certIndex);
}
get(url: string): Promise<any> {
return this.fetcher.get(url);
}

submitTxHex(txHex: string): SubmitResult {
const result = this.submitTx(Buffer.from(txHex, "hex"));
if (result.isSuccess) {
this.fetcher = new OfflineFetcher();
this.fetcher.addUTxOs(this.getAllUtxosMesh());
}
return result;
}

getAllUtxosMesh() {
const allUtxos = this.getAllUtxos();
const utxoList = allUtxos.map((u) => Buffer.from(u).toString("hex"));
return cborMapToUtxos(utxoList);
}
}
3 changes: 0 additions & 3 deletions packages/mesh-core/src/utils/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import JSONBig from "json-bigint";

import {
BuilderData,
Data,
DeserializedAddress,
NativeScript,
PlutusDataType,
Expand Down
Loading
Loading