Skip to content

Commit

Permalink
added assetOracle
Browse files Browse the repository at this point in the history
  • Loading branch information
CoNETProject committed Sep 3, 2024
1 parent 3f20a84 commit 918fc42
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 42 deletions.
10 changes: 9 additions & 1 deletion src/localServer/define.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,4 +672,12 @@ type ITypeTransferCount = {


//
// git reset --soft HEAD~[number] to delete commit
// git reset --soft HEAD~[number] to delete commit
interface assetPrice {
name: string
price: number
}
interface assetOracle {
lastUpdate: number
assets: assetPrice[]
}
95 changes: 95 additions & 0 deletions src/localServer/workers/utilities/smartContractABI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3950,4 +3950,99 @@ const faucet_ABI = [
"stateMutability": "payable",
"type": "receive"
}
]

const assetOracle_ABI = [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"name": "GuardianPrice",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "adminList",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "addr",
"type": "address"
},
{
"internalType": "bool",
"name": "status",
"type": "bool"
}
],
"name": "changeAddressInAdminlist",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "lastUpdateEpoch",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "string[]",
"name": "tokenNames",
"type": "string[]"
},
{
"internalType": "uint256[]",
"name": "prices",
"type": "uint256[]"
}
],
"name": "updatePrice",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
6 changes: 3 additions & 3 deletions src/localServer/workers/utilities/utilV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const Arbitrum_USDT = '0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9'
const blast_usdb_contract = '0x4300000000000000000000000000000000000003'
const bnb_wbnb_contract = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'
const bnb_usdt_contract = '0x55d398326f99059fF775485246999027B3197955'

const assetOracle_contract_addr = '0x8A7FD0B01B9CAb2Ef1FdcEe4134e32D066895e0c'
// ******************************************************************
const getAddress = (addr) => {
let ret = '';
Expand Down Expand Up @@ -517,8 +517,8 @@ const testFunction = async (cmd: worker_command) => {
return
}

await getAllOtherAssets()
await CONET_guardian_purchase(profiles[0], 1, 0.5, 'arb_usdt')
// await getAllOtherAssets()
// await CONET_guardian_purchase(profiles[0], 1, 0.5, 'arb_usdt')

// _startMiningV2(profiles[0], node)

Expand Down
139 changes: 101 additions & 38 deletions src/localServer/workers/utilities/web3Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const getRegion = async () => {
logger(ex)
return null
}
};
}

const registerReferrer = async (referrer) => {

if (!CoNET_Data?.profiles) {
Expand Down Expand Up @@ -305,29 +306,32 @@ const getProfileAssets_CONET_Balance = async (profile) => {
CGPN2s.toString() : '';
}
return true;
};
const maxfindNodeAddressNumber = 1000;
}

const maxfindNodeAddressNumber = 1000

const findNodeAddress = (nodeAddress, mnemonicPhrase) => {
const root = ethers.Wallet.fromPhrase(mnemonicPhrase);
const root = ethers.Wallet.fromPhrase(mnemonicPhrase)
if (!root?.deriveChild) {
logger(`findNodeAddress got null root?.deriveChild ERROR!`);
return -1;
logger(`findNodeAddress got null root?.deriveChild ERROR!`)
return -1
}
let index = 1;
const _nodeAddress = nodeAddress.toLowerCase();
let index = 1
const _nodeAddress = nodeAddress.toLowerCase()
const findIndex = () => {
const addr = root.deriveChild(index).address.toLowerCase();
const addr = root.deriveChild(index).address.toLowerCase()
if (_nodeAddress === addr) {
return index;
return index
}
if (++index > maxfindNodeAddressNumber) {
logger(`findNodeAddress reached maxfindNodeAddressNumber ERROR!`);
return -1;
logger(`findNodeAddress reached maxfindNodeAddressNumber ERROR!`)
return -1
}
return findIndex();
};
return findIndex();
};
return findIndex()
}
return findIndex()
}

const checkGuardianNodes = async () => {
if (!CoNET_Data || !CoNET_Data?.profiles) {
return logger(`checkGuardianNodes !CoNET_Data||!CoNET_Data?.profiles Error! STOP process.`);
Expand Down Expand Up @@ -401,49 +405,54 @@ const checkGuardianNodes = async () => {
await storagePieceToLocal();
await storeSystemData();
needUpgradeVer = epoch + 25;
};
let sendStateBeforeunload = false;
}


let sendStateBeforeunload = false

const sendState = (state, value) => {
const sendChannel = new BroadcastChannel(state);
let data = '';
const sendChannel = new BroadcastChannel(state)
let data = ''
try {
data = JSON.stringify(value);
}
catch (ex) {
logger(`sendState JSON.stringify(value) Error`);
logger(`sendState JSON.stringify(value) Error`)
}
sendChannel.postMessage(data);
sendChannel.close();
};
sendChannel.postMessage(data)
sendChannel.close()
}

const checkSmartContractAsset = async (eventLogs, tokenABILog, tokenName, profiles, smartContractObj) => {
let ret = false;
for (let transferLog of eventLogs) {
let uuu;
try {
uuu = tokenABILog.parseLog(transferLog);
uuu = tokenABILog.parseLog(transferLog)
}
catch (ex) {
console.log(`ifaceFor_cCNTP_ABI.parseLog transferLog!`);
console.log(`ifaceFor_cCNTP_ABI.parseLog transferLog!`)
continue;
}
if (uuu?.name === 'Transfer') {
const toAddr = uuu.args[1].toLowerCase();
const index = profiles.findIndex(n => n.keyID.toLowerCase() === toAddr);
const index = profiles.findIndex(n => n.keyID.toLowerCase() === toAddr)
if (index > -1) {
const profile = profiles[index];
const balance = await smartContractObj.balanceOf(profile.keyID);
const profile = profiles[index]
const balance = await smartContractObj.balanceOf(profile.keyID)
if (balance) {
profile.tokens[tokenName].balance = parseFloat(ethers.formatEther(balance)).toFixed(8);
profile.tokens[tokenName].balance = parseFloat(ethers.formatEther(balance)).toFixed(8)
}
else {
profile.tokens[tokenName].balance = '0';
profile.tokens[tokenName].balance = '0'
}
ret = true;
ret = true
}
}
}
return ret;
};
return ret
}

const listeningAddress = []


Expand Down Expand Up @@ -518,6 +527,7 @@ const checkAssets = async (block, provider) => {
}
}
}

let provideCONET
let lesteningBlock = false
let epoch = 0
Expand All @@ -529,19 +539,22 @@ const listenProfileVer = async () => {
lesteningBlock = true
provideCONET.on('block', async (block) => {
if (block === epoch + 1) {
epoch++;
epoch++

const profiles = CoNET_Data?.profiles
if (!profiles) {
return
}

await Promise.all([
selectLeaderboard(block),
checkAssets(block, provideCONET)
checkAssets(block, provideCONET),
getassetOracle()
])

const cmd = {
cmd: 'assets',
data: [profiles, RefereesList, leaderboardData]
data: [profiles, RefereesList, leaderboardData, assetOracle]
}

sendState('toFrontEnd', cmd)
Expand Down Expand Up @@ -1456,9 +1469,59 @@ let runninggetAllOtherAssets = false
let lastAllProfileAssetsBalanceTimeStamp = 0
let lastgetAllOtherAssetsBalanceTimeStamp = 0
const minCheckTimestamp = 1000 * 12 // must big than 12s

let assetOracle: assetOracle|null = null

const getassetOracle = async () => {
if (!provideCONET) {
return logger(`getassetOracle Error! provideCONET = null`)
}
if (assetOracle) {
const now = new Date().getTime()
if (now - assetOracle.lastUpdate < 1000 * 60 * 10) {
return
}
}

const oracle_SC = new ethers.Contract(assetOracle_contract_addr, assetOracle_ABI, provideCONET)
const assets = ['bnb', 'eth', 'usdt', 'usdc', 'dai']
const process: any[] = []
assets.forEach(n =>{
process.push (oracle_SC.GuardianPrice(n))
})

const [bnb, eth, usdt, usdc, dai] = await Promise.all(process)
assetOracle = {
lastUpdate: new Date().getTime(),
assets: [
{
name: 'bnb',
price: ethers.formatEther(bnb)
},
{
name: 'eth',
price: ethers.formatEther(eth)
},
{
name: 'usdt',
price: ethers.formatEther(usdt)
},
{
name: 'usdc',
price: ethers.formatEther(usdc)
},
{
name: 'dai',
price: ethers.formatEther(dai)
}
]
}
}

const getAllOtherAssets = async () => {

return new Promise(async (resolve) => {

if (!CoNET_Data?.profiles) {
logger(`getAllOtherAssets Error! CoNET_Data.profiles empty!`)
return resolve(false)
Expand Down Expand Up @@ -2268,7 +2331,7 @@ const getAssetERC20Address = (assetName) => {
return ``
}
}
};
}
const CONET_guardian_Address = (networkName) => {
switch (networkName) {
case 'usdc':
Expand Down

0 comments on commit 918fc42

Please sign in to comment.