Skip to content

Commit

Permalink
feat: Add workshop snippets file & Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
wottpal committed Dec 4, 2023
1 parent df8ec1f commit 14e8e11
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 25 deletions.
6 changes: 6 additions & 0 deletions .changeset/tiny-wombats-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@inkathon/frontend': patch
'@inkathon/contracts': patch
---

Add sample code snippets from live workshops (greeter message reversion & make-it-rain script)
6 changes: 0 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,4 @@ coverage
coverage.json

# VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-workspace
.history/
109 changes: 109 additions & 0 deletions .vscode/workshop.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"Workshop Contracts Snippet #1": {
"scope": "rust",
"prefix": "Workshop Contracts Snippet #1",
"body": [
"/// Reverses and stores the `message` string",
"#[ink(message)]",
"pub fn reverse_message(&mut self) {",
" self.message = self.message.chars().rev().collect::<String>();",
"}"
]
},
"Workshop Contracts Snippet #2": {
"scope": "rust",
"prefix": "Workshop Contracts Snippet #2",
"body": [
"#[ink::test]",
"fn reverse_message_works() {",
" let message_1 = String::from(\"gm ink!\");",
" let message_1_reversed = String::from(\"!kni mg\");",
" let mut greeter = Greeter::new(message_1.clone());",
" assert_eq!(greeter.greet(), message_1);",
" greeter.reverse_message();",
" assert_eq!(greeter.greet(), message_1_reversed);",
"}"
]
},
"Workshop Frontend Snippet #1": {
"scope": "typescriptreact",
"prefix": "Workshop Frontend Snippet #1",
"body": [
"// Reverse Greeting",
"const reverseGreeting = async () => {",
" if (!activeAccount || !contract || !activeSigner || !api) {",
" toast.error('Wallet not connected. Try again…')",
" return",
" }",
"",
" setUpdateIsLoading(true)",
" try {",
" await contractTxWithToast(api, activeAccount.address, contract, 'reverseMessage', {}, [])",
" } catch (e) {",
" console.error(e)",
" } finally {",
" setUpdateIsLoading(false)",
" await fetchGreeting()",
" }",
"}",
]
},
"Workshop Frontend Snippet #2": {
"scope": "typescriptreact",
"prefix": "Workshop Frontend Snippet #2",
"body": [
"{/* Reverse Greeting */}",
"<Card>",
" <CardContent className=\"pt-6\">",
" <Button",
" type=\"button\"",
" onClick={reverseGreeting}",
" className=\"w-full bg-primary font-bold\"",
" disabled={fetchIsLoading || updateIsLoading}",
" isLoading={updateIsLoading}",
" >",
" Submit",
" </Button>",
" </CardContent>",
"</Card>",
]
}
"Workshop Scripts Snippet #1": {
"scope": "typescript",
"prefix": "Workshop Scripts Snippet #1",
"body": [
"import { transferBalance } from '@scio-labs/use-inkathon'",
"import { initPolkadotJs } from './utils/initPolkadotJs'",
"",
"/**",
" * Example script that transfers tokens to the passed address from Alice.",
" *",
" * Parameters:",
" * - `ACCOUNT_URI`: Sender address URI (i.e. `//Alice`)",
" * - `ADDRESS`: Receiver address",
" * - `AMOUNT`: Token amount to transfer (optional, defaults to `100`)",
" * - `CHAIN`: Chain ID (optional, defaults to `development`)",
" *",
" * Example usage:",
" * - `ADDRESS=5fei… pnpm run script make-it-rain`",
" */",
"const main = async () => {",
" const { api, account, toBNWithDecimals } = await initPolkadotJs()",
"",
" const receiverAddress = process.env.ADDRESS",
" if (!receiverAddress) throw new Error('Missing `ADDRESS` variable')",
" const tokenAmount = toBNWithDecimals(process.env.AMOUNT ? parseInt(process.env.AMOUNT) : 100)",
"",
" console.log(`\nTransferring ${tokenAmount} tokens to ${receiverAddress}…`)",
" await transferBalance(api, account, receiverAddress, tokenAmount)",
"}",
"",
"main()",
" .catch((error) => {",
" console.error(error)",
" process.exit(1)",
" })",
" .finally(() => process.exit(0))",
]
}
}
39 changes: 20 additions & 19 deletions frontend/src/components/web3/greeter-contract-interactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,26 @@ export const GreeterContractInteractions: FC = () => {
{/* Update Greeting */}
<Card>
<CardContent className="pt-6">
<form onSubmit={handleSubmit(updateGreeting)}>
<div className="flex flex-col justify-end gap-2">
<FormItem>
<FormLabel className="text-base">Update Greeting</FormLabel>
<FormControl>
<div className="flex gap-2">
<Input disabled={updateIsLoading} {...register('newMessage')} />
<Button
className="bg-primary font-bold"
disabled={fetchIsLoading || updateIsLoading}
isLoading={updateIsLoading}
type="submit"
>
Submit
</Button>
</div>
</FormControl>
</FormItem>
</div>
<form
onSubmit={handleSubmit(updateGreeting)}
className="flex flex-col justify-end gap-2"
>
<FormItem>
<FormLabel className="text-base">Update Greeting</FormLabel>
<FormControl>
<div className="flex gap-2">
<Input disabled={updateIsLoading} {...register('newMessage')} />
<Button
type="submit"
className="bg-primary font-bold"
disabled={fetchIsLoading || updateIsLoading}
isLoading={updateIsLoading}
>
Submit
</Button>
</div>
</FormControl>
</FormItem>
</form>
</CardContent>
</Card>
Expand Down

1 comment on commit 14e8e11

@vercel
Copy link

@vercel vercel bot commented on 14e8e11 Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.