Skip to content

Commit

Permalink
make array of batch operations
Browse files Browse the repository at this point in the history
  • Loading branch information
letier3110 committed May 1, 2022
1 parent af44897 commit 2730fb9
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 72 deletions.
62 changes: 41 additions & 21 deletions src/components/Distribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,37 @@ export const Distribute = () => {
};

const handleDistributeForm = () =>
makeRpcCall({ asset, assetId, amount, receiver, deadline });
makeRpcCall([{ asset, assetId, amount, receiver, deadline }]);

const handleDistributeJSON = () => {
if (errorJSON) {
return;
}
makeRpcCall(JSON.parse(value));
const params = JSON.parse(value);
if (Array.isArray(params)) {
makeRpcCall(params);
} else {
makeRpcCall([params]);
}
};

const makeRpcCall = async ({
asset,
assetId,
amount,
receiver,
deadline,
}) => {
const makeRpcCall = async (opParams) => {
try {
let batchOp = Tezos.wallet.batch();
for (var i = 0; i < opParams.length; i++) {
batchOp = await makeParams(opParams[i], batchOp);
}
console.log(opParams);
batchOp.send();
} catch (e) {
console.log("error in makeRpcCall", e);
}
};

const makeParams = async (
{ asset, assetId, amount, receiver, deadline },
batchOp
) => {
const isFa2 = !!assetId || assetId === "0";
const isTez = asset === "tz";
const assetParam = isFa2
Expand All @@ -72,7 +87,6 @@ export const Distribute = () => {
};
const vestingParams = contract.methodsObject.start_vesting(tx_prm);
try {
let batchOp;
if (isFa2) {
const tokenContract = await Tezos.contract.at(asset);
const addOperatorParams = tokenContract.methods.update_operators([
Expand All @@ -93,34 +107,29 @@ export const Distribute = () => {
},
},
]);
batchOp = Tezos.wallet
.batch()
batchOp = batchOp
.withContractCall(addOperatorParams)
.withContractCall(vestingParams)
.withContractCall(removeOperatorParams)
.send();
.withContractCall(removeOperatorParams);
} else if (isTez) {
const vestingParamsTez = contract.methodsObject
.start_vesting({
...tx_prm,
amount: new BigNumber(amountParam).shiftedBy(6),
})
.toTransferParams({ amount: amountParam });
batchOp = Tezos.wallet.batch().withTransfer(vestingParamsTez).send();
batchOp = batchOp.withTransfer(vestingParamsTez);
} else {
const tokenContract = await Tezos.contract.at(asset);
const approveParams = tokenContract.methods.approve(
contractAddress,
amount
);
batchOp = Tezos.wallet
.batch()
batchOp = batchOp
.withContractCall(approveParams)
.withContractCall(vestingParams)
.send();
.withContractCall(vestingParams);
}
await batchOp;
await batchOp.confirmation();
return batchOp;
} catch (e) {
console.log(e);
}
Expand Down Expand Up @@ -197,6 +206,17 @@ export const Distribute = () => {
<div className="textarea-error">{errorJSON}</div>
)}

<label>Example:</label>
<pre>{`
[{
"amount": "1234",
"asset": "KT19H9YbHqsxFTayap7aTEfbcnyPeALKYgt9",
"assetId": "0",
"deadline": "2022-05-26",
"receiver": "tz1VvDQcafAxpAcc2hFWDpSmRYqdEmEhrW1h"
}]
`}</pre>

<Button
disabled={storage && pkh !== storage.admin}
onClick={handleDistributeJSON}
Expand Down
29 changes: 3 additions & 26 deletions src/components/Explore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,12 @@ import BigNumber from "bignumber.js";
import { getTokensMetadata } from "../utils/tokenMetadata.api";
import { Button } from "./Button";
import { RefreshableButton } from "./RefreshableButton";
import { useRewards } from "../hooks/useRewards";

export const Explore = () => {
const { contract, storage, pkh, connect, Tezos } = useBeacon();
const [rewards, setRewards] = useState([]);
const { contract, pkh, connect, Tezos } = useBeacon();
const [tokens, setTokens] = useState([]);
const [loading, setLoading] = useState(true);

const loadRewards = useCallback(async () => {
if (!storage || !storage.vestings_counter || !pkh) {
setLoading(false);
return;
}
const limit = storage.vestings_counter;
let arr = [];
for (let i = 0; i < limit; i++) {
const vesting = await storage.vestings.get([i]);
arr.push(vesting);
}
if (pkh) {
// arr = arr.filter((x) => x.receiver === pkh || x.admin !== pkh); // for debug
arr = arr.filter((x) => x.receiver === pkh || x.admin === pkh);
}
setRewards(arr);
setLoading(false);
}, [storage, pkh]);
const { rewards, loadingRewards: loading, loadRewards } = useRewards();

const loadTokensMetadata = useCallback(async () => {
if (rewards.length === 0) {
Expand Down Expand Up @@ -59,10 +40,6 @@ export const Explore = () => {
setTokens(newTokens);
}, [rewards]);

useEffect(() => {
loadRewards();
}, [loadRewards]);

useEffect(() => {
loadTokensMetadata();
}, [loadTokensMetadata]);
Expand Down
1 change: 1 addition & 0 deletions src/components/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const Header = () => {
onClick={() => setCurrentTab(index)}
selected={currentTab === index}
key={index}
admin={tab.admin}
>
{tab.label}
</Tab>
Expand Down
21 changes: 18 additions & 3 deletions src/components/Tab.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
/* eslint-disable jsx-a11y/anchor-is-valid */
import React from "react";
import useBeacon from "../hooks/useBeacon";

export const Tab = ({
onClick,
selected = false,
disabled,
admin,
children,
}) => {
const { storage, pkh } = useBeacon();
const isAdmin = storage && storage.admin === pkh && admin;
const handleClick = () => {
if (onClick && !disabled) {
if (admin && !isAdmin) return false;
onClick();
}
};

export const Tab = ({ onClick, selected = false, disabled, children }) => {
const handleClick = () => onClick && !disabled && onClick();
return (
<div
className={`material-tabs-content ${
disabled && "material-tabs-disabled"
(disabled || (admin && !isAdmin)) && "material-tabs-disabled"
}`}
>
<a onClick={handleClick} className="material-tabs-content" href="#">
Expand Down
32 changes: 16 additions & 16 deletions src/hooks/useBeacon.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ import { BeaconWallet } from "@taquito/beacon-wallet";
import { MichelCodecPacker, TezosToolkit } from "@taquito/taquito";

// TESTNET
// export const contractAddress = "KT1QwVmmYNp3Ke2JkNr2eaAdYyAJFusbi7yH";

// const DEFAULT_NETWORK = {
// id: "ithacanet",
// nextNetworkIndex: 1,
// name: "Ithacanet",
// type: "testnet",
// rpcBaseURL: "https://ithacanet.smartpy.io",
// };

// MAINNET
export const contractAddress = "KT1N5HyBD5HZ7NZwmDar1LmBN7WkHbdr6zb9";
export const contractAddress = "KT1QwVmmYNp3Ke2JkNr2eaAdYyAJFusbi7yH";

const DEFAULT_NETWORK = {
id: "mainnet",
id: "ithacanet",
nextNetworkIndex: 1,
name: "Mainnet",
type: "main",
rpcBaseURL: "https://mainnet.smartpy.io",
name: "Ithacanet",
type: "testnet",
rpcBaseURL: "https://ithacanet.smartpy.io",
};

// MAINNET
// export const contractAddress = "KT1N5HyBD5HZ7NZwmDar1LmBN7WkHbdr6zb9";

// const DEFAULT_NETWORK = {
// id: "mainnet",
// nextNetworkIndex: 1,
// name: "Mainnet",
// type: "main",
// rpcBaseURL: "https://mainnet.smartpy.io",
// };

class LambdaViewSigner {
async publicKeyHash() {
const acc = await wallet.client.getActiveAccount();
Expand Down
39 changes: 39 additions & 0 deletions src/hooks/useRewards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useCallback, useState, useEffect } from "react";
import constate from "constate";

import useBeacon from "./useBeacon";

export const [UseRewardsProvider, useRewards] = constate(() => {
const { storage, pkh } = useBeacon();
const [rewards, setRewards] = useState([]);
const [loading, setLoading] = useState(true);

const loadRewards = useCallback(async () => {
if (!storage || !storage.vestings_counter || !pkh) {
setLoading(false);
return;
}
const limit = storage.vestings_counter;
let arr = [];
for (let i = 0; i < limit; i++) {
const vesting = await storage.vestings.get([i]);
arr.push(vesting);
}
if (pkh) {
// arr = arr.filter((x) => x.receiver === pkh || x.admin !== pkh); // for debug
arr = arr.filter((x) => x.receiver === pkh || x.admin === pkh);
}
setRewards(arr);
setLoading(false);
}, [storage, pkh]);

useEffect(() => {
loadRewards();
}, [loadRewards]);

return {
rewards,
loadingRewards: loading,
loadRewards,
};
});
6 changes: 3 additions & 3 deletions src/hooks/useTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { Stop } from "../components/Stop";

const TABS = [
{ label: "Explore", component: Explore },
{ label: "Distribute", component: Distribute },
{ label: "Stop vesting", component: Stop },
{ label: "Change Admin", component: ChangeAdmin },
{ label: "Distribute", component: Distribute, admin: true },
{ label: "Stop vesting", component: Stop, admin: true },
{ label: "Change Admin", component: ChangeAdmin, admin: true },
];

export const [UseTabsProvider, useTabs] = constate(() => {
Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { UseBeaconProvider } from "./hooks/useBeacon";
import { UseRewardsProvider } from "./hooks/useRewards";
import { UseTabsProvider } from "./hooks/useTabs";

ReactDOM.render(
<React.StrictMode>
<UseBeaconProvider>
<UseTabsProvider>
<App />
</UseTabsProvider>
<UseRewardsProvider>
<UseTabsProvider>
<App />
</UseTabsProvider>
</UseRewardsProvider>
</UseBeaconProvider>
</React.StrictMode>,
document.getElementById("root")
Expand Down
9 changes: 9 additions & 0 deletions src/material-tabs.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
bottom: 32px;
}

.material-tabs-disabled {
cursor: not-allowed;
}

.material-tabs-disabled .material-tabs-content {
color: #ccc4ca;
cursor: not-allowed;
}

@media only screen and (max-width: 520px) {
.nav-tabs.material-tabs > li > a {
font-size: 11px;
Expand Down

0 comments on commit 2730fb9

Please sign in to comment.