From 99cb5d61dafed0b9427b39dc12e3ff3ea3ecd988 Mon Sep 17 00:00:00 2001 From: RaviChauhan <1chauhanravi0@gmail.com> Date: Mon, 23 Feb 2026 12:58:44 +0530 Subject: [PATCH 1/2] Feat: Introduce viem publicClient and walletClient abstraction --- djed-sdk/src/blockchain/client.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 djed-sdk/src/blockchain/client.js diff --git a/djed-sdk/src/blockchain/client.js b/djed-sdk/src/blockchain/client.js new file mode 100644 index 0000000..97b805f --- /dev/null +++ b/djed-sdk/src/blockchain/client.js @@ -0,0 +1,16 @@ +import { createPublicClient, createWalletClient, http } from "viem" +import { mainnet } from "viem/chains" + +export const createViemClients = (rpcUrl) => { + const publicClient = createPublicClient({ + chain: mainnet, + transport: http(rpcUrl), + }) + + const walletClient = createWalletClient({ + chain: mainnet, + transport: http(rpcUrl), + }) + + return { publicClient, walletClient } +} \ No newline at end of file From 9368ad29e998ca71c89f49f260d9e2b147c2a1fa Mon Sep 17 00:00:00 2001 From: RaviChauhan <1chauhanravi0@gmail.com> Date: Mon, 23 Feb 2026 13:33:47 +0530 Subject: [PATCH 2/2] Refactor: Migrate read-only contract calls from web3 to viem publicClient --- djed-sdk/src/blockchain/client.js | 16 ++++++++++++++++ djed-sdk/src/djed/stableCoin.js | 23 +++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 djed-sdk/src/blockchain/client.js diff --git a/djed-sdk/src/blockchain/client.js b/djed-sdk/src/blockchain/client.js new file mode 100644 index 0000000..685040f --- /dev/null +++ b/djed-sdk/src/blockchain/client.js @@ -0,0 +1,16 @@ +import { createPublicClient, createWalletClient, http } from "viem" +import { mainnet } from "viem/chains" + +export const createViemClients = (rpcUrl) => { + const publicClient = createPublicClient({ + chain: mainnet, + transport: http(rpcUrl), + }) + + const walletClient = createWalletClient({ + chain: mainnet, + transport: http(rpcUrl), + }) + + return { publicClient, walletClient } +} \ No newline at end of file diff --git a/djed-sdk/src/djed/stableCoin.js b/djed-sdk/src/djed/stableCoin.js index f845772..7df8d1e 100644 --- a/djed-sdk/src/djed/stableCoin.js +++ b/djed-sdk/src/djed/stableCoin.js @@ -1,5 +1,7 @@ import { BC_DECIMALS, TRANSACTION_VALIDITY } from "../constants"; import { decimalScaling, buildTx } from "../helpers"; +import { createViemClients } from "../blockchain/client"; +const { publicClient } = createViemClients(process.env.RPC_URL); import { tradeDataPriceCore, getFees, @@ -79,7 +81,7 @@ export const tradeDataPriceSellSc = async (djed, scDecimals, amountScaled) => { 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(); - + // `payer` is sending the funds return buildTx(payer, DJED_ADDRESS, value, data); }; @@ -111,9 +113,22 @@ export const calculateFutureScPrice = async ({ }) => { try { const [scTargetPrice, scSupply, ratio] = await Promise.all([ - web3Promise(oracleContract, "readData"), - web3Promise(stableCoinContract, "totalSupply"), - web3Promise(djedContract, "R", 0), + publicClient.readContract({ + address: oracleAddress, + abi: OracleABI, + functionName: "readData", + }), + publicClient.readContract({ + address: stableCoinAddress, + abi: CoinABI, + functionName: "totalSupply", + }), + publicClient.readContract({ + address: djedAddress, + abi: DjedABI, + functionName: "R", + args: [0], + }), ]); const futureScSupply = BigInt(scSupply) + BigInt(amountSC);