Skip to content

Commit

Permalink
fxied frontend bug and upgraded the matcher engine
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey committed Feb 19, 2024
1 parent e7903a3 commit 2b9e7d7
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 28 deletions.
1 change: 0 additions & 1 deletion services/spark-matcher-evm/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { loadVar } from "./utils/envUtils";
config({ path: path.join(__dirname, "../.env") });

export const PORT = loadVar("PORT", true);
export const INDEXER_URL = loadVar("INDEXER_URL");
export const PRIVATE_KEY = loadVar("PRIVATE_KEY");
export const CONTRACT_ADDRESS = loadVar("CONTRACT_ADDRESS");
export const NODE_URL = loadVar("NODE_URL");
2 changes: 1 addition & 1 deletion services/spark-matcher-evm/src/entity/SpotMarketOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class SpotMarketOrder {
readonly baseToken: Token;
readonly quoteToken = TOKENS_BY_SYMBOL.USDC;
readonly trader: string;
readonly baseSize: BN;
baseSize: BN;
readonly baseSizeUnits: BN;
readonly quoteSize: BN;
readonly quoteSizeUnits: BN;
Expand Down
33 changes: 28 additions & 5 deletions services/spark-matcher-evm/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,38 @@ class SparkMatcher {
public doMatch = async () => {
const market = TOKENS_BY_SYMBOL.BTC.assetId;

const [buyOrders, sellOrders]: [SpotMarketOrder[], SpotMarketOrder[]] = await Promise.all([
let [buyOrders, sellOrders]: [SpotMarketOrder[], SpotMarketOrder[]] = await Promise.all([
this.spotMarket.getOrders({ market, limit: 100, type: "BUY" }),
this.spotMarket.getOrders({ market, limit: 100, type: "SELL" }),
]);

for (let sellOrder of sellOrders) {
for (let buyOrder of buyOrders) {
if (sellOrder.baseToken === buyOrder.baseToken && sellOrder.price.lte(buyOrder.price)) {
await this.spotMarket.matchOrders(sellOrder.id, buyOrder.id).catch(console.log);
for (let i = 0; i < sellOrders.length; ++i) {
let sellOrder = sellOrders[i];
if (sellOrder.baseSize.eq(0)) continue;
for (let j = 0; j < buyOrders.length; ++j) {
let buyOrder = buyOrders[j];
if (buyOrder.baseSize.eq(0)) continue;
if (
sellOrder.baseToken === buyOrder.baseToken &&
sellOrder.price.lte(buyOrder.price) &&
sellOrder.type === "SELL" &&
buyOrder.type === "BUY" &&
sellOrder.baseSize.gt(0) &&
buyOrder.baseSize.gt(0)
) {
await this.spotMarket
.matchOrders(sellOrder.id, buyOrder.id)
.then(() => {
const amount =
sellOrder.baseSize > buyOrder.baseSize ? buyOrder.baseSize : sellOrder.baseSize;

sellOrder.baseSize = sellOrder.baseSize.minus(amount);
sellOrders[i].baseSize = sellOrder.baseSize;

buyOrder.baseSize = buyOrder.baseSize.minus(amount);
buyOrders[i].baseSize = buyOrder.baseSize;
})
.catch((e) => console.log(e.reason));
}
}
}
Expand Down
23 changes: 18 additions & 5 deletions services/spark-matcher-evm/src/services/SpotMarketService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import axios from "axios";
import { SpotMarketOrder } from "../entity/SpotMarketOrder";
import { ethers } from "ethers";
import { CONTRACT_ADDRESS, INDEXER_URL, NODE_URL, PRIVATE_KEY } from "../config";
import { CONTRACT_ADDRESS, NODE_URL, PRIVATE_KEY } from "../config";
import { SPOT_MARKET_ABI } from "../abi";
import { fetchIndexer } from "../utils/fetchIndexer";

type TOrderType = "BUY" | "SELL";
type TOrderResponse = {
Expand All @@ -21,6 +22,18 @@ export class SpotMarket {
constructor() {
const provider = new ethers.JsonRpcProvider(NODE_URL);
this.signer = new ethers.Wallet(PRIVATE_KEY, provider);

this.signer
.getAddress()
.then(async (address) => ({
balance: await provider.getBalance(address),
address,
}))
.then(({ balance, address }) =>
console.log(
`🕺 Matcher address: ${address}\n Balance: ${ethers.formatUnits(balance)} ETH\n`
)
);
this.contract = new ethers.Contract(CONTRACT_ADDRESS, SPOT_MARKET_ABI, this.signer);
}

Expand All @@ -46,7 +59,7 @@ export class SpotMarket {
}
`;
try {
const response = await axios.post(INDEXER_URL, { query });
const response = await fetchIndexer(query);
return response.data.data.orders.map((order: TOrderResponse) => new SpotMarketOrder(order));
} catch (error) {
console.error("Error during Orders request:", error);
Expand All @@ -59,9 +72,9 @@ export class SpotMarket {
const tx = await this.contract.matchOrders(sellOrder, buyOrder);
await tx.wait();

console.log("✅ Orders matched:", sellOrder, buyOrder);
} catch (error) {
console.log("❌Error matching orders:", error);
console.log("✅ Orders matched:", sellOrder, buyOrder, "\n");
} catch (error: any) {
console.log("❌ Error matching orders:", error.toString(), "\n");
}
};
}
31 changes: 17 additions & 14 deletions services/spark-matcher-evm/src/utils/fetchIndexer.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { INDEXER_URL } from "../config";
import axios from "axios";

export default async function fetchIndexer<T>(query: string) {
const config = {
method: "POST",
url: INDEXER_URL,
headers: { "Content-Type": "application/json", Accept: "application/json" },
data: { query },
};
const result: T = await axios
.request(config)
.then((result) => result.data.data[0])
.catch((e) => console.error(`❌ Indexer call: ${e.toString()}\nQuery: ${config.data.query}`));
return result;
}
export const INDEXER_URLS = [
"https://api.studio.thegraph.com/query/63182/arbitrum-sepolia-spot-market/version/latest",
"https://api.studio.thegraph.com/query/63182/spark-arbitrum-spor-market/version/latest",
];

export const fetchIndexer = async (query: string) => {
for (const i in INDEXER_URLS) {
const indexer = INDEXER_URLS[i];
try {
return await axios.post(indexer, { query });
} catch (error: any) {
console.error(`❌ Indexer call: ${error.toString()}\n Indexer: ${indexer}\n`);
/*eslint-disable-next-line */
}
}
return await axios.post(INDEXER_URLS[0], { query });
};
2 changes: 1 addition & 1 deletion services/spark-matcher-evm/src/utils/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ export const print = `
███████╗██║ ██║██████╔╝███████║
╚══════╝╚═╝ ╚═╝╚═════╝ ╚══════╝
${"🚀 Server ready at: http://localhost:" + (PORT ?? 5000)} `;
${"🚀 Server ready at: http://localhost:" + (PORT ?? 5000)} \n`;
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const BottomTablesInterfaceSpotImpl: React.FC<IProps> = observer(() => {
</SmartFlex>
),
price: toCurrency(order.priceUnits.toSignificant(2)),
filled: BN.ZERO,
filled: BN.ZERO.toString(),
}));

const getBalanceData = () =>
Expand Down

0 comments on commit 2b9e7d7

Please sign in to comment.