From 228cbefd6716aa153a1b0e74b48c48fe32fbdbf5 Mon Sep 17 00:00:00 2001 From: albert Date: Thu, 7 Nov 2024 11:12:10 +0100 Subject: [PATCH 01/19] feat: add cf-parameters versioning --- engine/src/witness/common/cf_parameters.rs | 10 ++++++--- engine/src/witness/evm/vault.rs | 24 ++++++++++++++-------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/engine/src/witness/common/cf_parameters.rs b/engine/src/witness/common/cf_parameters.rs index e84d88812f..d27c5834a7 100644 --- a/engine/src/witness/common/cf_parameters.rs +++ b/engine/src/witness/common/cf_parameters.rs @@ -3,15 +3,19 @@ use cf_primitives::{BasisPoints, Beneficiaries, DcaParameters}; use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; +#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Debug)] +pub enum VersionedCfParameters { + V0(CfParameters), +} + #[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Debug)] pub struct CfParameters { - /// CCMs may require additional data (for example CCMs to Solana require adding a list of - /// addresses). + /// CCMs may require additional data (e.g. CCMs to Solana requires a list of addresses). pub ccm_additional_data: CcmData, pub vault_swap_parameters: VaultSwapParameters, } -pub type CcmCfParameters = CfParameters; +pub type VersionedCcmCfParameters = VersionedCfParameters; // TODO: Define this / implement it on the SC - PRO-1743. pub type ShortId = u8; diff --git a/engine/src/witness/evm/vault.rs b/engine/src/witness/evm/vault.rs index 34a8f2a3b9..7f6f842abf 100644 --- a/engine/src/witness/evm/vault.rs +++ b/engine/src/witness/evm/vault.rs @@ -66,8 +66,10 @@ where sender: _, cf_parameters, }) => { - let CfParameters { ccm_additional_data: (), vault_swap_parameters } = - CfParameters::decode(&mut &cf_parameters[..])?; + let VersionedCfParameters::V0(CfParameters { + ccm_additional_data: (), + vault_swap_parameters, + }) = VersionedCfParameters::decode(&mut &cf_parameters[..])?; Some(CallBuilder::vault_swap_request( native_asset, @@ -91,8 +93,10 @@ where sender: _, cf_parameters, }) => { - let CfParameters { ccm_additional_data: (), vault_swap_parameters } = - CfParameters::decode(&mut &cf_parameters[..])?; + let VersionedCfParameters::V0(CfParameters { + ccm_additional_data: (), + vault_swap_parameters, + }) = VersionedCfParameters::decode(&mut &cf_parameters[..])?; Some(CallBuilder::vault_swap_request( *(supported_assets @@ -119,8 +123,10 @@ where gas_amount, cf_parameters, }) => { - let CfParameters { ccm_additional_data, vault_swap_parameters } = - CcmCfParameters::decode(&mut &cf_parameters[..])?; + let VersionedCcmCfParameters::V0(CfParameters { + ccm_additional_data, + vault_swap_parameters, + }) = VersionedCcmCfParameters::decode(&mut &cf_parameters[..])?; Some(CallBuilder::vault_swap_request( native_asset, @@ -161,8 +167,10 @@ where gas_amount, cf_parameters, }) => { - let CfParameters { ccm_additional_data, vault_swap_parameters } = - CcmCfParameters::decode(&mut &cf_parameters[..])?; + let VersionedCcmCfParameters::V0(CfParameters { + ccm_additional_data, + vault_swap_parameters, + }) = VersionedCcmCfParameters::decode(&mut &cf_parameters[..])?; Some(CallBuilder::vault_swap_request( *(supported_assets From 6add0fcc951b9a50b3e4b027c800c5935a15c2b8 Mon Sep 17 00:00:00 2001 From: albert Date: Thu, 7 Nov 2024 11:18:53 +0100 Subject: [PATCH 02/19] chore: update with SDK changes --- bouncer/package.json | 2 +- bouncer/pnpm-lock.yaml | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bouncer/package.json b/bouncer/package.json index 12bb5f412b..0baa157a4c 100644 --- a/bouncer/package.json +++ b/bouncer/package.json @@ -6,7 +6,7 @@ "prettier:write": "prettier --write ." }, "dependencies": { - "@chainflip/cli": "1.8.0-rc.1", + "@chainflip/cli": "1.8.0-rc.2", "@chainflip/utils": "^0.4.0", "@coral-xyz/anchor": "^0.30.1", "@iarna/toml": "^2.2.5", diff --git a/bouncer/pnpm-lock.yaml b/bouncer/pnpm-lock.yaml index 482e56ad92..17dee78a33 100644 --- a/bouncer/pnpm-lock.yaml +++ b/bouncer/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@chainflip/cli': - specifier: 1.8.0-rc.1 - version: 1.8.0-rc.1(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10) + specifier: 1.8.0-rc.2 + version: 1.8.0-rc.2(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10) '@chainflip/utils': specifier: ^0.4.0 version: 0.4.0 @@ -145,8 +145,8 @@ packages: '@chainflip/bitcoin@1.1.1': resolution: {integrity: sha512-Jr6X/0QTSFYpTp23ZPhyioL6wL9x3Xpj6OGjadQKZyhobRN4BZkhLogv20HDYTJcRzVphzrTJArbEaNie04XVA==} - '@chainflip/cli@1.8.0-rc.1': - resolution: {integrity: sha512-e8TWjBCgwRmfcGbq59ELs1xQglXc4/lGGYiC0DwYIsN58NNFbFL04UDsS2rOivrnxlx9kwOihGU5AZABtup12g==} + '@chainflip/cli@1.8.0-rc.2': + resolution: {integrity: sha512-CLAqtbQWhD4IzA1edwBbR0CgQej4DycvTPMcBPhNnSQoK85svB8u1ZYtn6XHz6yyoDJjbiDRrYN6AMIaGCjgDw==} hasBin: true peerDependencies: axios: ^1.x @@ -3169,7 +3169,7 @@ snapshots: transitivePeerDependencies: - typescript - '@chainflip/cli@1.8.0-rc.1(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10)': + '@chainflip/cli@1.8.0-rc.2(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10)': dependencies: '@chainflip/bitcoin': 1.1.1(typescript@5.5.3) '@chainflip/extrinsics': 1.6.1 From ab416feecb80754a4bad5e6d9118a683fb0ed2c7 Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 8 Nov 2024 10:28:04 +0100 Subject: [PATCH 03/19] chore: update broker and affiliates in engine and bouncer --- bouncer/shared/vault_swap.ts | 15 ++++++- bouncer/tests/all_swaps.ts | 46 +++++++++++----------- engine/src/witness/common/cf_parameters.rs | 6 ++- engine/src/witness/evm/vault.rs | 12 ++++-- 4 files changed, 50 insertions(+), 29 deletions(-) diff --git a/bouncer/shared/vault_swap.ts b/bouncer/shared/vault_swap.ts index 2ee750c526..22f747ffdf 100644 --- a/bouncer/shared/vault_swap.ts +++ b/bouncer/shared/vault_swap.ts @@ -9,6 +9,7 @@ import { } from '@chainflip/cli'; import { HDNodeWallet } from 'ethers'; import { randomBytes } from 'crypto'; +import Keyring from '../polkadot/keyring'; import { observeBalanceIncrease, getContractAddress, @@ -31,6 +32,10 @@ export async function executeVaultSwap( sourceAsset: Asset, destAsset: Asset, destAddress: string, + brokerFees: { + account: string; + commissionBps: number; + }, messageMetadata?: CcmDepositMetadata, amount?: string, boostFeeBps?: number, @@ -87,12 +92,13 @@ export async function executeVaultSwap( message: messageMetadata.message, ccmAdditionalData: messageMetadata.ccmAdditionalData, }, + brokerFees, // The SDK will encode these parameters and the ccmAdditionalData // into the `cfParameters` field for the vault swap. boostFeeBps, fillOrKillParams: fokParams, dcaParams, - beneficiaries: undefined, + affiliateFees: undefined, } as ExecuteSwapParams, networkOptions, txOptions, @@ -136,6 +142,12 @@ export async function performVaultSwap( ); } + const keyring = new Keyring({ type: 'sr25519' }); + const broker = keyring.createFromUri('//BROKER_1'); + const brokerFees = { + account: broker.address, + commissionBps: 1, + }; // 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 @@ -143,6 +155,7 @@ export async function performVaultSwap( sourceAsset, destAsset, destAddress, + brokerFees, messageMetadata, amountToSwap, boostFeeBps, diff --git a/bouncer/tests/all_swaps.ts b/bouncer/tests/all_swaps.ts index 21708fc17c..4d87b36544 100644 --- a/bouncer/tests/all_swaps.ts +++ b/bouncer/tests/all_swaps.ts @@ -42,31 +42,33 @@ async function main() { } } - Object.values(Assets).forEach((sourceAsset) => { - Object.values(Assets) - .filter((destAsset) => sourceAsset !== destAsset) - .forEach((destAsset) => { - // Regular swaps - appendSwap(sourceAsset, destAsset, testSwap); + // Object.values(Assets).forEach((sourceAsset) => { + // Object.values(Assets) + // .filter((destAsset) => sourceAsset !== destAsset) + // .forEach((destAsset) => { + // // Regular swaps + // appendSwap(sourceAsset, destAsset, testSwap); - const sourceChain = chainFromAsset(sourceAsset); - const destChain = chainFromAsset(destAsset); - if (sourceChain === 'Ethereum' || sourceChain === 'Arbitrum') { - // Vault Swaps - appendSwap(sourceAsset, destAsset, testVaultSwap); + // const sourceChain = chainFromAsset(sourceAsset); + // const destChain = chainFromAsset(destAsset); + // if (sourceChain === 'Ethereum' || sourceChain === 'Arbitrum') { + // // Vault Swaps + // appendSwap(sourceAsset, destAsset, testVaultSwap); - if (ccmSupportedChains.includes(destChain)) { - // CCM Vault swaps - appendSwap(sourceAsset, destAsset, testVaultSwap, true); - } - } + // if (ccmSupportedChains.includes(destChain)) { + // // CCM Vault swaps + // appendSwap(sourceAsset, destAsset, testVaultSwap, true); + // } + // } - if (ccmSupportedChains.includes(destChain)) { - // CCM swaps - appendSwap(sourceAsset, destAsset, testSwap, true); - } - }); - }); + // if (ccmSupportedChains.includes(destChain)) { + // // CCM swaps + // appendSwap(sourceAsset, destAsset, testSwap, true); + // } + // }); + // }); + + appendSwap('ArbEth', 'Eth', testVaultSwap); await Promise.all(allSwaps); } diff --git a/engine/src/witness/common/cf_parameters.rs b/engine/src/witness/common/cf_parameters.rs index d27c5834a7..c119a29977 100644 --- a/engine/src/witness/common/cf_parameters.rs +++ b/engine/src/witness/common/cf_parameters.rs @@ -1,5 +1,5 @@ use cf_chains::{CcmAdditionalData, ChannelRefundParameters}; -use cf_primitives::{BasisPoints, Beneficiaries, DcaParameters}; +use cf_primitives::{AccountId, Affiliates, BasisPoints, Beneficiary, DcaParameters}; use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; @@ -25,7 +25,9 @@ pub struct VaultSwapParameters { pub refund_params: ChannelRefundParameters, pub dca_params: Option, pub boost_fee: Option, - pub broker_fees: Beneficiaries, + // TODO: Should we use u8 instead of BasisPoints for fees as in Bitcoin? + pub broker_fees: Beneficiary, + pub affiliate_fees: Affiliates, } #[cfg(test)] diff --git a/engine/src/witness/evm/vault.rs b/engine/src/witness/evm/vault.rs index 7f6f842abf..a35a7070f5 100644 --- a/engine/src/witness/evm/vault.rs +++ b/engine/src/witness/evm/vault.rs @@ -78,7 +78,8 @@ where try_into_encoded_address(try_into_primitive(dst_chain)?, dst_address.to_vec())?, None, event.tx_hash.into(), - vault_swap_parameters.broker_fees, + // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees + Default::default(), Some(vault_swap_parameters.refund_params), vault_swap_parameters.dca_params, vault_swap_parameters.boost_fee, @@ -107,7 +108,8 @@ where try_into_encoded_address(try_into_primitive(dst_chain)?, dst_address.to_vec())?, None, event.tx_hash.into(), - vault_swap_parameters.broker_fees, + // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees + Default::default(), Some(vault_swap_parameters.refund_params), vault_swap_parameters.dca_params, vault_swap_parameters.boost_fee, @@ -150,7 +152,8 @@ where }, }), event.tx_hash.into(), - vault_swap_parameters.broker_fees, + // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees + Default::default(), Some(vault_swap_parameters.refund_params), vault_swap_parameters.dca_params, vault_swap_parameters.boost_fee, @@ -196,7 +199,8 @@ where }, }), event.tx_hash.into(), - vault_swap_parameters.broker_fees, + // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees + Default::default(), Some(vault_swap_parameters.refund_params), vault_swap_parameters.dca_params, vault_swap_parameters.boost_fee, From 269745f3fd022b3b3685473110eb32691ed7f395 Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 8 Nov 2024 11:35:05 +0100 Subject: [PATCH 04/19] chore: revert all_swaps --- bouncer/tests/all_swaps.ts | 46 ++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/bouncer/tests/all_swaps.ts b/bouncer/tests/all_swaps.ts index 4d87b36544..21708fc17c 100644 --- a/bouncer/tests/all_swaps.ts +++ b/bouncer/tests/all_swaps.ts @@ -42,33 +42,31 @@ async function main() { } } - // Object.values(Assets).forEach((sourceAsset) => { - // Object.values(Assets) - // .filter((destAsset) => sourceAsset !== destAsset) - // .forEach((destAsset) => { - // // Regular swaps - // appendSwap(sourceAsset, destAsset, testSwap); + Object.values(Assets).forEach((sourceAsset) => { + Object.values(Assets) + .filter((destAsset) => sourceAsset !== destAsset) + .forEach((destAsset) => { + // Regular swaps + appendSwap(sourceAsset, destAsset, testSwap); - // const sourceChain = chainFromAsset(sourceAsset); - // const destChain = chainFromAsset(destAsset); - // if (sourceChain === 'Ethereum' || sourceChain === 'Arbitrum') { - // // Vault Swaps - // appendSwap(sourceAsset, destAsset, testVaultSwap); + const sourceChain = chainFromAsset(sourceAsset); + const destChain = chainFromAsset(destAsset); + if (sourceChain === 'Ethereum' || sourceChain === 'Arbitrum') { + // Vault Swaps + appendSwap(sourceAsset, destAsset, testVaultSwap); - // if (ccmSupportedChains.includes(destChain)) { - // // CCM Vault swaps - // appendSwap(sourceAsset, destAsset, testVaultSwap, true); - // } - // } + if (ccmSupportedChains.includes(destChain)) { + // CCM Vault swaps + appendSwap(sourceAsset, destAsset, testVaultSwap, true); + } + } - // if (ccmSupportedChains.includes(destChain)) { - // // CCM swaps - // appendSwap(sourceAsset, destAsset, testSwap, true); - // } - // }); - // }); - - appendSwap('ArbEth', 'Eth', testVaultSwap); + if (ccmSupportedChains.includes(destChain)) { + // CCM swaps + appendSwap(sourceAsset, destAsset, testSwap, true); + } + }); + }); await Promise.all(allSwaps); } From f6a2ec83ff3089b7f80eb1c0f4f03fa54db08dbc Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 8 Nov 2024 11:47:00 +0100 Subject: [PATCH 05/19] chore: update to 1.8.0-rc.3 --- bouncer/package.json | 2 +- bouncer/pnpm-lock.yaml | 25 ++++++++++--------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/bouncer/package.json b/bouncer/package.json index 0baa157a4c..ef1a6e856f 100644 --- a/bouncer/package.json +++ b/bouncer/package.json @@ -6,7 +6,7 @@ "prettier:write": "prettier --write ." }, "dependencies": { - "@chainflip/cli": "1.8.0-rc.2", + "@chainflip/cli": "1.8.0-rc.3", "@chainflip/utils": "^0.4.0", "@coral-xyz/anchor": "^0.30.1", "@iarna/toml": "^2.2.5", diff --git a/bouncer/pnpm-lock.yaml b/bouncer/pnpm-lock.yaml index 17dee78a33..d28a0243c5 100644 --- a/bouncer/pnpm-lock.yaml +++ b/bouncer/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@chainflip/cli': - specifier: 1.8.0-rc.2 - version: 1.8.0-rc.2(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10) + specifier: 1.8.0-rc.3 + version: 1.8.0-rc.3(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10) '@chainflip/utils': specifier: ^0.4.0 version: 0.4.0 @@ -145,8 +145,8 @@ packages: '@chainflip/bitcoin@1.1.1': resolution: {integrity: sha512-Jr6X/0QTSFYpTp23ZPhyioL6wL9x3Xpj6OGjadQKZyhobRN4BZkhLogv20HDYTJcRzVphzrTJArbEaNie04XVA==} - '@chainflip/cli@1.8.0-rc.2': - resolution: {integrity: sha512-CLAqtbQWhD4IzA1edwBbR0CgQej4DycvTPMcBPhNnSQoK85svB8u1ZYtn6XHz6yyoDJjbiDRrYN6AMIaGCjgDw==} + '@chainflip/cli@1.8.0-rc.3': + resolution: {integrity: sha512-W6//r1T7MyETbpw5Dt5PGtEPrLwzMtqajwzA+IB8rIewxojYj26zpWRrBCfl4ORoAz9rL1Pa8Nip2Q7Dtd8EVw==} hasBin: true peerDependencies: axios: ^1.x @@ -155,15 +155,12 @@ packages: '@chainflip/extrinsics@1.6.1': resolution: {integrity: sha512-sm3v2QguNW4/RiIZYHd+VRSxup2q4cVBY0VjbQJw6xYqhNCzZlt+Hl38Kni+TCHQu3hP/WgIF72yvvSs7/PBdg==} - '@chainflip/rpc@1.6.9': - resolution: {integrity: sha512-FtaoZBey4kIFhWQY5mMMAGXIgzSpZCX0DTnXQqPEn9q5qvt6FoQLDQk9k2IYKOTiIP+XsLy9w/WPPiH7+3BpnQ==} + '@chainflip/rpc@1.6.10': + resolution: {integrity: sha512-Drn4EseSJRIdR5Jsi7uxAD3KEv0bK+zYL+AiMqQ/hZTRnWoVQArKm7xsJxFTOiGscQXTlqRO/igTcGaghYUmmQ==} '@chainflip/solana@1.0.2': resolution: {integrity: sha512-8M+z0yjVCZBMpdCTGuBRt/Ns9NBJCNFvnSOjDu/kyaBRvxdUvECJOIseL0ENLBd0J/YmO9TcrZ30M0po3a96BA==} - '@chainflip/utils@0.3.0': - resolution: {integrity: sha512-csW8E3lRcSG1udN/kgsOhjZ7bfRrSbgv8Hdh/pzjM+VjQ6Hh46/LuiZHeQR0rQMimfG+lu2Afxk4jeRXRGFWTg==} - '@chainflip/utils@0.4.0': resolution: {integrity: sha512-OuMFlgbIbyJ1Kt39zytfGh4HZ5dVY25/a3qb0c1K137+QmhdOuF+R7QSkFPqYAfDG4KhGRqo0rgUtEpaEEcSKA==} @@ -3169,11 +3166,11 @@ snapshots: transitivePeerDependencies: - typescript - '@chainflip/cli@1.8.0-rc.2(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10)': + '@chainflip/cli@1.8.0-rc.3(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10)': dependencies: '@chainflip/bitcoin': 1.1.1(typescript@5.5.3) '@chainflip/extrinsics': 1.6.1 - '@chainflip/rpc': 1.6.9 + '@chainflip/rpc': 1.6.10 '@chainflip/solana': 1.0.2 '@chainflip/utils': 0.4.0 axios: 1.7.2 @@ -3189,17 +3186,15 @@ snapshots: '@chainflip/extrinsics@1.6.1': {} - '@chainflip/rpc@1.6.9': + '@chainflip/rpc@1.6.10': dependencies: - '@chainflip/utils': 0.3.0 + '@chainflip/utils': 0.4.0 zod: 3.23.8 '@chainflip/solana@1.0.2': dependencies: '@chainflip/utils': 0.4.0 - '@chainflip/utils@0.3.0': {} - '@chainflip/utils@0.4.0': {} '@coral-xyz/anchor-errors@0.30.1': {} From 693b992423499e9ceaa79ec09d79894f97629490 Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 8 Nov 2024 11:50:38 +0100 Subject: [PATCH 06/19] chore: update broker and affiliates in engine and bouncer --- bouncer/shared/vault_swap.ts | 15 ++++++- bouncer/tests/all_swaps.ts | 46 +++++++++++----------- engine/src/witness/common/cf_parameters.rs | 6 ++- engine/src/witness/evm/vault.rs | 12 ++++-- 4 files changed, 50 insertions(+), 29 deletions(-) diff --git a/bouncer/shared/vault_swap.ts b/bouncer/shared/vault_swap.ts index 2ee750c526..22f747ffdf 100644 --- a/bouncer/shared/vault_swap.ts +++ b/bouncer/shared/vault_swap.ts @@ -9,6 +9,7 @@ import { } from '@chainflip/cli'; import { HDNodeWallet } from 'ethers'; import { randomBytes } from 'crypto'; +import Keyring from '../polkadot/keyring'; import { observeBalanceIncrease, getContractAddress, @@ -31,6 +32,10 @@ export async function executeVaultSwap( sourceAsset: Asset, destAsset: Asset, destAddress: string, + brokerFees: { + account: string; + commissionBps: number; + }, messageMetadata?: CcmDepositMetadata, amount?: string, boostFeeBps?: number, @@ -87,12 +92,13 @@ export async function executeVaultSwap( message: messageMetadata.message, ccmAdditionalData: messageMetadata.ccmAdditionalData, }, + brokerFees, // The SDK will encode these parameters and the ccmAdditionalData // into the `cfParameters` field for the vault swap. boostFeeBps, fillOrKillParams: fokParams, dcaParams, - beneficiaries: undefined, + affiliateFees: undefined, } as ExecuteSwapParams, networkOptions, txOptions, @@ -136,6 +142,12 @@ export async function performVaultSwap( ); } + const keyring = new Keyring({ type: 'sr25519' }); + const broker = keyring.createFromUri('//BROKER_1'); + const brokerFees = { + account: broker.address, + commissionBps: 1, + }; // 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 @@ -143,6 +155,7 @@ export async function performVaultSwap( sourceAsset, destAsset, destAddress, + brokerFees, messageMetadata, amountToSwap, boostFeeBps, diff --git a/bouncer/tests/all_swaps.ts b/bouncer/tests/all_swaps.ts index 21708fc17c..4d87b36544 100644 --- a/bouncer/tests/all_swaps.ts +++ b/bouncer/tests/all_swaps.ts @@ -42,31 +42,33 @@ async function main() { } } - Object.values(Assets).forEach((sourceAsset) => { - Object.values(Assets) - .filter((destAsset) => sourceAsset !== destAsset) - .forEach((destAsset) => { - // Regular swaps - appendSwap(sourceAsset, destAsset, testSwap); + // Object.values(Assets).forEach((sourceAsset) => { + // Object.values(Assets) + // .filter((destAsset) => sourceAsset !== destAsset) + // .forEach((destAsset) => { + // // Regular swaps + // appendSwap(sourceAsset, destAsset, testSwap); - const sourceChain = chainFromAsset(sourceAsset); - const destChain = chainFromAsset(destAsset); - if (sourceChain === 'Ethereum' || sourceChain === 'Arbitrum') { - // Vault Swaps - appendSwap(sourceAsset, destAsset, testVaultSwap); + // const sourceChain = chainFromAsset(sourceAsset); + // const destChain = chainFromAsset(destAsset); + // if (sourceChain === 'Ethereum' || sourceChain === 'Arbitrum') { + // // Vault Swaps + // appendSwap(sourceAsset, destAsset, testVaultSwap); - if (ccmSupportedChains.includes(destChain)) { - // CCM Vault swaps - appendSwap(sourceAsset, destAsset, testVaultSwap, true); - } - } + // if (ccmSupportedChains.includes(destChain)) { + // // CCM Vault swaps + // appendSwap(sourceAsset, destAsset, testVaultSwap, true); + // } + // } - if (ccmSupportedChains.includes(destChain)) { - // CCM swaps - appendSwap(sourceAsset, destAsset, testSwap, true); - } - }); - }); + // if (ccmSupportedChains.includes(destChain)) { + // // CCM swaps + // appendSwap(sourceAsset, destAsset, testSwap, true); + // } + // }); + // }); + + appendSwap('ArbEth', 'Eth', testVaultSwap); await Promise.all(allSwaps); } diff --git a/engine/src/witness/common/cf_parameters.rs b/engine/src/witness/common/cf_parameters.rs index d27c5834a7..c119a29977 100644 --- a/engine/src/witness/common/cf_parameters.rs +++ b/engine/src/witness/common/cf_parameters.rs @@ -1,5 +1,5 @@ use cf_chains::{CcmAdditionalData, ChannelRefundParameters}; -use cf_primitives::{BasisPoints, Beneficiaries, DcaParameters}; +use cf_primitives::{AccountId, Affiliates, BasisPoints, Beneficiary, DcaParameters}; use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; @@ -25,7 +25,9 @@ pub struct VaultSwapParameters { pub refund_params: ChannelRefundParameters, pub dca_params: Option, pub boost_fee: Option, - pub broker_fees: Beneficiaries, + // TODO: Should we use u8 instead of BasisPoints for fees as in Bitcoin? + pub broker_fees: Beneficiary, + pub affiliate_fees: Affiliates, } #[cfg(test)] diff --git a/engine/src/witness/evm/vault.rs b/engine/src/witness/evm/vault.rs index 7f6f842abf..a35a7070f5 100644 --- a/engine/src/witness/evm/vault.rs +++ b/engine/src/witness/evm/vault.rs @@ -78,7 +78,8 @@ where try_into_encoded_address(try_into_primitive(dst_chain)?, dst_address.to_vec())?, None, event.tx_hash.into(), - vault_swap_parameters.broker_fees, + // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees + Default::default(), Some(vault_swap_parameters.refund_params), vault_swap_parameters.dca_params, vault_swap_parameters.boost_fee, @@ -107,7 +108,8 @@ where try_into_encoded_address(try_into_primitive(dst_chain)?, dst_address.to_vec())?, None, event.tx_hash.into(), - vault_swap_parameters.broker_fees, + // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees + Default::default(), Some(vault_swap_parameters.refund_params), vault_swap_parameters.dca_params, vault_swap_parameters.boost_fee, @@ -150,7 +152,8 @@ where }, }), event.tx_hash.into(), - vault_swap_parameters.broker_fees, + // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees + Default::default(), Some(vault_swap_parameters.refund_params), vault_swap_parameters.dca_params, vault_swap_parameters.boost_fee, @@ -196,7 +199,8 @@ where }, }), event.tx_hash.into(), - vault_swap_parameters.broker_fees, + // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees + Default::default(), Some(vault_swap_parameters.refund_params), vault_swap_parameters.dca_params, vault_swap_parameters.boost_fee, From 8d07d541e7c863b521718c17c3656fc24cd9c247 Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 8 Nov 2024 11:50:39 +0100 Subject: [PATCH 07/19] chore: revert all_swaps --- bouncer/tests/all_swaps.ts | 46 ++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/bouncer/tests/all_swaps.ts b/bouncer/tests/all_swaps.ts index 4d87b36544..21708fc17c 100644 --- a/bouncer/tests/all_swaps.ts +++ b/bouncer/tests/all_swaps.ts @@ -42,33 +42,31 @@ async function main() { } } - // Object.values(Assets).forEach((sourceAsset) => { - // Object.values(Assets) - // .filter((destAsset) => sourceAsset !== destAsset) - // .forEach((destAsset) => { - // // Regular swaps - // appendSwap(sourceAsset, destAsset, testSwap); + Object.values(Assets).forEach((sourceAsset) => { + Object.values(Assets) + .filter((destAsset) => sourceAsset !== destAsset) + .forEach((destAsset) => { + // Regular swaps + appendSwap(sourceAsset, destAsset, testSwap); - // const sourceChain = chainFromAsset(sourceAsset); - // const destChain = chainFromAsset(destAsset); - // if (sourceChain === 'Ethereum' || sourceChain === 'Arbitrum') { - // // Vault Swaps - // appendSwap(sourceAsset, destAsset, testVaultSwap); + const sourceChain = chainFromAsset(sourceAsset); + const destChain = chainFromAsset(destAsset); + if (sourceChain === 'Ethereum' || sourceChain === 'Arbitrum') { + // Vault Swaps + appendSwap(sourceAsset, destAsset, testVaultSwap); - // if (ccmSupportedChains.includes(destChain)) { - // // CCM Vault swaps - // appendSwap(sourceAsset, destAsset, testVaultSwap, true); - // } - // } + if (ccmSupportedChains.includes(destChain)) { + // CCM Vault swaps + appendSwap(sourceAsset, destAsset, testVaultSwap, true); + } + } - // if (ccmSupportedChains.includes(destChain)) { - // // CCM swaps - // appendSwap(sourceAsset, destAsset, testSwap, true); - // } - // }); - // }); - - appendSwap('ArbEth', 'Eth', testVaultSwap); + if (ccmSupportedChains.includes(destChain)) { + // CCM swaps + appendSwap(sourceAsset, destAsset, testSwap, true); + } + }); + }); await Promise.all(allSwaps); } From b6b62ae7c4a06c76c2d5cc5aad209010df229317 Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 8 Nov 2024 11:50:39 +0100 Subject: [PATCH 08/19] chore: update to 1.8.0-rc.3 --- bouncer/package.json | 2 +- bouncer/pnpm-lock.yaml | 25 ++++++++++--------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/bouncer/package.json b/bouncer/package.json index 0baa157a4c..ef1a6e856f 100644 --- a/bouncer/package.json +++ b/bouncer/package.json @@ -6,7 +6,7 @@ "prettier:write": "prettier --write ." }, "dependencies": { - "@chainflip/cli": "1.8.0-rc.2", + "@chainflip/cli": "1.8.0-rc.3", "@chainflip/utils": "^0.4.0", "@coral-xyz/anchor": "^0.30.1", "@iarna/toml": "^2.2.5", diff --git a/bouncer/pnpm-lock.yaml b/bouncer/pnpm-lock.yaml index 17dee78a33..d28a0243c5 100644 --- a/bouncer/pnpm-lock.yaml +++ b/bouncer/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@chainflip/cli': - specifier: 1.8.0-rc.2 - version: 1.8.0-rc.2(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10) + specifier: 1.8.0-rc.3 + version: 1.8.0-rc.3(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10) '@chainflip/utils': specifier: ^0.4.0 version: 0.4.0 @@ -145,8 +145,8 @@ packages: '@chainflip/bitcoin@1.1.1': resolution: {integrity: sha512-Jr6X/0QTSFYpTp23ZPhyioL6wL9x3Xpj6OGjadQKZyhobRN4BZkhLogv20HDYTJcRzVphzrTJArbEaNie04XVA==} - '@chainflip/cli@1.8.0-rc.2': - resolution: {integrity: sha512-CLAqtbQWhD4IzA1edwBbR0CgQej4DycvTPMcBPhNnSQoK85svB8u1ZYtn6XHz6yyoDJjbiDRrYN6AMIaGCjgDw==} + '@chainflip/cli@1.8.0-rc.3': + resolution: {integrity: sha512-W6//r1T7MyETbpw5Dt5PGtEPrLwzMtqajwzA+IB8rIewxojYj26zpWRrBCfl4ORoAz9rL1Pa8Nip2Q7Dtd8EVw==} hasBin: true peerDependencies: axios: ^1.x @@ -155,15 +155,12 @@ packages: '@chainflip/extrinsics@1.6.1': resolution: {integrity: sha512-sm3v2QguNW4/RiIZYHd+VRSxup2q4cVBY0VjbQJw6xYqhNCzZlt+Hl38Kni+TCHQu3hP/WgIF72yvvSs7/PBdg==} - '@chainflip/rpc@1.6.9': - resolution: {integrity: sha512-FtaoZBey4kIFhWQY5mMMAGXIgzSpZCX0DTnXQqPEn9q5qvt6FoQLDQk9k2IYKOTiIP+XsLy9w/WPPiH7+3BpnQ==} + '@chainflip/rpc@1.6.10': + resolution: {integrity: sha512-Drn4EseSJRIdR5Jsi7uxAD3KEv0bK+zYL+AiMqQ/hZTRnWoVQArKm7xsJxFTOiGscQXTlqRO/igTcGaghYUmmQ==} '@chainflip/solana@1.0.2': resolution: {integrity: sha512-8M+z0yjVCZBMpdCTGuBRt/Ns9NBJCNFvnSOjDu/kyaBRvxdUvECJOIseL0ENLBd0J/YmO9TcrZ30M0po3a96BA==} - '@chainflip/utils@0.3.0': - resolution: {integrity: sha512-csW8E3lRcSG1udN/kgsOhjZ7bfRrSbgv8Hdh/pzjM+VjQ6Hh46/LuiZHeQR0rQMimfG+lu2Afxk4jeRXRGFWTg==} - '@chainflip/utils@0.4.0': resolution: {integrity: sha512-OuMFlgbIbyJ1Kt39zytfGh4HZ5dVY25/a3qb0c1K137+QmhdOuF+R7QSkFPqYAfDG4KhGRqo0rgUtEpaEEcSKA==} @@ -3169,11 +3166,11 @@ snapshots: transitivePeerDependencies: - typescript - '@chainflip/cli@1.8.0-rc.2(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10)': + '@chainflip/cli@1.8.0-rc.3(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10)': dependencies: '@chainflip/bitcoin': 1.1.1(typescript@5.5.3) '@chainflip/extrinsics': 1.6.1 - '@chainflip/rpc': 1.6.9 + '@chainflip/rpc': 1.6.10 '@chainflip/solana': 1.0.2 '@chainflip/utils': 0.4.0 axios: 1.7.2 @@ -3189,17 +3186,15 @@ snapshots: '@chainflip/extrinsics@1.6.1': {} - '@chainflip/rpc@1.6.9': + '@chainflip/rpc@1.6.10': dependencies: - '@chainflip/utils': 0.3.0 + '@chainflip/utils': 0.4.0 zod: 3.23.8 '@chainflip/solana@1.0.2': dependencies: '@chainflip/utils': 0.4.0 - '@chainflip/utils@0.3.0': {} - '@chainflip/utils@0.4.0': {} '@coral-xyz/anchor-errors@0.30.1': {} From fab6ad1635941bbd8b60d6fa1ca5fa6eb2037baa Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 8 Nov 2024 12:33:25 +0100 Subject: [PATCH 09/19] chore: fix vault_swap_request --- engine/src/witness/arb.rs | 9 ++++++--- engine/src/witness/common/cf_parameters.rs | 3 ++- engine/src/witness/eth.rs | 9 ++++++--- engine/src/witness/evm/vault.rs | 23 ++++++++++++---------- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/engine/src/witness/arb.rs b/engine/src/witness/arb.rs index 630f2cefff..8aaae8406d 100644 --- a/engine/src/witness/arb.rs +++ b/engine/src/witness/arb.rs @@ -177,7 +177,8 @@ struct ArbCallBuilder {} use cf_chains::{address::EncodedAddress, CcmDepositMetadata, ChannelRefundParameters}; use cf_primitives::{ - Asset, AssetAmount, BasisPoints, Beneficiaries, DcaParameters, TransactionHash, + AccountId, Affiliates, Asset, AssetAmount, BasisPoints, Beneficiary, DcaParameters, + TransactionHash, }; impl super::evm::vault::IngressCallBuilder for ArbCallBuilder { @@ -190,7 +191,8 @@ impl super::evm::vault::IngressCallBuilder for ArbCallBuilder { destination_address: EncodedAddress, deposit_metadata: Option, tx_hash: TransactionHash, - _broker_fees: Beneficiaries, + _broker_fees: Beneficiary, + _affiliate_fees: Affiliates, refund_params: Option, dca_params: Option, // This is only to be checked in the pre-witnessed version @@ -205,8 +207,9 @@ impl super::evm::vault::IngressCallBuilder for ArbCallBuilder { deposit_metadata, tx_hash, deposit_details: Box::new(DepositDetails { tx_hashes: Some(vec![tx_hash.into()]) }), - // Defaulting to no broker fees until PRO-1743 is completed. + // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees broker_fees: Default::default(), + // affiliate_fees: Default::default(), boost_fee: boost_fee.unwrap_or_default(), dca_params, refund_params: refund_params.map(Box::new), diff --git a/engine/src/witness/common/cf_parameters.rs b/engine/src/witness/common/cf_parameters.rs index c119a29977..5bb4df1588 100644 --- a/engine/src/witness/common/cf_parameters.rs +++ b/engine/src/witness/common/cf_parameters.rs @@ -25,8 +25,9 @@ pub struct VaultSwapParameters { pub refund_params: ChannelRefundParameters, pub dca_params: Option, pub boost_fee: Option, - // TODO: Should we use u8 instead of BasisPoints for fees as in Bitcoin? + // TODO: Create BrokerAndFee instead so fee is also a u8? pub broker_fees: Beneficiary, + // TODO: Use AffiliateAndFee in PRO-1751 pub affiliate_fees: Affiliates, } diff --git a/engine/src/witness/eth.rs b/engine/src/witness/eth.rs index 368980d100..92e19a7f16 100644 --- a/engine/src/witness/eth.rs +++ b/engine/src/witness/eth.rs @@ -223,7 +223,8 @@ where use cf_chains::{address::EncodedAddress, CcmDepositMetadata, ChannelRefundParameters}; use cf_primitives::{ - Asset, AssetAmount, BasisPoints, Beneficiaries, DcaParameters, TransactionHash, + AccountId, Affiliates, Asset, AssetAmount, BasisPoints, Beneficiary, DcaParameters, + TransactionHash, }; pub struct EthCallBuilder {} @@ -238,7 +239,8 @@ impl super::evm::vault::IngressCallBuilder for EthCallBuilder { destination_address: EncodedAddress, deposit_metadata: Option, tx_hash: TransactionHash, - _broker_fees: Beneficiaries, + _broker_fees: Beneficiary, + _affiliate_fees: Affiliates, refund_params: Option, dca_params: Option, // This is only to be checked in the pre-witnessed version @@ -253,8 +255,9 @@ impl super::evm::vault::IngressCallBuilder for EthCallBuilder { deposit_metadata, tx_hash, deposit_details: Box::new(DepositDetails { tx_hashes: Some(vec![tx_hash.into()]) }), - // Defaulting to no broker fees until PRO-1743 is completed. + // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees broker_fees: Default::default(), + // affiliate_fees: Default::default(), boost_fee: boost_fee.unwrap_or_default(), dca_params, refund_params: refund_params.map(Box::new), diff --git a/engine/src/witness/evm/vault.rs b/engine/src/witness/evm/vault.rs index a35a7070f5..1ccfea60e5 100644 --- a/engine/src/witness/evm/vault.rs +++ b/engine/src/witness/evm/vault.rs @@ -22,7 +22,9 @@ use cf_chains::{ evm::DepositDetails, CcmChannelMetadata, CcmDepositMetadata, Chain, ChannelRefundParameters, }; -use cf_primitives::{Asset, BasisPoints, DcaParameters, ForeignChain}; +use cf_primitives::{ + AccountId, Affiliates, Asset, BasisPoints, Beneficiary, DcaParameters, ForeignChain, +}; use ethers::prelude::*; use state_chain_runtime::{EthereumInstance, Runtime, RuntimeCall}; @@ -78,8 +80,8 @@ where try_into_encoded_address(try_into_primitive(dst_chain)?, dst_address.to_vec())?, None, event.tx_hash.into(), - // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees - Default::default(), + vault_swap_parameters.broker_fees, + vault_swap_parameters.affiliate_fees, Some(vault_swap_parameters.refund_params), vault_swap_parameters.dca_params, vault_swap_parameters.boost_fee, @@ -108,8 +110,8 @@ where try_into_encoded_address(try_into_primitive(dst_chain)?, dst_address.to_vec())?, None, event.tx_hash.into(), - // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees - Default::default(), + vault_swap_parameters.broker_fees, + vault_swap_parameters.affiliate_fees, Some(vault_swap_parameters.refund_params), vault_swap_parameters.dca_params, vault_swap_parameters.boost_fee, @@ -152,8 +154,8 @@ where }, }), event.tx_hash.into(), - // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees - Default::default(), + vault_swap_parameters.broker_fees, + vault_swap_parameters.affiliate_fees, Some(vault_swap_parameters.refund_params), vault_swap_parameters.dca_params, vault_swap_parameters.boost_fee, @@ -199,8 +201,8 @@ where }, }), event.tx_hash.into(), - // TODO: To update after PRO-1751 for both broker_fees and affiliate_fees - Default::default(), + vault_swap_parameters.broker_fees, + vault_swap_parameters.affiliate_fees, Some(vault_swap_parameters.refund_params), vault_swap_parameters.dca_params, vault_swap_parameters.boost_fee, @@ -247,7 +249,8 @@ pub trait IngressCallBuilder { destination_address: EncodedAddress, deposit_metadata: Option, tx_hash: cf_primitives::TransactionHash, - broker_fees: cf_primitives::Beneficiaries, + broker_fees: Beneficiary, + affiliate_fees: Affiliates, refund_params: Option, dca_params: Option, boost_fee: Option, From 01455b66adf3d09142c557bdf8f0ff8c6f7a33a5 Mon Sep 17 00:00:00 2001 From: albert Date: Mon, 11 Nov 2024 12:36:07 +0100 Subject: [PATCH 10/19] chore: make brokerFees optional --- bouncer/shared/vault_swap.ts | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/bouncer/shared/vault_swap.ts b/bouncer/shared/vault_swap.ts index 22f747ffdf..ec2cccc22b 100644 --- a/bouncer/shared/vault_swap.ts +++ b/bouncer/shared/vault_swap.ts @@ -32,16 +32,16 @@ export async function executeVaultSwap( sourceAsset: Asset, destAsset: Asset, destAddress: string, - brokerFees: { - account: string; - commissionBps: number; - }, messageMetadata?: CcmDepositMetadata, amount?: string, boostFeeBps?: number, fillOrKillParams?: FillOrKillParamsX128, dcaParams?: DcaParams, wallet?: HDNodeWallet, + brokerFees?: { + account: string; + commissionBps: number; + }, ): ReturnType { const srcChain = chainFromAsset(sourceAsset); const destChain = chainFromAsset(destAsset); @@ -56,6 +56,11 @@ export async function executeVaultSwap( const evmWallet = wallet ?? (await createEvmWalletAndFund(sourceAsset)); + const brokerComission = brokerFees ?? { + account: new Keyring({ type: 'sr25519' }).createFromUri('//BROKER_1').address, + commissionBps: 1, + }; + 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 @@ -92,7 +97,7 @@ export async function executeVaultSwap( message: messageMetadata.message, ccmAdditionalData: messageMetadata.ccmAdditionalData, }, - brokerFees, + brokerFees: brokerComission, // The SDK will encode these parameters and the ccmAdditionalData // into the `cfParameters` field for the vault swap. boostFeeBps, @@ -142,12 +147,6 @@ export async function performVaultSwap( ); } - const keyring = new Keyring({ type: 'sr25519' }); - const broker = keyring.createFromUri('//BROKER_1'); - const brokerFees = { - account: broker.address, - commissionBps: 1, - }; // 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 @@ -155,7 +154,6 @@ export async function performVaultSwap( sourceAsset, destAsset, destAddress, - brokerFees, messageMetadata, amountToSwap, boostFeeBps, From ba6fe7bcf6bedd584bf1d0d0eb588e794ad1d87e Mon Sep 17 00:00:00 2001 From: albert Date: Mon, 11 Nov 2024 15:06:55 +0100 Subject: [PATCH 11/19] chore: add encoding test and update evm test --- bouncer/tests/evm_deposits.ts | 4 +-- engine/src/witness/common/cf_parameters.rs | 33 +++++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/bouncer/tests/evm_deposits.ts b/bouncer/tests/evm_deposits.ts index a55d278130..2022ea6552 100644 --- a/bouncer/tests/evm_deposits.ts +++ b/bouncer/tests/evm_deposits.ts @@ -97,8 +97,8 @@ async function testTxMultipleVaultSwaps(sourceAsset: Asset, destAsset: Asset) { assetContractId(destAsset), getContractAddress(chainFromAsset(sourceAsset), sourceAsset), amount, - // Encoded EVM refund address and no other swap parameters. - '0x0000000000000e879c89cad7076b347bde13c99cf2c33e7299b60000000000000000000000000000000000000000000000000000000000000000000000', + // Dummy encoded data containing a refund address and a broker accountId. + '0x000001000000000202020202020202020202020202020202020202000000000000000000000000000000000000000000000000000000000000000000000303030303030303030303030303030303030303030303030303030303030303040000', numSwaps, ) .encodeABI(); diff --git a/engine/src/witness/common/cf_parameters.rs b/engine/src/witness/common/cf_parameters.rs index 5bb4df1588..8577f7c77f 100644 --- a/engine/src/witness/common/cf_parameters.rs +++ b/engine/src/witness/common/cf_parameters.rs @@ -34,7 +34,7 @@ pub struct VaultSwapParameters { #[cfg(test)] mod tests { use super::*; - use cf_chains::MAX_CCM_ADDITIONAL_DATA_LENGTH; + use cf_chains::{ChannelRefundParameters, ForeignChainAddress, MAX_CCM_ADDITIONAL_DATA_LENGTH}; const MAX_VAULT_SWAP_PARAMETERS_LENGTH: u32 = 1_000; const MAX_CF_PARAM_LENGTH: u32 = @@ -50,4 +50,35 @@ mod tests { MAX_VAULT_SWAP_PARAMETERS_LENGTH as usize >= VaultSwapParameters::max_encoded_len() ); } + + #[test] + fn test_encoding() { + let vault_swap_parameters = VaultSwapParameters { + refund_params: ChannelRefundParameters { + retry_duration: 1, + refund_address: ForeignChainAddress::Eth(sp_core::H160::from([2; 20])), + min_price: Default::default(), + }, + dca_params: None, + boost_fee: None, + broker_fees: Beneficiary { account: AccountId::new([3; 32]), bps: 4 }, + affiliate_fees: Default::default(), + }; + + let cf_parameters = CfParameters { + ccm_additional_data: CcmAdditionalData::default(), + vault_swap_parameters, + }; + + let encoded = VersionedCfParameters::V0(cf_parameters).encode(); + + let expected_encoded: Vec = vec![ + 0, 0, 1, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 4, 0, 0, + ]; + + assert_eq!(encoded, expected_encoded); + } } From 1e6a3dace454bd1e5daebfde814cfee5765adf5d Mon Sep 17 00:00:00 2001 From: albert Date: Mon, 11 Nov 2024 16:59:54 +0100 Subject: [PATCH 12/19] chore: use AffiliateAndFee instead of ShortId --- engine/src/witness/arb.rs | 6 +++--- engine/src/witness/common/cf_parameters.rs | 12 +++++------- engine/src/witness/eth.rs | 9 +++------ engine/src/witness/evm/vault.rs | 5 +++-- state-chain/chains/src/btc/vault_swap_encoding.rs | 8 +------- state-chain/primitives/src/lib.rs | 6 ++++++ 6 files changed, 21 insertions(+), 25 deletions(-) diff --git a/engine/src/witness/arb.rs b/engine/src/witness/arb.rs index 8aaae8406d..0810ccc87c 100644 --- a/engine/src/witness/arb.rs +++ b/engine/src/witness/arb.rs @@ -3,7 +3,7 @@ mod chain_tracking; use std::{collections::HashMap, sync::Arc}; use cf_chains::{assets::arb::Asset as ArbAsset, evm::DepositDetails, Arbitrum}; -use cf_primitives::EpochIndex; +use cf_primitives::{AffiliateAndFee, EpochIndex}; use cf_utilities::task_scope::Scope; use futures_core::Future; use sp_core::H160; @@ -18,7 +18,7 @@ use crate::{ stream_api::{StreamApi, FINALIZED}, STATE_CHAIN_CONNECTION, }, - witness::{common::cf_parameters::ShortId, evm::erc20_deposits::usdc::UsdcEvents}, + witness::evm::erc20_deposits::usdc::UsdcEvents, }; use super::{ @@ -192,7 +192,7 @@ impl super::evm::vault::IngressCallBuilder for ArbCallBuilder { deposit_metadata: Option, tx_hash: TransactionHash, _broker_fees: Beneficiary, - _affiliate_fees: Affiliates, + _affiliate_fees: Affiliates, refund_params: Option, dca_params: Option, // This is only to be checked in the pre-witnessed version diff --git a/engine/src/witness/common/cf_parameters.rs b/engine/src/witness/common/cf_parameters.rs index 8577f7c77f..1242a20747 100644 --- a/engine/src/witness/common/cf_parameters.rs +++ b/engine/src/witness/common/cf_parameters.rs @@ -1,5 +1,7 @@ use cf_chains::{CcmAdditionalData, ChannelRefundParameters}; -use cf_primitives::{AccountId, Affiliates, BasisPoints, Beneficiary, DcaParameters}; +use cf_primitives::{ + AccountId, AffiliateAndFee, Affiliates, BasisPoints, Beneficiary, DcaParameters, +}; use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; @@ -17,9 +19,6 @@ pub struct CfParameters { pub type VersionedCcmCfParameters = VersionedCfParameters; -// TODO: Define this / implement it on the SC - PRO-1743. -pub type ShortId = u8; - #[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Debug)] pub struct VaultSwapParameters { pub refund_params: ChannelRefundParameters, @@ -27,8 +26,7 @@ pub struct VaultSwapParameters { pub boost_fee: Option, // TODO: Create BrokerAndFee instead so fee is also a u8? pub broker_fees: Beneficiary, - // TODO: Use AffiliateAndFee in PRO-1751 - pub affiliate_fees: Affiliates, + pub affiliate_fees: Affiliates, } #[cfg(test)] @@ -62,7 +60,7 @@ mod tests { dca_params: None, boost_fee: None, broker_fees: Beneficiary { account: AccountId::new([3; 32]), bps: 4 }, - affiliate_fees: Default::default(), + affiliate_fees: sp_core::bounded_vec![], }; let cf_parameters = CfParameters { diff --git a/engine/src/witness/eth.rs b/engine/src/witness/eth.rs index 92e19a7f16..b358c97ef5 100644 --- a/engine/src/witness/eth.rs +++ b/engine/src/witness/eth.rs @@ -4,7 +4,7 @@ mod state_chain_gateway; use std::{collections::HashMap, sync::Arc}; use cf_chains::{evm::DepositDetails, Ethereum}; -use cf_primitives::{chains::assets::eth::Asset as EthAsset, EpochIndex}; +use cf_primitives::{chains::assets::eth::Asset as EthAsset, AffiliateAndFee, EpochIndex}; use cf_utilities::task_scope::Scope; use futures_core::Future; use sp_core::H160; @@ -19,10 +19,7 @@ use crate::{ stream_api::{StreamApi, FINALIZED}, STATE_CHAIN_CONNECTION, }, - witness::{ - common::cf_parameters::ShortId, - evm::erc20_deposits::{flip::FlipEvents, usdc::UsdcEvents, usdt::UsdtEvents}, - }, + witness::evm::erc20_deposits::{flip::FlipEvents, usdc::UsdcEvents, usdt::UsdtEvents}, }; use super::{common::epoch_source::EpochSourceBuilder, evm::source::EvmSource}; @@ -240,7 +237,7 @@ impl super::evm::vault::IngressCallBuilder for EthCallBuilder { deposit_metadata: Option, tx_hash: TransactionHash, _broker_fees: Beneficiary, - _affiliate_fees: Affiliates, + _affiliate_fees: Affiliates, refund_params: Option, dca_params: Option, // This is only to be checked in the pre-witnessed version diff --git a/engine/src/witness/evm/vault.rs b/engine/src/witness/evm/vault.rs index 1ccfea60e5..42ff0dbcb9 100644 --- a/engine/src/witness/evm/vault.rs +++ b/engine/src/witness/evm/vault.rs @@ -23,7 +23,8 @@ use cf_chains::{ CcmChannelMetadata, CcmDepositMetadata, Chain, ChannelRefundParameters, }; use cf_primitives::{ - AccountId, Affiliates, Asset, BasisPoints, Beneficiary, DcaParameters, ForeignChain, + AccountId, AffiliateAndFee, Affiliates, Asset, BasisPoints, Beneficiary, DcaParameters, + ForeignChain, }; use ethers::prelude::*; use state_chain_runtime::{EthereumInstance, Runtime, RuntimeCall}; @@ -250,7 +251,7 @@ pub trait IngressCallBuilder { deposit_metadata: Option, tx_hash: cf_primitives::TransactionHash, broker_fees: Beneficiary, - affiliate_fees: Affiliates, + affiliate_fees: Affiliates, refund_params: Option, dca_params: Option, boost_fee: Option, diff --git a/state-chain/chains/src/btc/vault_swap_encoding.rs b/state-chain/chains/src/btc/vault_swap_encoding.rs index 565ceb6eb6..8e228d1e0d 100644 --- a/state-chain/chains/src/btc/vault_swap_encoding.rs +++ b/state-chain/chains/src/btc/vault_swap_encoding.rs @@ -1,5 +1,5 @@ use crate::address::EncodedAddress; -use cf_primitives::{Asset, AssetAmount, ForeignChain}; +use cf_primitives::{AffiliateAndFee, Asset, AssetAmount, ForeignChain}; use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; use sp_core::ConstU32; @@ -70,12 +70,6 @@ impl Decode for UtxoEncodedData { } } -#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Debug)] -pub struct AffiliateAndFee { - pub affiliate: u8, - pub fee: u8, -} - // We limit the number of affiliates in btc vault swaps to ensure that we // can always encode them inside a UTXO const MAX_AFFILIATES: u32 = 2; diff --git a/state-chain/primitives/src/lib.rs b/state-chain/primitives/src/lib.rs index b5c584df1a..4cb1f8bc16 100644 --- a/state-chain/primitives/src/lib.rs +++ b/state-chain/primitives/src/lib.rs @@ -399,6 +399,12 @@ pub type Affiliates = BoundedVec, ConstU32>; pub type Beneficiaries = BoundedVec, ConstU32<{ MAX_AFFILIATES + 1 }>>; +#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Debug)] +pub struct AffiliateAndFee { + pub affiliate: u8, + pub fee: u8, +} + #[derive( Clone, Debug, PartialEq, Eq, MaxEncodedLen, Encode, Decode, TypeInfo, Serialize, Deserialize, )] From 5ebde8af406551a430b7f8c31a6710714d83148d Mon Sep 17 00:00:00 2001 From: albert Date: Mon, 11 Nov 2024 18:06:32 +0100 Subject: [PATCH 13/19] chore: improve tests --- engine/src/witness/common/cf_parameters.rs | 31 +++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/engine/src/witness/common/cf_parameters.rs b/engine/src/witness/common/cf_parameters.rs index 1242a20747..b1d6441323 100644 --- a/engine/src/witness/common/cf_parameters.rs +++ b/engine/src/witness/common/cf_parameters.rs @@ -38,6 +38,13 @@ mod tests { const MAX_CF_PARAM_LENGTH: u32 = MAX_CCM_ADDITIONAL_DATA_LENGTH + MAX_VAULT_SWAP_PARAMETERS_LENGTH; + const REFERENCE_EXPECTED_ENCODED: &[u8] = &[ + 0, 1, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 4, 0, 0, + ]; + #[test] fn test_cf_parameters_max_length() { assert!( @@ -50,7 +57,7 @@ mod tests { } #[test] - fn test_encoding() { + fn test_versioned_cf_parameters() { let vault_swap_parameters = VaultSwapParameters { refund_params: ChannelRefundParameters { retry_duration: 1, @@ -63,19 +70,25 @@ mod tests { affiliate_fees: sp_core::bounded_vec![], }; - let cf_parameters = CfParameters { + let cf_parameters = CfParameters::<()> { + ccm_additional_data: (), + vault_swap_parameters: vault_swap_parameters.clone(), + }; + + let mut encoded = VersionedCfParameters::V0(cf_parameters).encode(); + + assert_eq!(encoded, REFERENCE_EXPECTED_ENCODED); + + let ccm_cf_parameters = CfParameters { ccm_additional_data: CcmAdditionalData::default(), vault_swap_parameters, }; - let encoded = VersionedCfParameters::V0(cf_parameters).encode(); + encoded = VersionedCcmCfParameters::V0(ccm_cf_parameters).encode(); - let expected_encoded: Vec = vec![ - 0, 0, 1, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 4, 0, 0, - ]; + // Extra byte for the empty ccm metadata + let mut expected_encoded = vec![0]; + expected_encoded.extend_from_slice(REFERENCE_EXPECTED_ENCODED); assert_eq!(encoded, expected_encoded); } From 44736e6b1de059ad8282b2d067a222b69e21afd4 Mon Sep 17 00:00:00 2001 From: albert Date: Wed, 13 Nov 2024 09:50:00 +0100 Subject: [PATCH 14/19] chore: refactor and plug in broker and affiliate fees --- engine/src/witness/arb.rs | 50 +++++++++++----------- engine/src/witness/btc/vault_swaps.rs | 2 +- engine/src/witness/common/cf_parameters.rs | 7 +-- engine/src/witness/eth.rs | 42 +++++++++--------- engine/src/witness/evm/vault.rs | 38 +++------------- state-chain/primitives/src/lib.rs | 12 +++--- 6 files changed, 64 insertions(+), 87 deletions(-) diff --git a/engine/src/witness/arb.rs b/engine/src/witness/arb.rs index 0afa73b50f..39ba96cb1a 100644 --- a/engine/src/witness/arb.rs +++ b/engine/src/witness/arb.rs @@ -2,10 +2,14 @@ mod chain_tracking; use std::{collections::HashMap, sync::Arc}; -use cf_chains::{assets::arb::Asset as ArbAsset, evm::DepositDetails, Arbitrum}; -use cf_primitives::{AffiliateAndFee, EpochIndex}; +use cf_chains::{address::EncodedAddress, evm::DepositDetails, Arbitrum, CcmDepositMetadata}; +use cf_primitives::{ + chains::assets::arb::Asset as ArbAsset, Asset, AssetAmount, Beneficiary, EpochIndex, + TransactionHash, +}; use cf_utilities::task_scope::Scope; use futures_core::Future; +use itertools::Itertools; use sp_core::H160; use crate::{ @@ -18,7 +22,7 @@ use crate::{ stream_api::{StreamApi, FINALIZED}, STATE_CHAIN_CONNECTION, }, - witness::evm::erc20_deposits::usdc::UsdcEvents, + witness::{common::cf_parameters::VaultSwapParameters, evm::erc20_deposits::usdc::UsdcEvents}, }; use super::{ @@ -81,11 +85,10 @@ where .get(&ArbAsset::ArbUsdc) .context("ArbitrumSupportedAssets does not include USDC")?; - let supported_arb_erc20_assets: HashMap = - supported_arb_erc20_assets - .into_iter() - .map(|(asset, address)| (address, asset.into())) - .collect(); + let supported_arb_erc20_assets: HashMap = supported_arb_erc20_assets + .into_iter() + .map(|(asset, address)| (address, asset.into())) + .collect(); let arb_source = EvmSource::<_, Arbitrum>::new(arb_client.clone()) .strictly_monotonic() @@ -162,7 +165,7 @@ where process_call, arb_client.clone(), vault_address, - cf_primitives::Asset::ArbEth, + Asset::ArbEth, cf_primitives::ForeignChain::Arbitrum, supported_arb_erc20_assets, ) @@ -175,12 +178,6 @@ where struct ArbCallBuilder {} -use cf_chains::{address::EncodedAddress, CcmDepositMetadata, ChannelRefundParameters}; -use cf_primitives::{ - AccountId, Affiliates, Asset, AssetAmount, BasisPoints, Beneficiary, DcaParameters, - TransactionHash, -}; - impl super::evm::vault::IngressCallBuilder for ArbCallBuilder { type Chain = Arbitrum; @@ -191,12 +188,7 @@ impl super::evm::vault::IngressCallBuilder for ArbCallBuilder { destination_address: EncodedAddress, deposit_metadata: Option, tx_hash: TransactionHash, - broker_fee: Beneficiary, - affiliate_fees: Affiliates, - refund_params: Option, - dca_params: Option, - // This is only to be checked in the pre-witnessed version - boost_fee: Option, + vault_swap_parameters: VaultSwapParameters, ) -> state_chain_runtime::RuntimeCall { state_chain_runtime::RuntimeCall::ArbitrumIngressEgress( pallet_cf_ingress_egress::Call::vault_swap_request { @@ -207,11 +199,17 @@ impl super::evm::vault::IngressCallBuilder for ArbCallBuilder { deposit_metadata, tx_hash, deposit_details: Box::new(DepositDetails { tx_hashes: Some(vec![tx_hash.into()]) }), - broker_fee, - affiliate_fees, - boost_fee: boost_fee.unwrap_or_default(), - dca_params, - refund_params: refund_params.map(Box::new), + broker_fee: vault_swap_parameters.broker_fee, + affiliate_fees: vault_swap_parameters + .affiliate_fees + .into_iter() + .map(|entry| Beneficiary { account: entry.affiliate.into(), bps: entry.fee.into() }) + .collect_vec() + .try_into() + .expect("runtime supports at least as many affiliates as we allow in cf_parameters encoding"), + boost_fee: vault_swap_parameters.boost_fee.unwrap_or_default(), + dca_params: vault_swap_parameters.dca_params, + refund_params: Some(Box::new(vault_swap_parameters.refund_params)), }, ) } diff --git a/engine/src/witness/btc/vault_swaps.rs b/engine/src/witness/btc/vault_swaps.rs index 23a591acda..85af70bcf9 100644 --- a/engine/src/witness/btc/vault_swaps.rs +++ b/engine/src/witness/btc/vault_swaps.rs @@ -201,7 +201,7 @@ mod tests { chunk_interval: 2, boost_fee: 5, broker_fee: 10, - affiliates: bounded_vec![AffiliateAndFee { affiliate: 17, fee: 7 }], + affiliates: bounded_vec![cf_primitives::AffiliateAndFee { affiliate: 17, fee: 7 }], }, }); diff --git a/engine/src/witness/common/cf_parameters.rs b/engine/src/witness/common/cf_parameters.rs index 50a5284e4a..2b5b569adc 100644 --- a/engine/src/witness/common/cf_parameters.rs +++ b/engine/src/witness/common/cf_parameters.rs @@ -1,9 +1,11 @@ use cf_chains::{CcmAdditionalData, ChannelRefundParameters}; use cf_primitives::{ - AccountId, AffiliateAndFee, Affiliates, BasisPoints, Beneficiary, DcaParameters, + AccountId, AffiliateAndFee, BasisPoints, Beneficiary, DcaParameters, MAX_AFFILIATES, }; use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; +use sp_core::ConstU32; +use sp_runtime::BoundedVec; #[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Debug)] pub enum VersionedCfParameters { @@ -24,9 +26,8 @@ pub struct VaultSwapParameters { pub refund_params: ChannelRefundParameters, pub dca_params: Option, pub boost_fee: Option, - // TODO: Create BrokerAndFee instead so fee is also a u8? pub broker_fee: Beneficiary, - pub affiliate_fees: Affiliates, + pub affiliate_fees: BoundedVec>, } #[cfg(test)] diff --git a/engine/src/witness/eth.rs b/engine/src/witness/eth.rs index bda39f5deb..5ee48c657e 100644 --- a/engine/src/witness/eth.rs +++ b/engine/src/witness/eth.rs @@ -3,10 +3,14 @@ mod state_chain_gateway; use std::{collections::HashMap, sync::Arc}; -use cf_chains::{evm::DepositDetails, Ethereum}; -use cf_primitives::{chains::assets::eth::Asset as EthAsset, AffiliateAndFee, EpochIndex}; +use cf_chains::{address::EncodedAddress, evm::DepositDetails, CcmDepositMetadata, Ethereum}; +use cf_primitives::{ + chains::assets::eth::Asset as EthAsset, Asset, AssetAmount, Beneficiary, EpochIndex, + TransactionHash, +}; use cf_utilities::task_scope::Scope; use futures_core::Future; +use itertools::Itertools; use sp_core::H160; use crate::{ @@ -19,7 +23,10 @@ use crate::{ stream_api::{StreamApi, FINALIZED}, STATE_CHAIN_CONNECTION, }, - witness::evm::erc20_deposits::{flip::FlipEvents, usdc::UsdcEvents, usdt::UsdtEvents}, + witness::{ + common::cf_parameters::VaultSwapParameters, + evm::erc20_deposits::{flip::FlipEvents, usdc::UsdcEvents, usdt::UsdtEvents}, + }, }; use super::{common::epoch_source::EpochSourceBuilder, evm::source::EvmSource}; @@ -218,12 +225,6 @@ where Ok(()) } -use cf_chains::{address::EncodedAddress, CcmDepositMetadata, ChannelRefundParameters}; -use cf_primitives::{ - AccountId, Affiliates, Asset, AssetAmount, BasisPoints, Beneficiary, DcaParameters, - TransactionHash, -}; - pub struct EthCallBuilder {} impl super::evm::vault::IngressCallBuilder for EthCallBuilder { @@ -236,12 +237,7 @@ impl super::evm::vault::IngressCallBuilder for EthCallBuilder { destination_address: EncodedAddress, deposit_metadata: Option, tx_hash: TransactionHash, - _broker_fee: Beneficiary, - _affiliate_fees: Affiliates, - refund_params: Option, - dca_params: Option, - // This is only to be checked in the pre-witnessed version - boost_fee: Option, + vault_swap_parameters: VaultSwapParameters, ) -> state_chain_runtime::RuntimeCall { state_chain_runtime::RuntimeCall::EthereumIngressEgress( pallet_cf_ingress_egress::Call::vault_swap_request { @@ -252,11 +248,17 @@ impl super::evm::vault::IngressCallBuilder for EthCallBuilder { deposit_metadata, tx_hash, deposit_details: Box::new(DepositDetails { tx_hashes: Some(vec![tx_hash.into()]) }), - broker_fee, - affiliate_fees, - boost_fee: boost_fee.unwrap_or_default(), - dca_params, - refund_params: refund_params.map(Box::new), + broker_fee: vault_swap_parameters.broker_fee, + affiliate_fees: vault_swap_parameters + .affiliate_fees + .into_iter() + .map(|entry| Beneficiary { account: entry.affiliate.into(), bps: entry.fee.into() }) + .collect_vec() + .try_into() + .expect("runtime supports at least as many affiliates as we allow in cf_parameters encoding"), + boost_fee: vault_swap_parameters.boost_fee.unwrap_or_default(), + dca_params: vault_swap_parameters.dca_params, + refund_params: Some(Box::new(vault_swap_parameters.refund_params)), }, ) } diff --git a/engine/src/witness/evm/vault.rs b/engine/src/witness/evm/vault.rs index 490ae6264e..42d9bd43a8 100644 --- a/engine/src/witness/evm/vault.rs +++ b/engine/src/witness/evm/vault.rs @@ -12,7 +12,6 @@ use super::{ }, contract_common::{events_at_block, Event}, }; -use cf_primitives::{AffiliateShortId, AssetAmount, EpochIndex}; use futures_core::Future; use anyhow::{anyhow, Result}; @@ -20,12 +19,9 @@ use cf_chains::{ address::{EncodedAddress, IntoForeignChainAddress}, eth::Address as EthereumAddress, evm::DepositDetails, - CcmChannelMetadata, CcmDepositMetadata, Chain, ChannelRefundParameters, -}; -use cf_primitives::{ - AccountId, AffiliateAndFee, Affiliates, Asset, BasisPoints, Beneficiary, DcaParameters, - ForeignChain, + CcmChannelMetadata, CcmDepositMetadata, Chain, }; +use cf_primitives::{Asset, AssetAmount, EpochIndex, ForeignChain}; use ethers::prelude::*; use state_chain_runtime::{EthereumInstance, Runtime, RuntimeCall}; @@ -81,11 +77,7 @@ where try_into_encoded_address(try_into_primitive(dst_chain)?, dst_address.to_vec())?, None, event.tx_hash.into(), - vault_swap_parameters.broker_fees, - vault_swap_parameters.affiliate_fees, - Some(vault_swap_parameters.refund_params), - vault_swap_parameters.dca_params, - vault_swap_parameters.boost_fee, + vault_swap_parameters, )) }, VaultEvents::SwapTokenFilter(SwapTokenFilter { @@ -111,11 +103,7 @@ where try_into_encoded_address(try_into_primitive(dst_chain)?, dst_address.to_vec())?, None, event.tx_hash.into(), - vault_swap_parameters.broker_fees, - vault_swap_parameters.affiliate_fees, - Some(vault_swap_parameters.refund_params), - vault_swap_parameters.dca_params, - vault_swap_parameters.boost_fee, + vault_swap_parameters, )) }, VaultEvents::XcallNativeFilter(XcallNativeFilter { @@ -155,11 +143,7 @@ where }, }), event.tx_hash.into(), - vault_swap_parameters.broker_fees, - vault_swap_parameters.affiliate_fees, - Some(vault_swap_parameters.refund_params), - vault_swap_parameters.dca_params, - vault_swap_parameters.boost_fee, + vault_swap_parameters, )) }, VaultEvents::XcallTokenFilter(XcallTokenFilter { @@ -202,11 +186,7 @@ where }, }), event.tx_hash.into(), - vault_swap_parameters.broker_fees, - vault_swap_parameters.affiliate_fees, - Some(vault_swap_parameters.refund_params), - vault_swap_parameters.dca_params, - vault_swap_parameters.boost_fee, + vault_swap_parameters, )) }, VaultEvents::TransferNativeFailedFilter(TransferNativeFailedFilter { @@ -250,11 +230,7 @@ pub trait IngressCallBuilder { destination_address: EncodedAddress, deposit_metadata: Option, tx_hash: cf_primitives::TransactionHash, - broker_fee: Beneficiary, - affiliate_fees: Affiliates, - refund_params: Option, - dca_params: Option, - boost_fee: Option, + vault_swap_parameters: VaultSwapParameters, ) -> state_chain_runtime::RuntimeCall; fn vault_transfer_failed( diff --git a/state-chain/primitives/src/lib.rs b/state-chain/primitives/src/lib.rs index 4ea276ad9a..f372a6f4a7 100644 --- a/state-chain/primitives/src/lib.rs +++ b/state-chain/primitives/src/lib.rs @@ -402,12 +402,6 @@ pub type Affiliates = BoundedVec, ConstU32>; pub type Beneficiaries = BoundedVec, ConstU32>; -#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Debug)] -pub struct AffiliateAndFee { - pub affiliate: u8, - pub fee: u8, -} - #[derive( Clone, Debug, PartialEq, Eq, MaxEncodedLen, Encode, Decode, TypeInfo, Serialize, Deserialize, )] @@ -416,6 +410,12 @@ pub struct Beneficiary { pub bps: BasisPoints, } +#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Debug)] +pub struct AffiliateAndFee { + pub affiliate: u8, + pub fee: u8, +} + #[derive( Clone, RuntimeDebug, From 22967a218adbaeafec4e58b4de88fcf6015caf1a Mon Sep 17 00:00:00 2001 From: albert Date: Wed, 13 Nov 2024 09:53:42 +0100 Subject: [PATCH 15/19] chore: fix bouncer lint --- bouncer/shared/evm_vault_swap.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bouncer/shared/evm_vault_swap.ts b/bouncer/shared/evm_vault_swap.ts index 6a58da4955..221a476820 100644 --- a/bouncer/shared/evm_vault_swap.ts +++ b/bouncer/shared/evm_vault_swap.ts @@ -12,9 +12,9 @@ import { } from '@chainflip/cli'; import { HDNodeWallet } from 'ethers'; import { randomBytes } from 'crypto'; -import Keyring from '../polkadot/keyring'; import { PublicKey, sendAndConfirmTransaction, Keypair } from '@solana/web3.js'; import { getAssociatedTokenAddressSync, TOKEN_PROGRAM_ID } from '@solana/spl-token'; +import Keyring from '../polkadot/keyring'; import { observeBalanceIncrease, getContractAddress, From 575915f6b2b8d033a26210507cb9a522a3615a33 Mon Sep 17 00:00:00 2001 From: albert Date: Wed, 13 Nov 2024 16:45:08 +0100 Subject: [PATCH 16/19] feat: make boost_fee not optional --- bouncer/package.json | 2 +- bouncer/pnpm-lock.yaml | 10 +++++----- bouncer/tests/evm_deposits.ts | 2 +- engine/src/witness/arb.rs | 2 +- engine/src/witness/common/cf_parameters.rs | 8 ++++---- engine/src/witness/eth.rs | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/bouncer/package.json b/bouncer/package.json index ef1a6e856f..82f126e65c 100644 --- a/bouncer/package.json +++ b/bouncer/package.json @@ -6,7 +6,7 @@ "prettier:write": "prettier --write ." }, "dependencies": { - "@chainflip/cli": "1.8.0-rc.3", + "@chainflip/cli": "1.8.0-rc.4", "@chainflip/utils": "^0.4.0", "@coral-xyz/anchor": "^0.30.1", "@iarna/toml": "^2.2.5", diff --git a/bouncer/pnpm-lock.yaml b/bouncer/pnpm-lock.yaml index d28a0243c5..d152d7a26d 100644 --- a/bouncer/pnpm-lock.yaml +++ b/bouncer/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@chainflip/cli': - specifier: 1.8.0-rc.3 - version: 1.8.0-rc.3(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10) + specifier: 1.8.0-rc.4 + version: 1.8.0-rc.4(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10) '@chainflip/utils': specifier: ^0.4.0 version: 0.4.0 @@ -145,8 +145,8 @@ packages: '@chainflip/bitcoin@1.1.1': resolution: {integrity: sha512-Jr6X/0QTSFYpTp23ZPhyioL6wL9x3Xpj6OGjadQKZyhobRN4BZkhLogv20HDYTJcRzVphzrTJArbEaNie04XVA==} - '@chainflip/cli@1.8.0-rc.3': - resolution: {integrity: sha512-W6//r1T7MyETbpw5Dt5PGtEPrLwzMtqajwzA+IB8rIewxojYj26zpWRrBCfl4ORoAz9rL1Pa8Nip2Q7Dtd8EVw==} + '@chainflip/cli@1.8.0-rc.4': + resolution: {integrity: sha512-dDS1cn9Aw1e8SpKX8PQsglSMX7QfJE6auh3fLq2SSkimdY2DyL+ek5oZhVY0X52oCSXoyQKc19JeuS0TlKuMng==} hasBin: true peerDependencies: axios: ^1.x @@ -3166,7 +3166,7 @@ snapshots: transitivePeerDependencies: - typescript - '@chainflip/cli@1.8.0-rc.3(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10)': + '@chainflip/cli@1.8.0-rc.4(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10)': dependencies: '@chainflip/bitcoin': 1.1.1(typescript@5.5.3) '@chainflip/extrinsics': 1.6.1 diff --git a/bouncer/tests/evm_deposits.ts b/bouncer/tests/evm_deposits.ts index 2022ea6552..2cd10f0533 100644 --- a/bouncer/tests/evm_deposits.ts +++ b/bouncer/tests/evm_deposits.ts @@ -98,7 +98,7 @@ async function testTxMultipleVaultSwaps(sourceAsset: Asset, destAsset: Asset) { getContractAddress(chainFromAsset(sourceAsset), sourceAsset), amount, // Dummy encoded data containing a refund address and a broker accountId. - '0x000001000000000202020202020202020202020202020202020202000000000000000000000000000000000000000000000000000000000000000000000303030303030303030303030303030303030303030303030303030303030303040000', + '0x00000100000000020202020202020202020202020202020202020200000000000000000000000000000000000000000000000000000000000000000000000303030303030303030303030303030303030303030303030303030303030303040000', numSwaps, ) .encodeABI(); diff --git a/engine/src/witness/arb.rs b/engine/src/witness/arb.rs index 39ba96cb1a..dc61b58258 100644 --- a/engine/src/witness/arb.rs +++ b/engine/src/witness/arb.rs @@ -207,7 +207,7 @@ impl super::evm::vault::IngressCallBuilder for ArbCallBuilder { .collect_vec() .try_into() .expect("runtime supports at least as many affiliates as we allow in cf_parameters encoding"), - boost_fee: vault_swap_parameters.boost_fee.unwrap_or_default(), + boost_fee: vault_swap_parameters.boost_fee, dca_params: vault_swap_parameters.dca_params, refund_params: Some(Box::new(vault_swap_parameters.refund_params)), }, diff --git a/engine/src/witness/common/cf_parameters.rs b/engine/src/witness/common/cf_parameters.rs index 2b5b569adc..e19ac78d81 100644 --- a/engine/src/witness/common/cf_parameters.rs +++ b/engine/src/witness/common/cf_parameters.rs @@ -25,7 +25,7 @@ pub type VersionedCcmCfParameters = VersionedCfParameters; pub struct VaultSwapParameters { pub refund_params: ChannelRefundParameters, pub dca_params: Option, - pub boost_fee: Option, + pub boost_fee: BasisPoints, pub broker_fee: Beneficiary, pub affiliate_fees: BoundedVec>, } @@ -42,8 +42,8 @@ mod tests { const REFERENCE_EXPECTED_ENCODED: &[u8] = &[ 0, 1, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 4, 0, 0, + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 4, 0, 0, ]; #[test] @@ -66,7 +66,7 @@ mod tests { min_price: Default::default(), }, dca_params: None, - boost_fee: None, + boost_fee: 0, broker_fee: Beneficiary { account: AccountId::new([3; 32]), bps: 4 }, affiliate_fees: sp_core::bounded_vec![], }; diff --git a/engine/src/witness/eth.rs b/engine/src/witness/eth.rs index 5ee48c657e..3cc700951c 100644 --- a/engine/src/witness/eth.rs +++ b/engine/src/witness/eth.rs @@ -256,7 +256,7 @@ impl super::evm::vault::IngressCallBuilder for EthCallBuilder { .collect_vec() .try_into() .expect("runtime supports at least as many affiliates as we allow in cf_parameters encoding"), - boost_fee: vault_swap_parameters.boost_fee.unwrap_or_default(), + boost_fee: vault_swap_parameters.boost_fee, dca_params: vault_swap_parameters.dca_params, refund_params: Some(Box::new(vault_swap_parameters.refund_params)), }, From 2930e68a1a7edbc57d8f80feb4079dc94610a39e Mon Sep 17 00:00:00 2001 From: albert Date: Wed, 13 Nov 2024 17:42:55 +0100 Subject: [PATCH 17/19] chore: fix bug in sdk --- bouncer/package.json | 2 +- bouncer/pnpm-lock.yaml | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bouncer/package.json b/bouncer/package.json index 82f126e65c..43e6eb6013 100644 --- a/bouncer/package.json +++ b/bouncer/package.json @@ -6,7 +6,7 @@ "prettier:write": "prettier --write ." }, "dependencies": { - "@chainflip/cli": "1.8.0-rc.4", + "@chainflip/cli": "1.8.0-rc.5", "@chainflip/utils": "^0.4.0", "@coral-xyz/anchor": "^0.30.1", "@iarna/toml": "^2.2.5", diff --git a/bouncer/pnpm-lock.yaml b/bouncer/pnpm-lock.yaml index d152d7a26d..4851fc2de1 100644 --- a/bouncer/pnpm-lock.yaml +++ b/bouncer/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@chainflip/cli': - specifier: 1.8.0-rc.4 - version: 1.8.0-rc.4(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10) + specifier: 1.8.0-rc.5 + version: 1.8.0-rc.5(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10) '@chainflip/utils': specifier: ^0.4.0 version: 0.4.0 @@ -145,8 +145,8 @@ packages: '@chainflip/bitcoin@1.1.1': resolution: {integrity: sha512-Jr6X/0QTSFYpTp23ZPhyioL6wL9x3Xpj6OGjadQKZyhobRN4BZkhLogv20HDYTJcRzVphzrTJArbEaNie04XVA==} - '@chainflip/cli@1.8.0-rc.4': - resolution: {integrity: sha512-dDS1cn9Aw1e8SpKX8PQsglSMX7QfJE6auh3fLq2SSkimdY2DyL+ek5oZhVY0X52oCSXoyQKc19JeuS0TlKuMng==} + '@chainflip/cli@1.8.0-rc.5': + resolution: {integrity: sha512-L8vyyycg5IUybIYo6GAqnOAIcDIVL4v+oCB8WPwDzOA1nW2BEsEs+IHbLixzgFrS3b2DIuty1iQ6IyMEQzTj7w==} hasBin: true peerDependencies: axios: ^1.x @@ -3166,7 +3166,7 @@ snapshots: transitivePeerDependencies: - typescript - '@chainflip/cli@1.8.0-rc.4(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10)': + '@chainflip/cli@1.8.0-rc.5(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10)': dependencies: '@chainflip/bitcoin': 1.1.1(typescript@5.5.3) '@chainflip/extrinsics': 1.6.1 From d8076011023a94c00b7f875809277e915366f858 Mon Sep 17 00:00:00 2001 From: albert Date: Thu, 14 Nov 2024 07:59:34 +0100 Subject: [PATCH 18/19] chore: address comments --- bouncer/package.json | 2 +- bouncer/pnpm-lock.yaml | 10 +++++----- bouncer/tests/evm_deposits.ts | 2 +- engine/src/witness/arb.rs | 2 +- engine/src/witness/common/cf_parameters.rs | 13 +++++-------- engine/src/witness/eth.rs | 2 +- 6 files changed, 14 insertions(+), 17 deletions(-) diff --git a/bouncer/package.json b/bouncer/package.json index 43e6eb6013..f7dda43537 100644 --- a/bouncer/package.json +++ b/bouncer/package.json @@ -6,7 +6,7 @@ "prettier:write": "prettier --write ." }, "dependencies": { - "@chainflip/cli": "1.8.0-rc.5", + "@chainflip/cli": "1.8.0-rc.6", "@chainflip/utils": "^0.4.0", "@coral-xyz/anchor": "^0.30.1", "@iarna/toml": "^2.2.5", diff --git a/bouncer/pnpm-lock.yaml b/bouncer/pnpm-lock.yaml index 4851fc2de1..9ca03356b0 100644 --- a/bouncer/pnpm-lock.yaml +++ b/bouncer/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@chainflip/cli': - specifier: 1.8.0-rc.5 - version: 1.8.0-rc.5(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10) + specifier: 1.8.0-rc.6 + version: 1.8.0-rc.6(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10) '@chainflip/utils': specifier: ^0.4.0 version: 0.4.0 @@ -145,8 +145,8 @@ packages: '@chainflip/bitcoin@1.1.1': resolution: {integrity: sha512-Jr6X/0QTSFYpTp23ZPhyioL6wL9x3Xpj6OGjadQKZyhobRN4BZkhLogv20HDYTJcRzVphzrTJArbEaNie04XVA==} - '@chainflip/cli@1.8.0-rc.5': - resolution: {integrity: sha512-L8vyyycg5IUybIYo6GAqnOAIcDIVL4v+oCB8WPwDzOA1nW2BEsEs+IHbLixzgFrS3b2DIuty1iQ6IyMEQzTj7w==} + '@chainflip/cli@1.8.0-rc.6': + resolution: {integrity: sha512-3YtEFJNeDcwELcSk23gkJcoxcjAYuvcc+YyQsazYO7VHTMmWg910Md/RXlbTjE6BrBwW5l2Nlbzl6822qy88Kg==} hasBin: true peerDependencies: axios: ^1.x @@ -3166,7 +3166,7 @@ snapshots: transitivePeerDependencies: - typescript - '@chainflip/cli@1.8.0-rc.5(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10)': + '@chainflip/cli@1.8.0-rc.6(axios@1.7.2)(bufferutil@4.0.8)(ethers@6.13.2(bufferutil@4.0.8)(utf-8-validate@5.0.10))(typescript@5.5.3)(utf-8-validate@5.0.10)': dependencies: '@chainflip/bitcoin': 1.1.1(typescript@5.5.3) '@chainflip/extrinsics': 1.6.1 diff --git a/bouncer/tests/evm_deposits.ts b/bouncer/tests/evm_deposits.ts index 2cd10f0533..2022ea6552 100644 --- a/bouncer/tests/evm_deposits.ts +++ b/bouncer/tests/evm_deposits.ts @@ -98,7 +98,7 @@ async function testTxMultipleVaultSwaps(sourceAsset: Asset, destAsset: Asset) { getContractAddress(chainFromAsset(sourceAsset), sourceAsset), amount, // Dummy encoded data containing a refund address and a broker accountId. - '0x00000100000000020202020202020202020202020202020202020200000000000000000000000000000000000000000000000000000000000000000000000303030303030303030303030303030303030303030303030303030303030303040000', + '0x000001000000000202020202020202020202020202020202020202000000000000000000000000000000000000000000000000000000000000000000000303030303030303030303030303030303030303030303030303030303030303040000', numSwaps, ) .encodeABI(); diff --git a/engine/src/witness/arb.rs b/engine/src/witness/arb.rs index dc61b58258..2e400dc603 100644 --- a/engine/src/witness/arb.rs +++ b/engine/src/witness/arb.rs @@ -207,7 +207,7 @@ impl super::evm::vault::IngressCallBuilder for ArbCallBuilder { .collect_vec() .try_into() .expect("runtime supports at least as many affiliates as we allow in cf_parameters encoding"), - boost_fee: vault_swap_parameters.boost_fee, + boost_fee: vault_swap_parameters.boost_fee.into(), dca_params: vault_swap_parameters.dca_params, refund_params: Some(Box::new(vault_swap_parameters.refund_params)), }, diff --git a/engine/src/witness/common/cf_parameters.rs b/engine/src/witness/common/cf_parameters.rs index e19ac78d81..e873f0d69a 100644 --- a/engine/src/witness/common/cf_parameters.rs +++ b/engine/src/witness/common/cf_parameters.rs @@ -1,7 +1,5 @@ use cf_chains::{CcmAdditionalData, ChannelRefundParameters}; -use cf_primitives::{ - AccountId, AffiliateAndFee, BasisPoints, Beneficiary, DcaParameters, MAX_AFFILIATES, -}; +use cf_primitives::{AccountId, AffiliateAndFee, Beneficiary, DcaParameters, MAX_AFFILIATES}; use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; use sp_core::ConstU32; @@ -25,7 +23,7 @@ pub type VersionedCcmCfParameters = VersionedCfParameters; pub struct VaultSwapParameters { pub refund_params: ChannelRefundParameters, pub dca_params: Option, - pub boost_fee: BasisPoints, + pub boost_fee: u8, pub broker_fee: Beneficiary, pub affiliate_fees: BoundedVec>, } @@ -42,8 +40,8 @@ mod tests { const REFERENCE_EXPECTED_ENCODED: &[u8] = &[ 0, 1, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 4, 0, 0, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 4, 0, 0, ]; #[test] @@ -88,8 +86,7 @@ mod tests { encoded = VersionedCcmCfParameters::V0(ccm_cf_parameters).encode(); // Extra byte for the empty ccm metadata - let mut expected_encoded = vec![0]; - expected_encoded.extend_from_slice(REFERENCE_EXPECTED_ENCODED); + let expected_encoded = [vec![0], Vec::from(REFERENCE_EXPECTED_ENCODED)].concat(); assert_eq!(encoded, expected_encoded); } diff --git a/engine/src/witness/eth.rs b/engine/src/witness/eth.rs index 3cc700951c..a433f6e51d 100644 --- a/engine/src/witness/eth.rs +++ b/engine/src/witness/eth.rs @@ -256,7 +256,7 @@ impl super::evm::vault::IngressCallBuilder for EthCallBuilder { .collect_vec() .try_into() .expect("runtime supports at least as many affiliates as we allow in cf_parameters encoding"), - boost_fee: vault_swap_parameters.boost_fee, + boost_fee: vault_swap_parameters.boost_fee.into(), dca_params: vault_swap_parameters.dca_params, refund_params: Some(Box::new(vault_swap_parameters.refund_params)), }, From 53d82dd9c6c5194fd32e71789f61987c78854fe1 Mon Sep 17 00:00:00 2001 From: albert Date: Thu, 14 Nov 2024 09:11:07 +0100 Subject: [PATCH 19/19] feat: make refund_params non-optional for vault swaps in extrinsic --- engine/src/witness/arb.rs | 2 +- engine/src/witness/btc/vault_swaps.rs | 8 +++--- engine/src/witness/eth.rs | 2 +- .../cf-integration-tests/src/solana.rs | 17 ++++++++----- .../cf-integration-tests/src/swapping.rs | 17 ++++++++----- .../cf-ingress-egress/src/benchmarking.rs | 6 ++++- .../pallets/cf-ingress-egress/src/lib.rs | 22 ++++++++-------- .../pallets/cf-ingress-egress/src/tests.rs | 25 +++++++++++-------- 8 files changed, 59 insertions(+), 40 deletions(-) diff --git a/engine/src/witness/arb.rs b/engine/src/witness/arb.rs index 2e400dc603..60808c5503 100644 --- a/engine/src/witness/arb.rs +++ b/engine/src/witness/arb.rs @@ -209,7 +209,7 @@ impl super::evm::vault::IngressCallBuilder for ArbCallBuilder { .expect("runtime supports at least as many affiliates as we allow in cf_parameters encoding"), boost_fee: vault_swap_parameters.boost_fee.into(), dca_params: vault_swap_parameters.dca_params, - refund_params: Some(Box::new(vault_swap_parameters.refund_params)), + refund_params: Box::new(vault_swap_parameters.refund_params), }, ) } diff --git a/engine/src/witness/btc/vault_swaps.rs b/engine/src/witness/btc/vault_swaps.rs index 85af70bcf9..c476a60d2c 100644 --- a/engine/src/witness/btc/vault_swaps.rs +++ b/engine/src/witness/btc/vault_swaps.rs @@ -154,11 +154,11 @@ pub fn try_extract_vault_swap_call( .collect_vec() .try_into() .expect("runtime supports at least as many affiliates as we allow in UTXO encoding"), - refund_params: Some(Box::new(ChannelRefundParameters { + refund_params: Box::new(ChannelRefundParameters { retry_duration: data.parameters.retry_duration.into(), refund_address: ForeignChainAddress::Btc(refund_address), min_price, - })), + }), dca_params: Some(DcaParameters { number_of_chunks: data.parameters.number_of_chunks.into(), chunk_interval: data.parameters.chunk_interval.into(), @@ -306,14 +306,14 @@ mod tests { bps: MOCK_SWAP_PARAMS.parameters.affiliates[0].fee.into(), }], deposit_metadata: None, - refund_params: Some(Box::new(ChannelRefundParameters { + refund_params: Box::new(ChannelRefundParameters { retry_duration: MOCK_SWAP_PARAMS.parameters.retry_duration.into(), refund_address: ForeignChainAddress::Btc(refund_pubkey), min_price: sqrt_price_to_price(bounded_sqrt_price( MOCK_SWAP_PARAMS.parameters.min_output_amount.into(), DEPOSIT_AMOUNT.into(), )), - })), + }), dca_params: Some(DcaParameters { number_of_chunks: MOCK_SWAP_PARAMS.parameters.number_of_chunks.into(), chunk_interval: MOCK_SWAP_PARAMS.parameters.chunk_interval.into(), diff --git a/engine/src/witness/eth.rs b/engine/src/witness/eth.rs index a433f6e51d..f77dc0695e 100644 --- a/engine/src/witness/eth.rs +++ b/engine/src/witness/eth.rs @@ -258,7 +258,7 @@ impl super::evm::vault::IngressCallBuilder for EthCallBuilder { .expect("runtime supports at least as many affiliates as we allow in cf_parameters encoding"), boost_fee: vault_swap_parameters.boost_fee.into(), dca_params: vault_swap_parameters.dca_params, - refund_params: Some(Box::new(vault_swap_parameters.refund_params)), + refund_params: Box::new(vault_swap_parameters.refund_params), }, ) } diff --git a/state-chain/cf-integration-tests/src/solana.rs b/state-chain/cf-integration-tests/src/solana.rs index 889f26bc3c..d9e3ddf9e3 100644 --- a/state-chain/cf-integration-tests/src/solana.rs +++ b/state-chain/cf-integration-tests/src/solana.rs @@ -14,9 +14,9 @@ use cf_chains::{ SolAddress, SolApiEnvironment, SolCcmAccounts, SolCcmAddress, SolHash, SolPubkey, SolanaCrypto, }, - CcmChannelMetadata, CcmDepositMetadata, CcmFailReason, Chain, ExecutexSwapAndCallError, - ForeignChainAddress, RequiresSignatureRefresh, SetAggKeyWithAggKey, SetAggKeyWithAggKeyError, - Solana, SwapOrigin, TransactionBuilder, + CcmChannelMetadata, CcmDepositMetadata, CcmFailReason, Chain, ChannelRefundParameters, + ExecutexSwapAndCallError, ForeignChainAddress, RequiresSignatureRefresh, SetAggKeyWithAggKey, + SetAggKeyWithAggKeyError, Solana, SwapOrigin, TransactionBuilder, }; use cf_primitives::{AccountRole, AuthorityCount, ForeignChain, SwapRequestId}; use cf_test_utilities::{assert_events_match, assert_has_matching_event}; @@ -56,6 +56,11 @@ const BOB: AccountId = AccountId::new([0x44; 32]); const DEPOSIT_AMOUNT: u64 = 5_000_000_000u64; // 5 Sol const FALLBACK_ADDRESS: SolAddress = SolAddress([0xf0; 32]); +const REFUND_PARAMS: ChannelRefundParameters = ChannelRefundParameters { + retry_duration: 0, + refund_address: ForeignChainAddress::Sol(FALLBACK_ADDRESS), + min_price: sp_core::U256::zero(), +}; type SolanaElectionVote = BoundedBTreeMap< CompositeElectionIdentifierOf< @@ -442,7 +447,7 @@ fn solana_ccm_fails_with_invalid_input() { bps: 0, }, affiliate_fees: Default::default(), - refund_params: None, + refund_params: Box::new(REFUND_PARAMS), dca_params: None, boost_fee: 0, } @@ -500,7 +505,7 @@ fn solana_ccm_fails_with_invalid_input() { bps: 0, }, affiliate_fees: Default::default(), - refund_params: None, + refund_params: Box::new(REFUND_PARAMS), dca_params: None, boost_fee: 0, }, @@ -728,7 +733,7 @@ fn solana_ccm_execution_error_can_trigger_fallback() { bps: 0, }, affiliate_fees: Default::default(), - refund_params: None, + refund_params: Box::new(REFUND_PARAMS), dca_params: None, boost_fee: 0, diff --git a/state-chain/cf-integration-tests/src/swapping.rs b/state-chain/cf-integration-tests/src/swapping.rs index c6bb8d27ae..766fd44548 100644 --- a/state-chain/cf-integration-tests/src/swapping.rs +++ b/state-chain/cf-integration-tests/src/swapping.rs @@ -18,9 +18,9 @@ use cf_chains::{ assets::eth::Asset as EthAsset, eth::{api::EthereumApi, EthereumTrackedData}, evm::DepositDetails, - CcmChannelMetadata, CcmDepositMetadata, Chain, ChainState, DefaultRetryPolicy, Ethereum, - ExecutexSwapAndCall, ForeignChain, ForeignChainAddress, RetryPolicy, SwapOrigin, - TransactionBuilder, TransferAssetParams, + CcmChannelMetadata, CcmDepositMetadata, Chain, ChainState, ChannelRefundParameters, + DefaultRetryPolicy, Ethereum, ExecutexSwapAndCall, ForeignChain, ForeignChainAddress, + RetryPolicy, SwapOrigin, TransactionBuilder, TransferAssetParams, }; use cf_primitives::{ AccountId, AccountRole, Asset, AssetAmount, AuthorityCount, SwapId, FLIPPERINOS_PER_FLIP, @@ -53,6 +53,11 @@ use state_chain_runtime::{ const DORIS: AccountId = AccountId::new([0x11; 32]); const ZION: AccountId = AccountId::new([0x22; 32]); +const ETH_REFUND_PARAMS: ChannelRefundParameters = ChannelRefundParameters { + retry_duration: 5, + refund_address: ForeignChainAddress::Eth(sp_core::H160([100u8; 20])), + min_price: sp_core::U256::zero(), +}; fn new_pool(unstable_asset: Asset, fee_hundredth_pips: u32, initial_price: Price) { assert_ok!(LiquidityPools::new_pool( @@ -585,7 +590,7 @@ fn can_process_ccm_via_direct_deposit() { bps: 0, }, affiliate_fees: Default::default(), - refund_params: None, + refund_params: Box::new(ETH_REFUND_PARAMS), dca_params: None, boost_fee: 0, }, @@ -639,7 +644,7 @@ fn failed_swaps_are_rolled_back() { bps: 0, }, affiliate_fees: Default::default(), - refund_params: None, + refund_params: Box::new(ETH_REFUND_PARAMS), dca_params: None, boost_fee: 0, }, @@ -806,7 +811,7 @@ fn can_resign_failed_ccm() { }, affiliate_fees: Default::default(), - refund_params: None, + refund_params: Box::new(ETH_REFUND_PARAMS), dca_params: None, boost_fee: 0, }, diff --git a/state-chain/pallets/cf-ingress-egress/src/benchmarking.rs b/state-chain/pallets/cf-ingress-egress/src/benchmarking.rs index 80cd1360a5..662110aa4b 100644 --- a/state-chain/pallets/cf-ingress-egress/src/benchmarking.rs +++ b/state-chain/pallets/cf-ingress-egress/src/benchmarking.rs @@ -316,7 +316,11 @@ mod benchmarks { deposit_details: Box::new(BenchmarkValue::benchmark_value()), broker_fee: cf_primitives::Beneficiary { account: account("broker", 0, 0), bps: 0 }, affiliate_fees: Default::default(), - refund_params: None, + refund_params: Box::new(ChannelRefundParameters { + retry_duration: Default::default(), + refund_address: ForeignChainAddress::Eth(Default::default()), + min_price: Default::default(), + }), dca_params: None, boost_fee: 0, }; diff --git a/state-chain/pallets/cf-ingress-egress/src/lib.rs b/state-chain/pallets/cf-ingress-egress/src/lib.rs index 33c5f59d93..572ad6aa02 100644 --- a/state-chain/pallets/cf-ingress-egress/src/lib.rs +++ b/state-chain/pallets/cf-ingress-egress/src/lib.rs @@ -1309,7 +1309,7 @@ pub mod pallet { deposit_details: Box<::DepositDetails>, broker_fee: Beneficiary, affiliate_fees: Affiliates, - refund_params: Option>, + refund_params: Box, dca_params: Option, boost_fee: BasisPoints, ) -> DispatchResult { @@ -1324,7 +1324,7 @@ pub mod pallet { *deposit_details, broker_fee, affiliate_fees, - refund_params.map(|boxed| *boxed), + *refund_params, dca_params, boost_fee, ); @@ -2129,7 +2129,7 @@ impl, I: 'static> Pallet { deposit_details: ::DepositDetails, broker_fee: Beneficiary, affiliate_fees: Affiliates, - refund_params: Option, + refund_params: ChannelRefundParameters, dca_params: Option, // This is only to be checked in the pre-witnessed version (not implemented yet) _boost_fee: BasisPoints, @@ -2203,13 +2203,13 @@ impl, I: 'static> Pallet { SwapRequestType::Regular { output_address: destination_address_internal.clone() } }; - if let Some(params) = &refund_params { - if let Err(err) = T::SwapLimitsProvider::validate_refund_params(params.retry_duration) { - log::warn!( - "Failed to process vault swap due to invalid refund params. Tx hash: {tx_hash:?}. Error: {err:?}", - ); - return; - } + if let Err(err) = + T::SwapLimitsProvider::validate_refund_params(refund_params.retry_duration) + { + log::warn!( + "Failed to process vault swap due to invalid refund params. Tx hash: {tx_hash:?}. Error: {err:?}", + ); + return; } if let Some(params) = &dca_params { @@ -2266,7 +2266,7 @@ impl, I: 'static> Pallet { destination_asset, request_type, broker_fees, - refund_params, + Some(refund_params), dca_params, SwapOrigin::Vault { tx_hash }, ); diff --git a/state-chain/pallets/cf-ingress-egress/src/tests.rs b/state-chain/pallets/cf-ingress-egress/src/tests.rs index 1d5d2be1a3..2e35a880ab 100644 --- a/state-chain/pallets/cf-ingress-egress/src/tests.rs +++ b/state-chain/pallets/cf-ingress-egress/src/tests.rs @@ -16,8 +16,8 @@ use cf_chains::{ btc::{BitcoinNetwork, ScriptPubkey}, evm::{DepositDetails, EvmFetchId}, mocks::MockEthereum, - CcmChannelMetadata, CcmFailReason, DepositChannel, ExecutexSwapAndCall, SwapOrigin, - TransferAssetParams, + CcmChannelMetadata, CcmFailReason, ChannelRefundParameters, DepositChannel, + ExecutexSwapAndCall, SwapOrigin, TransferAssetParams, }; use cf_primitives::{ AffiliateShortId, AssetAmount, BasisPoints, Beneficiary, ChannelId, ForeignChain, @@ -54,6 +54,11 @@ const BOB_ETH_ADDRESS: EthereumAddress = H160([101u8; 20]); const ETH_ETH: EthAsset = EthAsset::Eth; const ETH_FLIP: EthAsset = EthAsset::Flip; const DEFAULT_DEPOSIT_AMOUNT: u128 = 1_000; +const ETH_REFUND_PARAMS: ChannelRefundParameters = ChannelRefundParameters { + retry_duration: 0, + refund_address: ForeignChainAddress::Eth(ALICE_ETH_ADDRESS), + min_price: sp_core::U256::zero(), +}; #[track_caller] fn expect_size_of_address_pool(size: usize) { @@ -1807,7 +1812,7 @@ fn can_request_swap_via_extrinsic() { Box::new(DepositDetails { tx_hashes: None }), Beneficiary { account: BROKER, bps: 0 }, Default::default(), - None, + Box::new(ETH_REFUND_PARAMS), None, 0, )); @@ -1865,7 +1870,7 @@ fn vault_swaps_support_affiliate_fees() { Beneficiary { account: AFFILIATE_SHORT_1, bps: AFFILIATE_FEE }, Beneficiary { account: AFFILIATE_SHORT_2, bps: AFFILIATE_FEE } ], - None, + Box::new(ETH_REFUND_PARAMS), None, 0 )); @@ -1919,7 +1924,7 @@ fn charge_no_broker_fees_on_unknown_primary_broker() { Box::new(DepositDetails { tx_hashes: None }), Beneficiary { account: NOT_A_BROKER, bps: BROKER_FEE }, Default::default(), - None, + Box::new(ETH_REFUND_PARAMS), None, 0 )); @@ -1975,7 +1980,7 @@ fn can_request_ccm_swap_via_extrinsic() { Box::new(DepositDetails { tx_hashes: None }), Beneficiary { account: BROKER, bps: 0 }, Default::default(), - None, + Box::new(ETH_REFUND_PARAMS), None, 0 )); @@ -2023,7 +2028,7 @@ fn rejects_invalid_swap_by_witnesser() { Box::new(DepositDetails { tx_hashes: None }), Beneficiary { account: 0, bps: 0 }, Default::default(), - None, + Box::new(ETH_REFUND_PARAMS), None, 0 ),); @@ -2043,7 +2048,7 @@ fn rejects_invalid_swap_by_witnesser() { Box::new(DepositDetails { tx_hashes: None }), Beneficiary { account: 0, bps: 0 }, Default::default(), - None, + Box::new(ETH_REFUND_PARAMS), None, 0 ),); @@ -2079,7 +2084,7 @@ fn failed_ccm_deposit_can_deposit_event() { Box::new(DepositDetails { tx_hashes: None }), Beneficiary { account: 0, bps: 0 }, Default::default(), - None, + Box::new(ETH_REFUND_PARAMS), None, 0 )); @@ -2106,7 +2111,7 @@ fn failed_ccm_deposit_can_deposit_event() { Box::new(DepositDetails { tx_hashes: None }), Beneficiary { account: 0, bps: 0 }, Default::default(), - None, + Box::new(ETH_REFUND_PARAMS), None, 0 ));