Skip to content

Commit 5d133ad

Browse files
committed
feat: flexible value in plan purchase
1 parent 98824ae commit 5d133ad

File tree

5 files changed

+175
-62
lines changed

5 files changed

+175
-62
lines changed

src/localServer/define.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ type ITypeTransferCount = {
814814
// git reset --soft HEAD~[number] to delete commit
815815
interface assetPrice {
816816
name: string
817-
price: number
817+
price: any
818818
}
819819
interface assetOracle {
820820
lastUpdate: number

src/localServer/workers/encrypt.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,21 @@ const processCmd = async (cmd: worker_command) => {
261261

262262
case "guardianPurchase": {
263263
const [nodes, amount, profile, payAssetName] = cmd.data;
264-
if (!nodes || !amount || !profile || !payAssetName) {
264+
265+
const isValid = validateFundsForPurchase(
266+
profile,
267+
payAssetName,
268+
amount
269+
);
270+
271+
if (!isValid || !nodes || !amount || !profile || !payAssetName) {
265272
const cmd1 = {
266273
cmd: "purchaseStatus",
267274
data: [-1],
268275
};
269276
return sendState("toFrontEnd", cmd1);
270277
}
278+
271279
const profiles = CoNET_Data?.profiles;
272280
if (!profiles) {
273281
const cmd1 = {
@@ -325,7 +333,13 @@ const processCmd = async (cmd: worker_command) => {
325333
const [referrer, amount, profile, payAssetName] = cmd.data;
326334
returnUUIDChannel(cmd);
327335

328-
if (amount?.length !== 1 || !profile || !payAssetName) {
336+
const isValid = validateFundsForPurchase(
337+
profile,
338+
payAssetName,
339+
amount
340+
);
341+
342+
if (!isValid || amount?.length !== 1 || !profile || !payAssetName) {
329343
const cmd1 = {
330344
cmd: "purchaseStatus",
331345
data: [-1],

src/localServer/workers/utilities/miningV2.ts

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,6 @@ const convertUSDTToCurrency = (currencyName: string, usdtAmount: number) => {
404404

405405
const CONETianPlan_purchase = async (referrer: string, profile: profile, amount: number[], tokenName: string) => new Promise(async resolve=> {
406406

407-
408407
let cryptoAsset: CryptoAsset
409408

410409
if (amount.length !== 1 || !profile?.tokens||! (cryptoAsset = profile.tokens[tokenName])) {
@@ -414,88 +413,102 @@ const CONETianPlan_purchase = async (referrer: string, profile: profile, amount:
414413
const ntfs = amount[0]
415414
const totalUSDT = ntfs * getGuardianPrice(0)
416415

417-
const total = convertUSDTToCurrency(tokenName, totalUSDT)
416+
const priceInConvertedCurrency = convertUSDTToCurrency(tokenName, totalUSDT)
417+
418+
const total = getAmountToPay(profile, cryptoAsset, priceInConvertedCurrency)
419+
420+
if (!total) {
421+
const cmd1 = {
422+
cmd: 'purchaseStatus',
423+
data: [-1]
424+
}
425+
426+
sendState('toFrontEnd', cmd1)
427+
return false
428+
}
418429

419430
if (total < 0.00001) {
420431
return resolve(false)
421432
}
422433

423434
if (parseFloat(cryptoAsset.balance) - total < 0 || !profile.privateKeyArmor) {
424-
const cmd1 = {
425-
cmd: 'purchaseStatus',
426-
data: [-1]
427-
}
428-
sendState('toFrontEnd', cmd1)
429-
return false
430-
}
435+
const cmd1 = {
436+
cmd: 'purchaseStatus',
437+
data: [-1]
438+
}
439+
440+
sendState('toFrontEnd', cmd1)
441+
return false
442+
}
431443

432444
let receiptTx: any = await transferAssetToCONET_guardian(profile.privateKeyArmor, cryptoAsset.name, total.toFixed(8))
433445

434446
if (typeof receiptTx === 'boolean') {
435-
const cmd1 = {
436-
cmd: 'purchaseStatus',
437-
data: [-1]
438-
}
439-
sendState('toFrontEnd', cmd1)
440-
return false
441-
}
447+
const cmd1 = {
448+
cmd: 'purchaseStatus',
449+
data: [-1]
450+
}
442451

443-
const cmd2 = {
444-
cmd: 'purchaseStatus',
445-
data: [2]
446-
}
452+
sendState('toFrontEnd', cmd1)
453+
return false
454+
}
447455

448-
sendState('toFrontEnd', cmd2)
456+
const cmd2 = {
457+
cmd: 'purchaseStatus',
458+
data: [2]
459+
}
449460

461+
sendState('toFrontEnd', cmd2)
450462

451-
const kk1: CryptoAssetHistory = {
452-
status: 'Confirmed',
453-
Nonce: receiptTx.nonce,
454-
to: receiptTx.to,
455-
transactionFee: stringFix(ethers.formatEther(parseFloat(receiptTx.gasUsed)*parseFloat(receiptTx.gasPrice))),
456-
gasUsed: receiptTx.gasUsed.toString(),
457-
isSend: true,
458-
value: parseEther(total.toFixed(8), cryptoAsset.name).toString(),
459-
time: new Date().toISOString(),
460-
transactionHash: receiptTx.hash,
461-
}
463+
const kk1: CryptoAssetHistory = {
464+
status: 'Confirmed',
465+
Nonce: receiptTx.nonce,
466+
to: receiptTx.to,
467+
transactionFee: stringFix(ethers.formatEther(parseFloat(receiptTx.gasUsed)*parseFloat(receiptTx.gasPrice))),
468+
gasUsed: receiptTx.gasUsed.toString(),
469+
isSend: true,
470+
value: parseEther(total.toFixed(8), cryptoAsset.name).toString(),
471+
time: new Date().toISOString(),
472+
transactionHash: receiptTx.hash,
473+
}
462474

463-
cryptoAsset.history.push(kk1)
475+
cryptoAsset.history.push(kk1)
464476

465477
getProfileAssets_allOthers_Balance(profile)
466478

467-
const data = {
479+
const data = {
468480
receiptTx : receiptTx.hash,
469-
tokenName,
470-
amount: parseEther(total.toFixed(8), cryptoAsset.name).toString(),
471-
ntfs,
481+
tokenName,
482+
amount: parseEther(total.toFixed(8), cryptoAsset.name).toString(),
483+
ntfs,
472484
referrer
473-
}
485+
}
474486

475487
const message = JSON.stringify({ walletAddress: profile.keyID, data })
476488
const wallet = new ethers.Wallet(profile.privateKeyArmor)
477489
const signMessage = await wallet.signMessage(message)
478490

479491
const sendData = {
480-
message, signMessage
481-
}
492+
message, signMessage
493+
}
494+
495+
const cmd3 = {
496+
cmd: 'purchaseStatus',
497+
data: [3]
498+
}
482499

483-
const cmd3 = {
484-
cmd: 'purchaseStatus',
485-
data: [3]
486-
}
487500
sendState('toFrontEnd', cmd3)
488501

489502
const url = `${apiv4_endpoint}PurchaseCONETianPlan`
490-
let result
503+
let result
491504

492-
try {
493-
result = await postToEndpoint(url, true, sendData)
494-
}
495-
catch (ex) {
505+
try {
506+
result = await postToEndpoint(url, true, sendData)
507+
}
508+
catch (ex) {
509+
return resolve(false)
510+
}
496511

497-
return resolve(false)
498-
}
499512
return resolve(true)
500513
})
501514

src/localServer/workers/utilities/utilV2.ts

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const CONET_Guardian_Nodes1 = '0x5e4aE81285b86f35e3370B3EF72df1363DD05286'
3434
const fx168OrderContractAddress = '0x9aE6D3Bd3029C8B2A73817b9aFa1C029237E3e30'
3535
const christmas2024ContractAddress = "0xb188e707f4544835aEe28E4206C65edfF23221C0";
3636

37+
const CONETIAN_PRICE = 100
38+
const GUARDIAN_PRICE = 1250
39+
3740
//const CNTPB_contract = '0x6056473ADD8bC89a95325845F6a431CCD7A849bb'
3841
// const Claimable_ETHUSDTv3 = '0x79E2EdE2F479fA7E44C89Bbaa721EB1f0d529b7B'.toLowerCase()
3942
// const Claimable_BNBUSDTv3 = '0xd008D56aa9A963FAD8FB1FbA1997C28dB85933e6'.toLowerCase()
@@ -405,12 +408,20 @@ const prePurchase = async (cmd) => {
405408

406409
const asset = profile.tokens[payAssetName]
407410

408-
if (!profile.privateKeyArmor || !asset) {
411+
const isValid = validateFundsForPurchase(
412+
profile,
413+
asset,
414+
amount
415+
);
416+
417+
if (!profile.privateKeyArmor || !asset || !isValid) {
409418
cmd.err = 'INVALID_DATA'
410419
return returnUUIDChannel(cmd)
411420
}
412421

413-
const data: any = await getEstimateGas(profile.privateKeyArmor, payAssetName, amount)
422+
const amountToPay = getAmountToPay(profile, asset, amount);
423+
424+
const data: any = await getEstimateGas(profile.privateKeyArmor, payAssetName, amountToPay)
414425
if (data === false) {
415426
if (!profile.privateKeyArmor || !asset) {
416427
cmd.err = 'INVALID_DATA'
@@ -422,9 +433,75 @@ const prePurchase = async (cmd) => {
422433
return returnUUIDChannel(cmd)
423434
}
424435

425-
const nodePrice = 1250
436+
const validateFundsForPurchase = (userProfile: profile, assetName: string, amount: any) => {
437+
let userAsset: CryptoAsset = userProfile?.tokens?.[assetName];
438+
439+
if (userAsset.name === "arbETH") userAsset.name = "arb_eth";
440+
441+
if (!assetOracle) return false;
442+
443+
const oracleAssets: assetPrice[] =
444+
assetOracle.assets;
445+
const foundAsset = findAsset(userAsset.name);
446+
447+
if (!foundAsset) return false;
448+
449+
const assetPrice: any =
450+
userAsset.balance === "usdt" || userAsset.balance === "wusdt" || userAsset.balance === "arb_usdt"
451+
? "1"
452+
: parseFloat(foundAsset.price).toFixed(4);
453+
454+
const relativePriceWindow = 2 / assetPrice;
455+
456+
return parseFloat(userAsset?.balance) >= amount - relativePriceWindow
457+
458+
function findAsset(
459+
asset: string
460+
): assetPrice | undefined {
461+
if (asset === "arb_eth") asset = "eth";
462+
if (asset === "arb_usdt") asset = "usdt";
463+
if (asset === "wusdt") asset = "usdt";
464+
return oracleAssets.find((a) => a.name === asset);
465+
}
466+
}
426467

468+
const getAmountToPay = (userProfile: profile, profileAsset: CryptoAsset, amount: any) => {
469+
if (profileAsset.name === "arbETH") profileAsset.name = "arb_eth";
427470

471+
let userBalance = userProfile?.tokens?.[profileAsset.name];
472+
473+
if (!assetOracle) return 0;
474+
475+
const oracleAssets: assetPrice[] =
476+
assetOracle.assets;
477+
const foundAsset = findAsset(profileAsset.name);
478+
479+
if (!foundAsset) return 0;
480+
481+
const assetPrice: any =
482+
profileAsset.balance === "usdt" || profileAsset.balance === "wusdt" || profileAsset.balance === "arb_usdt"
483+
? "1"
484+
: parseFloat(foundAsset.price).toFixed(4);
485+
486+
const balanceAsFloat = parseFloat(userBalance?.balance);
487+
const relativePriceWindow = 2 / assetPrice;
488+
const relaxedAmount = amount - relativePriceWindow;
489+
490+
if (balanceAsFloat >= amount)
491+
return amount;
492+
else if (balanceAsFloat < amount && balanceAsFloat >= relaxedAmount)
493+
return balanceAsFloat
494+
else return 0
495+
496+
function findAsset(
497+
asset: string
498+
): assetPrice | undefined {
499+
if (asset === "arb_eth") asset = "eth";
500+
if (asset === "arb_usdt") asset = "usdt";
501+
if (asset === "wusdt") asset = "usdt";
502+
return oracleAssets.find((a) => a.name === asset);
503+
}
504+
}
428505

429506
const getClaimableAddress = (CONET_claimableName) => {
430507
switch (CONET_claimableName) {
@@ -507,9 +584,6 @@ interface nodeResponse {
507584
nodeWallets?: string[]
508585
}
509586

510-
511-
512-
513587
const getTicket = async (profile: profile) => {
514588
const message = JSON.stringify({ walletAddress: profile.keyID })
515589

src/localServer/workers/utilities/web3Util.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,7 +2734,7 @@ const getEstimateGasForNftTransfer = (
27342734
}
27352735
});
27362736

2737-
const CONET_guardian_purchase = async (profile: profile, nodes, _total, tokenName) => {
2737+
const CONET_guardian_purchase = async (profile: profile, nodes, totalPrice, tokenName) => {
27382738

27392739
let cryptoAsset: CryptoAsset
27402740
// const total = await getAmountOfNodes(nodes, tokenName)
@@ -2747,7 +2747,18 @@ const CONET_guardian_purchase = async (profile: profile, nodes, _total, tokenNam
27472747
return false
27482748
}
27492749

2750-
2750+
2751+
const _total = getAmountToPay(profile, cryptoAsset, totalPrice);
2752+
2753+
if (!_total) {
2754+
const cmd1 = {
2755+
cmd: "purchaseStatus",
2756+
data: [-1],
2757+
};
2758+
2759+
sendState("toFrontEnd", cmd1);
2760+
return false;
2761+
}
27512762

27522763
if (parseFloat(cryptoAsset.balance) - _total < 0 || !profile.privateKeyArmor) {
27532764
const cmd1 = {
@@ -2762,6 +2773,7 @@ const CONET_guardian_purchase = async (profile: profile, nodes, _total, tokenNam
27622773
cmd: 'purchaseStatus',
27632774
data: [1]
27642775
}
2776+
27652777
sendState('toFrontEnd', cmd1)
27662778

27672779
const tx: any = await transferAssetToCONET_guardian(profile.privateKeyArmor, cryptoAsset.name, _total.toString())

0 commit comments

Comments
 (0)