-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Destination network liquidity check (#290)
* destination liquidity check * fix existing test * update substrate balance checking * add tests for balance check * fix lint * apply feedback * clean up code * add check to BuildTransferTx * update tests * Apply suggestions from code review Co-authored-by: Matija Petrunić <[email protected]> * throw error when calling `createFungibleTransfer` * add test for not enough liquidity * mark param as optional * update param order * fix lint * Update packages/sdk/src/chains/BaseAssetTransfer.ts Co-authored-by: Matija Petrunić <[email protected]> * remove stale test --------- Co-authored-by: Matija Petrunić <[email protected]>
- Loading branch information
Showing
14 changed files
with
279 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ERC20__factory } from '@buildwithsygma/sygma-contracts'; | ||
import { JsonRpcProvider } from '@ethersproject/providers'; | ||
import { EvmResource } from 'types'; | ||
|
||
export const getEvmHandlerBalance = async ( | ||
destinationProviderUrl: string, | ||
resource: EvmResource, | ||
handlerAddress: string, | ||
): Promise<bigint> => { | ||
const provider = new JsonRpcProvider(destinationProviderUrl); | ||
if (resource.native) { | ||
return BigInt((await provider.getBalance(handlerAddress)).toString()); | ||
} else { | ||
const tokenAddress = resource.address; | ||
const erc20Contract = ERC20__factory.connect(tokenAddress, provider); | ||
return BigInt((await erc20Contract.balanceOf(handlerAddress)).toString()); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/sdk/src/chains/Substrate/utils/getSubstrateHandlerBalance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { WsProvider } from '@polkadot/rpc-provider'; | ||
import { SubstrateResource } from 'types'; | ||
import { getNativeTokenBalance } from './getNativeTokenBalance'; | ||
import { getAssetBalance } from './getAssetBalance'; | ||
|
||
export const getSubstrateHandlerBalance = async ( | ||
destinationProviderUrl: string, | ||
resource: SubstrateResource, | ||
handlerAddress: string, | ||
): Promise<bigint> => { | ||
const wsProvider = new WsProvider(destinationProviderUrl); | ||
const apiPromise = new ApiPromise({ provider: wsProvider }); | ||
if (resource.native) { | ||
const accountInfo = await getNativeTokenBalance(apiPromise, handlerAddress); | ||
return BigInt(accountInfo.free.toString()); | ||
} else { | ||
const assetBalance = await getAssetBalance(apiPromise, resource.assetId ?? 0, handlerAddress); | ||
return BigInt(assetBalance.balance.toString()); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class SygmaSdkError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
} | ||
} | ||
|
||
export class LiquidityError extends SygmaSdkError { | ||
availableLiquidity: bigint; | ||
|
||
constructor(destinationLiquidity: bigint) { | ||
super( | ||
`Destination chain liquidity is too low to perform this transfer. Transfer is limited to ${destinationLiquidity.toString()}`, | ||
); | ||
this.availableLiquidity = destinationLiquidity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.