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

feat: add GRC-721 support #614

Merged
merged 16 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions packages/adena-extension/src/assets/icon-empty-image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions packages/adena-extension/src/assets/icon-hide.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable react/no-unknown-property */
import React from 'react';

const IconHide = ({ className }: { className?: string }): JSX.Element => (
<svg
className={className}
xmlns='http://www.w3.org/2000/svg'
width='14'
height='14'
viewBox='0 0 14 14'
fill='none'
>
<g clip-path='url(#clip0_9658_22285)'>
<path
d='M3.48681 9.68767L2.09118 11.0833L2.91602 11.9081L4.46331 10.3608C5.21056 10.7893 6.06252 11.0833 6.99935 11.0833C10.4993 11.0833 12.8327 6.99996 12.8327 6.99996C12.8327 6.99996 11.9711 5.49496 10.5119 4.31226L11.9075 2.91663L11.0827 2.0918L9.53539 3.63909C8.78814 3.21063 7.93618 2.91663 6.99935 2.91663C3.49935 2.91663 1.16602 6.99996 1.16602 6.99996C1.16602 6.99996 2.0276 8.50496 3.48681 9.68767ZM9.68385 5.1403C10.462 5.75017 11.0684 6.49305 11.4327 6.99792C10.7117 7.99046 9.04802 9.91663 6.99935 9.91663C6.40202 9.91663 5.84056 9.75242 5.3246 9.49955L5.81577 9.00838C6.16314 9.21342 6.56681 9.3333 6.99935 9.3333C8.28793 9.3333 9.33268 8.28855 9.33268 6.99996C9.33268 6.56742 9.21281 6.16375 9.00777 5.81638L9.68385 5.1403ZM5.87614 7.29834C5.85077 7.20238 5.83268 7.1038 5.83268 6.99996C5.83268 6.35655 6.35593 5.8333 6.99935 5.8333C7.10318 5.8333 7.20177 5.85138 7.29772 5.87676L5.87614 7.29834ZM8.12256 6.70159C8.14793 6.79755 8.16602 6.89613 8.16602 6.99996C8.16602 7.64338 7.64277 8.16663 6.99935 8.16663C6.89552 8.16663 6.79693 8.14855 6.70097 8.12317L8.12256 6.70159ZM6.99935 4.0833C7.59668 4.0833 8.15814 4.24751 8.6741 4.50038L8.18293 4.99155C7.83556 4.78651 7.43189 4.66663 6.99935 4.66663C5.71077 4.66663 4.66602 5.71138 4.66602 6.99996C4.66602 7.4325 4.78589 7.83617 4.99093 8.18355L4.31485 8.85963C3.53668 8.24975 2.93031 7.50688 2.56572 7.002C3.28702 6.00946 4.95068 4.0833 6.99935 4.0833Z'
fill='#777777'
/>
</g>
<defs>
<clipPath id='clip0_9658_22285'>
<rect width='14' height='14' fill='white' />
</clipPath>
</defs>
</svg>
);

export default IconHide;
22 changes: 22 additions & 0 deletions packages/adena-extension/src/assets/icon-pin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable react/no-unknown-property */
import React from 'react';

const IconPin = ({ className }: { className?: string }): JSX.Element => (
<svg
className={className}
xmlns='http://www.w3.org/2000/svg'
width='10'
height='10'
viewBox='0 0 10 10'
fill='none'
>
<path
fillRule='evenodd'
clipRule='evenodd'
d='M7.35461 0.400363C6.73303 -0.221239 5.69468 -0.102768 5.22908 0.642882L3.933 2.71856C3.933 2.71856 3.93232 2.7197 3.92989 2.72144C3.92692 2.72357 3.92058 2.72724 3.90994 2.73024C3.8867 2.73678 3.85634 2.73627 3.82922 2.72622C3.38686 2.56223 2.62753 2.38208 1.83902 2.67909C1.4142 2.8391 1.20222 3.20738 1.17911 3.58101C1.15708 3.93751 1.30105 4.30398 1.56586 4.56878L3.17641 6.17933L0.133423 9.22232C-0.0444744 9.40025 -0.0444744 9.68865 0.133423 9.86658C0.311319 10.0445 0.599751 10.0445 0.777647 9.86658L3.82064 6.82355L5.4312 8.43415C5.696 8.69895 6.06248 8.84295 6.41898 8.8209C6.79261 8.7978 7.16091 8.5858 7.3209 8.16101C7.61791 7.37247 7.43774 6.61314 7.2738 6.17077C7.26373 6.14367 7.26323 6.11333 7.26974 6.09005C7.27275 6.07943 7.27644 6.0731 7.27858 6.0701C7.28031 6.06768 7.2814 6.06704 7.2814 6.06704L9.3571 4.77091C10.1028 4.3053 10.2212 3.26698 9.59963 2.64538L7.35461 0.400363ZM6.00189 1.12543C6.1571 0.876882 6.50321 0.837391 6.71039 1.04459L8.95541 3.28961C9.16259 3.49681 9.12314 3.84292 8.87455 3.99811L6.7989 5.29423C6.36856 5.56295 6.27276 6.0916 6.41953 6.48746C6.54972 6.83868 6.65358 7.34797 6.46832 7.83986C6.45565 7.87343 6.44158 7.88555 6.43292 7.89151C6.4219 7.89912 6.40017 7.90923 6.36277 7.91156C6.28191 7.91657 6.16643 7.8809 6.07542 7.78993L2.21009 3.92456C2.1191 3.83357 2.08345 3.71808 2.08845 3.63722C2.09076 3.59981 2.1009 3.57811 2.10851 3.56706C2.11447 3.55843 2.12659 3.54433 2.16016 3.53168C2.65206 3.3464 3.16131 3.45028 3.51254 3.58049C3.90843 3.72725 4.43704 3.63146 4.7058 3.2011L6.00189 1.12543Z'
fill='white'
/>
</svg>
);

export default IconPin;
31 changes: 31 additions & 0 deletions packages/adena-extension/src/assets/icon-show.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable react/no-unknown-property */
import React from 'react';

const IconShow = ({ className }: { className?: string }): JSX.Element => (
<svg
className={className}
xmlns='http://www.w3.org/2000/svg'
width='14'
height='14'
viewBox='0 0 14 14'
fill='none'
>
<g clip-path='url(#clip0_9658_22315)'>
<path
d='M6.99935 11.0832C10.4993 11.0832 12.8327 6.99984 12.8327 6.99984C12.8327 6.99984 10.4993 2.9165 6.99935 2.9165C3.49935 2.9165 1.16602 6.99984 1.16602 6.99984C1.16602 6.99984 3.49935 11.0832 6.99935 11.0832ZM6.99935 4.08317C9.06201 4.08317 10.7155 6.0035 11.433 6.9975C10.7117 7.99034 9.04801 9.9165 6.99935 9.9165C4.93668 9.9165 3.28322 7.99617 2.56572 7.00217C3.28702 6.00934 4.95068 4.08317 6.99935 4.08317Z'
fill='#777777'
/>
<path
d='M6.99935 9.33317C8.28793 9.33317 9.33268 8.28842 9.33268 6.99984C9.33268 5.71125 8.28793 4.6665 6.99935 4.6665C5.71077 4.6665 4.66602 5.71125 4.66602 6.99984C4.66602 8.28842 5.71077 9.33317 6.99935 9.33317ZM6.99935 5.83317C7.64277 5.83317 8.16602 6.35642 8.16602 6.99984C8.16602 7.64325 7.64277 8.1665 6.99935 8.1665C6.35593 8.1665 5.83268 7.64325 5.83268 6.99984C5.83268 6.35642 6.35593 5.83317 6.99935 5.83317Z'
fill='#777777'
/>
</g>
<defs>
<clipPath id='clip0_9658_22315'>
<rect width='14' height='14' fill='white' />
</clipPath>
</defs>
</svg>
);

export default IconShow;
35 changes: 35 additions & 0 deletions packages/adena-extension/src/assets/icon-unpin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable react/no-unknown-property */
import React from 'react';

const IconUnpin = ({ className }: { className?: string }): JSX.Element => (
<svg
className={className}
xmlns='http://www.w3.org/2000/svg'
width='12'
height='12'
viewBox='0 0 12 12'
fill='none'
>
<g clip-path='url(#clip0_9658_22308)'>
<path
d='M8.55994 0.935047C7.87769 0.252773 6.73799 0.382807 6.22694 1.20124L4.875 3.36638L5.5975 4.0889C5.6168 4.06358 5.6352 4.03701 5.6526 4.00916L7.07519 1.73089C7.24554 1.45808 7.62544 1.41473 7.85284 1.64216L10.317 4.1063C10.5444 4.33373 10.5011 4.71362 10.2282 4.88396L7.95 6.30658C7.92215 6.32398 7.89555 6.34233 7.87024 6.36163L8.59275 7.08413L10.7579 5.73218C11.5763 5.22113 11.7064 4.08147 11.0241 3.39919L8.55994 0.935047Z'
fill='#777777'
/>
<path
d='M1.78249 4.42631C1.80061 4.13316 1.92467 3.84301 2.16428 3.63721L2.88854 4.36147C2.87878 4.36493 2.86904 4.3685 2.85929 4.37217C2.82244 4.38605 2.80914 4.40152 2.8026 4.411C2.79425 4.42312 2.78312 4.44695 2.78058 4.48801C2.77509 4.57676 2.81422 4.70353 2.91409 4.8034L7.15671 9.04605C7.25661 9.1459 7.38336 9.18505 7.47211 9.17955C7.51316 9.177 7.53701 9.1659 7.54911 9.15755C7.55861 9.151 7.57406 9.1377 7.58796 9.10085C7.59161 9.0911 7.59521 9.08135 7.59866 9.0716L8.32291 9.79585C8.11711 10.0354 7.82696 10.1595 7.53381 10.1776C7.14251 10.2018 6.74026 10.0438 6.44961 9.75315L4.68185 7.98535L1.34183 11.3254C1.14657 11.5206 0.829988 11.5206 0.634728 11.3254C0.439466 11.1301 0.439466 10.8135 0.634728 10.6182L3.97474 7.27825L2.20699 5.5105C1.91633 5.21985 1.7583 4.81761 1.78249 4.42631Z'
fill='#777777'
/>
<path
d='M1.00387 1.00387C0.808606 1.19913 0.808606 1.51572 1.00387 1.71098L10.293 11.0001C10.4883 11.1954 10.8049 11.1954 11.0001 11.0001C11.1954 10.8049 11.1954 10.4883 11.0001 10.293L1.71098 1.00387C1.51572 0.808606 1.19913 0.808606 1.00387 1.00387Z'
fill='#777777'
/>
</g>
<defs>
<clipPath id='clip0_9658_22308'>
<rect width='12' height='12' fill='white' />
</clipPath>
</defs>
</svg>
);

export default IconUnpin;
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const DEFAULT_GAS_WANTED = 10_000_000;

export const DEFAULT_NETWORK_FEE = 1;

export const TRANSACTION_MESSAGE_SEND_OF_REGISTER = '200000000ugnot';
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,21 @@ export class GnoProvider extends GnoJSONRPCProvider {

return this.evaluateExpression(packagePath, expression)
.then((result) => {
const parseData = result.replace('(', '').replace(')', '').split(' ');
if (parseData.length === 0) {
return null;
const regex = /\((?:"((?:\\.|[^"\\])*)"|(\S+))\s+\w+\)/g;
const matches = result.matchAll(regex);

for (const match of matches) {
if (match?.[1] !== undefined) {
const unescaped = match[1].replace(/\\"/g, '"').replace(/\\\\/g, '\\');
return unescaped;
}

if (match?.[2] !== undefined) {
return `${match[2]}`;
}
}
return parseData[0];

return null;
})
.catch(() => null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Storage } from '.';
import { StorageMigrator, StorageModelLatest } from '@migrates/storage-migrator';
import { CommonError } from '@common/errors/common';
import { StorageMigrator, StorageModelLatest } from '@migrates/storage-migrator';
import { Storage } from '.';

type StorageKeyTypes =
| 'NETWORKS'
Expand All @@ -15,7 +15,9 @@ type StorageKeyTypes =
| 'ACCOUNT_TOKEN_METAINFOS'
| 'QUESTIONNAIRE_EXPIRED_DATE'
| 'WALLET_CREATION_GUIDE_CONFIRM_DATE'
| 'ADD_ACCOUNT_GUIDE_CONFIRM_DATE';
| 'ADD_ACCOUNT_GUIDE_CONFIRM_DATE'
| 'ACCOUNT_GRC721_COLLECTIONS'
| 'ACCOUNT_GRC721_PINNED_PACKAGES';

const StorageKeys: StorageKeyTypes[] = [
'NETWORKS',
Expand All @@ -31,6 +33,8 @@ const StorageKeys: StorageKeyTypes[] = [
'QUESTIONNAIRE_EXPIRED_DATE',
'WALLET_CREATION_GUIDE_CONFIRM_DATE',
'ADD_ACCOUNT_GUIDE_CONFIRM_DATE',
'ACCOUNT_GRC721_COLLECTIONS',
'ACCOUNT_GRC721_PINNED_PACKAGES',
];

function isStorageKey(key: string): key is StorageKeyTypes {
Expand Down
145 changes: 145 additions & 0 deletions packages/adena-extension/src/common/utils/parse-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,148 @@ export const parseGRC20ByFileContents = (
tokenDecimals,
};
};

function checkImport(code: string, importPath: string): boolean {
const importRegex = /import\s*\(([^)]+)\)/m;
const match = code.match(importRegex);
if (!match) return false;

const imports = match[1].split('\n').map((line) =>
line
.trim()
.replace(/"/g, '')
.replace(/^[a-zA-Z_][a-zA-Z0-9_]*\s+/, ''),
);
return imports.includes(importPath);
}

interface MethodSignature {
[methodName: string]: string;
}

function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

function extractMethods(code: string, typeName: string): MethodSignature {
const escapedTypeName = escapeRegExp(typeName);
const methodRegex = new RegExp(
// eslint-disable-next-line no-useless-escape
`func\\s+${escapedTypeName}\\.([a-zA-Z0-9_]+)\\s*\$begin:math:text$([^)]*)\\$end:math:text$\\s*([^{}]+)\\{`,
'g',
);

let match: RegExpExecArray | null;
const methods: MethodSignature = {};

while ((match = methodRegex.exec(code)) !== null) {
const methodName = match[1];
const params = match[2].trim();
const returns = match[3].trim().replace(/\s+/g, ' ');
methods[methodName] = `${methodName}(${params}) ${returns}`;
}

return methods;
}

interface InterfaceCheckResult {
implementsInterface: boolean;
missingMethods: string[];
}

function checkInterfaceImplementation(
code: string,
typeName: string,
interfaceDef: { [key: string]: string },
): InterfaceCheckResult {
const methods = extractMethods(code, typeName);
const missingMethods: string[] = [];

for (const methodName in interfaceDef) {
if (!(methodName in methods)) {
missingMethods.push(methodName);
}
}

return {
implementsInterface: missingMethods.length === 0,
missingMethods,
};
}

interface GRC721Meta {
variableName: string;
name: string;
symbol: string;
isTokenUri: boolean;
isMetadata: boolean;
}

function parseGRC721NewFunctions(code: string): GRC721Meta | null {
const grcNewRegex =
/([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*grc721\.New\w+\s*\(\s*"([^"]+)"\s*,\s*"([^"]+)"\s*\)/g;
let match: RegExpExecArray | null;

while ((match = grcNewRegex.exec(code)) !== null) {
if (match.length < 3) {
continue;
}

const variableName = match[1];
const name = match[2];
const symbol = match[3];

return {
variableName,
name,
symbol,
isTokenUri: false,
isMetadata: false,
};
}

return null;
}

export function parseGRC721FileContents(contents: string): GRC721Meta | null {
const importPath = 'gno.land/p/demo/grc/grc721';
const hasImport = checkImport(contents, importPath);
if (!hasImport) {
return null;
}

const grc721Meta = parseGRC721NewFunctions(contents);
if (!grc721Meta) {
return null;
}

const interfaceCheck = checkInterfaceImplementation(contents, grc721Meta.variableName, {
BalanceOf: 'BalanceOf(owner std.Address) (uint64, error)',
OwnerOf: 'OwnerOf(tid TokenID) (std.Address, error)',
SetTokenURI: 'SetTokenURI(tid TokenID, tURI TokenURI) (bool, error)',
SafeTransferFrom: 'SafeTransferFrom(from, to std.Address, tid TokenID) error',
TransferFrom: 'TransferFrom(from, to std.Address, tid TokenID) error',
Approve: 'Approve(approved std.Address, tid TokenID) error',
SetApprovalForAll: 'SetApprovalForAll(operator std.Address, approved bool) error',
GetApproved: 'GetApproved(tid TokenID) (std.Address, error)',
IsApprovedForAll: 'IsApprovedForAll(owner, operator std.Address) bool',
});

if (!interfaceCheck.implementsInterface) {
return null;
}

const tokenUriCheck = checkInterfaceImplementation(contents, grc721Meta.variableName, {
TokenUri: 'TokenURI(tid TokenID) (string, error)',
});

const metadataCheck = checkInterfaceImplementation(contents, grc721Meta.variableName, {
TokenMetadata: 'TokenMetadata(tid TokenID) (Metadata, error)',
});

return {
...grc721Meta,
isTokenUri: tokenUriCheck.implementsInterface,
isMetadata: metadataCheck.implementsInterface,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './option-dropdown';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import { GlobalPopupStyle } from '@styles/global-style';
import theme from '@styles/theme';
import { render } from '@testing-library/react';
import { RecoilRoot } from 'recoil';
import { ThemeProvider } from 'styled-components';
import OptionDropdown, { OptionDropdownProps } from './option-dropdown';

describe('OptionDropdown Component', () => {
it('OptionDropdown render', () => {
const args: OptionDropdownProps = {
options: [],
buttonNode: <></>,
hover: false,
};

render(
<RecoilRoot>
<GlobalPopupStyle />
<ThemeProvider theme={theme}>
<OptionDropdown {...args} />
</ThemeProvider>
</RecoilRoot>,
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Meta, StoryObj } from '@storybook/react';
import OptionDropdown, { type OptionDropdownProps } from './option-dropdown';

export default {
title: 'components/common/OptionDropdown',
component: OptionDropdown,
} as Meta<typeof OptionDropdown>;

export const Default: StoryObj<OptionDropdownProps> = {
args: {},
};
Loading
Loading