TokenWrapperService.ts
1 import { TokenWrapperServiceInterface } from '@aave/contract-helpers'; 2 import { SignatureLike } from '@ethersproject/bytes'; 3 import { Provider } from '@ethersproject/providers'; 4 import { BigNumber } from 'ethers'; 5 6 export class TokenWrapperService { 7 private tokenWrapperService: { 8 [chainId: number]: { [tokenWrapperAddress: string]: TokenWrapperServiceInterface }; 9 } = {}; 10 11 constructor(private chainId: number, private provider: Provider) {} 12 13 private async getService(tokenWrapperAddress: string) { 14 if (!this.tokenWrapperService[this.chainId]) { 15 this.tokenWrapperService[this.chainId] = {}; 16 } 17 18 if (!this.tokenWrapperService[this.chainId][tokenWrapperAddress]) { 19 this.tokenWrapperService[this.chainId][tokenWrapperAddress] = new ( 20 await import('@aave/contract-helpers') 21 ).TokenWrapperService(this.provider, tokenWrapperAddress); 22 } 23 24 return this.tokenWrapperService[this.chainId][tokenWrapperAddress]; 25 } 26 27 public async getTokenInForTokenOut( 28 amount: string, 29 tokenWrapperAddress: string 30 ): Promise<BigNumber> { 31 const service = await this.getService(tokenWrapperAddress); 32 return service.getTokenInForTokenOut(amount); 33 } 34 35 public async getTokenOutForTokenIn( 36 amount: string, 37 tokenWrapperAddress: string 38 ): Promise<BigNumber> { 39 const service = await this.getService(tokenWrapperAddress); 40 return service.getTokenOutForTokenIn(amount); 41 } 42 43 public async supplyWrappedToken(amount: string, tokenWrapperAddress: string, user: string) { 44 const service = await this.getService(tokenWrapperAddress); 45 return service.supplyToken(amount, user, '0'); 46 } 47 48 public async supplyWrappedTokenWithPermit( 49 amount: string, 50 tokenWrapperAddress: string, 51 user: string, 52 deadline: string, 53 signature: SignatureLike 54 ) { 55 const service = await this.getService(tokenWrapperAddress); 56 return service.supplyTokenWithPermit({ 57 amount, 58 onBehalfOf: user, 59 referralCode: '0', 60 deadline, 61 signature, 62 }); 63 } 64 65 public async withdrawWrappedToken(amount: string, tokenWrapperAddress: string, user: string) { 66 const service = await this.getService(tokenWrapperAddress); 67 return service.withdrawToken(amount, user); 68 } 69 70 public async withdrawWrappedTokenWithPermit( 71 amount: string, 72 tokenWrapperAddress: string, 73 user: string, 74 deadline: string, 75 signature: SignatureLike 76 ) { 77 const service = await this.getService(tokenWrapperAddress); 78 return service.withdrawTokenWithPermit(amount, user, deadline, signature); 79 } 80 }