Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions djed-sdk/src/djed/djed.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import { ethers } from "ethers";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

find . -name "package.json" -type f | head -20

Repository: DjedAlliance/StablePay

Length of output: 118


🏁 Script executed:

cat package.json | grep -A 5 -B 5 "ethers"

Repository: DjedAlliance/StablePay

Length of output: 110


🏁 Script executed:

cat djed-sdk/package.json | jq '.dependencies.ethers, .devDependencies.ethers'

Repository: DjedAlliance/StablePay

Length of output: 75


🏁 Script executed:

grep -r "web3Promise" djed-sdk/src/ -A 5 -B 2

Repository: DjedAlliance/StablePay

Length of output: 8867


🏁 Script executed:

cat djed-sdk/package.json

Repository: DjedAlliance/StablePay

Length of output: 608


🏁 Script executed:

cat djed-sdk/src/djed/djed.js | head -20

Repository: DjedAlliance/StablePay

Length of output: 814


Add ethers as a dependency in package.json.

The file imports ethers but the package is not listed in djed-sdk/package.json. Specify the ethers version in dependencies (or devDependencies) to resolve the missing module error at runtime. Confirm the version aligns with the API patterns used (e.g., new ethers.Contract(address, abi, provider)).

🤖 Prompt for AI Agents
In djed-sdk/src/djed/djed.js around line 1, the file imports "ethers" but
djed-sdk/package.json does not list ethers as a dependency; add ethers to
package.json dependencies with a concrete version (for example "ethers":
"^6.8.0") so the module is installed at runtime, ensure the chosen version is
compatible with the code's API usage (e.g., new ethers.Contract(address, abi,
provider)), then run npm/yarn install and update lockfile.

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")),
Expand Down
17 changes: 11 additions & 6 deletions djed-sdk/src/djed/reserveCoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
19 changes: 13 additions & 6 deletions djed-sdk/src/djed/stableCoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
4 changes: 2 additions & 2 deletions djed-sdk/src/djed/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const getSystemParams = async (djed) => {
};

export const getAccountDetails = async (
web3,
provider,
account,
stableCoin,
reserveCoin,
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions djed-sdk/src/djed/tradeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions djed-sdk/src/helpers.js
Original file line number Diff line number Diff line change
@@ -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;
Comment on lines 8 to 18
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check if dist files contain outdated BigInt hex conversion patterns

echo "=== Checking dist/esm/index.js ==="
rg -n "0x.*BigInt.*toString\(16\)" djed-sdk/dist/esm/index.js || echo "Pattern not found"

echo ""
echo "=== Checking dist/umd/index.js ==="
rg -n "0x.*BigInt.*toString\(16\)" djed-sdk/dist/umd/index.js || echo "Pattern not found"

echo ""
echo "=== Checking if source uses direct BigInt ==="
rg -n "value_.*BigInt.*:" djed-sdk/src/helpers.js

Repository: DjedAlliance/StablePay

Length of output: 385


Rebuild dist files to reflect the updated source code.

The source code uses direct BigInt values (value_ ? BigInt(value_) : 0n at line 12 and 500_000n at line 16), which ethers v6 accepts directly. However, the dist files contain outdated hex string conversion patterns ("0x" + BigInt(value_).toString(16) at djed-sdk/dist/esm/index.js:26 and djed-sdk/dist/umd/index.js:30), indicating the dist files need to be rebuilt.

🤖 Prompt for AI Agents
In djed-sdk/src/helpers.js around lines 8 to 18, the source now uses BigInt
literals and BigInt(value_) but the distributed builds still contain old
hex-string conversions; rebuild the dist artifacts so they reflect the updated
source (replace occurrences like '"0x" + BigInt(value_).toString(16)' with the
new BigInt usage), run the project build step (e.g., npm run build or the
repository's build command) to regenerate djed-sdk/dist/esm and
djed-sdk/dist/umd, and verify the generated files contain the direct BigInt
values instead of manual hex conversions.

}
Expand Down
13 changes: 8 additions & 5 deletions djed-sdk/src/oracle/oracle.js
Original file line number Diff line number Diff line change
@@ -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;
};
8 changes: 5 additions & 3 deletions djed-sdk/src/web3.js
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
23 changes: 12 additions & 11 deletions stablepay-sdk/src/core/Transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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',
Expand All @@ -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;
Expand All @@ -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);
Expand Down