Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Feat: Implement network switch in the front-end #15

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
# and commit this file to your remote git repository to share the goodness with others.

tasks:
- init: yarn install


9 changes: 9 additions & 0 deletions assets/cloud/addresses.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ const vaultAddresses = [
"0x31597c64f04a723823F96A1654417eb6A1c50885",
"0xE7ab1839Cd96D34D38552944CC79570Ce8D098D3"
]}
];

const spreadAddresses = [
{"polygon": [
"0x87945Ea3BDCe665461348EA8AfE0b07b0e4E121F"
]},
{"ethereum": [
"0x87945Ea3BDCe665461348EA8AfE0b07b0e4E121F"
]}
];
159 changes: 159 additions & 0 deletions assets/js/spread/ABI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
const ABI = [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "Error_1",
"type": "error"
},
{
"inputs": [],
"name": "Error_2",
"type": "error"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "previousOwner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "OwnershipTransferred",
"type": "event"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "renounceOwnership",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "saveAsset",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "contract IERC20",
"name": "token",
"type": "address"
}
],
"name": "saveERC20",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address[]",
"name": "recipients",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "values",
"type": "uint256[]"
}
],
"name": "spreadAsset",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "contract IERC20",
"name": "token",
"type": "address"
},
{
"internalType": "address[]",
"name": "recipients",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "values",
"type": "uint256[]"
}
],
"name": "spreadERC20",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "contract IERC20",
"name": "token",
"type": "address"
},
{
"internalType": "address[]",
"name": "recipients",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "values",
"type": "uint256[]"
}
],
"name": "spreadERC20Simple",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "transferOwnership",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"stateMutability": "payable",
"type": "receive"
}
]

export { ABI }
46 changes: 24 additions & 22 deletions assets/js/spread/app.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
// FIXME: login.
let user = Moralis.User.current();
if(!user) (Moralis.authenticate().then((user) => console.log(user)))();
// FIXME: logout.
const logOut = async () => await Moralis.User.logOut();
import { setNetworkId, getNetworkData, getAssets, networkSwitch } from "./utils.js";

let selectedChain = document.querySelector("#selectedChain");
let token, logOut;

// Auto log in with metamask and get native assets and token balances.
(async function () {
const web3Provider = await Moralis.enableWeb3();
const network = await web3Provider.getNetwork();

let response = await fetch("https://chainid.network/chains.json");
let data = await response.json();
await setNetworkId();

await getNativeAsset(network, data);
await getERC20Assets();
let user = Moralis.User.current();
if(!user) (Moralis.authenticate().then((user) => console.log(user)))();
logOut = async () => await Moralis.User.logOut();

selectAssets.addEventListener("change", (event) => token = event.target.value);
await Moralis.enableWeb3();
let networkData = await getNetworkData();
await getAssets(networkData[0], networkData[1]);
})()

async function spread() {
const web3Provider = await Moralis.enableWeb3();
const network = await web3Provider.getNetwork();
// event listeners,
// listens when user selects chain in front-end
selectedChain.addEventListener("change", async (event) => {
let chain = event.target.value;
await networkSwitch(chain);

let networkData = await getNetworkData();
await getAssets(networkData[0], networkData[1]);
});

// listens when user selects a token in front-end
selectAssets.addEventListener("change", (event) => token = event.target.value);

if(selectAssets.value === "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") sendNativeAsset(network);
else if(selectAssets.value === "") {
alert("Please select an asset to spread");
throw "no selected asset";
} else sendErc20(token, network);
}
export { token }
65 changes: 0 additions & 65 deletions assets/js/spread/elements.js

This file was deleted.

Loading