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
16 changes: 16 additions & 0 deletions djed-sdk/src/blockchain/client.js
Original file line number Diff line number Diff line change
@@ -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 }
}
23 changes: 19 additions & 4 deletions djed-sdk/src/djed/stableCoin.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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);
Expand Down