diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f223ef1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +djed-sdk/.npmrc +package-lock.json diff --git a/djed-sdk/src/adapters/djedAdapter.js b/djed-sdk/src/adapters/djedAdapter.js new file mode 100644 index 0000000..825a808 --- /dev/null +++ b/djed-sdk/src/adapters/djedAdapter.js @@ -0,0 +1,33 @@ +import { FEE_UI_UNSCALED } from "../djed/tradeUtils"; + +export const createDjedAdapter = (djedContract) => { + const methods = djedContract?.methods; + + const missingMethods = []; + + if (!methods) { + throw new Error("Invalid Djed contract instance: methods object is missing"); + } + + if (typeof methods.buyStableCoins !== "function") { + missingMethods.push("buyStableCoins"); + } + + if (typeof methods.sellStableCoins !== "function") { + missingMethods.push("sellStableCoins"); + } + + if (missingMethods.length > 0) { + throw new Error( + `Djed contract is missing required methods: ${missingMethods.join(", ")}` + ); + } + + return { + buyStableCoins: (receiver, ui) => + methods.buyStableCoins(receiver, FEE_UI_UNSCALED, ui), + + sellStableCoins: (amount, account, ui) => + methods.sellStableCoins(amount, account, FEE_UI_UNSCALED, ui), + }; +}; \ No newline at end of file diff --git a/djed-sdk/src/djed/djed.js b/djed-sdk/src/djed/djed.js index 443c313..4842436 100644 --- a/djed-sdk/src/djed/djed.js +++ b/djed-sdk/src/djed/djed.js @@ -1,6 +1,7 @@ import djedArtifact from "../artifacts/DjedABI.json"; import coinArtifact from "../artifacts/CoinABI.json"; -import { convertInt, web3Promise } from "../helpers"; +import { convertInt, web3Promise, buildTx } from "../helpers"; +import { createDjedAdapter } from "../adapters/djedAdapter"; //setting up djed export const getDjedContract = (web3, DJED_ADDRESS) => { @@ -27,3 +28,14 @@ export const getDecimals = async (stableCoin, reserveCoin) => { ]); return { scDecimals, rcDecimals }; }; + +// Adapter should be created once and passed in (dependency injection) +const buyScTx = (adapter, payer, receiver, value, ui, DJED_ADDRESS) => { + const data = adapter.buyStableCoins(receiver, ui).encodeABI(); + return buildTx(payer, DJED_ADDRESS, value, data); +}; + +const sellScTx = (adapter, account, amount, ui, DJED_ADDRESS) => { + const data = adapter.sellStableCoins(amount, account, ui).encodeABI(); + return buildTx(account, DJED_ADDRESS, 0, data); +};