diff --git a/djed-sdk/src/djed/djed.js b/djed-sdk/src/djed/djed.js index 443c313..36563a9 100644 --- a/djed-sdk/src/djed/djed.js +++ b/djed-sdk/src/djed/djed.js @@ -1,25 +1,32 @@ +import { ethers } from "ethers"; import djedArtifact from "../artifacts/DjedABI.json"; import coinArtifact from "../artifacts/CoinABI.json"; import { convertInt, web3Promise } from "../helpers"; -//setting up djed -export const getDjedContract = (web3, DJED_ADDRESS) => { - const djed = new web3.eth.Contract(djedArtifact.abi, DJED_ADDRESS); +// Create ethers contract instance for read-only operations +export const getDjedContract = (provider, DJED_ADDRESS) => { + const djed = new ethers.Contract(DJED_ADDRESS, djedArtifact.abi, provider); return djed; }; -export const getCoinContracts = async (djedContract, web3) => { +export const getCoinContracts = async (djedContract, provider) => { const [stableCoinAddress, reserveCoinAddress] = await Promise.all([ web3Promise(djedContract, "stableCoin"), web3Promise(djedContract, "reserveCoin"), ]); - const stableCoin = new web3.eth.Contract(coinArtifact.abi, stableCoinAddress); - const reserveCoin = new web3.eth.Contract( + const stableCoin = new ethers.Contract( + stableCoinAddress, coinArtifact.abi, - reserveCoinAddress + provider + ); + const reserveCoin = new ethers.Contract( + reserveCoinAddress, + coinArtifact.abi, + provider ); return { stableCoin, reserveCoin }; }; + export const getDecimals = async (stableCoin, reserveCoin) => { const [scDecimals, rcDecimals] = await Promise.all([ convertInt(web3Promise(stableCoin, "decimals")), diff --git a/djed-sdk/src/djed/reserveCoin.js b/djed-sdk/src/djed/reserveCoin.js index da2a6bc..8476093 100644 --- a/djed-sdk/src/djed/reserveCoin.js +++ b/djed-sdk/src/djed/reserveCoin.js @@ -71,15 +71,20 @@ export const tradeDataPriceSellRc = async (djed, rcDecimals, amountScaled) => { }; export const buyRcTx = (djed, account, value, UI, DJED_ADDRESS) => { - const data = djed.methods - .buyReserveCoins(account, FEE_UI_UNSCALED, UI) - .encodeABI(); + const data = djed.interface.encodeFunctionData("buyReserveCoins", [ + account, + FEE_UI_UNSCALED, + UI, + ]); return buildTx(account, DJED_ADDRESS, value, data); }; export const sellRcTx = (djed, account, amount, UI, DJED_ADDRESS) => { - const data = djed.methods - .sellReserveCoins(amount, account, FEE_UI_UNSCALED, UI) - .encodeABI(); + const data = djed.interface.encodeFunctionData("sellReserveCoins", [ + amount, + account, + FEE_UI_UNSCALED, + UI, + ]); return buildTx(account, DJED_ADDRESS, 0, data); }; diff --git a/djed-sdk/src/djed/stableCoin.js b/djed-sdk/src/djed/stableCoin.js index f845772..dedd0df 100644 --- a/djed-sdk/src/djed/stableCoin.js +++ b/djed-sdk/src/djed/stableCoin.js @@ -77,17 +77,24 @@ export const tradeDataPriceSellSc = async (djed, scDecimals, amountScaled) => { // Function to allow User 1 (payer) to pay and User 2 (receiver) to receive stablecoins export const buyScTx = (djed, payer, receiver, value, UI, DJED_ADDRESS) => { - // `receiver` will get the stablecoins - const data = djed.methods.buyStableCoins(receiver, FEE_UI_UNSCALED, UI).encodeABI(); - + // Encode the contract method call using ethers + const data = djed.interface.encodeFunctionData("buyStableCoins", [ + receiver, + FEE_UI_UNSCALED, + UI, + ]); + // `payer` is sending the funds return buildTx(payer, DJED_ADDRESS, value, data); }; export const sellScTx = (djed, account, amount, UI, DJED_ADDRESS) => { - const data = djed.methods - .sellStableCoins(amount, account, FEE_UI_UNSCALED, UI) - .encodeABI(); + const data = djed.interface.encodeFunctionData("sellStableCoins", [ + amount, + account, + FEE_UI_UNSCALED, + UI, + ]); return buildTx(account, DJED_ADDRESS, 0, data); }; diff --git a/djed-sdk/src/djed/system.js b/djed-sdk/src/djed/system.js index 90d33e2..056d007 100644 --- a/djed-sdk/src/djed/system.js +++ b/djed-sdk/src/djed/system.js @@ -115,7 +115,7 @@ export const getSystemParams = async (djed) => { }; export const getAccountDetails = async ( - web3, + provider, account, stableCoin, reserveCoin, @@ -135,7 +135,7 @@ export const getAccountDetails = async ( web3Promise(reserveCoin, "balanceOf", account), rcDecimals ), - scaledUnscaledPromise(web3.eth.getBalance(account), BC_DECIMALS), + scaledUnscaledPromise(provider.getBalance(account), BC_DECIMALS), ]); return { diff --git a/djed-sdk/src/djed/tradeUtils.js b/djed-sdk/src/djed/tradeUtils.js index 456f1f1..a427399 100644 --- a/djed-sdk/src/djed/tradeUtils.js +++ b/djed-sdk/src/djed/tradeUtils.js @@ -128,10 +128,10 @@ export const promiseTx = (isWalletConnected, tx, signer) => { return signer.sendTransaction(tx); }; -export const verifyTx = (web3, hash) => { +export const verifyTx = (provider, hash) => { return new Promise((res) => { setTimeout(() => { - web3.eth + provider .getTransactionReceipt(hash) .then((receipt) => res(receipt.status)); }, CONFIRMATION_WAIT_PERIOD); diff --git a/djed-sdk/src/helpers.js b/djed-sdk/src/helpers.js index 6738053..e556862 100644 --- a/djed-sdk/src/helpers.js +++ b/djed-sdk/src/helpers.js @@ -1,17 +1,19 @@ +// Call contract read method using ethers export function web3Promise(contract, method, ...args) { - return contract.methods[method](...args).call(); + return contract[method](...args); } -// Function to build a transaction + +// Function to build a transaction object for ethers signer // Set gas limit to 500,000 by default export function buildTx(from_, to_, value_, data_, setGasLimit = true) { const tx = { to: to_, from: from_, - value: "0x" + BigInt(value_).toString(16), // Use BigInt instead of BN + value: value_ ? BigInt(value_) : 0n, data: data_, }; if (setGasLimit) { - tx.gasLimit = 500_000; + tx.gasLimit = 500_000n; } return tx; } diff --git a/djed-sdk/src/oracle/oracle.js b/djed-sdk/src/oracle/oracle.js index 54644e0..c200e4a 100644 --- a/djed-sdk/src/oracle/oracle.js +++ b/djed-sdk/src/oracle/oracle.js @@ -1,13 +1,16 @@ -import { convertInt,web3Promise } from "../helpers"; +import { ethers } from "ethers"; +import { convertInt, web3Promise } from "../helpers"; import oracleArtifact from "../artifacts/OracleABI.json"; export const getOracleAddress = async (djedContract) => { return await web3Promise(djedContract, "oracle"); }; -export const getOracleContract = (web3, oracleAddress, msgSender) => { - const oracle = new web3.eth.Contract(oracleArtifact.abi, oracleAddress, { - from: msgSender - }); +export const getOracleContract = (provider, oracleAddress) => { + const oracle = new ethers.Contract( + oracleAddress, + oracleArtifact.abi, + provider + ); return oracle; }; diff --git a/djed-sdk/src/web3.js b/djed-sdk/src/web3.js index 4aa8cd6..9da960b 100644 --- a/djed-sdk/src/web3.js +++ b/djed-sdk/src/web3.js @@ -1,11 +1,13 @@ -import Web3 from "web3"; +import { ethers } from "ethers"; +// Returns an ethers.Provider - Web3Provider for browser environment export const getWeb3 = (BLOCKCHAIN_URI) => new Promise((resolve, reject) => { if (window.ethereum) { try { - const web3 = new Web3(BLOCKCHAIN_URI); - resolve(web3); + // Use Web3Provider for browser-based wallet integration (MetaMask, etc.) + const provider = new ethers.BrowserProvider(window.ethereum); + resolve(provider); } catch (error) { reject(error); } diff --git a/stablepay-sdk/src/core/Transaction.js b/stablepay-sdk/src/core/Transaction.js index 9a4d91b..b34f6e0 100644 --- a/stablepay-sdk/src/core/Transaction.js +++ b/stablepay-sdk/src/core/Transaction.js @@ -12,9 +12,10 @@ export class Transaction { } try { - this.web3 = await getWeb3(this.networkUri); - this.djedContract = getDjedContract(this.web3, this.djedAddress); - const { stableCoin, reserveCoin } = await getCoinContracts(this.djedContract, this.web3); + // Get ethers provider from BrowserProvider + this.provider = await getWeb3(this.networkUri); + this.djedContract = getDjedContract(this.provider, this.djedAddress); + const { stableCoin, reserveCoin } = await getCoinContracts(this.djedContract, this.provider); const { scDecimals, rcDecimals } = await getDecimals(stableCoin, reserveCoin); this.stableCoin = stableCoin; this.reserveCoin = reserveCoin; @@ -23,10 +24,10 @@ export class Transaction { // Get the oracle contract this.oracleContract = await getOracleAddress(this.djedContract).then((addr) => - getOracleContract(this.web3, addr, this.djedContract._address) + getOracleContract(this.provider, addr) ); - this.oracleAddress = this.oracleContract._address; + this.oracleAddress = this.oracleContract.target; console.log('Transaction initialized successfully'); } catch (error) { @@ -37,10 +38,10 @@ export class Transaction { getBlockchainDetails() { return { - web3Available: !!this.web3, + providerAvailable: !!this.provider, djedContractAvailable: !!this.djedContract, - stableCoinAddress: this.stableCoin ? this.stableCoin._address : 'N/A', - reserveCoinAddress: this.reserveCoin ? this.reserveCoin._address : 'N/A', + stableCoinAddress: this.stableCoin ? this.stableCoin.target : 'N/A', + reserveCoinAddress: this.reserveCoin ? this.reserveCoin.target : 'N/A', stableCoinDecimals: this.scDecimals, reserveCoinDecimals: this.rcDecimals, oracleAddress: this.oracleAddress || 'N/A', @@ -57,7 +58,7 @@ export class Transaction { } try { const result = await tradeDataPriceBuySc(this.djedContract, this.scDecimals, amountScaled); - return result.totalBCScaled; //converted ETH equivalent + return result.totalBCScaled; // converted ETH equivalent } catch (error) { console.error("Error fetching trade data for buying stablecoins: ", error); throw error; @@ -72,10 +73,10 @@ export class Transaction { try { console.log(`Building stablecoin purchase transaction from ${payer} to ${receiver} with value ${value}`); - //Hardcoded UI address + // Hardcoded UI address const UI = '0x0232556C83791b8291E9b23BfEa7d67405Bd9839'; - //buyScTx from djed-sdk + // buyScTx from djed-sdk returns tx object for ethers signer const txData = await buyScTx(this.djedContract, payer, receiver, value, UI, this.djedAddress); console.log("Transaction built:", txData);