Skip to content

Commit

Permalink
verse claim and send done
Browse files Browse the repository at this point in the history
  • Loading branch information
ma12ki committed Dec 27, 2022
1 parent a3c96bd commit 0146c30
Show file tree
Hide file tree
Showing 8 changed files with 3,094 additions and 2,974 deletions.
5,957 changes: 3,045 additions & 2,912 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 4 additions & 9 deletions src/components/Strategies/Blocks/Arbitrum_Magic_Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { useAutomateConnection, useStrategyStore } from '../../../hooks';
import { ethereum, StrategyBlock } from '../../../constants';
import { ethereumAddressValidator, retryRpcCallOnIntermittentError } from '../../../utils';
import { MAGIC_ADDRESS, MAGIC_DECIMAL_UNIT } from '../../../constants';
import { IStrategyBlockTxWithFallback } from '../../../types';
import { getStrategyByUrl } from '../strategyData';
import BaseBlock from './BaseBlock';

const { Text } = Typography;
Expand All @@ -19,7 +17,6 @@ const web3 = new Web3(ethereum as any);
const magicContract = new web3.eth.Contract(ERC20ABI as any, MAGIC_ADDRESS);

function Arbitrum_Magic_Send() {
const strategyName = useStrategyStore((state) => state.strategyName);
const strategyChainId = useStrategyStore((state) => state.chainId);
const setTx = useStrategyStore((state) => state.setTx);
const { account, chainId } = useAutomateConnection();
Expand All @@ -32,19 +29,17 @@ function Arbitrum_Magic_Send() {
const amountWei = web3.utils.toWei(amount, MAGIC_DECIMAL_UNIT);
const callData = magicContract.methods.transfer(address, amountWei).encodeABI();

const tx: IStrategyBlockTxWithFallback = {
setTx(StrategyBlock.Arbitrum_Magic_Send, {
to: MAGIC_ADDRESS,
data: callData,
amount: amountWei,
asset: MAGIC_ADDRESS,
fallback: getStrategyByUrl(strategyName).fallbacks![StrategyBlock.Ethereum_Verse_Send]!(),
};

setTx(StrategyBlock.Arbitrum_Magic_Send, tx);
fallback: true,
});
} catch (e) {
console.error(e);
}
}, [address, amount, setTx, strategyName]);
}, [address, amount, setTx]);

return (
<Container>
Expand Down
18 changes: 5 additions & 13 deletions src/components/Strategies/Blocks/Ethereum_Verse_Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import Web3 from 'web3';

import ERC20ABI from '../../../abi/ERC20.json';
import { useAutomateConnection, useStrategyStore } from '../../../hooks';
import { IStrategyBlockTxWithFallback } from '../../../types';
import { ethereum, StrategyBlock } from '../../../constants';
import { ethereumAddressValidator, retryRpcCallOnIntermittentError } from '../../../utils';
import { getStrategyByUrl } from '../strategyData';
import BaseBlock from './BaseBlock';

const { Text } = Typography;
Expand All @@ -20,7 +18,6 @@ const verseContract = new web3.eth.Contract(ERC20ABI as any, verseAddress);
const verseDecimalUnit = 'ether';

function Ethereum_Verse_Send() {
const strategyName = useStrategyStore((state) => state.strategyName);
const setTx = useStrategyStore((state) => state.setTx);
const strategyChainId = useStrategyStore((state) => state.chainId);
const { account, chainId } = useAutomateConnection();
Expand All @@ -34,22 +31,17 @@ function Ethereum_Verse_Send() {
const amountWei = web3.utils.toWei(amount, verseDecimalUnit);
const callData = verseContract.methods.transfer(address, amountWei).encodeABI();

const tx: IStrategyBlockTxWithFallback = {
setTx(StrategyBlock.Ethereum_Verse_Send, {
to: verseAddress,
data: callData,
amount: amountWei,
asset: verseAddress,
};

if (withFallback) {
tx.fallback = getStrategyByUrl(strategyName).fallbacks![StrategyBlock.Ethereum_Verse_Send]!();
}

setTx(StrategyBlock.Ethereum_Verse_Send, tx);
fallback: withFallback,
});
} catch (e) {
console.error(e);
}
}, [address, amount, withFallback, setTx, strategyName]);
}, [address, amount, withFallback, setTx]);

return (
<Container>
Expand Down Expand Up @@ -95,7 +87,7 @@ function Ethereum_Verse_Send() {
<Form.Item
name={`${StrategyBlock.Ethereum_Verse_Send}_withFallback`}
label="Fallback"
tooltip="Enabling this option will generate a fallback Claim tx for every Send. The fallback tx will be executed if there's not enough $VERSE tokens in your wallet"
tooltip="Enabling this option will generate a fallback Claim tx for every Send. The fallback tx will be executed if there's not enough $VERSE tokens in your wallet. Without the fallback, the strategy is more likely to get stuck but requires you to sign more transactions."
colon={false}
>
<Checkbox checked={withFallback} onChange={(e) => setWithFallback(!withFallback)} />
Expand Down
11 changes: 5 additions & 6 deletions src/components/Strategies/Blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ export { default as SigningPopup } from '../SigningPopup';

interface IBlockConfig {
component: () => JSX.Element;
requiresFallback: boolean;
}

const blockConfig: {
[key in StrategyBlock]: IBlockConfig;
} = {
'arbitrum:bridgeworld:claim': { component: Arbitrum_Bridgeworld_Claim, requiresFallback: false },
'arbitrum:magic:send': { component: Arbitrum_Magic_Send, requiresFallback: true },
'arbitrum:magicdragon:claim': { component: Arbitrum_MagicDragon_Claim, requiresFallback: false },
'ethereum:verse:claim': { component: Ethereum_Verse_Claim, requiresFallback: false },
'ethereum:verse:send': { component: Ethereum_Verse_Send, requiresFallback: false },
'arbitrum:bridgeworld:claim': { component: Arbitrum_Bridgeworld_Claim },
'arbitrum:magic:send': { component: Arbitrum_Magic_Send },
'arbitrum:magicdragon:claim': { component: Arbitrum_MagicDragon_Claim },
'ethereum:verse:claim': { component: Ethereum_Verse_Claim },
'ethereum:verse:send': { component: Ethereum_Verse_Send },
};

export { blockConfig };
47 changes: 21 additions & 26 deletions src/components/Strategies/StrategyDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { useStrategyApi, useStrategyStore, useAutomateConnection } from '../../hooks';
import { ChainId, ethereum, Network } from '../../constants';
import { retryRpcCallOnIntermittentError } from '../../utils';
import { strategies } from './strategyData';
import { getFallback, strategies } from './strategyData';
import { blockConfig, Repeat } from './Blocks';
import SigningPopup from './SigningPopup';

Expand Down Expand Up @@ -227,32 +227,27 @@ function buildPrepTxs({
timeConditionTZ: repetition.tz,
});

// TODO: use new 'fallbacks' prop here
const { requiresFallback } = blockConfig[block];

if (requiresFallback) {
const otherBlocks = strategy.blocks.filter((b) => b !== block);

for (const otherBlock of otherBlocks) {
const otherTx = txs[otherBlock];
priority += 1;
prepTxs.push({
assetType: strategy.assetType,
chainId: strategy.chainId,
from,
to: otherTx.to,
data: otherTx.data,
order: order++,
iteration,
position: position++,
priority,
conditionAsset: otherTx.asset,
conditionAmount: otherTx.amount,
timeCondition: repetition.time,
timeConditionTZ: repetition.tz,
});
}
const fallbackTx = tx.fallback ? getFallback(strategy.url, block) : undefined;

if (fallbackTx) {
priority += 1;
prepTxs.push({
assetType: strategy.assetType,
chainId: strategy.chainId,
from,
to: fallbackTx.to,
data: fallbackTx.data,
order: order++,
iteration,
position: position++,
priority,
conditionAsset: fallbackTx.asset,
conditionAmount: fallbackTx.amount,
timeCondition: repetition.time,
timeConditionTZ: repetition.tz,
});
}

prepTxs[prepTxs.length - 1].isLastForNonce = true;
}
iteration++;
Expand Down
11 changes: 10 additions & 1 deletion src/components/Strategies/strategyData.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChainId, StrategyBlock } from '../../constants';
import { AssetType, IStrategy } from '../../types';
import { AssetType, IStrategy, IStrategyBlockTx } from '../../types';
import { getTx as getVerseClaimTx } from './Blocks/Ethereum_Verse_Claim';
import { getTx as getBridgeworldClaimTx } from './Blocks/Arbitrum_Bridgeworld_Claim';

Expand Down Expand Up @@ -105,3 +105,12 @@ export const strategies: IStrategy[] = [
export function getStrategyByUrl(url: string): IStrategy {
return strategies.find((s) => s.url === url)!;
}

export function getFallback(stragegyUrl: string, blockName: StrategyBlock): IStrategyBlockTx | undefined {
const strategy = getStrategyByUrl(stragegyUrl);
const fallbacks = strategy.fallbacks;
if (!fallbacks) return;
if (!fallbacks[blockName]) return;

return fallbacks[blockName]!();
}
6 changes: 3 additions & 3 deletions src/hooks/useStrategyStore.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import create from 'zustand';

import { ChainId, StrategyBlock } from '../constants';
import { IStrategyBlockTxWithFallback, IStrategyRepetition, StrategyBlockTxs } from '../types';
import { IStrategyBlockTx, IStrategyRepetition, StrategyBlockTxs } from '../types';

interface IStrategyStore {
strategyName: string;
setStrategyName: (strategyName: string) => void;
txs: StrategyBlockTxs;
setTx: (key: StrategyBlock, tx: IStrategyBlockTxWithFallback) => void;
setTx: (key: StrategyBlock, tx: IStrategyBlockTx) => void;
repetitions: IStrategyRepetition[];
setRepetitions: (repetitions: IStrategyRepetition[]) => void;
chainId: ChainId | null;
Expand All @@ -16,7 +16,7 @@ interface IStrategyStore {

export const useStrategyStore = create<IStrategyStore>((set) => ({
strategyName: '',
setStrategyName: (strategyName: string) => set((state) => ({ strategyName })),
setStrategyName: (strategyName: string) => set(() => ({ strategyName })),
txs: {} as any,
setTx: (key, tx) => set((state) => ({ txs: { ...state.txs, [key]: tx } })),
repetitions: [],
Expand Down
5 changes: 1 addition & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,12 @@ export interface IStrategy {
hidden?: boolean;
}

export interface IStrategyBlockTxWithFallback extends IStrategyBlockTx {
fallback?: IStrategyBlockTx;
}

export interface IStrategyBlockTx {
to: string;
data: string;
asset?: string;
amount?: string;
fallback?: boolean;
}

export interface IStrategyPrepTx {
Expand Down

0 comments on commit 0146c30

Please sign in to comment.