Skip to content

Commit

Permalink
same interfaces for substrate and evm
Browse files Browse the repository at this point in the history
  • Loading branch information
saadahmsiddiqui committed Oct 21, 2024
1 parent 6ba1c2b commit b8d9980
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
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';
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

0 comments on commit b8d9980

Please sign in to comment.