diff --git a/package-lock.json b/package-lock.json index 7fa734a..ec4ab94 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@debridge-finance/dln-taker", - "version": "3.1.0-beta.0", + "version": "3.1.0-beta.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@debridge-finance/dln-taker", - "version": "3.1.0-beta.0", + "version": "3.1.0-beta.1", "license": "GPL-3.0-only", "dependencies": { "@debridge-finance/dln-client": "8.2.3", diff --git a/package.json b/package.json index 48da9b0..4dd55c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@debridge-finance/dln-taker", - "version": "3.1.0-beta.0", + "version": "3.1.0-beta.1", "description": "DLN executor is the rule-based daemon service developed to automatically execute orders placed on the deSwap Liquidity Network (DLN) across supported blockchains", "license": "GPL-3.0-only", "author": "deBridge", diff --git a/src/authority-forDefi/client.ts b/src/authority-forDefi/client.ts index 50d1a0d..21f79d4 100644 --- a/src/authority-forDefi/client.ts +++ b/src/authority-forDefi/client.ts @@ -4,7 +4,7 @@ import { CreateTransactionResponse, SignedCreateTransactionRequest, } from './types/createTransaction'; -import { GetEvmVaultResponse, GetSolanaVaultResponse } from './types/getVault'; +import { GetVaultResponse } from './types/getVault'; import { ListTransactionsRequest, ListTransactionsResponse } from './types/listTransactions'; import { ErrorResponse } from './types/shared'; @@ -43,9 +43,9 @@ export class ForDefiClient { return this.getResponse(requestPath); } - async getVault(vaultId: string): Promise { + async getVault(vaultId: string): Promise { const requestPath = `/api/v1/vaults/${vaultId}`; - return this.getResponse(requestPath); + return this.getResponse(requestPath); } async createTransaction( diff --git a/src/authority-forDefi/types/getVault.ts b/src/authority-forDefi/types/getVault.ts index 20a1591..412d29e 100644 --- a/src/authority-forDefi/types/getVault.ts +++ b/src/authority-forDefi/types/getVault.ts @@ -1,8 +1,5 @@ -export type GetSolanaVaultResponse = { - id: string; - address: string; -}; -export type GetEvmVaultResponse = { +export type GetVaultResponse = { id: string; address: string; + type: 'evm' | 'solana'; }; diff --git a/src/executor.ts b/src/executor.ts index 3cff1f5..970af74 100644 --- a/src/executor.ts +++ b/src/executor.ts @@ -59,14 +59,15 @@ import { EvmForDefiTransactionAdapter } from './chain-evm/fordefi-adapter'; import { SolanaForDefiTransactionAdapter } from './chain-solana/fordefi-adapter'; import { EmptyTransactionBuilder } from './chain-common/tx-builder-disabled'; import { isValidEvmAddress } from './chain-evm/utils'; +import { die } from './errors'; const DEFAULT_MIN_PROFITABILITY_BPS = 4; type EvmClientChainConfig = ConstructorParameters[0]['chainConfig']; -const safeExec = (func: Function, error: string) => { +const safeExec = async (func: () => Promise, error: string) => { try { - return func(); + return await func(); } catch (e) { throw new Error(`${error}: ${e}`); } @@ -419,7 +420,7 @@ export class Executor implements IExecutor { clients.push(client); - unlockBeneficiary = safeExec(() => { + unlockBeneficiary = await safeExec(async () => { const buffer = tokenStringToBuffer(chain.chain, chain.beneficiary); if (buffer.length !== 32) throw new Error(); return buffer; @@ -666,8 +667,8 @@ export class Executor implements IExecutor { case 'ForDefi': { const signer = new ForDefiSigner( - safeExec( - () => + await safeExec( + async () => crypto.createPrivateKey({ key: authority.signerPrivateKey, passphrase: authority.privateKeyPassphrase, @@ -707,12 +708,31 @@ export class Executor implements IExecutor { client: ForDefiClient, vaultId: string, ): Promise { - try { - const vault = await client.getVault(vaultId); - return tokenStringToBuffer(chainId, vault.address); - } catch (e) { - throw new Error(`Unable to retrieve ForDefi vault ${vaultId} for ${ChainId[chainId]}: ${e}`); + const vault = await safeExec( + async () => client.getVault(vaultId), + `Unable to retrieve ForDefi vault ${vaultId} for ${ChainId[chainId]}`, + ); + + switch (getEngineByChainId(chainId)) { + case ChainEngine.EVM: { + if (vault.type !== 'evm') + throw new Error( + `Unexpected type of vault ${vaultId} for ${ChainId[chainId]}: expected "evm", but got "${vault.type}"`, + ); + break; + } + case ChainEngine.Solana: { + if (vault.type !== 'solana') + throw new Error( + `Unexpected type of vault ${vaultId} for ${ChainId[chainId]}: expected "solana", but got "${vault.type}"`, + ); + break; + } + default: + die(`Unexpected engine`); } + + return tokenStringToBuffer(chainId, vault.address); } private async getSolanaProvider( @@ -728,8 +748,8 @@ export class Executor implements IExecutor { client, new SolanaTxSigner( connection, - safeExec( - () => + await safeExec( + async () => Keypair.fromSecretKey( authority.privateKey.startsWith('0x') ? helpers.hexToBuffer(authority.privateKey) @@ -750,8 +770,8 @@ export class Executor implements IExecutor { chain.chain, forDefiClient, new ForDefiSigner( - safeExec( - () => + await safeExec( + async () => crypto.createPrivateKey({ key: authority.signerPrivateKey, passphrase: authority.privateKeyPassphrase,