Skip to content

Commit

Permalink
Fix query
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Nov 22, 2023
1 parent ff942dd commit 0be3b7a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
2 changes: 0 additions & 2 deletions NBXplorer.Client/ExplorerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,11 @@ public async Task<KeyPathInformation> GetKeyInformationAsync(TrackedSource track
return await SendAsync<KeyPathInformation>(HttpMethod.Get, null, $"{GetBasePath(trackedSource)}/scripts/{script.ToHex()}", cancellation).ConfigureAwait(false);
}

[Obsolete("Use GetKeyInformationAsync(DerivationStrategyBase strategy, Script script) instead")]
public async Task<KeyPathInformation[]> GetKeyInformationsAsync(Script script, CancellationToken cancellation = default)
{
return await SendAsync<KeyPathInformation[]>(HttpMethod.Get, null, $"v1/cryptos/{CryptoCode}/scripts/{script.ToHex()}", cancellation).ConfigureAwait(false);
}

[Obsolete("Use GetKeyInformation(DerivationStrategyBase strategy, Script script) instead")]
public KeyPathInformation[] GetKeyInformations(Script script, CancellationToken cancellation = default)
{
return GetKeyInformationsAsync(script, cancellation).GetAwaiter().GetResult();
Expand Down
4 changes: 2 additions & 2 deletions NBXplorer.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4465,7 +4465,7 @@ private async Task Eventually(Func<Task> tsk)
break;

}
catch (Exception e)
catch (Exception)
{
await Task.Delay(500);
}
Expand Down Expand Up @@ -4554,7 +4554,7 @@ await Eventually(async () =>
{
parentWalletUtxos = await tester.Client.GetUTXOsAsync(parentWalletTS);
scriptBagUtxos = await tester.Client.GetUTXOsAsync(wallet1TS);
Assert.Equal(1, scriptBagUtxos.GetUnspentUTXOs().Length);
Assert.Single(scriptBagUtxos.GetUnspentUTXOs());
Assert.Equal(2, parentWalletUtxos.GetUnspentUTXOs().Length);
Assert.Equal(derivationUtxos.GetUnspentUTXOs().Count() + scriptBagUtxos.GetUnspentUTXOs().Length,
parentWalletUtxos.GetUnspentUTXOs().Count());
Expand Down
8 changes: 3 additions & 5 deletions NBXplorer/Backends/Postgres/PostgresRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,20 +485,18 @@ async Task<MultiValueDictionary<Script, KeyPathInformation>> GetKeyInformations(
string additionalColumn = Network.IsElement ? ", ts.blinded_addr" : "";
var rows = await connection.QueryAsync($@"
SELECT ts.code, ts.script, ts.addr, ts.derivation, ts.keypath, ts.redeem{additionalColumn},
ws.wallet_id,
ts.wallet_id,
w.metadata->>'type' AS wallet_type
FROM unnest(@records) AS r (script),
LATERAL (
SELECT code, script, addr, descriptor_metadata->>'derivation' derivation,
SELECT code, script, wallet_id, addr, descriptor_metadata->>'derivation' derivation,
keypath, descriptors_scripts_metadata->>'redeem' redeem,
descriptors_scripts_metadata->>'blindedAddress' blinded_addr,
descriptor_metadata->>'descriptor' descriptor
FROM nbxv1_keypath_info ki
WHERE ki.code=@code AND ki.script=r.script
) ts
LEFT JOIN wallets_scripts ws USING(code, script)
LEFT JOIN wallets w USING(wallet_id)
WHERE ws.wallet_id IS NOT NULL;",
JOIN wallets w USING(wallet_id)",
new { code = Network.CryptoCode, records = scripts.Select(s => s.ToHex()).ToArray() });

foreach (var r in rows)
Expand Down
13 changes: 13 additions & 0 deletions NBXplorer/DBScripts/019.KeyPathInfoReturnsWalletId.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE OR REPLACE VIEW nbxv1_keypath_info AS
SELECT ws.code,
ws.script,
s.addr,
d.metadata AS descriptor_metadata,
nbxv1_get_keypath(d.metadata, ds.idx) AS keypath,
ds.metadata AS descriptors_scripts_metadata,
ws.wallet_id
FROM ((wallets_scripts ws
JOIN scripts s ON (((s.code = ws.code) AND (s.script = ws.script))))
LEFT JOIN ((wallets_descriptors wd
JOIN descriptors_scripts ds ON (((ds.code = wd.code) AND (ds.descriptor = wd.descriptor))))
JOIN descriptors d ON (((d.code = ds.code) AND (d.descriptor = ds.descriptor)))) ON (((wd.wallet_id = ws.wallet_id) AND (wd.code = ws.code) AND (ds.script = ws.script))));
4 changes: 3 additions & 1 deletion NBXplorer/DBScripts/FullSchema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,8 @@ CREATE VIEW nbxv1_keypath_info AS
s.addr,
d.metadata AS descriptor_metadata,
nbxv1_get_keypath(d.metadata, ds.idx) AS keypath,
ds.metadata AS descriptors_scripts_metadata
ds.metadata AS descriptors_scripts_metadata,
ws.wallet_id
FROM ((wallets_scripts ws
JOIN scripts s ON (((s.code = ws.code) AND (s.script = ws.script))))
LEFT JOIN ((wallets_descriptors wd
Expand Down Expand Up @@ -1347,6 +1348,7 @@ INSERT INTO nbxv1_migrations VALUES ('015.AvoidWAL');
INSERT INTO nbxv1_migrations VALUES ('016.FixTempTableCreation');
INSERT INTO nbxv1_migrations VALUES ('017.FixDoubleSpendDetection');
INSERT INTO nbxv1_migrations VALUES ('018.FastWalletRecent');
INSERT INTO nbxv1_migrations VALUES ('019.KeyPathInfoReturnsWalletId');

ALTER TABLE ONLY nbxv1_migrations
ADD CONSTRAINT nbxv1_migrations_pkey PRIMARY KEY (script_name);
1 change: 1 addition & 0 deletions NBXplorer/NBXplorer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<None Remove="DBScripts\016.FixTempTableCreation.sql" />
<None Remove="DBScripts\017.FixDoubleSpendDetection.sql" />
<None Remove="DBScripts\018.FastWalletRecent.sql" />
<None Remove="DBScripts\019.KeyPathInfoReturnsWalletId.sql" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="DBScripts\*.sql" />
Expand Down

0 comments on commit 0be3b7a

Please sign in to comment.