-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1067 from mrgnlabs/feat/arena-client
feat(mfi-trading): introduction of arena client
- Loading branch information
Showing
14 changed files
with
159 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
apps/marginfi-v2-trading/src/pages/api/processTransaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import cookie from "cookie"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { fetchAuthToken } from "~/utils"; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (!process.env.MARGINFI_API_URL) { | ||
return res.status(500).json({ error: "API URL is not set" }); | ||
} | ||
|
||
const cookies = cookie.parse(req.headers.cookie || ""); | ||
let token = cookies.jwt; | ||
|
||
// If the token is missing, fetch a new one | ||
if (!token) { | ||
try { | ||
token = await fetchAuthToken(req); | ||
} catch (error) { | ||
console.error("Error fetching new JWT:", error); | ||
return res.status(401).json({ error: "Unauthorized: Unable to fetch token" }); | ||
} | ||
} | ||
|
||
try { | ||
const response = await fetch(`${process.env.MARGINFI_API_URL}/arena/process-transactions`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
body: JSON.stringify({}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`API responded with status: ${response.status}`); | ||
} | ||
|
||
return res.status(200).json({ message: "Transaction processed successfully" }); | ||
} catch (error) { | ||
console.error("Error processing transaction:", error); | ||
return res.status(500).json({ | ||
error: "Internal server error", | ||
message: process.env.NODE_ENV === "development" ? error : undefined, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { AddressLookupTableAccount, PublicKey, TransactionSignature } from "@solana/web3.js"; | ||
|
||
import { BankMetadataMap, SolanaTransaction, TransactionOptions, Wallet } from "@mrgnlabs/mrgn-common"; | ||
|
||
import { MarginfiGroup } from "../models/group"; | ||
import { MarginfiConfig, MarginfiProgram } from "../types"; | ||
import { PythPushFeedIdMap } from "../utils"; | ||
import { ProcessTransactionStrategy, ProcessTransactionsClientOpts } from "../services"; | ||
import { MarginfiClient, BankMap, OraclePriceMap, MintDataMap } from ".."; | ||
|
||
class ArenaClient extends MarginfiClient { | ||
constructor( | ||
config: MarginfiConfig, | ||
program: MarginfiProgram, | ||
wallet: Wallet, | ||
isReadOnly: boolean, | ||
group: MarginfiGroup, | ||
banks: BankMap, | ||
priceInfos: OraclePriceMap, | ||
mintDatas: MintDataMap, | ||
feedIdMap: PythPushFeedIdMap, | ||
addressLookupTables?: AddressLookupTableAccount[], | ||
preloadedBankAddresses?: PublicKey[], | ||
bankMetadataMap?: BankMetadataMap, | ||
bundleSimRpcEndpoint?: string, | ||
processTransactionStrategy?: ProcessTransactionStrategy, | ||
lookupTablesAddresses?: PublicKey[] | ||
) { | ||
super( | ||
config, | ||
program, | ||
wallet, | ||
isReadOnly, | ||
group, | ||
banks, | ||
priceInfos, | ||
mintDatas, | ||
feedIdMap, | ||
addressLookupTables, | ||
preloadedBankAddresses, | ||
bankMetadataMap, | ||
bundleSimRpcEndpoint, | ||
processTransactionStrategy, | ||
lookupTablesAddresses | ||
); | ||
} | ||
|
||
async processTransaction( | ||
transaction: SolanaTransaction, | ||
processOpts?: ProcessTransactionsClientOpts, | ||
txOpts?: TransactionOptions | ||
): Promise<TransactionSignature> { | ||
try { | ||
await fetch("/api/processTransaction", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error("Error processing transaction to the backend:", error); | ||
} | ||
|
||
return super.processTransaction(transaction, { ...processOpts, addArenaTxTag: true }, txOpts); | ||
} | ||
|
||
async processTransactions( | ||
transactions: SolanaTransaction[], | ||
processOpts?: ProcessTransactionsClientOpts, | ||
txOpts?: TransactionOptions | ||
): Promise<TransactionSignature[]> { | ||
try { | ||
await fetch("/api/processTransaction", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error("Error processing transaction to the backend:", error); | ||
} | ||
|
||
return super.processTransactions(transactions, { ...processOpts, addArenaTxTag: true }, txOpts); | ||
} | ||
} | ||
|
||
export default ArenaClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.