Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MNS support in MW #940

Merged
merged 45 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
46aae62
Add mns display in home page
pivilartisant May 14, 2024
6d52ac5
Avoid infinite prepareCall + improve useMns
pivilartisant May 14, 2024
31084f9
add usePrepareScCall custom hook
pivilartisant May 14, 2024
c2fc952
Add contracts cont
pivilartisant May 14, 2024
cb6c494
Improved callData args
pivilartisant May 14, 2024
d21a284
resolve conflict
pivilartisant May 14, 2024
a8dee70
Use network chainID for contracts
pivilartisant May 14, 2024
d9fa4ca
Add Mns Address correlation message
pivilartisant May 14, 2024
bf3efd1
add ms on confirmation screen
pivilartisant May 14, 2024
609c5d4
change setRecipient fn name
pivilartisant May 15, 2024
bb6b6ab
rename targetMnsAddress and add reset fn
pivilartisant May 15, 2024
5e76171
add .massa mns search and improve logic
pivilartisant May 15, 2024
be1a7a1
fix test
pivilartisant May 15, 2024
c38a4c3
better error removal
pivilartisant May 15, 2024
921fb69
fix mns on send confirm
pivilartisant May 15, 2024
ad506a5
add Mns component
pivilartisant May 15, 2024
7afd89f
improve contract network logic
pivilartisant May 15, 2024
cacaff8
fix correlation naming
pivilartisant May 15, 2024
4ae7776
simplify useEffect
pivilartisant May 15, 2024
6172c49
isMainnet replace in confirm screen
pivilartisant May 15, 2024
f6ec820
replace correlation message
pivilartisant May 15, 2024
97f4382
add useCallback
pivilartisant May 15, 2024
2c2921d
improve targetMnsAddress confirmation
pivilartisant May 15, 2024
66220e3
rename mns var
pivilartisant May 16, 2024
0f7eb2e
client as props + domainName id dataFromObject
pivilartisant May 16, 2024
434922d
improve useMns
pivilartisant May 16, 2024
382b2fb
revamp home page to support multiple dns
pivilartisant May 16, 2024
0b45bc9
remove no client throw
pivilartisant May 16, 2024
3256a27
clean
pivilartisant May 16, 2024
0a53cdb
add store.ts for initialization
pivilartisant May 7, 2024
e524e71
Add massaWeb3Store
pivilartisant May 16, 2024
60bd364
refacto to use massaWeb3Store
pivilartisant May 16, 2024
0d064dd
clean up
pivilartisant May 20, 2024
aecb16d
rebase
pivilartisant May 20, 2024
0ff0d44
remove mnsExtension in dep
pivilartisant May 20, 2024
f8dfc0e
use callbakc instead of useEffect
pivilartisant May 20, 2024
fbec687
improve logic
pivilartisant May 20, 2024
eecbf0a
revamp value init
pivilartisant May 20, 2024
63ce3da
rollback generated files
Thykof May 20, 2024
4aa8e8c
fix lint errors + new set error
pivilartisant May 20, 2024
4685dd7
use export const .massa
pivilartisant May 21, 2024
8c4e11f
add mainnet contracts
pivilartisant May 21, 2024
a3d38fd
add try catch to useMns reverseResolve
pivilartisant May 21, 2024
824e67d
add input success correlation message
pivilartisant May 21, 2024
9835e48
improve re-render while switch wallets on home screen
pivilartisant May 21, 2024
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: 1 addition & 1 deletion api/server/models/account.go

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

2 changes: 1 addition & 1 deletion api/server/models/rolls_request.go

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

8 changes: 4 additions & 4 deletions api/server/restapi/embedded_spec.go

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

2 changes: 1 addition & 1 deletion api/server/restapi/server.go

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

6 changes: 3 additions & 3 deletions web-frontend/package-lock.json

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

41 changes: 6 additions & 35 deletions web-frontend/src/custom/smart-contract/useFTTransfer.tsx
Original file line number Diff line number Diff line change
@@ -1,56 +1,27 @@
import { useCallback, useEffect, useState } from 'react';
import { useCallback, useState } from 'react';

import {
Client,
EOperationStatus,
ICallData,
MAX_GAS_CALL,
Args,
ClientFactory,
MAINNET_CHAIN_ID,
strToBytes,
STORAGE_BYTE_COST,
fromMAS,
} from '@massalabs/massa-web3';
import { ToastContent, parseAmount, toast } from '@massalabs/react-ui-kit';
import { OperationToast } from '@massalabs/react-ui-kit/src/lib/ConnectMassaWallets/components/OperationToast';
import { logSmartContractEvents } from '@massalabs/react-ui-kit/src/lib/massa-react/utils';
import { providers } from '@massalabs/wallet-provider';

import { usePrepareScCall } from '../usePrepareScCall';
import Intl from '@/i18n/i18n';
import { useMassaWeb3Store } from '@/store/store';

async function prepareFTTransfer(nickname: string) {
const massaProvider = (await providers())?.find(
(p) => p.name() === 'MASSASTATION',
);
if (!massaProvider) {
console.error('FATAL: Massa provider not found');
return;
}

const account = (await massaProvider.accounts()).find(
(a) => a.name() === nickname,
);
if (!account) {
return;
}

return {
client: await ClientFactory.fromWalletProvider(massaProvider, account),
chainId: await massaProvider.getChainId(),
};
}
export function useFTTransfer() {
const { isMainnet } = useMassaWeb3Store();
const { client } = usePrepareScCall();

export function useFTTransfer(nickname: string) {
const [client, setClient] = useState<Client>();
const [chainId, setChainId] = useState<bigint>();
const isMainnet = chainId === MAINNET_CHAIN_ID;
useEffect(() => {
prepareFTTransfer(nickname).then((result) => {
setClient(result?.client);
setChainId(result?.chainId);
});
}, [nickname, client, setClient]);
const {
opId,
isPending,
Expand Down
68 changes: 68 additions & 0 deletions web-frontend/src/custom/useMNS.tsx
Thykof marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { useCallback, useState } from 'react';

import { Args } from '@massalabs/massa-web3';
import { bytesToStr } from '@massalabs/web3-utils';

import { useMassaWeb3Store } from '@/store/store';
import { contracts } from '@/utils/const';

export function useMNS() {
const { defaultClient: client, isMainnet } = useMassaWeb3Store();

const [targetMnsAddress, setTargetMnsAddress] = useState<string>('');
const [domainNameList, setDomainNameList] = useState<string[]>([]);

const targetContractAddress = isMainnet
? contracts.mainnet.mnsContract
: contracts.buildnet.mnsContract;

const reverseResolveDns = useCallback(
async (targetAddress: string) => {
if (!client || !targetAddress) return;
const result = await client.smartContracts().readSmartContract({
targetAddress: targetContractAddress,
targetFunction: 'dnsReverseResolve',
parameter: new Args().addString(targetAddress).serialize(),
});
const domains = bytesToStr(result.returnValue).split(',');

setDomainNameList(domains);
return domains;
},
[client, targetContractAddress],
);

const resolveDns = useCallback(
async (domain: string): Promise<string | undefined> => {
try {
if (!client) return;
const result = await client.smartContracts().readSmartContract({
targetAddress: targetContractAddress,
targetFunction: 'dnsResolve',
parameter: new Args().addString(domain).serialize(),
});

const targetAddress = bytesToStr(result.returnValue);
setTargetMnsAddress(bytesToStr(result.returnValue));
return targetAddress;
} catch (e) {
console.error(e);
setTargetMnsAddress('');
return '';
}
},
[client, targetContractAddress],
);

function resetTargetMnsAddress() {
setTargetMnsAddress('');
}

return {
domainNameList,
resolveDns,
targetMnsAddress,
reverseResolveDns,
resetTargetMnsAddress,
};
}
22 changes: 22 additions & 0 deletions web-frontend/src/custom/usePrepareScCall.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useEffect, useState } from 'react';

import { Client } from '@massalabs/massa-web3';
import { useParams } from 'react-router-dom';

import { prepareSCCall } from '@/utils/prepareSCCall';

export function usePrepareScCall() {
const { nickname } = useParams();
const [client, setClient] = useState<Client>();

useEffect(() => {
if (!nickname) {
throw new Error('Nickname not found');
}
prepareSCCall(nickname).then((result) => {
setClient(result?.client);
});
}, [nickname, setClient]);

return { client };
}
7 changes: 6 additions & 1 deletion web-frontend/src/i18n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"buttons": {
"send": "Send",
"receive": "Receive"
}
},
"title-mns": "Massa Domains",
"desc-mns": "Domains that targets your wallet address."
},
"assets": {
"title": "Assets",
Expand Down Expand Up @@ -188,6 +190,9 @@
"ft-transfer-success": "Successfully transfered",
"ft-transfer-failed": "Transfer failed",
"failed-timeout": "Operation failed due to timeout, please retry with more fees."
},
"mns": {
"mns-correlation": "{mns} correlates with the following address: {address}"
}
},
"receive-coins": {
Expand Down
2 changes: 1 addition & 1 deletion web-frontend/src/layouts/WalletLayout/WalletLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from 'react-router-dom';

import Intl from '@/i18n/i18n';
import { useAppStore } from '@/store/appStore';
import { useAppStore } from '@/store/store';
import { routeFor, useFetchAccounts } from '@/utils';

export enum MenuItem {
Expand Down
1 change: 0 additions & 1 deletion web-frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {

import '@massalabs/react-ui-kit/src/global.css';
import './index.css';

import { ENV } from '@/const/env/env';
import { mockServer, mockServerWithCypress } from '@/mirage';
import AccountCreate from '@/pages/AccountCreate/AccountCreate.tsx';
Expand Down
4 changes: 1 addition & 3 deletions web-frontend/src/pages/Assets/AssetsList.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState } from 'react';

import { Token, getAssetIcons } from '@massalabs/react-ui-kit';
import { useParams } from 'react-router-dom';

import { useFTTransfer } from '@/custom/smart-contract/useFTTransfer';
import Intl from '@/i18n/i18n';
Expand All @@ -24,8 +23,7 @@ export function AssetsList(props: AssetsListProps) {
setModal(true);
}

const { nickname } = useParams();
const { isMainnet } = useFTTransfer(nickname || '');
const { isMainnet } = useFTTransfer();

return (
<>
Expand Down
42 changes: 34 additions & 8 deletions web-frontend/src/pages/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
Balance,
Clipboard,
formatAmount,
Mns,
} from '@massalabs/react-ui-kit';
import { FiArrowDownLeft, FiArrowUpRight } from 'react-icons/fi';
import { useNavigate, useParams } from 'react-router-dom';

import { Loading } from './Loading';
import { TAB_SEND, TAB_RECEIVE } from '@/const/tabs/tabs';
import { useResource } from '@/custom/api';
import { useMNS } from '@/custom/useMNS';
import Intl from '@/i18n/i18n';
import { WalletLayout, MenuItem } from '@/layouts/WalletLayout/WalletLayout';
import { AccountObject } from '@/models/AccountModel';
Expand All @@ -20,12 +22,20 @@ import { routeFor } from '@/utils';
export default function Home() {
const navigate = useNavigate();
const { nickname } = useParams();

const {
error,
data: account,
isLoading,
} = useResource<AccountObject>(`accounts/${nickname}`);

const { reverseResolveDns, domainNameList } = useMNS();
const accountAddress = account?.address ?? '';

useEffect(() => {
reverseResolveDns(accountAddress);
}, [reverseResolveDns, accountAddress]);

useEffect(() => {
if (error) {
navigate(routeFor('error'));
Expand All @@ -39,7 +49,6 @@ export default function Home() {
const formattedBalance = formatAmount(
balance.toString(),
).amountFormattedPreview;
const address = account?.address ?? '';

return (
<WalletLayout menuItem={MenuItem.Home}>
Expand Down Expand Up @@ -79,18 +88,35 @@ export default function Home() {
</div>
</div>

<div className="bg-secondary rounded-2xl w-full max-w-lg p-10">
<div className="bg-secondary rounded-2xl w-full max-w-lg p-10 flex flex-col justify-between">
<p className="mas-body text-f-primary mb-6">
{Intl.t('home.title-account-address')}
</p>
<Clipboard
displayedContent={address}
rawContent={address}
error={Intl.t('errors.no-content-to-copy')}
className="flex flex-row items-center mas-body2 justify-between
<div className="flex w-full justify-between items-center">
<Clipboard
displayedContent={accountAddress}
rawContent={accountAddress}
error={Intl.t('errors.no-content-to-copy')}
className="flex flex-row items-center mas-body2 justify-between
w-full h-12 px-3 rounded bg-primary cursor-pointer"
/>
/>
</div>
</div>
{domainNameList.length > 0 && (
<div className="bg-secondary rounded-2xl w-full max-w-lg p-10 gap-6 flex flex-col justify-between">
<div className="flex flex-col">
<p className="mas-body text-f-primary">
{Intl.t('home.title-mns')}
</p>
<p className="mas-caption">{Intl.t('home.desc-mns')}</p>
</div>
<div className="flex flex-col w-full gap-4 max-h-32 overflow-y-scroll ">
{domainNameList?.map((domainName) => (
<Mns mns={domainName} />
))}
</div>
</div>
)}
</div>
)}
</WalletLayout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface GenerateLinkProps {
function GenerateLink(props: GenerateLinkProps) {
const { account, presetURL, setURL, setModal } = props;

const { isMainnet } = useFTTransfer(account.nickname);
const { isMainnet } = useFTTransfer();

const [amount, setAmount] = useState<string>('');
const [link, setLink] = useState('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface AssetSelectorProps {
export function AssetSelector(props: AssetSelectorProps) {
const { selectedAsset, setSelectedAsset, selectSymbol } = props;
const { nickname } = useParams();
const { isMainnet } = useFTTransfer(nickname || '');
const { isMainnet } = useFTTransfer();

const { data: assets, isLoading: isAssetsLoading } = useResource<Asset[]>(
`accounts/${nickname}/assets`,
Expand Down
4 changes: 2 additions & 2 deletions web-frontend/src/pages/TransferCoins/SendCoins/SendCoins.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '@/pages/TransferCoins/SendCoins/SendConfirmation';
import { SendForm } from '@/pages/TransferCoins/SendCoins/SendForm';
import { Redirect } from '@/pages/TransferCoins/TransferCoins';
import { useAppStore } from '@/store/appStore';
import { useAppStore } from '@/store/store';
import { routeFor } from '@/utils';

interface SendCoinsProps {
Expand Down Expand Up @@ -45,7 +45,7 @@ export default function SendCoins(props: SendCoinsProps) {
transfer: transferFT,
isOpPending: transferFTPending,
isPending: transferFTLoading,
} = useFTTransfer(nickname || '');
} = useFTTransfer();

useEffect(() => {
if (transferMASError && !errorToastId) {
Expand Down
Loading
Loading