Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: similar interface for substrate and evm transfers #570

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/substrate/src/__test__/fungible.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('SubstrateFungibleAssetTransfer', () => {
sourceNetworkProvider: api,
resource: '0x0000000000000000000000000000000000000000000000000000000000000300',
amount: BigInt(100),
destinationAddress: '0x98729c03c4D5e820F5e8c45558ae07aE63F97461',
recipientAddress: '0x98729c03c4D5e820F5e8c45558ae07aE63F97461',
environment: Environment.LOCAL,
};
});
Expand All @@ -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');
Expand All @@ -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 () => {
Expand Down
26 changes: 13 additions & 13 deletions packages/substrate/src/fungible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface SubstrateAssetTransferRequest extends BaseTransferParams {
sourceAddress: string;
sourceNetworkProvider: ApiPromise;
amount: bigint;
destinationAddress: string;
recipientAddress: string;
}

export async function createSubstrateFungibleAssetTransfer(
Expand All @@ -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 {
Expand All @@ -62,7 +62,7 @@ class SubstrateFungibleAssetTransfer extends BaseTransfer {
* @param {bigint} amount
*/
setAmount(amount: bigint): void {
this.amount = amount;
this.transferAmount = amount;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -80,7 +80,7 @@ class SubstrateFungibleAssetTransfer extends BaseTransfer {
* @returns {Promise<SubstrateFee>}
*/
async getFee(amount?: bigint): Promise<SubstrateFee> {
if (amount) this.amount = amount;
if (amount) this.transferAmount = amount;

const resource = this.resource as SubstrateResource;

Expand All @@ -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: '',
Expand All @@ -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)) {
Expand Down Expand Up @@ -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,
);
}
}
1 change: 1 addition & 0 deletions packages/substrate/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './fungible.js';
export * from './utils/index.js';
export * from './types.js';
6 changes: 3 additions & 3 deletions packages/utils/src/__test__/liquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const mockedTransferEVM = {
};

const mockedTransferSubstrate = {
amount: 0n,
transferAmount: 0n,
resource: mockedResource,
config: {
findDomainConfig: jest.fn(),
Expand Down Expand Up @@ -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<ReturnType<typeof createFungibleAssetTransfer>>,
Expand All @@ -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<ReturnType<typeof createFungibleAssetTransfer>>,
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/liquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export async function hasEnoughLiquidity(
);

return (
(transfer as Awaited<ReturnType<typeof createSubstrateFungibleAssetTransfer>>).amount <=
substrateHandlerBalance
(transfer as Awaited<ReturnType<typeof createSubstrateFungibleAssetTransfer>>)
.transferAmount <= substrateHandlerBalance
);
}
// TODO: Bitcoin?
Expand Down