Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
check fordefi vault type explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeychr committed Nov 7, 2023
1 parent 063e2c1 commit 3ee62d5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 25 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/authority-forDefi/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -43,9 +43,9 @@ export class ForDefiClient {
return this.getResponse<ListTransactionsResponse>(requestPath);
}

async getVault(vaultId: string): Promise<GetEvmVaultResponse | GetSolanaVaultResponse> {
async getVault(vaultId: string): Promise<GetVaultResponse> {
const requestPath = `/api/v1/vaults/${vaultId}`;
return this.getResponse<GetEvmVaultResponse | GetSolanaVaultResponse>(requestPath);
return this.getResponse<GetVaultResponse>(requestPath);
}

async createTransaction(
Expand Down
7 changes: 2 additions & 5 deletions src/authority-forDefi/types/getVault.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
export type GetSolanaVaultResponse = {
id: string;
address: string;
};
export type GetEvmVaultResponse = {
export type GetVaultResponse = {
id: string;
address: string;
type: 'evm' | 'solana';
};
48 changes: 34 additions & 14 deletions src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof Evm.DlnClient>[0]['chainConfig'];

const safeExec = (func: Function, error: string) => {
const safeExec = async (func: () => Promise<any>, error: string) => {
try {
return func();
return await func();
} catch (e) {
throw new Error(`${error}: ${e}`);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -707,12 +708,31 @@ export class Executor implements IExecutor {
client: ForDefiClient,
vaultId: string,
): Promise<Uint8Array> {
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(
Expand All @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit 3ee62d5

Please sign in to comment.