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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
djed-sdk/.npmrc
package-lock.json
33 changes: 33 additions & 0 deletions djed-sdk/src/adapters/djedAdapter.js
Original file line number Diff line number Diff line change
@@ -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),
};
};
14 changes: 13 additions & 1 deletion djed-sdk/src/djed/djed.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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);
};