Skip to content

Commit

Permalink
Merge pull request #742 from onflow/clean-up-accounts
Browse files Browse the repository at this point in the history
Clean up accounts
  • Loading branch information
bthaile authored Sep 20, 2023
2 parents 1e83848 + 31630fd commit 8438c01
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 119 deletions.
44 changes: 26 additions & 18 deletions src/components/ContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const ContextMenu = ({ options, showEllipsis }: ContextMenuType) => {
},
menuRelative: {
position: 'relative',
visibility: isOpen ? 'visible' : 'hidden',
},
menuAbsolute: {
display: 'flex',
Expand Down Expand Up @@ -71,6 +72,7 @@ export const ContextMenu = ({ options, showEllipsis }: ContextMenuType) => {
};

const ref = useRef(null);
const menuRef = useRef(null);

useEffect(() => {
const handleClickOutside = (event: Event) => {
Expand All @@ -86,6 +88,14 @@ export const ContextMenu = ({ options, showEllipsis }: ContextMenuType) => {

if (!showEllipsis) return null;

if (isOpen) {
const rect = menuRef?.current?.getBoundingClientRect();
const scrollTop = document.documentElement.scrollTop;
const top = rect.top + scrollTop;
if (top < 0) {
menuRef.current.style.bottom = `-${rect.height}px`;
}
}
return (
<Container sx={styles.container} ref={ref}>
<Button
Expand All @@ -96,25 +106,23 @@ export const ContextMenu = ({ options, showEllipsis }: ContextMenuType) => {
>
{ExplorerEllipseIcon()}
</Button>
{isOpen && (
<Flex sx={styles.menuRelative}>
<Flex sx={styles.menuAbsolute}>
{options.map(
({ icon, name, onClick, args }: ContextMenuOptionsType) => (
<Button
sx={styles.ctaOption}
variant="explorer"
key={name}
onClick={() => clickOption(onClick, args)}
>
{getIcon(icon)}
<Text>{name}</Text>
</Button>
),
)}
</Flex>
<Flex sx={styles.menuRelative}>
<Flex sx={styles.menuAbsolute} ref={menuRef}>
{options.map(
({ icon, name, onClick, args }: ContextMenuOptionsType) => (
<Button
sx={styles.ctaOption}
variant="explorer"
key={name}
onClick={() => clickOption(onClick, args)}
>
{getIcon(icon)}
<Text>{name}</Text>
</Button>
),
)}
</Flex>
)}
</Flex>
</Container>
);
};
127 changes: 26 additions & 101 deletions src/providers/Project/projectDefault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,136 +5,59 @@ import {
ScriptTemplate,
TransactionTemplate,
} from 'api/apollo/generated/graphql';
import { accountIndexes } from 'util/accounts';
import { strToSeed, uuid } from 'util/rng';
import { LOCAL_PROJECT_ID } from 'util/url';

const DEFAULT_CONTRACT_1 = `// HelloWorld.cdc
const HelloWorldContract = `// HelloWorld.cdc
//
// Welcome to Cadence! This is one of the simplest programs you can deploy on Flow.
//
// The HelloWorld contract contains a single string field and a public getter function.
//
// Follow the "Hello, World!" tutorial to learn more: https://docs.onflow.org/cadence/tutorial/02-hello-world/
access(all) contract HelloWorld {
// Declare a public field of type String.
//
// All fields must be initialized in the init() function.
access(all) let greeting: String
// The init() function is required if the contract contains any fields.
init() {
self.greeting = "Hello, World!"
}
// Public function that returns our friendly greeting!
access(all) fun hello(): String {
return self.greeting
}
}
`;

const DEFAULT_CONTRACT_2 = `access(all) contract HelloWorld {
pub contract HelloWorld {
// Declare a public field of type String.
//
// All fields must be initialized in the init() function.
access(all) let greeting: String
// The init() function is required if the contract contains any fields.
init() {
self.greeting = "Hello from account 2!"
}
pub var greeting: String
// Public function that returns our friendly greeting!
access(all) fun hello(): String {
return self.greeting
}
}
`;

const DEFAULT_CONTRACT_3 = `access(all) contract HelloWorld {
// Declare a public field of type String.
//
// All fields must be initialized in the init() function.
access(all) let greeting: String
// The init() function is required if the contract contains any fields.
init() {
self.greeting = "Hello from account 3!"
access(all) fun changeGreeting(newGreeting: String) {
self.greeting = newGreeting
}
// Public function that returns our friendly greeting!
access(all) fun hello(): String {
return self.greeting
}
}
`;

const DEFAULT_CONTRACT_4 = `access(all) contract HelloWorld {
// Declare a public field of type String.
//
// All fields must be initialized in the init() function.
access(all) let greeting: String
// The init() function is required if the contract contains any fields.
init() {
self.greeting = "Hello from account 4!"
}
// Public function that returns our friendly greeting!
access(all) fun hello(): String {
return self.greeting
}
}
`;

const DEFAULT_CONTRACT_5 = `access(all) contract HelloWorld {
// Declare a public field of type String.
//
// All fields must be initialized in the init() function.
access(all) let greeting: String
// The init() function is required if the contract contains any fields.
init() {
self.greeting = "Hello from account 5!"
}
// Public function that returns our friendly greeting!
access(all) fun hello(): String {
return self.greeting
self.greeting = "Hello, World!"
}
}
`;

export const DEFAULT_ACCOUNT_STATE = '{}';

const DEFAULT_CONTRACTS = [
DEFAULT_CONTRACT_1,
DEFAULT_CONTRACT_2,
DEFAULT_CONTRACT_3,
DEFAULT_CONTRACT_4,
DEFAULT_CONTRACT_5,
];

const DEFAULT_TRANSACTION = `import HelloWorld from 0x05
transaction {
transaction(greeting: String) {
prepare(acct: AuthAccount) {}
prepare(acct: AuthAccount) {
log(acct.address)
}
execute {
log(HelloWorld.hello())
HelloWorld.changeGreeting(newGreeting: greeting)
}
}
`;

const DEFAULT_SCRIPT = `pub fun main(): Int {
return 1
const DEFAULT_SCRIPT = `import HelloWorld from 0x05
pub fun main() {
log(HelloWorld.hello())
}
`;

Expand All @@ -155,13 +78,15 @@ export function createDefaultProject(): Project {
SEED_TITLE,
SEED_DESCRIPTION,
SEED_README,
DEFAULT_CONTRACTS.map((contract) => contract),
DEFAULT_CONTRACTS.map((contract, i) => ({
title: `Contract ${i + 1}`,
code: contract,
})),
[{ title: 'Transaction', code: DEFAULT_TRANSACTION }],
[{ title: 'Script', code: DEFAULT_SCRIPT }],
accountIndexes(),
[
{
title: `HelloWorld`,
code: HelloWorldContract,
},
],
[{ title: 'ChangeGreeting', code: DEFAULT_TRANSACTION }],
[{ title: 'GetGreeting', code: DEFAULT_SCRIPT }],
);
}

Expand All @@ -181,10 +106,10 @@ export function createLocalProject(
transactionTemplates: Array<ScriptDetails>,
scriptTemplates: Array<ScriptDetails>,
): Project {
const accountEntities: Account[] = accounts.map((_account, i) => {
const accountEntities: Account[] = accounts.map((_account) => {
return {
__typename: 'Account',
address: `000000000000000${i + 1}`,
address: `000000000000000${_account}`,
deployedContracts: [],
state: DEFAULT_ACCOUNT_STATE,
};
Expand Down
5 changes: 5 additions & 0 deletions src/util/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const accountAddressIndexes = ['5', '6', '7', '8', '9'];
const accountAddresses = ['0x05', '0x06', '0x07', '0x08', '0x09'];
export const storageMapByIndex = (index: number): string => {
if (index < 0) return null;
Expand All @@ -13,3 +14,7 @@ export const addressToAccount = (address: string): string => {
const acctNum = address.charAt(address.length - 1);
return `0x0${acctNum}`;
};

export const accountIndexes = (): string[] => {
return accountAddressIndexes;
};

0 comments on commit 8438c01

Please sign in to comment.