From 7fc4a3b0989e37c0262c0003e0e87995f693b132 Mon Sep 17 00:00:00 2001 From: extg3 Date: Mon, 10 Jun 2024 20:24:50 +0100 Subject: [PATCH] feat(core): add create workflow step to the sandbox --- examples/react-example/src/app/app.tsx | 2 +- examples/sandbox/src/app/app.tsx | 94 ++++++++++++++++++++++++-- 2 files changed, 89 insertions(+), 7 deletions(-) diff --git a/examples/react-example/src/app/app.tsx b/examples/react-example/src/app/app.tsx index 8b71ff7..4b3f24c 100644 --- a/examples/react-example/src/app/app.tsx +++ b/examples/react-example/src/app/app.tsx @@ -60,7 +60,7 @@ function App() { accountAddress, vaultAddress, provider: dittoProvider!, - }; + } as const; const fromToken = { address: from, decimals: fromDecimals } satisfies TokenLight; const toToken = { address: to, decimals: toDecimals } satisfies TokenLight; diff --git a/examples/sandbox/src/app/app.tsx b/examples/sandbox/src/app/app.tsx index 1acb8c0..6a68b57 100644 --- a/examples/sandbox/src/app/app.tsx +++ b/examples/sandbox/src/app/app.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { Button } from '../components/ui'; import { useSDK } from '@metamask/sdk-react'; -import { ethers } from 'ethers'; +import { ethers, parseUnits } from 'ethers'; import { Chain, CommonBuilderOptions, @@ -15,6 +15,7 @@ import { UniswapSwapActionCallDataBuilder, WorkflowsFactory, BrowserStorage, + Address, } from '@ditto-network/core'; import { EthersSigner, EthersContractFactory } from '@ditto-network/ethers'; import useLocalStorage from '../hooks/use-local-storage'; @@ -54,18 +55,58 @@ const ConnectWalletButton = () => { ); }; +interface Token { + address: Address; + name: string; + decimals: number; + symbol: string; + chain: Chain; +} + +const tokensMap: Record> = { + [Chain.Polygon]: { + wmatic: { + address: '0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270', + name: 'Wrapped MATIC', + decimals: 18, + symbol: 'WMATIC', + chain: Chain.Polygon, + }, + usdt: { + address: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f', + name: 'USDT', + decimals: 6, + symbol: 'USDT', + chain: Chain.Polygon, + } + }, + [Chain.Arbitrum]: { + + } +} + export function App() { const { account, chainId: _chainId } = useSDK(); - const chainId = (_chainId as unknown) as Chain | undefined; + const chainId = Number(_chainId) as Chain | undefined; + const tokens = chainId ? tokensMap[chainId] : {}; + console.log('tokens', chainId, tokens) const [signer, setSigner] = React.useState(null); const [provider, setProvider] = React.useState(null); const [swFactory, setSWFactory] = React.useState(null); const [isAuthenticated, setAuth] = React.useState(false); - const [swAddress, setSWAddress] = React.useState(''); + const [swAddress, setSWAddress] = React.useState
(null); const [workflowHash, setWorkflowHash] = React.useState(''); const [nextVaultId, setNextVaultId] = useLocalStorage('nextVaultId', 1); + const commonConfig = React.useMemo(() => ({ + chainId: chainId!, + recipient: swAddress as Address, + accountAddress: account as Address, + vaultAddress: swAddress as Address, + provider: provider!, + }), [chainId ,account, swAddress, provider]); + React.useEffect(() => { if (chainId) initProvider(); }, [chainId]); @@ -88,7 +129,7 @@ export function App() { setSigner(signer); setProvider(provider); setSWFactory(swFactory); - setSWAddress(vaultAddress); + if (vaultAddress) setSWAddress(vaultAddress); setNextVaultId(nextVaultId); } catch (error) { console.error('Error initializing provider:', error); @@ -132,9 +173,50 @@ export function App() { }; const handleCreateWorkflow = async () => { - if (!provider || !signer || !chainId) return; + if (!account || !provider || !signer || !chainId || !swAddress) return; - console.log('not implemented'); + try { + const workflowFactory = new WorkflowsFactory(provider); + + const wf = await workflowFactory.create({ + name: 'My first workflow', + triggers: [ + new TimeBasedTrigger( + { + repeatTimes: 2, + startAtTimestamp: new Date().getTime() / 1000 + 60, + cycle: { + frequency: 2, + scale: TimeScale.Minutes, + }, + }, + commonConfig + ), + ], + actions: [ + new UniswapSwapActionCallDataBuilder( + { + fromToken: tokens.wmatic, + toToken: tokens.usdt, + fromAmount: parseUnits('100', 6).toString(), + slippagePercent: 0.05, + providerStrategy: { + type: 'browser', + provider: (window as any).ethereum!, + }, + }, + commonConfig + ), + ], + chainId, + }); + + const deployedWorkflow = await wf.buildAndDeploy(swAddress, account as Address); + + setWorkflowHash(deployedWorkflow); + } catch (error) { + console.error('Error creating workflow:', error); + } }; return (