Skip to content

Commit

Permalink
chore: rename contract swaps -> vault swaps (#5357)
Browse files Browse the repository at this point in the history
* chore: rename contract swaps -> vault swaps

* chore: bump dca test timeout
  • Loading branch information
msgmaxim authored Oct 29, 2024
1 parent 5baa3dd commit 439fc17
Show file tree
Hide file tree
Showing 22 changed files with 82 additions and 146 deletions.
2 changes: 1 addition & 1 deletion api/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{anyhow, bail, Context, Result};
use async_trait::async_trait;
use cf_chains::{
address::{try_from_encoded_address, EncodedAddress},
btc::smart_contract_encoding::{
btc::vault_swap_encoding::{
encode_swap_params_in_nulldata_utxo, SharedCfParameters, UtxoEncodedData,
},
dot::PolkadotAccountId,
Expand Down
8 changes: 4 additions & 4 deletions bouncer/shared/swapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '../shared/utils';
import { BtcAddressType } from '../shared/new_btc_address';
import { CcmDepositMetadata } from '../shared/new_swap';
import { performSwapViaContract } from '../shared/contract_swap';
import { performVaultSwap } from '../shared/vault_swap';
import { SwapContext, SwapStatus } from './swap_context';

enum SolidityType {
Expand Down Expand Up @@ -236,7 +236,7 @@ export async function testSwap(
swapContext,
);
}
export async function testSwapViaContract(
export async function testVaultSwap(
sourceAsset: Asset,
destAsset: Asset,
addressType?: BtcAddressType,
Expand All @@ -250,12 +250,12 @@ export async function testSwapViaContract(
destAsset,
addressType,
messageMetadata,
(tagSuffix ?? '') + ' Contract',
(tagSuffix ?? '') + ' Vault',
log,
swapContext,
);

return performSwapViaContract(
return performVaultSwap(
sourceAsset,
destAsset,
destAddress,
Expand Down
16 changes: 8 additions & 8 deletions bouncer/shared/contract_swap.ts → bouncer/shared/vault_swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { SwapContext, SwapStatus } from './swap_context';

const erc20Assets: Asset[] = ['Flip', 'Usdc', 'Usdt', 'ArbUsdc'];

export async function executeContractSwap(
export async function executeVaultSwap(
srcAsset: Asset,
destAsset: Asset,
destAddress: string,
Expand Down Expand Up @@ -71,27 +71,27 @@ export async function executeContractSwap(

return receipt;
}
export type ContractSwapParams = {
export type VaultSwapParams = {
sourceAsset: Asset;
destAsset: Asset;
destAddress: string;
txHash: string;
};

export async function performSwapViaContract(
export async function performVaultSwap(
sourceAsset: Asset,
destAsset: Asset,
destAddress: string,
swapTag = '',
messageMetadata?: CcmDepositMetadata,
swapContext?: SwapContext,
log = true,
): Promise<ContractSwapParams> {
): Promise<VaultSwapParams> {
const tag = swapTag ?? '';

const srcChain = chainFromAsset(sourceAsset);

// Generate a new wallet for each contract swap to prevent nonce issues when running in parallel
// Generate a new wallet for each vault swap to prevent nonce issues when running in parallel
// with other swaps via deposit channels.
const mnemonic = Wallet.createRandom().mnemonic?.phrase ?? '';
if (mnemonic === '') {
Expand Down Expand Up @@ -122,14 +122,14 @@ export async function performSwapViaContract(
if (log) {
console.log(`${tag} Old balance: ${oldBalance}`);
console.log(
`${tag} Executing (${sourceAsset}) contract swap to(${destAsset}) ${destAddress}. Current balance: ${oldBalance}`,
`${tag} Executing (${sourceAsset}) vault swap to(${destAsset}) ${destAddress}. Current balance: ${oldBalance}`,
);
}

// To uniquely identify the contractSwap, we need to use the TX hash. This is only known
// To uniquely identify the VaultSwap, we need to use the TX hash. This is only known
// after sending the transaction, so we send it first and observe the events afterwards.
// There are still multiple blocks of safety margin inbetween before the event is emitted
const receipt = await executeContractSwap(
const receipt = await executeVaultSwap(
sourceAsset,
destAsset,
destAddress,
Expand Down
2 changes: 1 addition & 1 deletion bouncer/tests/DCA_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { requestNewSwap } from '../shared/perform_swap';
import { DcaParams } from '../shared/new_swap';

/* eslint-disable @typescript-eslint/no-use-before-define */
export const testDCASwaps = new ExecutableTest('DCA-Swaps', main, 150);
export const testDCASwaps = new ExecutableTest('DCA-Swaps', main, 250);

// Requested number of blocks between each chunk
const CHUNK_INTERVAL = 2;
Expand Down
16 changes: 8 additions & 8 deletions bouncer/tests/all_swaps.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { InternalAsset as Asset, InternalAssets as Assets } from '@chainflip/cli';
import { ContractSwapParams } from '../shared/contract_swap';
import { VaultSwapParams } from '../shared/vault_swap';
import { ExecutableTest } from '../shared/executable_test';
import { SwapParams } from '../shared/perform_swap';
import { newCcmMetadata, testSwap, testSwapViaContract } from '../shared/swapping';
import { newCcmMetadata, testSwap, testVaultSwap } from '../shared/swapping';
import { btcAddressTypes } from '../shared/new_btc_address';
import { ccmSupportedChains, chainFromAsset } from '../shared/utils';

/* eslint-disable @typescript-eslint/no-use-before-define */
export const testAllSwaps = new ExecutableTest('All-Swaps', main, 3000);

async function main() {
const allSwaps: Promise<SwapParams | ContractSwapParams>[] = [];
const allSwaps: Promise<SwapParams | VaultSwapParams>[] = [];

function appendSwap(
sourceAsset: Asset,
destAsset: Asset,
functionCall: typeof testSwap | typeof testSwapViaContract,
functionCall: typeof testSwap | typeof testVaultSwap,
ccmSwap: boolean = false,
) {
if (destAsset === 'Btc') {
Expand Down Expand Up @@ -52,12 +52,12 @@ async function main() {
const sourceChain = chainFromAsset(sourceAsset);
const destChain = chainFromAsset(destAsset);
if (sourceChain === 'Ethereum' || sourceChain === 'Arbitrum') {
// Contract Swaps
appendSwap(sourceAsset, destAsset, testSwapViaContract);
// Vault Swaps
appendSwap(sourceAsset, destAsset, testVaultSwap);

if (ccmSupportedChains.includes(destChain)) {
// CCM contract swaps
appendSwap(sourceAsset, destAsset, testSwapViaContract, true);
// CCM Vault swaps
appendSwap(sourceAsset, destAsset, testVaultSwap, true);
}
}

Expand Down
10 changes: 5 additions & 5 deletions bouncer/tests/evm_deposits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async function testNoDuplicateWitnessing(sourceAsset: Asset, destAsset: Asset) {
}

// Not supporting Btc to avoid adding more unnecessary complexity with address encoding.
async function testTxMultipleContractSwaps(sourceAsset: Asset, destAsset: Asset) {
async function testTxMultipleVaultSwaps(sourceAsset: Asset, destAsset: Asset) {
const { destAddress, tag } = await prepareSwap(sourceAsset, destAsset);

const web3 = new Web3(getEvmEndpoint(chainFromAsset(sourceAsset)));
Expand Down Expand Up @@ -194,10 +194,10 @@ async function main() {
]);

const multipleTxSwapsTest = Promise.all([
testTxMultipleContractSwaps('Eth', 'Dot'),
testTxMultipleContractSwaps('Eth', 'Flip'),
testTxMultipleContractSwaps('ArbEth', 'Dot'),
testTxMultipleContractSwaps('ArbEth', 'Flip'),
testTxMultipleVaultSwaps('Eth', 'Dot'),
testTxMultipleVaultSwaps('Eth', 'Flip'),
testTxMultipleVaultSwaps('ArbEth', 'Dot'),
testTxMultipleVaultSwaps('ArbEth', 'Flip'),
]);

const doubleDepositTests = Promise.all([
Expand Down
4 changes: 2 additions & 2 deletions bouncer/tests/rotation_barrier.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env -S pnpm tsx
import { submitGovernanceExtrinsic } from '../shared/cf_governance';
import { ExecutableTest } from '../shared/executable_test';
import { testSwapViaContract } from '../shared/swapping';
import { testVaultSwap } from '../shared/swapping';
import { observeEvent, observeBadEvent } from '../shared/utils/substrate';

/* eslint-disable @typescript-eslint/no-use-before-define */
Expand All @@ -22,7 +22,7 @@ async function main() {
const broadcastAborted = observeBadEvent(':BroadcastAborted', { label: 'Rotate and swap' });

// Using Arbitrum as the ingress chain to make the swap as fast as possible
await testSwapViaContract('ArbEth', 'Eth');
await testVaultSwap('ArbEth', 'Eth');

await broadcastAborted.stop();
}
4 changes: 2 additions & 2 deletions engine/src/witness/arb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ use cf_primitives::{Asset, AssetAmount, TransactionHash};
impl super::evm::vault::IngressCallBuilder for ArbCallBuilder {
type Chain = Arbitrum;

fn contract_swap_request(
fn vault_swap_request(
source_asset: Asset,
deposit_amount: AssetAmount,
destination_asset: Asset,
Expand All @@ -190,7 +190,7 @@ impl super::evm::vault::IngressCallBuilder for ArbCallBuilder {
tx_hash: TransactionHash,
) -> state_chain_runtime::RuntimeCall {
state_chain_runtime::RuntimeCall::ArbitrumIngressEgress(
pallet_cf_ingress_egress::Call::contract_swap_request {
pallet_cf_ingress_egress::Call::vault_swap_request {
input_asset: source_asset.try_into().expect("invalid asset for chain"),
output_asset: destination_asset,
deposit_amount,
Expand Down
2 changes: 1 addition & 1 deletion engine/src/witness/btc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod chain_tracking;
mod deposits;
pub mod smart_contract;
pub mod source;
pub mod vault_swaps;

use crate::{
btc::{
Expand Down
2 changes: 1 addition & 1 deletion engine/src/witness/btc/deposits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<Inner: ChunkedByVault> ChunkedByVaultBuilder<Inner> {
for vault_address in vault_addresses {
for tx in &txs {
if let Some(call) =
super::smart_contract::try_extract_contract_call(tx, &vault_address)
super::vault_swaps::try_extract_vault_swap_call(tx, &vault_address)
{
process_call(call.into(), epoch.index).await;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use cf_amm::common::{bounded_sqrt_price, sqrt_price_to_price};
use cf_chains::{
assets::btc::Asset as BtcAsset,
btc::{
deposit_address::DepositAddress, smart_contract_encoding::UtxoEncodedData,
BtcDepositDetails, ScriptPubkey, UtxoId,
deposit_address::DepositAddress, vault_swap_encoding::UtxoEncodedData, BtcDepositDetails,
ScriptPubkey, UtxoId,
},
ChannelRefundParameters, ForeignChainAddress,
};
Expand Down Expand Up @@ -79,7 +79,7 @@ fn script_buf_to_script_pubkey(script: &ScriptBuf) -> Option<ScriptPubkey> {
type BtcIngressEgressCall =
pallet_cf_ingress_egress::Call<state_chain_runtime::Runtime, BitcoinInstance>;

pub fn try_extract_contract_call(
pub fn try_extract_vault_swap_call(
tx: &VerboseTransaction,
vault_address: &DepositAddress,
) -> Option<BtcIngressEgressCall> {
Expand Down Expand Up @@ -128,7 +128,7 @@ pub fn try_extract_contract_call(

let tx_id: [u8; 32] = tx.txid.to_byte_array();

Some(BtcIngressEgressCall::contract_swap_request {
Some(BtcIngressEgressCall::vault_swap_request {
input_asset: NATIVE_ASSET,
output_asset: data.output_asset,
deposit_amount,
Expand Down Expand Up @@ -172,7 +172,7 @@ mod tests {

use super::*;

use cf_chains::btc::smart_contract_encoding::*;
use cf_chains::btc::vault_swap_encoding::*;

const MOCK_DOT_ADDRESS: [u8; 32] = [9u8; 32];

Expand Down Expand Up @@ -267,8 +267,8 @@ mod tests {
);

assert_eq!(
try_extract_contract_call(&tx, &vault_deposit_address),
Some(BtcIngressEgressCall::contract_swap_request {
try_extract_vault_swap_call(&tx, &vault_deposit_address),
Some(BtcIngressEgressCall::vault_swap_request {
input_asset: NATIVE_ASSET,
output_asset: MOCK_SWAP_PARAMS.output_asset,
deposit_amount: DEPOSIT_AMOUNT,
Expand Down
4 changes: 2 additions & 2 deletions engine/src/witness/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ pub struct EthCallBuilder {}
impl super::evm::vault::IngressCallBuilder for EthCallBuilder {
type Chain = Ethereum;

fn contract_swap_request(
fn vault_swap_request(
source_asset: Asset,
deposit_amount: AssetAmount,
destination_asset: Asset,
Expand All @@ -235,7 +235,7 @@ impl super::evm::vault::IngressCallBuilder for EthCallBuilder {
tx_hash: TransactionHash,
) -> state_chain_runtime::RuntimeCall {
state_chain_runtime::RuntimeCall::EthereumIngressEgress(
pallet_cf_ingress_egress::Call::contract_swap_request {
pallet_cf_ingress_egress::Call::vault_swap_request {
input_asset: source_asset.try_into().expect("invalid asset for chain"),
output_asset: destination_asset,
deposit_amount,
Expand Down
10 changes: 5 additions & 5 deletions engine/src/witness/evm/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where
amount,
sender: _,
cf_parameters: _,
}) => Some(CallBuilder::contract_swap_request(
}) => Some(CallBuilder::vault_swap_request(
native_asset,
try_into_primitive(amount)?,
try_into_primitive(dst_token)?,
Expand All @@ -80,7 +80,7 @@ where
amount,
sender: _,
cf_parameters: _,
}) => Some(CallBuilder::contract_swap_request(
}) => Some(CallBuilder::vault_swap_request(
*(supported_assets
.get(&src_token)
.ok_or(anyhow!("Source token {src_token:?} not found"))?),
Expand All @@ -100,7 +100,7 @@ where
gas_amount,
cf_parameters,
}) =>
Some(CallBuilder::contract_swap_request(
Some(CallBuilder::vault_swap_request(
native_asset,
try_into_primitive(amount)?,
try_into_primitive(dst_token)?,
Expand Down Expand Up @@ -136,7 +136,7 @@ where
gas_amount,
cf_parameters,
}) =>
Some(CallBuilder::contract_swap_request(
Some(CallBuilder::vault_swap_request(
*(supported_assets
.get(&src_token)
.ok_or(anyhow!("Source token {src_token:?} not found"))?),
Expand Down Expand Up @@ -197,7 +197,7 @@ where
pub trait IngressCallBuilder {
type Chain: cf_chains::Chain<ChainAccount = EthereumAddress>;

fn contract_swap_request(
fn vault_swap_request(
source_asset: Asset,
deposit_amount: cf_primitives::AssetAmount,
destination_asset: Asset,
Expand Down
6 changes: 3 additions & 3 deletions state-chain/cf-integration-tests/src/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ fn solana_ccm_fails_with_invalid_input() {

// Contract call fails with invalid CCM
assert_ok!(RuntimeCall::SolanaIngressEgress(
pallet_cf_ingress_egress::Call::contract_swap_request {
pallet_cf_ingress_egress::Call::vault_swap_request {
input_asset: SolAsset::Sol,
output_asset: Asset::SolUsdc,
deposit_amount: 1_000_000_000_000u64,
Expand Down Expand Up @@ -481,7 +481,7 @@ fn solana_ccm_fails_with_invalid_input() {
};

witness_call(RuntimeCall::SolanaIngressEgress(
pallet_cf_ingress_egress::Call::contract_swap_request {
pallet_cf_ingress_egress::Call::vault_swap_request {
input_asset: SolAsset::Sol,
output_asset: Asset::SolUsdc,
deposit_amount: 1_000_000_000_000u64,
Expand Down Expand Up @@ -705,7 +705,7 @@ fn solana_ccm_execution_error_can_trigger_fallback() {
},
};
witness_call(RuntimeCall::SolanaIngressEgress(
pallet_cf_ingress_egress::Call::contract_swap_request {
pallet_cf_ingress_egress::Call::vault_swap_request {
input_asset: SolAsset::Sol,
output_asset: Asset::SolUsdc,
deposit_amount: 1_000_000_000_000u64,
Expand Down
6 changes: 3 additions & 3 deletions state-chain/cf-integration-tests/src/swapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ fn can_process_ccm_via_direct_deposit() {
let deposit_amount = 100_000_000_000;

witness_call(RuntimeCall::EthereumIngressEgress(
pallet_cf_ingress_egress::Call::contract_swap_request {
pallet_cf_ingress_egress::Call::vault_swap_request {
input_asset: EthAsset::Flip,
output_asset: Asset::Usdc,
deposit_amount,
Expand Down Expand Up @@ -622,7 +622,7 @@ fn failed_swaps_are_rolled_back() {
let flip_pool = get_pool(Asset::Flip);

witness_call(RuntimeCall::EthereumIngressEgress(
pallet_cf_ingress_egress::Call::contract_swap_request {
pallet_cf_ingress_egress::Call::vault_swap_request {
input_asset: EthAsset::Eth,
output_asset: Asset::Flip,
deposit_amount: 10_000 * DECIMALS,
Expand Down Expand Up @@ -784,7 +784,7 @@ fn can_resign_failed_ccm() {
setup_pool_and_accounts(vec![Asset::Eth, Asset::Flip], OrderType::LimitOrder);

witness_call(RuntimeCall::EthereumIngressEgress(
pallet_cf_ingress_egress::Call::contract_swap_request {
pallet_cf_ingress_egress::Call::vault_swap_request {
input_asset: EthAsset::Flip,
output_asset: Asset::Usdc,
deposit_amount: 10_000_000_000_000,
Expand Down
Loading

0 comments on commit 439fc17

Please sign in to comment.