From b8d99808c74e19032cf21fcc35b739f4b4a409c1 Mon Sep 17 00:00:00 2001 From: Saad Ahmed Siddiqui Date: Mon, 21 Oct 2024 11:43:20 +0200 Subject: [PATCH 1/3] same interfaces for substrate and evm --- packages/substrate/src/fungible.ts | 26 +++++++++++++------------- packages/substrate/src/index.ts | 1 + packages/utils/src/liquidity.ts | 4 ++-- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/substrate/src/fungible.ts b/packages/substrate/src/fungible.ts index e0b3acb7a..5b296a0c7 100644 --- a/packages/substrate/src/fungible.ts +++ b/packages/substrate/src/fungible.ts @@ -26,7 +26,7 @@ export interface SubstrateAssetTransferRequest extends BaseTransferParams { sourceAddress: string; sourceNetworkProvider: ApiPromise; amount: bigint; - destinationAddress: string; + recipientAddress: string; } export async function createSubstrateFungibleAssetTransfer( @@ -39,18 +39,18 @@ export async function createSubstrateFungibleAssetTransfer( } class SubstrateFungibleAssetTransfer extends BaseTransfer { - amount: bigint; - destinationAddress: string = ''; + transferAmount: bigint; + recipientAddress: string = ''; sourceNetworkProvider: ApiPromise; constructor(transfer: SubstrateAssetTransferRequest, config: Config) { super(transfer, config); - this.amount = transfer.amount; + this.transferAmount = transfer.amount; this.sourceNetworkProvider = transfer.sourceNetworkProvider; const environment = transfer.environment ?? Environment.MAINNET; - if (isValidAddressForNetwork(environment, transfer.destinationAddress, this.destination.type)) - this.destinationAddress = transfer.destinationAddress; + if (isValidAddressForNetwork(environment, transfer.recipientAddress, this.destination.type)) + this.recipientAddress = transfer.recipientAddress; } public getSourceNetworkProvider(): ApiPromise { @@ -62,7 +62,7 @@ class SubstrateFungibleAssetTransfer extends BaseTransfer { * @param {bigint} amount */ setAmount(amount: bigint): void { - this.amount = amount; + this.transferAmount = amount; } /** @@ -71,7 +71,7 @@ class SubstrateFungibleAssetTransfer extends BaseTransfer { */ setDestinationAddress(destinationAddress: string): void { if (isValidAddressForNetwork(this.environment, destinationAddress, this.destination.type)) - this.destinationAddress = destinationAddress; + this.recipientAddress = destinationAddress; } /** @@ -80,7 +80,7 @@ class SubstrateFungibleAssetTransfer extends BaseTransfer { * @returns {Promise} */ async getFee(amount?: bigint): Promise { - if (amount) this.amount = amount; + if (amount) this.transferAmount = amount; const resource = this.resource as SubstrateResource; @@ -99,7 +99,7 @@ class SubstrateFungibleAssetTransfer extends BaseTransfer { ); case FeeHandlerType.PERCENTAGE: return await getPercentageFee(this.sourceNetworkProvider, { - details: { amount: this.amount.toString(), recipient: this.destinationAddress }, + details: { amount: this.transferAmount.toString(), recipient: this.recipientAddress }, from: this.source, resource, sender: '', @@ -118,7 +118,7 @@ class SubstrateFungibleAssetTransfer extends BaseTransfer { // Native token balance check if ([FeeHandlerType.BASIC].includes(fee.type)) { - const amountBigNumber = new BN(this.amount.toString()); + const amountBigNumber = new BN(this.transferAmount.toString()); const balance = await getNativeTokenBalance(this.sourceNetworkProvider, this.sourceAddress); if (new BN(balance.free).lt(amountBigNumber)) { @@ -161,9 +161,9 @@ class SubstrateFungibleAssetTransfer extends BaseTransfer { this.environment, this.sourceNetworkProvider, resource.xcmMultiAssetId, - this.amount.toString(), + this.transferAmount.toString(), this.destination.id.toString(), - this.destinationAddress, + this.recipientAddress, ); } } diff --git a/packages/substrate/src/index.ts b/packages/substrate/src/index.ts index 9070b18a5..fcb5d4211 100644 --- a/packages/substrate/src/index.ts +++ b/packages/substrate/src/index.ts @@ -1,2 +1,3 @@ export * from './fungible.js'; export * from './utils/index.js'; +export * from './types.js'; diff --git a/packages/utils/src/liquidity.ts b/packages/utils/src/liquidity.ts index b3e84b1ae..f95778cd3 100644 --- a/packages/utils/src/liquidity.ts +++ b/packages/utils/src/liquidity.ts @@ -64,8 +64,8 @@ export async function hasEnoughLiquidity( ); return ( - (transfer as Awaited>).amount <= - substrateHandlerBalance + (transfer as Awaited>) + .transferAmount <= substrateHandlerBalance ); } // TODO: Bitcoin? From 2fd34d248a4aa956644966e47013f55743a409e2 Mon Sep 17 00:00:00 2001 From: Saad Ahmed Siddiqui Date: Wed, 23 Oct 2024 14:11:08 +0200 Subject: [PATCH 2/3] fix tests --- packages/substrate/src/__test__/fungible.test.ts | 10 +++++----- packages/utils/src/__test__/liquidity.test.ts | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/substrate/src/__test__/fungible.test.ts b/packages/substrate/src/__test__/fungible.test.ts index a82edce02..94f3b784e 100644 --- a/packages/substrate/src/__test__/fungible.test.ts +++ b/packages/substrate/src/__test__/fungible.test.ts @@ -30,7 +30,7 @@ describe('SubstrateFungibleAssetTransfer', () => { sourceNetworkProvider: api, resource: '0x0000000000000000000000000000000000000000000000000000000000000300', amount: BigInt(100), - destinationAddress: '0x98729c03c4D5e820F5e8c45558ae07aE63F97461', + recipientAddress: '0x98729c03c4D5e820F5e8c45558ae07aE63F97461', environment: Environment.LOCAL, }; }); @@ -43,16 +43,16 @@ describe('SubstrateFungibleAssetTransfer', () => { test('should set constructor values', async () => { const transfer = await createSubstrateFungibleAssetTransfer(transferRequest); - expect(transfer.amount).toBe(BigInt(100)); + expect(transfer.transferAmount).toBe(BigInt(100)); expect(transfer.sourceNetworkProvider).toBe(transfer.sourceNetworkProvider); - expect(transfer.destinationAddress).toBe(transferRequest.destinationAddress); + expect(transfer.recipientAddress).toBe(transferRequest.recipientAddress); }); test('should throw an error if destination address is Invalid', async () => { const invalidDestinationAddress = 'someAddress'; const transfer = createSubstrateFungibleAssetTransfer({ ...transferRequest, - destinationAddress: invalidDestinationAddress, + recipientAddress: invalidDestinationAddress, }); await expect(() => transfer).rejects.toThrow('Invalid EVM Address'); @@ -63,7 +63,7 @@ describe('SubstrateFungibleAssetTransfer', () => { test('should set another EVM destination address', async () => { const transfer = await createSubstrateFungibleAssetTransfer(transferRequest); transfer.setDestinationAddress('0x742d35Cc6634C0532925a3b844Bc454e4438f44e'); - expect(transfer.destinationAddress).toBe('0x742d35Cc6634C0532925a3b844Bc454e4438f44e'); + expect(transfer.recipientAddress).toBe('0x742d35Cc6634C0532925a3b844Bc454e4438f44e'); }); test('should not set an invalid destination address', async () => { diff --git a/packages/utils/src/__test__/liquidity.test.ts b/packages/utils/src/__test__/liquidity.test.ts index eb5ebf433..8d87bf9f2 100644 --- a/packages/utils/src/__test__/liquidity.test.ts +++ b/packages/utils/src/__test__/liquidity.test.ts @@ -47,7 +47,7 @@ const mockedTransferEVM = { }; const mockedTransferSubstrate = { - amount: 0n, + transferAmount: 0n, resource: mockedResource, config: { findDomainConfig: jest.fn(), @@ -128,7 +128,7 @@ describe('hasEnoughLiquidity - substrate', () => { }); it('should return true if there is enough liquidity', async () => { - mockedTransferSubstrate.amount = BigInt(5); + mockedTransferSubstrate.transferAmount = BigInt(5); const isEnough = await hasEnoughLiquidity( mockedTransferSubstrate as unknown as Awaited>, @@ -138,7 +138,7 @@ describe('hasEnoughLiquidity - substrate', () => { expect(isEnough).toEqual(true); }); it('should return false if there isnt enough liquidity', async () => { - mockedTransferSubstrate.amount = BigInt(10); + mockedTransferSubstrate.transferAmount = BigInt(10); const isEnough = await hasEnoughLiquidity( mockedTransferSubstrate as unknown as Awaited>, From c57bd12bcede7b14a2b72735bcfe8d03142f08f1 Mon Sep 17 00:00:00 2001 From: Saad Ahmed Siddiqui Date: Mon, 11 Nov 2024 17:24:50 +0100 Subject: [PATCH 3/3] update: examples --- examples/evm-to-evm-non-fungible-transfer/src/transfer.ts | 6 +----- examples/substrate-to-evm-fungible-transfer/src/transfer.ts | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/evm-to-evm-non-fungible-transfer/src/transfer.ts b/examples/evm-to-evm-non-fungible-transfer/src/transfer.ts index 4d4d64d5c..3eb04ae73 100644 --- a/examples/evm-to-evm-non-fungible-transfer/src/transfer.ts +++ b/examples/evm-to-evm-non-fungible-transfer/src/transfer.ts @@ -1,8 +1,4 @@ -import { - Eip1193Provider, - Environment, - getSygmaScanLink, -} from "@buildwithsygma/core"; +import { Eip1193Provider, getSygmaScanLink } from "@buildwithsygma/core"; import { createNonFungibleAssetTransfer, NonFungibleTransferParams, diff --git a/examples/substrate-to-evm-fungible-transfer/src/transfer.ts b/examples/substrate-to-evm-fungible-transfer/src/transfer.ts index 1d7ef8cfe..1d11d9017 100644 --- a/examples/substrate-to-evm-fungible-transfer/src/transfer.ts +++ b/examples/substrate-to-evm-fungible-transfer/src/transfer.ts @@ -43,7 +43,7 @@ const substrateTransfer = async (): Promise => { sourceAddress: account.address, resource: RESOURCE_ID_SYGMA_USD, amount: BigInt(1) * BigInt(1e18), - destinationAddress: recipient, + recipientAddress: recipient, environment: process.env.SYGMA_ENV, };