Skip to content

Commit

Permalink
fix: similar interface for substrate and evm transfers (#570)
Browse files Browse the repository at this point in the history
## Description
Change `destinationAddress` to `recipientAddress` in substrate transfers
as well as `amount` to `transferAmount` to match `evm`

## How Has This Been Tested? Testing details.
Updated existing unit tests

## Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
- [X] I have ensured that all acceptance criteria (or expected behavior)
from issue are met
- [X] I have added tests to cover my changes.
- [X] I have ensured that all the checks are passing and green, I've
signed the CLA bot
  • Loading branch information
saadahmsiddiqui authored Nov 11, 2024
1 parent 0f9af9a commit 8cc52e0
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 29 deletions.
6 changes: 1 addition & 5 deletions examples/evm-to-evm-non-fungible-transfer/src/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
Eip1193Provider,
Environment,
getSygmaScanLink,
} from "@buildwithsygma/core";
import { Eip1193Provider, getSygmaScanLink } from "@buildwithsygma/core";
import {
createNonFungibleAssetTransfer,
NonFungibleTransferParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const substrateTransfer = async (): Promise<void> => {
sourceAddress: account.address,
resource: RESOURCE_ID,
amount: BigInt(1) * BigInt(1e18),
destinationAddress: RECIPIENT_ADDRESS,
recipientAddress: RECIPIENT_ADDRESS,
environment: process.env.SYGMA_ENV,
};

Expand Down
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

0 comments on commit 8cc52e0

Please sign in to comment.