Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Extend Evm Vault swaps functionality #5344

Merged
merged 43 commits into from
Oct 29, 2024
Merged
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
6f26b19
feat: witnessing btc smart contract swaps
msgmaxim Oct 15, 2024
a7fd6d4
Merge branch 'main' into feat/btc-smart-contract-witnessing
msgmaxim Oct 16, 2024
d0446ae
chore: address minor review comments
msgmaxim Oct 16, 2024
9c2e4c3
chore: start implementation
albert-llimos Oct 16, 2024
6557606
test: fix deposit witnessing tests
msgmaxim Oct 17, 2024
e682f16
chore: add intial scale encoding for cfParameters
albert-llimos Oct 17, 2024
115342e
chore: improve logic
albert-llimos Oct 17, 2024
4256a5b
fix: address RuntimeCall size limit
msgmaxim Oct 18, 2024
7f91a99
Merge branch 'main' into feat/btc-smart-contract-witnessing
msgmaxim Oct 18, 2024
4234046
chore: address clippy
msgmaxim Oct 18, 2024
bd67b36
chore: more cleanup and refactoring
albert-llimos Oct 18, 2024
b510b81
chore: add contract swaps to dca test
albert-llimos Oct 18, 2024
11d4fa9
chore: merge from base
albert-llimos Oct 18, 2024
f436393
chore: engine refactor
albert-llimos Oct 18, 2024
e27808c
chore: refactor createEvmWallet
albert-llimos Oct 21, 2024
3f90224
chore: merge from main
albert-llimos Oct 21, 2024
d14c40d
chore: cleanup
albert-llimos Oct 21, 2024
7a80f89
chore: add MAX_VAULT_SWAP_ATTRIBUTES_LENGTH
albert-llimos Oct 21, 2024
cd8fa68
chore: refactor bouncer
albert-llimos Oct 21, 2024
da74dbb
chore: refactor into common for reusal
albert-llimos Oct 21, 2024
1851094
chore: rename attributes to parameters
albert-llimos Oct 21, 2024
7305042
chore: pass extra parameters to sdk
albert-llimos Oct 21, 2024
f9e67ab
chore: fix issues
albert-llimos Oct 22, 2024
de1bf99
chore: engine renaming
albert-llimos Oct 22, 2024
5eba5c7
chore: update SDK with new encoding logic
albert-llimos Oct 23, 2024
b14cb63
chore: remove unnecessary bouncer ts-scale
albert-llimos Oct 23, 2024
c06e294
chore: lint
albert-llimos Oct 23, 2024
854cbaf
chore: fix merge conflicts
albert-llimos Oct 23, 2024
c621a7d
chore: update to right name
albert-llimos Oct 23, 2024
2381415
chore: fix missing rename
albert-llimos Oct 23, 2024
cfe63fd
chore: add beneficiares and make FoK mandatory
albert-llimos Oct 23, 2024
399846f
chore: lint
albert-llimos Oct 23, 2024
d77a0da
chore: update broker_fees and cli
albert-llimos Oct 23, 2024
ea61dce
chore: update with hardcoded cfParameters
albert-llimos Oct 24, 2024
2282405
chore: bump sdk
albert-llimos Oct 24, 2024
de719c5
fix: simplify cf params decoding
dandanlen Oct 24, 2024
e3b8314
chore: downgrade error -> warning
dandanlen Oct 24, 2024
1d9b88e
chore: merge form main and fix conflicts
albert-llimos Oct 25, 2024
785ab48
chore: bump sdk with new broker_fees type
albert-llimos Oct 25, 2024
2033df7
chore: fix failing test
albert-llimos Oct 25, 2024
99310e5
chore: bump sdk
albert-llimos Oct 25, 2024
2d070ea
chore: lint
albert-llimos Oct 25, 2024
3bd922a
Merge commit 'd4eff081774475a80b140dc554bb3ae6ebf121b3' into feat/evm…
albert-llimos Oct 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: refactor createEvmWallet
  • Loading branch information
albert-llimos committed Oct 21, 2024
commit e27808c4b2df63168b85bbc0dbd9e03f67e6dbb3
15 changes: 3 additions & 12 deletions bouncer/shared/contract_swap.ts
Original file line number Diff line number Diff line change
@@ -5,28 +5,25 @@ import {
approveVault,
Asset as SCAsset,
Chains,
InternalAsset,
Chain,
} from '@chainflip/cli';
import { u8aToHex, hexToU8a } from '@polkadot/util';

import { HDNodeWallet, Wallet, getDefaultProvider } from 'ethers';
import { HDNodeWallet, Wallet } from 'ethers';
import {
observeBalanceIncrease,
getContractAddress,
observeCcmReceived,
amountToFineAmount,
defaultAssetAmounts,
chainFromAsset,
getEvmEndpoint,
assetDecimals,
stateChainAssetFromAsset,
chainGasAsset,
shortChainFromAsset,
createEvmWalletAndFund,
} from './utils';
import { getBalance } from './get_balance';
import { CcmDepositMetadata, DcaParams, FillOrKillParamsX128 } from '../shared/new_swap';
import { send } from './send';
import { SwapContext, SwapStatus } from './swap_context';
import { vaultSwapCfParametersCodec } from './swapping';

@@ -146,21 +143,15 @@ export async function performSwapViaContract(
const tag = swapTag ?? '';
const amountToSwap = amount ?? defaultAssetAmounts(sourceAsset);

const srcChain = chainFromAsset(sourceAsset);

// Generate a new wallet for each contract swap to prevent nonce issues when running in parallel
// with other swaps via deposit channels.
const mnemonic = Wallet.createRandom().mnemonic?.phrase ?? '';
if (mnemonic === '') {
throw new Error('Failed to create random mnemonic');
}
const wallet = Wallet.fromPhrase(mnemonic).connect(getDefaultProvider(getEvmEndpoint(srcChain)));
const wallet = await createEvmWalletAndFund(sourceAsset);

try {
// Fund new key with native asset and asset to swap.
await send(chainGasAsset(srcChain) as InternalAsset, wallet.address);
await send(sourceAsset, wallet.address);

if (erc20Assets.includes(sourceAsset)) {
// Doing effectively infinite approvals to make sure it doesn't fail.
// eslint-disable-next-line @typescript-eslint/no-use-before-define
17 changes: 17 additions & 0 deletions bouncer/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { execSync } from 'child_process';
import * as crypto from 'crypto';
import { HDNodeWallet, Wallet, getDefaultProvider } from 'ethers';
import { setTimeout as sleep } from 'timers/promises';
import Client from 'bitcoin-core';
import { ApiPromise, Keyring } from '@polkadot/api';
@@ -30,6 +31,7 @@ import { SwapParams } from './perform_swap';
import { newSolAddress } from './new_sol_address';
import { getChainflipApi, observeBadEvent, observeEvent } from './utils/substrate';
import { execWithLog } from './utils/exec_with_log';
import { send } from './send';

const cfTesterAbi = await getCFTesterAbi();
const cfTesterIdl = await getCfTesterIdl();
@@ -1161,3 +1163,18 @@ export function getTimeStamp(): string {
const seconds = now.getSeconds().toString().padStart(2, '0');
return `${hours}:${minutes}:${seconds}`;
}

export async function createEvmWalletAndFund(asset: Asset): Promise<HDNodeWallet> {
const chain = chainFromAsset(asset);

const mnemonic = Wallet.createRandom().mnemonic?.phrase ?? '';
if (mnemonic === '') {
throw new Error('Failed to create random mnemonic');
}
const wallet = Wallet.fromPhrase(mnemonic).connect(
getDefaultProvider(getEvmEndpoint(chain)),
);
await send(chainGasAsset(chain) as SDKAsset, wallet.address);
await send(asset, wallet.address);
return wallet;
}
19 changes: 3 additions & 16 deletions bouncer/tests/DCA_test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { InternalAsset as Asset, InternalAssets as Assets, InternalAsset } from '@chainflip/cli';
import { InternalAsset as Asset, InternalAssets as Assets } from '@chainflip/cli';
import { randomBytes } from 'crypto';
import { getDefaultProvider, Wallet } from 'ethers';
import assert from 'assert';
import {
chainFromAsset,
chainGasAsset,
createEvmWalletAndFund,
getContractAddress,
getEvmEndpoint,
newAddress,
observeBalanceIncrease,
observeSwapRequested,
@@ -83,18 +81,7 @@ async function testDCASwap(
await send(inputAsset, swapRequest.depositAddress, amount.toString());
testDCASwaps.log(`Sent ${amount} ${inputAsset} to ${swapRequest.depositAddress}`);
} else {
const srcChain = chainFromAsset(inputAsset);

// Probably refactor this into a function
const mnemonic = Wallet.createRandom().mnemonic?.phrase ?? '';
if (mnemonic === '') {
throw new Error('Failed to create random mnemonic');
}
const wallet = Wallet.fromPhrase(mnemonic).connect(
getDefaultProvider(getEvmEndpoint(srcChain)),
);
await send(chainGasAsset(srcChain) as InternalAsset, wallet.address);
await send(inputAsset, wallet.address);
const wallet = await createEvmWalletAndFund(inputAsset);

const contractSwapParams = await executeContractSwap(
inputAsset,