Skip to content

Commit

Permalink
feat(core): add create workflow step to the sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
extg3 committed Jun 10, 2024
1 parent 9953c2c commit 7fc4a3b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/react-example/src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
94 changes: 88 additions & 6 deletions examples/sandbox/src/app/app.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -54,18 +55,58 @@ const ConnectWalletButton = () => {
);
};

interface Token {
address: Address;
name: string;
decimals: number;
symbol: string;
chain: Chain;
}

const tokensMap: Record<Chain, Record<string, Token>> = {
[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<ethers.Signer | null>(null);
const [provider, setProvider] = React.useState<DittoProvider | null>(null);
const [swFactory, setSWFactory] = React.useState<SmartWalletFactory | null>(null);
const [isAuthenticated, setAuth] = React.useState(false);
const [swAddress, setSWAddress] = React.useState('');
const [swAddress, setSWAddress] = React.useState<Address | null>(null);
const [workflowHash, setWorkflowHash] = React.useState<string>('');
const [nextVaultId, setNextVaultId] = useLocalStorage<number>('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]);
Expand All @@ -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);
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 7fc4a3b

Please sign in to comment.