Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added working getUtxos to kupmios provider #40

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
Binary file removed bun.lockb
Binary file not shown.
File renamed without changes.
42 changes: 37 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
{
"name": "translucent-monorepo",
"workspaces": [
"packages/*"
],
"version": "0.0.1"
"name": "translucent-cardano",
"module": "index.ts",
"type": "module",
"version": "0.0.5",
"scripts": {
"build:wasm": "cd ./uplc && ./build.sh && rm pkg-node/.gitignore && rm pkg-web/.gitignore && cd .. && npm i"
},
"devDependencies": {
"@types/jest": "^29.5.5",
"@types/sha256": "^0.2.2",
"bun-types": "latest",
"eslint": "^8.50.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"typescript": "^5.3.3",
"@cardano-ogmios/schema": "^6.0.0-rc6"
},
"dependencies": {
"@dcspark/cardano-multiplatform-lib-browser": "^3.1.2",
"@dcspark/cardano-multiplatform-lib-nodejs": "^3.1.2",
"@emurgo/cardano-message-signing-browser": "^1.0.1",
"@emurgo/cardano-message-signing-nodejs": "^1.0.1",
"@sinclair/typebox": "^0.31.28",
"fast-check": "^3.14.0",
"prettier": "^3.1.1",
"sha256": "^0.2.0",
"uplc-node": "^0.0.3",
"uplc-web": "^0.0.3"
},
"browser": {
"@dcspark/cardano-multiplatform-lib-nodejs": "@dcspark/cardano-multiplatform-lib-browser",
"@emurgo/cardano-message-signing-nodejs": "@emurgo/cardano-message-signing-browser",
"uplc-node": "uplc-web"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}
Empty file modified packages/translucent-blueprint/cli.ts
100644 → 100755
Empty file.
39 changes: 0 additions & 39 deletions packages/translucent/package.json

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
UTxO,
} from "../types/mod.ts";
import { C } from "../core/mod.ts";
import { costModelKeys, fromUnit } from "../utils/mod.ts";
import { costModelKeys, fromHex, fromUnit, toHex } from "../utils/mod.ts";
import * as ogmios from "@cardano-ogmios/schema";

function fromMaybeBuffer(x: string | Buffer) {
Expand Down Expand Up @@ -68,7 +68,7 @@ export class Kupmios implements Provider {
client.addEventListener(
"message",
(msg: MessageEvent<string | Buffer>): unknown => {
console.log("queryLedgerState/protocolParameters", msg.data);
//console.log("queryLedgerState/protocolParameters", msg.data);
try {
const {
result,
Expand Down Expand Up @@ -97,11 +97,11 @@ export class Kupmios implements Provider {
result.scriptExecutionPrices!.cpu.split("/");
const protocolParams: ProtocolParameters = {
minFeeA: result.minFeeCoefficient,
minFeeB: Number(result.minFeeConstant.lovelace),
minFeeB: Number(result.minFeeConstant.ada.lovelace),
maxTxSize: result.maxTransactionSize!.bytes,
maxValSize: result.maxValueSize!.bytes,
keyDeposit: BigInt(result.stakeCredentialDeposit.lovelace),
poolDeposit: BigInt(result.stakePoolDeposit.lovelace),
keyDeposit: BigInt(result.stakeCredentialDeposit.ada.lovelace),
poolDeposit: BigInt(result.stakePoolDeposit.ada.lovelace),
priceMem: [BigInt(memNum), BigInt(memDenom)],
priceStep: [BigInt(stepsNum), BigInt(stepsDenom)],
maxTxExMem: BigInt(
Expand All @@ -126,54 +126,17 @@ export class Kupmios implements Provider {
}

async getUtxos(addressOrCredential: Address | Credential): Promise<UTxO[]> {
let addy =
typeof addressOrCredential == "string"
const isAddress = typeof addressOrCredential === "string";
const queryPredicate = isAddress
? addressOrCredential
: C.EnterpriseAddress.new(
0,
C.StakeCredential.from_keyhash(
C.Ed25519KeyHash.from_hex(addressOrCredential.hash),
),
)
.to_address()
.to_bech32(undefined);
let params: ogmios.UtxoByAddresses | ogmios.UtxoByOutputReferences = {
addresses: [addy],
};
const client = await this.ogmiosWsp("queryLedgerState/utxo", params);
return new Promise((res, rej) => {
client.addEventListener(
"message",
(msg: MessageEvent<string | Buffer>) => {
try {
const response:
| ogmios.QueryLedgerStateUtxoResponse
| ogmios.QueryLedgerStateEraMismatch
| ogmios.QueryLedgerStateAcquiredExpired = JSON.parse(
fromMaybeBuffer(msg.data),
);
if ("result" in response) {
res(
response.result.map((utxo) => {
return {
txHash: utxo.transaction.id,
outputIndex: utxo.index,
assets: fromOgmiosValue(utxo.value),
address: utxo.address,
datumHash: utxo.datumHash,
datum: utxo.datum,
script: utxo.script,
} as UTxO;
}),
);
} else {
console.error("UTXO Fetch error", response.error);
}
} catch {}
},
);
});
}
: addressOrCredential.hash;
const result = await fetch(
`${this.kupoUrl}/matches/${queryPredicate}${
isAddress ? "" : "/*"
}?unspent`,
).then((res) => res.json());
return this.kupmiosUtxosToUtxos(result);
}

async getUtxosWithUnit(
addressOrCredential: Address | Credential,
Expand Down Expand Up @@ -231,44 +194,26 @@ export class Kupmios implements Provider {
}

async getUtxosByOutRef(outRefs: Array<OutRef>): Promise<UTxO[]> {
let params: ogmios.UtxoByAddresses | ogmios.UtxoByOutputReferences = {
outputReferences: outRefs.map((x) => {
return { transaction: { id: x.txHash }, index: x.outputIndex };
const queryHashes = [...new Set(outRefs.map((outRef) => outRef.txHash))];

const utxos = await Promise.all(
queryHashes.map(async (txHash) => {
const result = await fetch(
`${this.kupoUrl}/matches/*@${txHash}?unspent`,
).then((res) => res.json());
return this.kupmiosUtxosToUtxos(result);
}),
};
const client = await this.ogmiosWsp("queryLedgerState/utxo", params);
return new Promise((res, rej) => {
client.addEventListener(
"message",
(msg: MessageEvent<string | Buffer>) => {
try {
const response:
| ogmios.QueryLedgerStateUtxoResponse
| ogmios.QueryLedgerStateEraMismatch
| ogmios.QueryLedgerStateAcquiredExpired = JSON.parse(
fromMaybeBuffer(msg.data),
);
if ("result" in response) {
res(
response.result.map((utxo) => {
return {
txHash: utxo.transaction.id,
outputIndex: utxo.index,
assets: fromOgmiosValue(utxo.value),
address: utxo.address,
datumHash: utxo.datumHash,
datum: utxo.datum,
script: utxo.script,
} as UTxO;
}),
);
} else {
console.error("UTXO Fetch error", response.error);
}
} catch {}
},
);

return utxos
.reduce((acc, utxos) => acc.concat(utxos), [])
.filter((utxo) =>
outRefs.some(
(outRef) =>
utxo.txHash === outRef.txHash &&
utxo.outputIndex === outRef.outputIndex,
),
);
});
}

async getDelegation(rewardAddress: RewardAddress): Promise<Delegation> {
Expand Down Expand Up @@ -376,4 +321,54 @@ export class Kupmios implements Provider {
);
return client;
}

private kupmiosUtxosToUtxos(utxos: unknown): Promise<UTxO[]> {
// deno-lint-ignore no-explicit-any
return Promise.all(
(utxos as any).map(async (utxo: any) => {
return {
txHash: utxo.transaction_id,
outputIndex: parseInt(utxo.output_index),
address: utxo.address,
assets: (() => {
const a: Assets = { lovelace: BigInt(utxo.value.coins) };
Object.keys(utxo.value.assets).forEach((unit) => {
a[unit.replace(".", "")] = BigInt(utxo.value.assets[unit]);
});
return a;
})(),
datumHash: utxo?.datum_type === "hash" ? utxo.datum_hash : null,
datum:
utxo?.datum_type === "inline"
? await this.getDatum(utxo.datum_hash)
: null,
scriptRef:
utxo.script_hash &&
(await (async () => {
const { script, language } = await fetch(
`${this.kupoUrl}/scripts/${utxo.script_hash}`,
).then((res) => res.json());

if (language === "native") {
return { type: "Native", script };
} else if (language === "plutus:v1") {
return {
type: "PlutusV1",
script: toHex(
C.PlutusV1Script.new(fromHex(script)).to_bytes(),
),
};
} else if (language === "plutus:v2") {
return {
type: "PlutusV2",
script: toHex(
C.PlutusV2Script.new(fromHex(script)).to_bytes(),
),
};
}
})()),
} as UTxO;
}),
);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"esModuleInterop": true
},
"exclude": ["node_modules"],
"include": ["packages/**/*.ts", "*.json"]
"include": ["src/**/*.ts", "*.json"]
}