Skip to content

Commit

Permalink
Merge pull request #152 from Chia-Network/develop
Browse files Browse the repository at this point in the history
Release: 1.3.2
  • Loading branch information
MichaelTaylor3D authored Sep 7, 2023
2 parents 23f474c + f03b3b4 commit 2330e8a
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 284 deletions.
17 changes: 14 additions & 3 deletions mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@ const port = 31313;

app.get("/v1/activities", (req, res) => {
if (req.query.page == 1) {
if (req.query.minHeight > 1000000) {
if (req.query.minHeight > 6000010) {
res.json({
activities: [],
});
}
res.json({
activities: [
{
height: 1000000,
height: 6000000,
beneficiary_name: "John Doe",
beneficiary_address: "0x1234",
amount: 10000,
amount: 1000000,
mode: "PERMISSIONLESS_RETIREMENT",
cw_unit: {
marketplaceIdentifier:
"0xf4d61bef9f3022cc2961ca8861c327d104c5e0761cb5fe010d718134d4cd4441",
},
},
{
height: 6000010,
beneficiary_name: "JANE Doe",
beneficiary_address: "0x1234",
amount: 500000,
mode: "PERMISSIONLESS_RETIREMENT",
cw_unit: {
marketplaceIdentifier:
Expand Down
13 changes: 11 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "climate-tokenization-engine",
"version": "1.3.1",
"version": "1.3.2",
"bin": "./server.js",
"description": "",
"main": "proxy.js",
Expand Down Expand Up @@ -29,6 +29,7 @@
"homepage": "https://github.com/Chia-Network/climate-tokenization-engine#readme",
"dependencies": {
"body-parser": "^1.20.2",
"chia-datalayer": "^1.0.0",
"chia-root-resolver": "^1.0.0",
"cors": "^2.8.5",
"express": "^4.18.2",
Expand Down
43 changes: 40 additions & 3 deletions tasks/sync-retirements.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { SimpleIntervalJob, Task } = require("toad-scheduler");
const Datalayer = require("chia-datalayer");
const superagent = require("superagent");
const { getConfig } = require("../utils/config-loader");
const {
getLastProcessedHeight,
getAssetUnitBlocks,
setLastProcessedHeight,
getHomeOrg,
} = require("../utils/coreRegApi");
const warehouseApi = require("../warehouse");
const { waitForAllTransactionsToConfirm } = require("../wallet");
Expand All @@ -20,13 +22,13 @@ async function processResult({
}) {
try {
await waitForAllTransactionsToConfirm();
// return the CADT unit records that all match this marketplaceIdentifier
await waitForCadtSync();
const unitBlocks = await getAssetUnitBlocks(marketplaceIdentifier);

// sort from smallest to largest
// sort from largest to smallest
const units = unitBlocks?.body
.filter((unit) => unit.unitStatus !== "Retired")
.sort((a, b) => a.unitCount - b.unitCount);
.sort((a, b) => b.unitCount - a.unitCount);

if (!units || units.length === 0) {
console.log(
Expand Down Expand Up @@ -103,6 +105,39 @@ function findHighestHeight(activities) {
return highestHeight;
}

async function waitForCadtSync() {
await new Promise((resolve) => setTimeout(() => resolve(), 5000));

const dataLayerConfig = {};
if (CONFIG.DATA_LAYER_HOST) {
dataLayerConfig.datalayer_host = CONFIG.DATA_LAYER_HOST;
}
if (CONFIG.WALLET_HOST) {
dataLayerConfig.wallet_host = CONFIG.WALLET_HOST;
}
if (CONFIG.CERTIFICATE_FOLDER_PATH) {
dataLayerConfig.certificate_folder_path = CONFIG.CERTIFICATE_FOLDER_PATH;
}

const datalayer = Datalayer.rpc(dataLayerConfig);
const homeOrg = await getHomeOrg();

const onChainRoot = await datalayer.getRoot({
id: homeOrg.registryId,
});

if (!onChainRoot.confirmed) {
return waitForCadtSync();
}

console.log(onChainRoot.hash);
console.log(homeOrg.registryHash);

if (onChainRoot.hash !== homeOrg.registryHash) {
return waitForCadtSync();
}
}

async function getAndProcessActivities(minHeight = 0) {
// Dont run if a previous task is still running
if (isTaskInProgress) {
Expand Down Expand Up @@ -148,6 +183,7 @@ async function getAndProcessActivities(minHeight = 0) {
if (retirements.length > 0) {
for (const activity of retirements) {
console.log("Processing retirement", activity);
await waitForCadtSync();
await processResult({
marketplaceIdentifier: activity.cw_unit.marketplaceIdentifier,
// the amount is mojos but each climate token is 1000 mojos
Expand All @@ -156,6 +192,7 @@ async function getAndProcessActivities(minHeight = 0) {
beneficiaryName: activity.beneficiary_name,
beneficiaryAddress: activity.beneficiary_address,
});
await new Promise((resolve) => setTimeout(() => resolve(), 5000));
}

const blockHeightsProcessed = findHighestHeight(retirements);
Expand Down
76 changes: 21 additions & 55 deletions utils/coreRegApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,6 @@ const getAssetUnitBlocks = (marketplaceIdentifier) => {
return request;
};

const splitRetiredUnit = async (
unit,
amount,
beneficiaryName,
beneficiaryPublicKey
) => {
const splitData = {
warehouseUnitId: unit.warehouseUnitId,
records: [
{
unitCount: unit.unitCount - amount,
unitBlockStart: unit.unitBlockStart,
unitBlockEnd: unit.unitBlockEnd,
marketplace: unit.marketplace,
marketplaceIdentifier: unit.marketplaceIdentifier,
unitStatusReason: beneficiaryPublicKey || unit.unitStatusReason,
unitOwner: beneficiaryName || unit.unitOwner,
},
{
unitCount: amount,
unitBlockStart: unit.unitBlockStart,
unitBlockEnd: unit.unitBlockEnd,
marketplace: null,
marketplaceIdentifier: null,
},
],
};

try {
const request = superagent
.post(`${CONFIG.CADT_API_SERVER_HOST}/v1/units/split`)
.send(splitData)
.set({ "Content-Type": "application/json" });

if (CONFIG.CADT_API_KEY) {
request.set("x-api-key", CONFIG.CADT_API_KEY);
}

await request;
} catch (error) {
throw new Error(`Could not split detokenize unit on warehouse: ${error}`);
}
};

const retireUnit = async (unit, beneficiaryName, beneficiaryAddress) => {
unit.status = "retired";
unit.unitOwner = beneficiaryName;
Expand All @@ -66,7 +22,7 @@ const retireUnit = async (unit, beneficiaryName, beneficiaryAddress) => {
};

const updateUnit = async (unit) => {
try {
// try {
const request = superagent
.put(`${CONFIG.CADT_API_SERVER_HOST}/v1/units`)
.send(unit)
Expand All @@ -77,15 +33,16 @@ const updateUnit = async (unit) => {
}

await request;
} catch (error) {
throw new Error(`Warehouse unit could not be updated: ${error}`);
}
// } catch (error) {
// throw new Error(`Warehouse unit could not be updated: ${error}`);
// }
};

const getLastProcessedHeight = async () => {

try {
const homeOrgUid = await getHomeOrgUid();

const request = superagent
.get(`${CONFIG.CADT_API_SERVER_HOST}/v1/organizations/metadata`)
.query({ orgUid: homeOrgUid });
Expand All @@ -109,6 +66,16 @@ const getLastProcessedHeight = async () => {
};

const getHomeOrgUid = async () => {
try {
const homeOrg = await getHomeOrg();
return homeOrg.orgUid;
} catch (error) {
console.error(`Could not get home organization UID: ${error}`);
return null;
}
};

const getHomeOrg = async () => {
try {
const request = superagent.get(
`${CONFIG.CADT_API_SERVER_HOST}/v1/organizations`
Expand All @@ -125,28 +92,27 @@ const getHomeOrgUid = async () => {
}

// Iterate through the response keys and find the object where "isHome" is true
let homeOrgUid = null;
let homeOrg = null;
for (const key in response.body) {
if (response.body[key].isHome === true) {
homeOrgUid = response.body[key].orgUid;
homeOrg = response.body[key];
break;
}
}

if (!homeOrgUid) {
if (!homeOrg) {
throw new Error(
'No organization with "isHome" equal to true found in the response'
);
}

return homeOrgUid;
return homeOrg;
} catch (error) {
console.error(`Could not get home organization UID: ${error}`);
return null;
}
};


const setLastProcessedHeight = async (height) => {
try {
console.log(`Setting last processed height to ${height}`);
Expand All @@ -169,9 +135,9 @@ const setLastProcessedHeight = async (height) => {

module.exports = {
getAssetUnitBlocks,
splitRetiredUnit,
retireUnit,
getLastProcessedHeight,
setLastProcessedHeight,
getHomeOrgUid,
getHomeOrg,
};
Loading

0 comments on commit 2330e8a

Please sign in to comment.