From fac012f70c9ef6aa1e7dc21e9263f80166ff5554 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 20 Oct 2024 21:43:32 +0700 Subject: [PATCH] Create DEXFrontend.js --- frontend/DEXFrontend.js | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 frontend/DEXFrontend.js diff --git a/frontend/DEXFrontend.js b/frontend/DEXFrontend.js new file mode 100644 index 000000000..81286c165 --- /dev/null +++ b/frontend/DEXFrontend.js @@ -0,0 +1,53 @@ +import Web3 from 'web3'; +import { DEXContract } from './DEXContract.json'; + +const web3 = new Web3(window.ethereum); + +const dexContract = new web3.eth.Contract( + DEXContract.abi, + '0x...DEXContractAddress...' +); + +// Function to set the exchange rate for a token pair +async function setExchangeRate(tokenIn, tokenOut, rate) { + try { + const tx = await dexContract.methods.setExchangeRate(tokenIn, tokenOut, rate).send({ + from: web3.eth.accounts[0], + }); + console.log(`Exchange rate set: ${tx.transactionHash}`); + } catch (error) { + console.error(`Error setting exchange rate: ${error.message}`); + } +} + +// Function to execute a trade +async function trade(tokenIn, tokenOut, amountIn) { + try { + const tx = await dexContract.methods.trade(tokenIn, tokenOut, amountIn).send({ + from: web3.eth.accounts[0], + }); + console.log(`Trade executed: ${tx.transactionHash}`); + } catch (error) { + console.error(`Error executing trade: ${error.message}`); + } +} + +// Function to get trade details +async function getTrade(index) { + try { + const trade = await dexContract.methods.getTrade(index).call(); + console.log(`Trade details: ${JSON.stringify(trade)}`); + } catch (error) { + console.error(`Error getting trade details: ${error.message}`); + } +} + +// Function to get the number of trades +async function getTradeCount() { + try { + const count = await dexContract.methods.getTradeCount().call(); + console.log(`Number of trades: ${count}`); + } catch (error) { + console.error(`Error getting trade count: ${error.message}`); + } +}