Skip to content

Commit f8eee14

Browse files
committed
Initial commit
0 parents  commit f8eee14

31 files changed

+15788
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.DS_Store
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
.npm
8+
.eslintcache
9+
.yarn-integrity
10+
11+
node_modules/

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Phil Li
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# MesonTo
2+
3+
The blockchain world has entered a period of multi-chain coexistence. For web3 app developers, they often encounter a problem: they need to deploy smart contracts on a specific chain, but this will greatly limit them to attract assets and users on other chains.
4+
5+
*MesonTo* has provided a solution for those web3 developers. [Meson](https://meson.fi) is a safe, costless and instant cross-chain protocol for stablecoins. MesonTo is an integratable sdk supported meson cross-chain protocol. With MesonTo, web3 applications can allow their users to transfer stablecoins from any chain directly to their smart contracts.
6+
7+
Get a feel for how MesonTo works with this [demo](https://demo.meson.to).
8+
9+
## How to integrate MesonTo
10+
11+
### Frontend
12+
13+
Add MesonTo to your frontend project through `npm i -S @mesonfi/to` or `yarn add @mesonfi/to`. Then import `MesonTo` by
14+
15+
```js
16+
import MesonTo from '@mesonfi/to'
17+
```
18+
19+
You can also add it directly to the html file
20+
21+
```html
22+
<script src="./meson-to.js"></script>
23+
```
24+
25+
Once the script is loaded, you will be able to use `MesonTo` globally.
26+
27+
To open the popup of MesonTo for cross-chain transfer, run
28+
29+
```js
30+
const appId = 'example'
31+
const meson2 = new MesonTo(window)
32+
meson2.open(appId)
33+
.then(() => {
34+
// on popup closed; doesn't mean a cross-chain transfer is completed
35+
})
36+
37+
meson2.onCompleted(data => {
38+
// when a cross-chain transfer is successful
39+
console.log(data)
40+
})
41+
```
42+
43+
There are complete examples of how to use MesonTo in the `examples` folder.
44+
45+
### Smart contract
46+
47+
The cross-chain'ed stablecoin will be transferred to the app contract from meson's contract. In order to accept the transfer correctly, your app contract needs to implement the `transferWithBeneficiary` method.
48+
49+
```solidity
50+
function transferWithBeneficiary(address token, uint256 amount, address beneficiary, uint64 data) external returns (bool);
51+
```
52+
53+
This will allow the app contract to obtain depositing tokens from meson's contract, but transfer the corresponding benefits (purchased NFTs, exchanged tokens, etc) to the user's address.
54+
55+
See an example in `contracts/SampleAppContract.sol`.
56+
57+
## Submit app information
58+
59+
In order to keep users' funds safe, we need to confirm that your contract has correctly supported `transferWithBeneficiary`. Otherwise, users' cross-chain assets may be stuck and cannot be withdrawn.
60+
61+
Once `transferWithBeneficiary` is supported, you can [contact us]() to add your app to the MesonTo. Please have the following information ready
62+
63+
- App name
64+
- Prefered appId (popup will open at https://meson.to/{appId})
65+
- App URL (need to open popup from it in *release* mode)
66+
- App icon (w144 x h144)
67+
- App logo (height 144, width <= 1080)
68+
- Smart contract info (deployed chain, supported types of stablecoins, contract address)
69+
- A short paragraph informs the user of the effect of the cross-chain transfer and guides the user to complete the operation
70+
71+
We will review and input your app's information within 24 hours. After that, you can use `meson2.open(yourAppId)` to finish cross-chain transfers for your own app!
72+
73+
## Dev mode & release mode
74+
75+
In dev mode, MesonTo popup can be opened from any hostname, but it only allows cross-chain transfers under $5. Note that dev mode also runs on mainnet.
76+
77+
After the app is launched, please switch to release mode. In release mode, MesonTo can only be opened from your app URL.

babel.config.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
plugins: [
3+
["@babel/plugin-transform-modules-umd", {
4+
exactGlobals: true,
5+
globals: {
6+
index: 'MesonTo'
7+
}
8+
}]
9+
],
10+
presets: [
11+
[
12+
'@babel/preset-env',
13+
{
14+
targets: {
15+
node: 'current',
16+
},
17+
},
18+
],
19+
],
20+
};

contracts/IERC20Minimal.sol

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.6;
3+
4+
interface IERC20Minimal {
5+
function transfer(address recipient, uint256 amount) external returns (bool);
6+
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
7+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.6;
3+
4+
/// @title Interface for transferWithBeneficiary
5+
interface ITransferWithBeneficiary {
6+
/// @notice Make a token transfer that the *signer* is paying tokens but benefits are given to the *beneficiary*
7+
/// @param token The contract address of the transferring token
8+
/// @param amount The amount of the transfer
9+
/// @param beneficiary The address that will receive benefits of this transfer
10+
/// @param data Extra data passed to the contract
11+
/// @return Returns true for a successful transfer.
12+
function transferWithBeneficiary(address token, uint256 amount, address beneficiary, uint64 data) external returns (bool);
13+
}

contracts/SampleAppContract.sol

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.6;
3+
4+
import "./IERC20Minimal.sol";
5+
import "./ITransferWithBeneficiary.sol";
6+
7+
/// @notice A sample of 3rd-party dapp that interacts with meson
8+
/// With `transferWithBeneficiary`, the meson contract will be able
9+
/// to deposit cross-chain'ed stablecoins to the 3rd-party dapp contract
10+
/// on behalf of the user. The user will receive the benefits corresponding
11+
/// to this deposit.
12+
contract SampleThirdPartyDapp is ITransferWithBeneficiary {
13+
function transferWithBeneficiary(
14+
address token,
15+
uint256 amount,
16+
address beneficiary,
17+
uint64 data
18+
) external override returns (bool) {
19+
// Required. Take cross-chain'ed token to dapp's contract
20+
IERC20Minimal(token).transferFrom(msg.sender, address(this), amount);
21+
22+
23+
// The dapp can do it's own logic with depositing tokens
24+
// e.g. exchange for other tokens, mint NFTs, etc.
25+
// Could use data to determine specific operations.
26+
27+
28+
// Send benefits to the user. Here as an example that we just
29+
// transfer deposited tokens to the user.
30+
IERC20Minimal(token).transfer(beneficiary, amount);
31+
32+
return true;
33+
}
34+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# An example app using MesonTo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "meson-to-example-react",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@mesonfi/to": "^0.1.0-rc.1",
7+
"@testing-library/jest-dom": "^5.14.1",
8+
"@testing-library/react": "^13.0.0",
9+
"@testing-library/user-event": "^13.2.1",
10+
"classnames": "^2.3.1",
11+
"react": "^18.2.0",
12+
"react-dom": "^18.2.0",
13+
"react-scripts": "5.0.1",
14+
"web-vitals": "^2.1.0"
15+
},
16+
"scripts": {
17+
"start": "react-scripts start",
18+
"build": "react-scripts build",
19+
"test": "react-scripts test",
20+
"eject": "react-scripts eject"
21+
},
22+
"eslintConfig": {
23+
"extends": [
24+
"react-app",
25+
"react-app/jest"
26+
]
27+
},
28+
"browserslist": {
29+
"production": [
30+
">0.2%",
31+
"not dead",
32+
"not op_mini all"
33+
],
34+
"development": [
35+
"last 1 chrome version",
36+
"last 1 firefox version",
37+
"last 1 safari version"
38+
]
39+
}
40+
}
Binary file not shown.
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="en" class="w-full h-full">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta name="description" content="An example web3 app using meson.to" />
9+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
10+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
11+
<title>Web3 App</title>
12+
13+
<script src="https://cdn.tailwindcss.com"></script>
14+
</head>
15+
<body class="w-full h-full">
16+
<noscript>You need to enable JavaScript to run this app.</noscript>
17+
<div id="root" class="w-full h-full"></div>
18+
</body>
19+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
},
10+
{
11+
"src": "icon192.png",
12+
"type": "image/png",
13+
"sizes": "192x192"
14+
},
15+
{
16+
"src": "icon512.png",
17+
"type": "image/png",
18+
"sizes": "512x512"
19+
}
20+
],
21+
"start_url": ".",
22+
"display": "standalone",
23+
"theme_color": "#000000",
24+
"background_color": "#ffffff"
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://www.robotstxt.org/robotstxt.html
2+
User-agent: *
3+
Disallow:
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import React from 'react'
2+
import classnames from 'classnames'
3+
4+
import MesonTo from '@mesonfi/to'
5+
6+
export default function App() {
7+
const [pending, setPending] = React.useState(false)
8+
const [received, setReceived] = React.useState('')
9+
10+
const meson2 = React.useMemo(() => new MesonTo(window), [])
11+
12+
React.useEffect(() => {
13+
meson2.onCompleted(data => {
14+
setReceived(`Received ${data.amount / 1e6} ${data.from.token} from ${data.fromAddress} of ${data.from.chain}.`)
15+
})
16+
}, [])
17+
18+
const onClick = React.useCallback(() => {
19+
setPending(true)
20+
meson2.open('example').then(() => {
21+
setPending(false)
22+
})
23+
}, [])
24+
25+
return (
26+
<div className='w-full h-full bg-indigo-50'>
27+
<header className='flex flex-row items-center w-full px-6 py-2'>
28+
<img className='h-8 mr-2' src='/icon192.png' />
29+
<div>
30+
<div className='text-lg'>Web3 Application</div>
31+
<div className='text-xs font-light text-gray'>
32+
An example to show deposit with meson
33+
</div>
34+
</div>
35+
</header>
36+
<div className='flex flex-col items-center'>
37+
<div className='bg-white mt-24 pt-12 pb-4 px-4 text-center sm:px-12'>
38+
<h2 className='tracking-tight text-gray-900'>
39+
<div className='font-bold text-2xl mb-2'>
40+
Ready to dive in?
41+
</div>
42+
<div className='block text-base'>
43+
Experience <b>Web 3.0</b> with stablecoins.
44+
</div>
45+
</h2>
46+
<div className='mt-8 flex justify-center'>
47+
<button
48+
className={classnames(
49+
'items-center justify-center rounded-md px-3 py-2 sm:px-4 sm:py-3',
50+
'font-medium text-white bg-indigo-600 hover:bg-indigo-700'
51+
)}
52+
>
53+
Get started
54+
</button>
55+
<button
56+
className={classnames(
57+
'ml-3',
58+
'items-center justify-center rounded-md px-3 py-2 sm:px-4 sm:py-3',
59+
'font-medium bg-emerald-100',
60+
pending ? 'opacity-60 text-emerald-800' : 'hover:bg-emerald-200 text-emerald-600'
61+
)}
62+
onClick={onClick}
63+
disabled={pending}
64+
>
65+
{pending ? 'Waiting for meson...' : 'Deposit with meson'}
66+
</button>
67+
</div>
68+
<div className='mt-2 sm:mt-6'>{received}</div>
69+
</div>
70+
</div>
71+
</div>
72+
)
73+
}

0 commit comments

Comments
 (0)