Skip to content

Commit

Permalink
updated contract
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey committed Apr 17, 2024
1 parent 62eaf4e commit e7f5333
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 54 deletions.
19 changes: 17 additions & 2 deletions src/sdk/blockchain/fuel/FuelNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Provider, Wallet } from "fuels";
import { getDecodedLogs, Provider, Wallet, WalletLocked } from "fuels";
import { makeObservable } from "mobx";
import { Nullable } from "tsdef";

Expand All @@ -16,8 +16,15 @@ import {
} from "../types";

import { Api } from "./Api";
import { NETWORKS, TOKENS_BY_ASSET_ID, TOKENS_BY_SYMBOL, TOKENS_LIST } from "./constants";
import {
CONTRACT_ADDRESSES,
NETWORKS,
TOKENS_BY_ASSET_ID,
TOKENS_BY_SYMBOL,
TOKENS_LIST,
} from "./constants";
import { WalletManager } from "./WalletManager";
import { OrderbookAbi__factory } from "./types/orderbook";

export class FuelNetwork extends BlockchainNetwork {
NETWORK_TYPE = NETWORK.FUEL;
Expand All @@ -37,6 +44,14 @@ export class FuelNetwork extends BlockchainNetwork {
this.providerPromise = Provider.create(NETWORKS[0].url);
}

decodeSpotReceipts(receipts: any[]) {
const orderbookFactory = OrderbookAbi__factory.connect(
CONTRACT_ADDRESSES.spotMarket,
this.walletManager.wallet!
);
return getDecodedLogs(receipts, orderbookFactory.interface);
}

getAddress = (): Nullable<string> => {
return this.walletManager.address;
};
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/blockchain/fuel/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Token } from "../../entity";
import TOKENS_JSON from "./tokens.json";

export const CONTRACT_ADDRESSES = {
spotMarket: "0x1fdc9849457604cc8af9103c5d3cc339da2ce29df8b6e46d358f7c330918cd79",
spotMarket: "0x7134802bdefd097f1c9d8ad86ef27081ae609b84de0afc87b58bd4e04afc6a23",
tokenFactory: "0x6bd9643c9279204b474a778dea7f923226060cb94a4c61c5aae015cf96b5aad2",
vault: "0xe8beef1c4c94e8732b89c5e783c80e9fb7f80fd43ad0c594ba380e4b5556106a",
accountBalance: "0xa842702d600b43a3c7be0e36a0e08452b3d6fc36f0d4015fb6a06cb056cd312d",
Expand Down
4 changes: 2 additions & 2 deletions src/sdk/blockchain/fuel/types/orderbook/OrderbookAbi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export type MarketCreateEventInput = { asset_id: AssetIdInput, asset_decimals: B
export type MarketCreateEventOutput = { asset_id: AssetIdOutput, asset_decimals: number, timestamp: BN };
export type OrderInput = { id: string, trader: AddressInput, base_token: AssetIdInput, base_size: I64Input, base_price: BigNumberish };
export type OrderOutput = { id: string, trader: AddressOutput, base_token: AssetIdOutput, base_size: I64Output, base_price: BN };
export type OrderChangeEventInput = { order_id: string, trader: AddressInput, base_token: AssetIdInput, base_size_change: I64Input, base_price: BigNumberish, timestamp: BigNumberish };
export type OrderChangeEventOutput = { order_id: string, trader: AddressOutput, base_token: AssetIdOutput, base_size_change: I64Output, base_price: BN, timestamp: BN };
export type OrderChangeEventInput = { order_id: string, timestamp: BigNumberish, order: Option<OrderInput> };
export type OrderChangeEventOutput = { order_id: string, timestamp: BN, order: Option<OrderOutput> };
export type TradeEventInput = { base_token: AssetIdInput, order_matcher: AddressInput, seller: AddressInput, buyer: AddressInput, trade_size: BigNumberish, trade_price: BigNumberish, sell_order_id: string, buy_order_id: string, timestamp: BigNumberish };
export type TradeEventOutput = { base_token: AssetIdOutput, order_matcher: AddressOutput, seller: AddressOutput, buyer: AddressOutput, trade_size: BN, trade_price: BN, sell_order_id: string, buy_order_id: string, timestamp: BN };

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -297,29 +297,20 @@ const _abi = {
"typeArguments": null
},
{
"name": "trader",
"type": 9,
"typeArguments": null
},
{
"name": "base_token",
"type": 10,
"typeArguments": null
},
{
"name": "base_size_change",
"type": 11,
"typeArguments": null
},
{
"name": "base_price",
"name": "timestamp",
"type": 20,
"typeArguments": null
},
{
"name": "timestamp",
"type": 20,
"typeArguments": null
"name": "order",
"type": 5,
"typeArguments": [
{
"name": "",
"type": 14,
"typeArguments": null
}
]
}
],
"typeParameters": null
Expand Down Expand Up @@ -597,16 +588,16 @@ const _abi = {
"typeArguments": null
},
"attributes": [
{
"name": "payable",
"arguments": []
},
{
"name": "storage",
"arguments": [
"read",
"write"
]
},
{
"name": "payable",
"arguments": []
}
]
},
Expand Down Expand Up @@ -698,7 +689,7 @@ const _abi = {
"logId": 3,
"loggedType": {
"name": "",
"type": 15,
"type": 4,
"typeArguments": []
}
},
Expand Down Expand Up @@ -738,7 +729,7 @@ const _abi = {
"logId": 8,
"loggedType": {
"name": "",
"type": 4,
"type": 15,
"typeArguments": []
}
},
Expand Down Expand Up @@ -1048,7 +1039,7 @@ const _abi = {
"type": 10,
"typeArguments": []
},
"offset": 99744
"offset": 102128
},
{
"name": "QUOTE_TOKEN_DECIMALS",
Expand All @@ -1057,7 +1048,7 @@ const _abi = {
"type": 19,
"typeArguments": null
},
"offset": 99680
"offset": 102064
},
{
"name": "PRICE_DECIMALS",
Expand All @@ -1066,7 +1057,7 @@ const _abi = {
"type": 19,
"typeArguments": null
},
"offset": 99664
"offset": 102048
}
]
};
Expand Down
56 changes: 36 additions & 20 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { app } from "./app";
import { CONTRACT_ADDRESSES, TOKENS_BY_SYMBOL } from "./sdk/blockchain/fuel/constants";
import { FuelNetwork } from "./sdk/blockchain/fuel";
import { sleep } from "fuels";
import { OrderbookAbi__factory } from "./sdk/blockchain/fuel/types/orderbook";
import BN from "./sdk/utils/BN";

export const NETWORK = {
name: "Fuel",
Expand Down Expand Up @@ -75,16 +77,25 @@ class SparkMatcher {
sellOrder.baseSize.gt(0) &&
buyOrder.baseSize.gt(0)
) {
// const orderbookFactory = OrderbookAbi__factory.connect(
// CONTRACT_ADDRESSES.spotMarket,
// this.sdk.walletManager.wallet!
// );
// const orders = await Promise.all([
// orderbookFactory.functions.order_by_id(sellOrder.id).simulate(),
// orderbookFactory.functions.order_by_id(buyOrder.id).simulate()
// ]).then((res) => res.map((res) => decodeOrder(res.value)));
// console.log(orders);

const orderbookFactory = OrderbookAbi__factory.connect(
CONTRACT_ADDRESSES.spotMarket,
this.sdk.walletManager.wallet!
);
const [sell_res, buy_res] = await Promise.all([
orderbookFactory.functions.order_by_id(sellOrder.id).simulate(),
orderbookFactory.functions.order_by_id(buyOrder.id).simulate(),
]).then((res) => res.map((res) => decodeOrder(res.value)));

if (sell_res == null) {
console.log("👽 Phantom orders: " + sellOrder.id);
sellOrders[i].baseSize = new BN(0);
continue;
}
if (buy_res == null) {
console.log("👽 Phantom orders: " + buyOrder.id);
buyOrders[i].baseSize = new BN(0);
continue;
}
await this.sdk.api
.matchSpotOrders(
sellOrder.id,
Expand All @@ -103,7 +114,10 @@ class SparkMatcher {
buyOrders[i].baseSize = buyOrder.baseSize;
})
.then(() => console.log("✅ Orders matched ", sellOrder.id, buyOrder.id, "\n"))
.catch((e) => console.error(sellOrder.id, buyOrder.id, "\n", e.toString(), "\n"));
.catch((e) => {
// console.log(this.sdk.decodeSpotReceipts([e.receipt]));
console.error(sellOrder.id, buyOrder.id, "\n", e.toString(), "\n");
});
await sleep(100);
}
}
Expand Down Expand Up @@ -135,12 +149,14 @@ const print = `
`;
app.listen(PORT ?? 5000, () => console.log(print));

// function decodeOrder(order: any) {
// return {
// id: order.id,
// trader: order.trader.value,
// base_token: order.base_token.value,
// base_size: (order.base_size.negative ? "-" : "") + order.base_size.value.toString(),
// base_price: order.base_price.toString(),
// };
// }
function decodeOrder(order: any) {
return order != null
? {
id: order.id,
trader: order.trader.value,
base_token: order.base_token.value,
base_size: (order.base_size.negative ? "-" : "") + order.base_size.value.toString(),
base_price: order.base_price.toString(),
}
: null;
}

0 comments on commit e7f5333

Please sign in to comment.