-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`); | ||
} | ||
} |