Skip to content

Commit

Permalink
Improvements and Fix's to the stability of the Hub
Browse files Browse the repository at this point in the history
- Airdrop fix to the Airdrop System, now shows names, only uses the limited users inputed.
- Added new commands for getting the wallet nexus and peers.
  • Loading branch information
TeknoPT committed Jan 9, 2024
1 parent 715d411 commit 5e4c890
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phantasma-hub",
"version": "1.0.7",
"version": "1.0.8",
"private": true,
"scripts": {
"dev": "npm run version:update && vite dev --cors true",
Expand Down
59 changes: 58 additions & 1 deletion src/lib/Commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import type { PhantasmaLink } from 'phantasma-ts';
import { Base16, PBinaryReader, VMObject } from 'phantasma-ts/src';
import {
NotificationError,
NotificationSuccess
NotificationSuccess,
NotificationWarning
} from '$lib/Components/Notification/NotificationsBuilder';
import { ethers } from 'ethers';

Expand Down Expand Up @@ -343,3 +344,59 @@ export async function CheckURLStatus(url: string): Promise<boolean> {
return false;
}
}

/**
* Get the Wallet Nexus
*/
export async function GetWalletNexus(connectedNexus: string) {
await Link.getNexus(
(result) => {
console.log({ result });
if (result.nexus != connectedNexus) {
NotificationWarning(
`Wallet connected to ${result.nexus}!`,
`Wallet is connected to <b>${result.nexus}</b> and the Phantasma Hub is using the <b>${connectedNexus} API</b>.`,
6000
);
} else {
NotificationSuccess(
`Wallet connected to ${result.nexus}!`,
`Phantasma Hub and the wallet are using the <b>${result.nexus} API</b>.`
);
}
},
(error) => {
console.log(error);
}
);
}

export async function GetWalletPeer(connectedPeer: string) {
await Link.getPeer(
(result) => {
console.log('peer:', { result });
if (result.peer != connectedPeer) {
NotificationWarning(
`Wallet connected to ${result.peer}!`,
`Wallet is connected to Peer <b>${result.peer}</b> and the Phantasma Hub is using the <b>${connectedPeer} API</b>.`,
6000
);
} else {
NotificationSuccess(
`Wallet connected to ${result.peer}!`,
`Phantasma Hub and the wallet are using the <b>${result.peer} API</b>.`
);
}
},
(error) => {
NotificationError('Wallet Error!', 'Please connect your wallet first.');
console.log(error);
}
);
}

export function GetNexusPhantasmaLink(connectedNexus: string) {
let sb = new ScriptBuilder();
//sb.CallRPC('interop', 'GetNexusName', []);
return sb;
}
39 changes: 21 additions & 18 deletions src/lib/Components/Airdrop/AirdropCommands.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
Address,
PhantasmaAPI,
ScriptBuilder,
Token,
Base16,
} from 'phantasma-ts/src';
import { Address, PhantasmaAPI, ScriptBuilder, Token, Base16 } from 'phantasma-ts/src';
import {
AirdropFee,
GasLimit,
Expand Down Expand Up @@ -96,23 +90,27 @@ export function AirdropFT(
return;
}

const numberOfDistributions = userList.length / 100;
let comulativeFee = (userList.length / 100) * AirdropFee;
const numberOfDistributions = userList.length / 50;
let comulativeFee = (userList.length / 50) * AirdropFee;
if (numberOfDistributions < 1) comulativeFee = AirdropFee;
const from = Address.FromText(String(Link.account.address));
const payload = Base16.encode('Tools.AirdropFT');
const sb = new ScriptBuilder();
sb.AllowGas(from, Address.Null, gasPrice, gasLimit);

if (tipActive) {
sb.CallInterop('Runtime.TransferTokens', [from, TipAddress, 'KCAL', String(comulativeFee)]);
//sb.CallInterop('Runtime.TransferTokens', [from, TipAddress, 'KCAL', comulativeFee]);
sb.CallInterop('Runtime.TransferTokens', [
from.Text,
TipAddress,
'KCAL',
String(comulativeFee)
]);
}

for (const user of userList) {
const toAddress = Address.FromText(user.user);
const amount = String(user.amount);
sb.CallInterop('Runtime.TransferTokens', [from, toAddress, symbol, amount]);
sb.CallInterop('Runtime.TransferTokens', [from.Text, toAddress.Text, symbol, String(amount)]);
}
const myScript = sb.SpendGas(from).EndScript();

Expand All @@ -130,7 +128,7 @@ export function AirdropFT(
);
},
function () {
NotificationError('Airdrop Error!', 'Error doing the Airdrop!');
NotificationError('Airdrop Error!', 'User canceled the Airdrop!');
}
);
}
Expand All @@ -156,19 +154,24 @@ export function AirdropNFT(symbol: string, userList: Array<{ user; id }>, totalA
const from = Address.FromText(String(Link.account.address));
const payload = Base16.encode('Tools.AirdropNFT');

const numberOfDistributions = userList.length / 100;
let comulativeFee = (userList.length / 100) * AirdropFee;
const numberOfDistributions = userList.length / 50;
let comulativeFee = (userList.length / 50) * AirdropFee;
if (numberOfDistributions < 1) comulativeFee = AirdropFee;
const sb = new ScriptBuilder();
sb.AllowGas(from, Address.Null, gasPrice, gasLimit);
if (tipActive) {
//sb.CallInterop('Runtime.TransferTokens', [from, TipAddress, 'KCAL', comulativeFee]);
sb.CallInterop('Runtime.TransferTokens', [
from.Text,
TipAddress,
'KCAL',
String(comulativeFee)
]);
}

for (const user of userList) {
const toAddress = Address.FromText(user.user);
const id = String(user.id);
sb.CallInterop('Runtime.TransferToken', [from, toAddress, symbol, id]);
sb.CallInterop('Runtime.TransferToken', [from.Text, toAddress.Text, symbol, String(id)]);
}

const myScript = sb.SpendGas(from).EndScript();
Expand All @@ -183,7 +186,7 @@ export function AirdropNFT(symbol: string, userList: Array<{ user; id }>, totalA
);
},
function () {
NotificationError('Airdrop Error!', 'Error doing the Airdrop!');
NotificationError('Airdrop Error!', 'User canceled the Airdrop!');
}
);
}
52 changes: 51 additions & 1 deletion src/lib/Components/Airdrop/AirdropFT.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@
continue;
}
if (userDistribution == 0) {
if (userAddressess.length >= numberOfUsers) {
numberOfInvalidAddresses++;
userAddressessRAWEdited = userAddressessRAWEdited.replace(
addr,
`<span class="error" style="color:red !important">${addr}</span>`
);
continue;
}
}
if (userAddressess.includes(addrTrimmed)) {
numberOfRepeatedAddresses++;
userAddressessRAWEdited = userAddressessRAWEdited.replaceAll(
Expand Down Expand Up @@ -196,7 +207,43 @@
}
function onChangeNumberOfUsers() {
console.log(numberOfUsers);
let listBefore;
listBefore = userAddressessRAW.split(/[\n ,]+/);
userAddressessRAW = listBefore.join('\n');
userAddressessRAWEdited = listBefore.join('<br>');
numberOfInvalidAddresses = 0;
numberOfRepeatedAddresses = 0;
userAddressess = [];
for (let addr of listBefore) {
let addrTrimmed = addr.trim();
if (!Address.IsValidAddress(addrTrimmed)) {
numberOfInvalidAddresses++;
//userAddressessRAW = userAddressessRAW.replace(addr, `<span class="error">${addr}</span>`);
continue;
}
if (userDistribution == 0) {
if (userAddressess.length >= numberOfUsers) {
numberOfInvalidAddresses++;
userAddressessRAWEdited = userAddressessRAWEdited.replace(
addr,
`<span class="error" style="color:red !important">${addr}</span>`
);
continue;
}
}
if (userAddressess.includes(addrTrimmed)) {
numberOfRepeatedAddresses++;
userAddressessRAWEdited = userAddressessRAWEdited.replaceAll(
addr,
`<span class="error" style="color:red !important">${addr}</span>`
);
}
userAddressess.push(addr);
}
}
</script>

Expand Down Expand Up @@ -330,6 +377,9 @@
id="numberOfUsers"
bind:value={numberOfUsers}
on:change={onChangeNumberOfUsers}
on:keypress={onChangeNumberOfUsers}
on:keydown={onChangeNumberOfUsers}
on:keyup={onChangeNumberOfUsers}
class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-solid border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer"
placeholder=" "
required
Expand Down
55 changes: 55 additions & 0 deletions src/lib/Components/Airdrop/AirdropNFT.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@
continue;
}
if (userDistribution == 0) {
if (userAddressess.length >= numberOfUsers) {
numberOfInvalidAddresses++;
userAddressessRAWEdited = userAddressessRAWEdited.replace(
addr,
`<span class="error" style="color:red !important">${addr}</span>`
);
continue;
}
}
if (userAddressess.includes(addrTrimmed)) {
numberOfRepeatedAddresses++;
userAddressessRAWEdited = userAddressessRAWEdited.replaceAll(
Expand Down Expand Up @@ -188,6 +199,46 @@
console.log(selectedToken);
listOfNFTs = await GetNFTList(selectedToken);
}
function onChangeNumberOfUsers() {
let listBefore;
listBefore = userAddressessRAW.split(/[\n ,]+/);
userAddressessRAW = listBefore.join('\n');
userAddressessRAWEdited = listBefore.join('<br>');
numberOfInvalidAddresses = 0;
numberOfRepeatedAddresses = 0;
userAddressess = [];
for (let addr of listBefore) {
let addrTrimmed = addr.trim();
if (!Address.IsValidAddress(addrTrimmed)) {
numberOfInvalidAddresses++;
//userAddressessRAWEdited = userAddressessRAWEdited.replace(addr, `<span class="error">${addr}</span>`);
continue;
}
if (userDistribution == 0) {
if (userAddressess.length >= numberOfUsers) {
numberOfInvalidAddresses++;
userAddressessRAWEdited = userAddressessRAWEdited.replace(
addr,
`<span class="error" style="color:red !important">${addr}</span>`
);
continue;
}
}
if (userAddressess.includes(addrTrimmed)) {
numberOfRepeatedAddresses++;
userAddressessRAWEdited = userAddressessRAWEdited.replaceAll(
addr,
`<span class="error" style="color:red !important">${addr}</span>`
);
}
userAddressess.push(addr);
}
}
</script>

<Card
Expand Down Expand Up @@ -321,6 +372,10 @@
name="numberOfUsers"
id="numberOfUsers"
bind:value={numberOfUsers}
on:change={onChangeNumberOfUsers}
on:keypress={onChangeNumberOfUsers}
on:keydown={onChangeNumberOfUsers}
on:keyup={onChangeNumberOfUsers}
class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-solid border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer"
placeholder=" "
required
Expand Down
4 changes: 3 additions & 1 deletion src/lib/Components/Card/APISelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
DefaultAPIURL,
ExplorerURL,
ExplorerURLMainnet,
ExplorerURLTestnet
ExplorerURLTestnet,
SelectedNexus
} from '$lib/store';
import { NotificationSuccess } from '../Notification/NotificationsBuilder';
import { PhantasmaAPI } from 'phantasma-ts';
Expand All @@ -33,6 +34,7 @@
let nexusName = e.target.selectedOptions[0].dataset.net;
PhantasmaAPIClient.set(new PhantasmaAPI(selectedAPI, null, nexusName));
API_URL.set(selectedAPI);
SelectedNexus.set(nexusName);
ExplorerURL.set(nexusName == 'mainnet' ? ExplorerURLMainnet : ExplorerURLTestnet);
connectToAPI();
NotificationSuccess('API Changed', `API has been changed to <b>${nexusName}</b> network.`);
Expand Down
1 change: 1 addition & 0 deletions src/lib/Components/Footer/Footer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
rel="noreferrer">Phantasma Contributors</a
>. All Rights Reserved.
</span>

<div class="relative text-[12px] text-gray-500 sm:text-center dark:text-gray-400">
<span class="">
Developed with <Icon icon="ant-design:heart-filled" /> by
Expand Down
24 changes: 22 additions & 2 deletions src/lib/Components/Notification/NotificationsBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ export function NotificationError(title: string, message: string) {
});
}

export function NotificationWarning(title: string = "Warning!", message: string) {
export function NotificationWarning(
title: string = 'Warning!',
message: string,
duration: number = 3000
) {
const finalMessage = `<h5 class="text-orange-800">${title}</h5>${message}`;
toast.push(finalMessage, {
duration: 3000,
duration: duration,
theme: {
'--toastColor': 'mintcream',
'--toastBackground': 'rgb(251 146 60)',
Expand All @@ -50,6 +54,22 @@ export function NotificationWarning(title: string = "Warning!", message: string)
});
}

export function NotificationInformation(
title: string = 'Information!',
message: string,
duration: number = 3000
) {
const finalMessage = `<h5 class="text-blue-800 text-xl font-medium">${title}</h5>${message}`;
toast.push(finalMessage, {
duration: duration,
theme: {
'--toastColor': 'mintcream',
'--toastBackground': '#4299e1',
'--toastBarBackground': '#2a4365'
}
});
}

export function ClearNotifications() {
toast.pop(0);
}
Loading

0 comments on commit 5e4c890

Please sign in to comment.