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 basic_contract_caller ui #27

Merged
merged 6 commits into from
Nov 9, 2023
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
multi-contract-caller/**
upgradeable-contracts/**
ui/**
**/frontend/**
json: true

- name: Look for changes in multi and upgradable contracts
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ The `.contract` file combines the Wasm and metadata into one file and can be use
4. Run each example with `pnpm <contract-example-name>`. e.g. `pnpm flipper`
5. Visit [http://localhost:5173](http://localhost:5173) in your browser.

### Commands

* `pnpm flipper`
* `pnpm incrementer`
* `pnpm contract_transfer`

All examples are built with [useink](https://use.ink/frontend/overview), a React hooks library built by the ink! team.

## License
Expand Down
21 changes: 21 additions & 0 deletions basic-contract-caller/frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Logs
logs
*.log
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
4 changes: 4 additions & 0 deletions basic-contract-caller/frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Have Questions?

For any questions about building front end applications with [useink](https://use.ink/frontend/overview/), join the [Element chat](https://matrix.to/#/%23useink:parity.io).

13 changes: 13 additions & 0 deletions basic-contract-caller/frontend/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/logo.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ink! Examples</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
30 changes: 30 additions & 0 deletions basic-contract-caller/frontend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "basic_contract_caller",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"ui": "workspace:ui@*"
},
"devDependencies": {
"@types/react": "^18.0.37",
"@types/react-dom": "^18.0.11",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"@vitejs/plugin-react": "^4.0.0",
"autoprefixer": "^10.4.14",
"eslint": "^8.38.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.4",
"postcss": "^8.4.24",
"tailwindcss": "^3.3.2",
"typescript": "^5.0.2",
"vite": "^4.3.9"
}
}
6 changes: 6 additions & 0 deletions basic-contract-caller/frontend/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
1 change: 1 addition & 0 deletions basic-contract-caller/frontend/public/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions basic-contract-caller/frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Other } from './Other';
import basicContractMetadata from './assets/basic_contract_caller.json';
import { BASIC_CONTRACT_ROCOCO_ADDRESS } from './constants';
import { useEffect } from 'react';
import { Button, Card, ConnectButton, InkLayout } from 'ui';
import { useCall, useContract, useTx, useWallet } from 'useink';
import { useTxNotifications } from 'useink/notifications';
import { isPendingSignature, pickDecoded, shouldDisable } from 'useink/utils';

function App() {
const { account } = useWallet();
const basicContract = useContract(
BASIC_CONTRACT_ROCOCO_ADDRESS,
basicContractMetadata,
);
const flipAndGet = useTx<boolean>(basicContract, 'flipAndGet');
useTxNotifications(flipAndGet);

const other = useCall<string>(basicContract, 'other');

useEffect(() => {
other?.send([], { defaultCaller: true });
}, [other.send]);

const otherAddress = pickDecoded(other.result);

return (
<InkLayout
className='md:py-12 md:p-6 p-4 h-screen flex items-center justify-center'
animationSrc='https://raw.githubusercontent.com/paritytech/ink-workshop/d819d10a35b2ac3d2bff4f77a96701a527b3ad3a/frontend/public/dark-sea-creatures.json'
>
<div className='flex flex-col md:flex-row md:justify-center justify-start items-stretch gap-3'>
<Card className='p-6 flex flex-col w-full max-w-md backdrop-blur-sm bg-opacity-70'>
<h1 className='text-2xl font-bold'>
{basicContractMetadata.contract.name.toUpperCase()}
</h1>

{account ? (
<Button
disabled={shouldDisable(flipAndGet)}
onClick={() => flipAndGet.signAndSend()}
className='mt-6'
>
{isPendingSignature(flipAndGet)
? 'Please sign transaction...'
: shouldDisable(flipAndGet)
? 'Flipping...'
: 'Flip Other Contract'}
</Button>
) : (
<ConnectButton className='mt-6' />
)}
</Card>

{otherAddress && <Other address={otherAddress} />}
</div>
</InkLayout>
);
}

export default App;
7 changes: 7 additions & 0 deletions basic-contract-caller/frontend/src/Global.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

body {
background-color: #1A1452;
}
30 changes: 30 additions & 0 deletions basic-contract-caller/frontend/src/Other.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import otherContractMetadata from './assets/other_contract.json';
import { Card } from 'ui';
import { useCallSubscription, useContract } from 'useink';
import { pickDecoded } from 'useink/utils';

interface Props {
address: string;
}

export const Other: React.FC<Props> = ({ address }) => {
const otherContract = useContract(address, otherContractMetadata);
const getOtherSub = useCallSubscription<boolean>(otherContract, 'get', [], {
defaultCaller: true,
});

return (
<Card className='p-6 flex flex-col w-full max-w-md backdrop-blur-sm bg-opacity-70'>
<h1 className='text-2xl font-bold'>
{otherContractMetadata.contract.name.toUpperCase()}
</h1>

<p className='mt-6'>
Flipped:{' '}
<b className='uppercase'>
{pickDecoded(getOtherSub.result)?.toString()}
</b>
</p>
</Card>
);
};
Loading
Loading