Skip to content

Commit

Permalink
initial commit with upload CSV button working
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith committed Nov 27, 2020
0 parents commit 66980a8
Show file tree
Hide file tree
Showing 21 changed files with 12,096 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PORT=3000
HTTPS=true
REACT_APP_INFURA_ID=
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# My Safe App

## Getting Started

Install dependencies and start a local dev server.

```
yarn install
cp .env.sample .env
yarn start
```

Then:

- If HTTPS is used (by default enabled)
- Open your Safe app locally (by default via https://localhost:3000/) and accept the SSL error.
- Go to Safe Multisig web interface
- [Mainnet](https://app.gnosis-safe.io)
- [Rinkeby](https://rinkeby.gnosis-safe.io/app)
- Create your test safe
- Go to Apps -> Manage Apps -> Add Custom App
- Paste your localhost URL, default is https://localhost:3000/
- You should see Safe App Starter as a new app
- Develop your app from there

## Features

Gnosis Safe App Starter combines recommendations described in the following repositories:

- [Safe Apps SDK](https://github.com/gnosis/safe-apps-sdk)
- [safe-react-components](https://github.com/gnosis/safe-react-components)

You can use the `useSafe` React hook to interact with the Safe Apps SDK

```
const safe = useSafe();
console.log(safe.info);
```

Safe React Components are also integrated and ready to use. [See all components](https://components.gnosis-safe.io/).

## Dependencies

### Included
- [`@gnosis.pm/safe-react-components`](https://github.com/gnosis/safe-react-components) (UI components themed for the Safe Multisig interface)
- [`@rmeissner/safe-apps-react-sdk`](https://github.com/rmeissner/safe-sdks-js/tree/master/safe-apps-react-sdk) (React hook for the Safe Apps SDK)

### Recommended
- [`ethers`](https://github.com/ethers-io/ethers.js) (Library for interacting with Ethereum)
- [`web3`](https://github.com/ethereum/web3.js/) (Library for interacting with Ethereum)
- [`@studydefi/money-legos`](https://github.com/studydefi/money-legos) (Library for DeFi interactions)
25 changes: 25 additions & 0 deletions config-overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
webpack: function (config, env) {
return config;
},
jest: function (config) {
return config;
},
devServer: function (configFunction) {
return function (proxy, allowedHost) {
const config = configFunction(proxy, allowedHost);

config.headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers":
"X-Requested-With, content-type, Authorization",
};

return config;
};
},
paths: function (paths, env) {
return paths;
},
};
133 changes: 133 additions & 0 deletions example.csv

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "my-safe-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@gnosis.pm/safe-apps-sdk": "^0.3.1",
"@gnosis.pm/safe-react-components": "^0.2.0",
"@material-ui/core": "^4.11.0",
"@rmeissner/safe-apps-react-sdk": "0.4.0",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^10.4.9",
"@testing-library/user-event": "^12.1.3",
"@types/jest": "^26.0.10",
"@types/node": "^14.6.2",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/styled-components": "^5.1.2",
"bignumber.js": "^9.0.1",
"csv-parser": "^2.3.3",
"fast-csv": "^4.3.5",
"react": "^16.13.1",
"react-app-rewired": "^2.1.6",
"react-csv-reader": "^3.1.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.3",
"styled-components": "^5.1.1",
"typescript": "~4.0.2"
},
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"plugin:jsx-a11y/recommended"
],
"plugins": [
"jsx-a11y"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"homepage": "./"
}
Binary file added public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>Gnosis Safe App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
7 changes: 7 additions & 0 deletions 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.
Binary file added public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/logo512.png
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 public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"short_name": "Safe Airdrop",
"name": "Gnosis Safe CSV Airdrop",
"description": "A simple Gnosis Safe app used to upload CSV transfer files to send aribitrarily many distinct tokens to arbitrarily many recipients",
"iconPath": "logo.svg",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
149 changes: 149 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/** @format */
import { Transaction } from "@gnosis.pm/safe-apps-sdk";
import BigNumber from "bignumber.js";
import React, { useCallback, useState } from "react";
import styled from "styled-components";
import { Button, Loader, Title } from "@gnosis.pm/safe-react-components";
import { useSafe } from "@rmeissner/safe-apps-react-sdk";
import { parseString } from "@fast-csv/parse";

const Container = styled.form`
margin-bottom: 2rem;
width: 100%;
max-width: 480px;
display: grid;
grid-template-columns: 1fr;
grid-column-gap: 1rem;
grid-row-gap: 1rem;
`;

interface SnakePayment {
receiver: string;
amount: string;
token_address: string;
}
interface Payment {
receiver: string;
amount: BigNumber;
tokenAddress: string;
}

const App: React.FC = () => {
const safe = useSafe();
const [submitting, setSubmitting] = useState(false);
const [transferContent, setTransferContent] = useState<Payment[]>([]);

const onChangeHandler = async (event: any) => {
console.log("Received Filename", event.target.files[0].name);

const reader = new FileReader();
const filePromise = new Promise<SnakePayment[]>((resolve, reject) => {
reader.onload = function (evt) {
if (!evt.target) {
return;
}
// Parse CSV
const results: any[] = [];
parseString(evt.target.result as string, { headers: true })
.on("data", (data) => results.push(data))
.on("end", () => resolve(results))
.on("error", (error) => reject(error));
};
});

reader.readAsText(event.target.files[0]);
const parsedFile = await filePromise;

const results: Payment[] = parsedFile
.map(({ amount, receiver, token_address }) => ({
amount: new BigNumber(amount),
receiver,
tokenAddress: token_address,
}))
.filter((payment) => !payment.amount.isZero());
setTransferContent(results);
};

const submitTx = useCallback(async () => {
setSubmitting(true);
try {
// const safeTxHash = await safe.sendTransactions([
// {
// to: safe.info.safeAddress,
// value: "0",
// data: "0x",
// },
// {
// to: safe.info.safeAddress,
// value: "0",
// data: "0x",
// },
// ]);
const txList: Transaction[] = transferContent.map((transfer) => {
return {
to: transfer.tokenAddress,
value: "0",
data: "0x",
};
});
const safeTxHash = await safe.sendTransactions(txList);
console.log({ safeTxHash });
const safeTx = await safe.loadSafeTransaction(safeTxHash);
console.log({ safeTx });
} catch (e) {
console.error(e);
}
setSubmitting(false);
}, [safe]);

return (
<Container>
<Title size="md">{safe.info.safeAddress}</Title>

<input type="file" name="file" onChange={onChangeHandler} />

<table>
<thead>
<tr>
<td>Token</td>
<td>Receiver</td>
<td>Amount</td>
</tr>
</thead>
<tbody>
{transferContent.map((row, index) => {
return (
<tr key={index}>
<td>{row.tokenAddress}</td>
<td>{row.receiver}</td>
<td>{row.amount.toString(10)}</td>
</tr>
);
})}
</tbody>
</table>
{submitting ? (
<>
<Loader size="md" />
<br />
<Button
size="lg"
color="secondary"
onClick={() => {
setSubmitting(false);
}}
>
Cancel
</Button>
</>
) : (
<Button size="lg" color="primary" onClick={submitTx}>
Submit
</Button>
)}
</Container>
);
};

export default App;
Loading

0 comments on commit 66980a8

Please sign in to comment.