Skip to content

Commit

Permalink
shakedex: query finalize tx when not found in db (#333)
Browse files Browse the repository at this point in the history
* shakedex: query finalize tx when not found in db

* refactor query finalize tx by transfer tx logic

* add back log
  • Loading branch information
chikeichan authored Apr 18, 2021
1 parent ff76917 commit 24a39ff
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 28 deletions.
27 changes: 24 additions & 3 deletions app/background/shakedex/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ import {encrypt, decrypt} from "../../utils/encrypt";
import path from "path";
import {app} from "electron";
import bdb from "bdb";
import {auctionSchema, finalizeLockScheme, fulfillmentSchema, nameLockSchema, paramSchema} from "../../utils/shakedex";
import {
auctionSchema,
finalizeLockScheme,
fulfillmentSchema,
getFinalizeFromTransferTx,
nameLockSchema,
paramSchema
} from "../../utils/shakedex";
import {Client} from "bcurl";

let db;
Expand Down Expand Up @@ -337,6 +344,17 @@ export async function launchAuction(nameLock, passphrase, paramsOverride) {
break;
}

const {
tx: finalizeTx,
coin: finalizeCoin,
} = await getFinalizeFromTransferTx(
listing.nameLock.transferTxHash,
listing.nameLock.name,
nodeService,
);

if (!finalizeCoin) throw new Error('cannot find finalize coin');

const auctionFactory = new AuctionFactory({
name: listing.nameLock.name,
startTime: Date.now(),
Expand All @@ -346,11 +364,14 @@ export async function launchAuction(nameLock, passphrase, paramsOverride) {
reductionTimeMS,
reductionStrategy: linearReductionStrategy,
});

const auction = await auctionFactory.createAuction(
context,
new NameLockFinalize({
...listing.finalizeLock,
privateKey: decrypt(listing.finalizeLock.encryptedPrivateKey, passphrase)
...listing.nameLock,
finalizeTxHash: finalizeTx.hash,
finalizeOutputIdx: finalizeCoin.index,
privateKey: decrypt(listing.nameLock.encryptedPrivateKey, passphrase)
}),
);
const auctionJSON = auction.toJSON(context);
Expand Down
18 changes: 12 additions & 6 deletions app/ducks/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { clientStub as shakedexClientStub } from '../background/shakedex/client.
import { clientStub as nodeClientStub } from '../background/node/client.js';
import { showSuccess, showError } from './notifications.js';
import networks from 'hsd/lib/protocol/networks.js';
import {getFinalizeFromTransferTx} from "../utils/shakedex";

const shakedex = shakedexClientStub(() => require('electron').ipcRenderer);
const nodeClient = nodeClientStub(() => require('electron').ipcRenderer);
Expand Down Expand Up @@ -213,6 +214,7 @@ export const getExchangeListings = (page = 1) => async (dispatch, getState) => {
const transferLockup = networks[info.network].names.transferLockup;

for (const listing of listings) {
console.log({ listing });
let transferTx;
let finalizeTx;
let finalizeCoin;
Expand All @@ -221,12 +223,16 @@ export const getExchangeListings = (page = 1) => async (dispatch, getState) => {
let cancelCoin;
try {
transferTx = await nodeClient.getTx(listing.nameLock.transferTxHash);
finalizeTx = listing.finalizeLock
? await nodeClient.getTx(listing.finalizeLock.finalizeTxHash)
: null;
finalizeCoin = listing.finalizeLock
? await nodeClient.getCoin(listing.finalizeLock.finalizeTxHash, listing.finalizeLock.finalizeOutputIdx)
: null;

const finalize = await getFinalizeFromTransferTx(
listing.nameLock.transferTxHash,
listing.nameLock.name,
nodeClient,
);

finalizeTx = finalize.tx;
finalizeCoin = finalize.coin;

cancelTx = listing.nameLockCancel
? await nodeClient.getTx(listing.nameLockCancel.transferTxHash)
: null;
Expand Down
67 changes: 48 additions & 19 deletions app/utils/shakedex.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,25 +225,6 @@ export async function validateAuction(auction, nodeClient) {
if (!res.valid) {
throw new Error('Invalid auction schema.');
}

// const proofs = auction.data.map(a => new SwapProof({
// name: auction.name,
// lockingTxHash: auction.lockingTxHash,
// lockingOutputIdx: auction.lockingOutputIdx,
// publicKey: auction.publicKey,
// paymentAddr: auction.paymentAddr,
// price: a.price,
// lockTime: a.lockTime,
// signature: a.signature,
// }));


// for (const proof of proofs) {
// const ok = await proof.verify({ nodeClient });
// if (!ok) {
// throw new Error('Swap proofs failed validation.');
// }
// }
}

export function fromAuctionJSON(json) {
Expand All @@ -262,3 +243,51 @@ export function fromAuctionJSON(json) {
})),
};
}

export async function getFinalizeFromTransferTx(transferTxHash, name, nodeClient) {
let finalizeTx, finalizeCoin, finalizeOutputIdx;

const { info: { nameHash }} = await nodeClient.getNameInfo(name);
const transferTx = await nodeClient.getTx(transferTxHash);
let prevoutIndex;
const transferOutput = transferTx?.outputs.filter((output, i) => {
if (output.covenant.action === 'TRANSFER'
&& output.covenant.items
&& output.covenant.items[0] === nameHash) {
prevoutIndex = i;
return true;
}
})[0];

const transactions = transferOutput && await nodeClient.getTXByAddresses([transferOutput.address]);

finalizeTx = transactions
? transactions.filter((transaction) => {
return transaction.inputs.filter(input => {
return input.prevout.hash === transferTx.hash
&& input.prevout.index === prevoutIndex;
})[0];
})[0]
: null;

if (finalizeTx) {
const txHash = finalizeTx.hash;
finalizeOutputIdx = 0;

for (let i = 0; i < finalizeTx.outputs.length; i++) {
if (finalizeTx.outputs[i].covenant.action === 'FINALIZE') {
finalizeOutputIdx = i;
}
}

finalizeCoin = await nodeClient.getCoin(
txHash,
finalizeOutputIdx,
);
}

return {
tx: finalizeTx,
coin: finalizeCoin,
};
}

0 comments on commit 24a39ff

Please sign in to comment.