/* eslint-disable max-len */ /* eslint-disable complexity */ import { useEffect, useState } from 'react'; import MetaApi, { CopyFactory } from 'metaapi.cloud-sdk'; import axios from 'axios'; import { PrintLog, // for log Sections, Section, // Layout Form, Field // Form } from '../../shared'; interface ICopyFactoryCopyTradeProps { providerAccountId?: string domain?: string token?: string } export function CopyfactoryWebhooks({ providerAccountId: defaultProviderAccountId, domain: defaultDomain, token: defaultToken }: ICopyFactoryCopyTradeProps) { /* UI control */ const [areResourcesNarrowedDown, setAreResourcesNarrowedDown] = useState(true); const [isConnecting, setIsConnecting] = useState(false); const [isConnected, setIsConnected] = useState(false); /* Logging */ const [resultLog, setResultLog] = useState([]); const [errorLog, setErrorLog] = useState([]); const logErr = (...args: unknown[]) => setErrorLog(logs => { console.log(...args); return [...logs, ...args.map((arg: any) => arg.message || arg)]; }); const log = (...args: unknown[]) => setResultLog(logs => { console.log(...args); return [...logs, ...args]; }); /* Your provider MetaApi account id provider account must have PROVIDER value in copyFactoryRoles */ const [providerAccountId, setProviderAccountId] = useState(defaultProviderAccountId || ''); // your MetaApi API token const [domain, setDomain] = useState(defaultDomain || ''); const [token, setToken] = useState(defaultToken || ''); const makeRequest = async () => { const copyFactory = new CopyFactory(token, { domain }); const metaApi = new MetaApi(token, { domain }); const areTokenResourcesNarrowedDown = metaApi.tokenManagementApi.areTokenResourcesNarrowedDown(token); setAreResourcesNarrowedDown(areTokenResourcesNarrowedDown); const configurationApi = copyFactory.configurationApi; try { const providerMetaapiAccount = await metaApi.tradingAccountApi.getAccount(providerAccountId); if( !providerMetaapiAccount.copyFactoryRoles || !providerMetaapiAccount.copyFactoryRoles.includes('PROVIDER') ) { throw new Error( 'Please specify PROVIDER copyFactoryRoles value in your MetaApi account in ' + 'order to use it in CopyFactory API' ); } const strategies = await configurationApi.getStrategiesWithInfiniteScrollPagination(); const strategy = strategies.find((s: any) => s.accountId === providerMetaapiAccount.id); const strategyId = !strategy ? (await configurationApi.generateStrategyId()).id : strategy._id; log('Creating strategy'); await configurationApi.updateStrategy(strategyId, { name: 'Test strategy', description: 'Some useful description about your strategy', accountId: providerMetaapiAccount.id }); log('Strategy created'); log('Creating webhook'); let webhook = await configurationApi.createWebhook(strategyId, { symbolMapping: [{from: 'EURUSD.m', to: 'EURUSD'}], magic: 100 }); log('Webhook created', webhook); log('Updating webhook'); await configurationApi.updateWebhook(strategyId, webhook.id, { symbolMapping: [ {from: 'EURUSD.m', to: 'EURUSD'}, {from: 'BTCUSD.m', to: 'BTCUSD'} ], magic: 100 }); log('Retrieving webhooks with infinite scroll pagination'); let webhooks1 = await configurationApi.getWebhooksWithInfiniteScrollPagination(strategyId); log('Retrieved webhooks', webhooks1); log('Retrieving webhooks with classic pagination'); let webhooks2 = await configurationApi.getWebhooksWithClassicPagination(strategyId); log('Retrieved webhooks', webhooks2); log('Sending a trading signal to the webhook. Curl command:'); let payload = { symbol: 'EURUSD', type: 'POSITION_TYPE_BUY', time: new Date().toISOString(), volume: 0.1 }; log(`curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '${ JSON.stringify(payload) }' '${webhook.url}'`); let response = await axios.post(webhook.url, payload); log('Sent the signal, signal ID: ' + response.data.signalId); log('Deleting webhook ' + webhook.id); await configurationApi.deleteWebhook(strategyId, webhook.id); } catch (err){ logErr(err); throw err; } }; /* Control */ const reset = () => { setIsConnecting(false); setIsConnected(false); setResultLog([]); setErrorLog([]); setProviderAccountId(defaultProviderAccountId || ''); setToken(defaultToken ||''); setDomain(defaultDomain || ''); }; const triggerToMakeRequest = () => { if (isConnected || !providerAccountId || !token) {return;} setIsConnecting(true); }; /* Use one for control request and rerender */ useEffect(() => { if (isConnected || !isConnecting) { return; } makeRequest() .then(() => setIsConnected(true)) // If success .catch(err => console.log('failed', err)) // If failed .finally(() => setIsConnecting(false)); // Enable an interaction with UI }, [isConnecting]); // if change isConnecting run useEffect return (

CopyFactory. Copy Trade

Connect to your account

{!areResourcesNarrowedDown &&

Warning

It seems like you are using a admin API token.

Since the token can be retrieven from the browser or mobile apps by end user this can lead to your application being compromised, unless you understand what are you doing.

Please use Token Management API in your backend application to produce secure tokens which you can then use in web UI or mobile apps.

} {resultLog && resultLog.length > 0 &&

Logs

} {errorLog && errorLog.length > 0 &&

Errors

}
); }