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

Adjust Maestro::getUnspentOutputByNFT flow to prevent race condition. #178

Merged
merged 1 commit into from
Sep 26, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/gentle-tools-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@blaze-cardano/query": patch
---

Adjust Maestro::getUnspentOutputByNFT flow to prevent race condition.
81 changes: 39 additions & 42 deletions packages/blaze-query/src/maestro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,7 @@ export class Maestro implements Provider {
}

getUnspentOutputByNFT(unit: AssetId): Promise<TransactionUnspentOutput> {
const asset = unit;
const query = `/assets/${asset}/`;

return fetch(`${this.url}${query}utxos?count=100`, {
return fetch(`${this.url}/assets/${unit}/utxos?count=2`, {
headers: this.headers(),
})
.then((resp) => resp.json())
Expand All @@ -221,50 +218,50 @@ export class Maestro implements Provider {
`getUnspentOutputs: Maestro threw "${response.message}"`,
);
}
const utxos: TransactionUnspentOutput[] = [];
for (const maestroUTxO of response.data) {
const txIn = new TransactionInput(
TransactionId(maestroUTxO.tx_hash),
BigInt(maestroUTxO.index),
);

const query2 = `/transactions/${maestroUTxO.tx_hash}/outputs/${maestroUTxO.index}/txo`;

fetch(`${this.url}${query2}?with_cbor=true`, {
headers: this.headers(),
})
.then((resp) => resp.json())
.then((json) => {
if (json) {
const response =
json as MaestroResponse<MaestroOneUTxOResponse>;
if ("message" in response) {
throw new Error(
`getUnspentOutputs: Maestro threw "${response.message}"`,
);
}

const txOut = TransactionOutput.fromCbor(
HexBlob(response.data.txout_cbor),
);
utxos.push(new TransactionUnspentOutput(txIn, txOut));
} else {
throw new Error(
"getUnspentOutputByNFT: Could not parse response json",
);
}
});
}

if (utxos.length !== 1) {
const utxo = response.data[0];
if (response.data.length !== 1 || !utxo) {
throw new Error(
"getUnspentOutputByNFT: Expected 1 UTxO, got " + utxos.length,
"getUnspentOutputByNFT: Expected 1 UTxO, got " +
response.data.length,
);
}

return utxos[0];
return utxo;
} else {
throw new Error("getUnspentOutputs: Could not parse response json");
}
throw new Error("getUnspentOutputs: Could not parse response json");
})
.then((utxo) => {
const txIn = new TransactionInput(
TransactionId(utxo.tx_hash),
BigInt(utxo.index),
);
const query = `/transactions/${utxo.tx_hash}/outputs/${utxo.index}/txo`;

return fetch(`${this.url}${query}?with_cbor=true`, {
headers: this.headers(),
})
.then((resp) => resp.json())
.then((json) => {
if (json) {
const response = json as MaestroResponse<MaestroOneUTxOResponse>;
if ("message" in response) {
throw new Error(
`getUnspentOutputs: Maestro threw "${response.message}"`,
);
}

const txOut = TransactionOutput.fromCbor(
HexBlob(response.data.txout_cbor),
);
return new TransactionUnspentOutput(txIn, txOut);
} else {
throw new Error(
"getUnspentOutputByNFT: Could not parse response json",
);
}
});
})
.then((x) => x!);
}
Expand Down