Skip to content

Commit

Permalink
Merge pull request #9 from oasisprotocol/matevz/contracts/require-owner
Browse files Browse the repository at this point in the history
contracts: Basic CI and require message owner
  • Loading branch information
matevz authored Mar 6, 2024
2 parents e9e8071 + 26b49a9 commit 1755c42
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 306 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/ci-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: ci-test

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test-backend-frontend:
name: test-backend-frontend
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 18

- uses: pnpm/[email protected]
name: Install pnpm
id: pnpm-install
with:
version: 8
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- uses: actions/cache@v4
name: Setup pnpm cache
with:
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
working-directory: backend
run: pnpm install

- name: Build backend
working-directory: backend
run: pnpm build

- name: Test backend
working-directory: backend
run: pnpm test

- name: Build frontend
working-directory: frontend
run: pnpm build
11 changes: 9 additions & 2 deletions backend/contracts/MessageBox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
pragma solidity ^0.8.0;

contract MessageBox {
string public message;
string private _message;
address public author;

function setMessage(string calldata in_message) external {
message = in_message;
_message = in_message;
author = msg.sender;
}

function message() external view returns (string memory) {
if (msg.sender!=author) {
revert("not allowed");
}
return _message;
}
}
4 changes: 2 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
},
"devDependencies": {
"@nomicfoundation/hardhat-ethers": "^3.0.5",
"@oasisprotocol/sapphire-contracts": "^0.2.6",
"@oasisprotocol/sapphire-hardhat": "^2.16.1",
"@oasisprotocol/sapphire-contracts": "^0.2.7",
"@oasisprotocol/sapphire-hardhat": "^2.19.4",
"@typechain/ethers-v6": "^0.5.1",
"@typechain/hardhat": "^9.1.0",
"@types/chai": "^4.3.4",
Expand Down
2 changes: 1 addition & 1 deletion backend/test/MessageBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("MessageBox", function () {
return { messageBox };
}

it("Should send message", async function () {
it("Should set message", async function () {
const {messageBox} = await deployMessageBox();

await messageBox.setMessage("hello world");
Expand Down
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@metamask/detect-provider": "^2.0.0",
"@metamask/jazzicon": "^2.0.0",
"@oasisprotocol/demo-starter-backend": "workspace:^",
"@oasisprotocol/sapphire-paratime": "^1.2.2",
"@oasisprotocol/sapphire-paratime": "^1.3.2",
"ethers": "^6.10.0",
"pinia": "^2.0.28",
"vue": "^3.2.45",
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { type MessageBox, MessageBox__factory } from '@oasisprotocol/demo-starte
export type { MessageBox } from '@oasisprotocol/demo-starter-backend';

import { useEthereumStore } from './stores/ethereum';
import { VoidSigner } from 'ethers';
import { type ContractRunner, VoidSigner } from 'ethers';

const addr = import.meta.env.VITE_MESSAGE_BOX_ADDR!;

Expand All @@ -23,7 +23,7 @@ export function useMessageBox(): ComputedRef<MessageBox | null> {
return null;
}

return MessageBox__factory.connect(addr, eth.signer);
return MessageBox__factory.connect(addr, eth.signer as ContractRunner);
});
}

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/stores/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function networkName(network?: Network): string {

declare global {
interface Window {
ethereum?: BrowserProvider & Eip1193Provider & sapphire.SapphireAnnex;
ethereum: BrowserProvider & Eip1193Provider & sapphire.SapphireAnnex;
}
}

Expand Down Expand Up @@ -91,7 +91,7 @@ export const useEthereumStore = defineStore('ethereum', () => {
throw new MetaMaskNotInstalledError('MetaMask not installed!');
}

return ethProvider;
return ethProvider as unknown as typeof window.ethereum;
}

async function init(addr: string, eth: Eip1193Provider) {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/views/HomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function handleError(error: Error, errorMessage: string) {
}
async function fetchMessage(): Promise<Message> {
const message = await uwMessageBox.value!.message();
const author = await uwMessageBox.value!.author();
const message = await messageBox.value!.message();
const author = await messageBox.value!.author();
return { message, author };
}
Expand Down
Loading

0 comments on commit 1755c42

Please sign in to comment.