-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #597 from beethovenxfi/fix/sor
native asset support, fixes: #587
- Loading branch information
Showing
5 changed files
with
47 additions
and
44 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 |
---|---|---|
@@ -1,20 +1,32 @@ | ||
import { TokenAmount, Token, Address } from '@balancer/sdk'; | ||
import { TokenAmount, Token, Address, NATIVE_ADDRESS, NATIVE_ASSETS } from '@balancer/sdk'; | ||
import { tokenService } from '../token/token.service'; | ||
import { Chain } from '@prisma/client'; | ||
import { chainToIdMap } from '../network/network-config'; | ||
|
||
export async function getTokenAmountHuman(tokenAddr: string, humanAmount: string, chain: Chain): Promise<TokenAmount> { | ||
const chainId = Number(chainToIdMap[chain]); | ||
const prismaToken = await tokenService.getToken(tokenAddr, chain); | ||
if (!prismaToken) throw Error(`Missing token from tokenService ${tokenAddr}`); | ||
const token = new Token(chainId, prismaToken.address as Address, prismaToken.decimals); | ||
const token = await getToken(tokenAddr, chain); | ||
return TokenAmount.fromHumanAmount(token, humanAmount as `${number}`); | ||
} | ||
|
||
export async function getTokenAmountRaw(tokenAddr: string, rawAmount: string, chain: Chain): Promise<TokenAmount> { | ||
const chainId = Number(chainToIdMap[chain]); | ||
const prismaToken = await tokenService.getToken(tokenAddr, chain); | ||
if (!prismaToken) throw Error(`Missing token from tokenService ${tokenAddr}`); | ||
const token = new Token(chainId, prismaToken.address as Address, prismaToken.decimals); | ||
const token = await getToken(tokenAddr, chain); | ||
return TokenAmount.fromRawAmount(token, rawAmount); | ||
} | ||
|
||
/** | ||
* Gets a b-sdk Token based off tokenAddr. | ||
* @param address | ||
* @param chain | ||
* @returns | ||
*/ | ||
export const getToken = async (tokenAddr: string, chain: Chain): Promise<Token> => { | ||
const chainId = Number(chainToIdMap[chain]); | ||
|
||
if (tokenAddr === NATIVE_ADDRESS && chainId in NATIVE_ASSETS) { | ||
return NATIVE_ASSETS[chainId as keyof typeof NATIVE_ASSETS]; | ||
} else { | ||
const prismaToken = await tokenService.getToken(tokenAddr, chain); | ||
if (!prismaToken) throw Error(`Missing token from tokenService ${tokenAddr}`); | ||
return new Token(chainId, prismaToken.address as Address, prismaToken.decimals); | ||
} | ||
}; |