-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
f4b0610
commit d7d2004
Showing
16 changed files
with
301 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
87 changes: 87 additions & 0 deletions
87
packages/internal/dex/sdk/src/lib/nativeTokenService.test.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,87 @@ | ||
import { | ||
expectERC20, | ||
expectNative, | ||
formatAmount, | ||
FUN_TEST_TOKEN, | ||
nativeTokenService, | ||
newAmountFromString, | ||
} from 'test/utils'; | ||
|
||
describe('NativeTokenService', () => { | ||
describe('wrapAmount', () => { | ||
it('converts native token amounts to their wrapped equivalent', () => { | ||
const nativeAmount = newAmountFromString('1', nativeTokenService.nativeToken); | ||
const wrappedEquivalent = nativeTokenService.wrapAmount(nativeAmount); | ||
expectERC20(wrappedEquivalent.token, nativeTokenService.wrappedToken.address); | ||
expect(formatAmount(wrappedEquivalent)).toEqual('1.0'); | ||
}); | ||
}); | ||
|
||
describe('unwrapAmount', () => { | ||
it('converts wrapped token amounts to their native equivalent', () => { | ||
const wrappedAmount = newAmountFromString('1', nativeTokenService.wrappedToken); | ||
const nativeEquivalent = nativeTokenService.unwrapAmount(wrappedAmount); | ||
expectNative(nativeEquivalent.token); | ||
expect(formatAmount(nativeEquivalent)).toEqual('1.0'); | ||
}); | ||
|
||
it('throws an error if the token is not a wrapped native amount', () => { | ||
const erc20Amount = newAmountFromString('1', FUN_TEST_TOKEN); | ||
expect(() => nativeTokenService.unwrapAmount(erc20Amount)).toThrowError( | ||
'token 0xCc7bb2D219A0FC08033E130629C2B854b7bA9195 is not wrapped', | ||
); | ||
}); | ||
}); | ||
|
||
describe('maybeWrapToken', () => { | ||
it('wraps native tokens', () => { | ||
const wrappedToken = nativeTokenService.maybeWrapToken(nativeTokenService.nativeToken); | ||
expectERC20(wrappedToken); | ||
}); | ||
|
||
it('does not wrap an already wrapped token', () => { | ||
const stillTheWrappedToken = nativeTokenService.maybeWrapToken(nativeTokenService.wrappedToken); | ||
expect(stillTheWrappedToken).toEqual(nativeTokenService.wrappedToken); | ||
}); | ||
|
||
it('does not wrap a normal ERC20 token', () => { | ||
const stillTheFunToken = nativeTokenService.maybeWrapToken(FUN_TEST_TOKEN); | ||
expect(stillTheFunToken).toEqual(stillTheFunToken); | ||
}); | ||
}); | ||
|
||
describe('maybeWrapAmount', () => { | ||
it('wraps native amounts', () => { | ||
const nativeAmount = newAmountFromString('1', nativeTokenService.nativeToken); | ||
const wrappedEquivalent = nativeTokenService.maybeWrapAmount(nativeAmount); | ||
expectERC20(wrappedEquivalent.token, nativeTokenService.wrappedToken.address); | ||
expect(formatAmount(wrappedEquivalent)).toEqual('1.0'); | ||
}); | ||
|
||
it('does not wrap an already wrapped amount', () => { | ||
const wrappedAmount = newAmountFromString('1', nativeTokenService.wrappedToken); | ||
const stillTheWrappedAmount = nativeTokenService.maybeWrapAmount(wrappedAmount); | ||
expect(stillTheWrappedAmount).toEqual(wrappedAmount); | ||
}); | ||
|
||
it('does not wrap a normal ERC20 amount', () => { | ||
const erc20Amount = newAmountFromString('1', FUN_TEST_TOKEN); | ||
const stillTheFunAmount = nativeTokenService.maybeWrapAmount(erc20Amount); | ||
expect(stillTheFunAmount).toEqual(erc20Amount); | ||
}); | ||
}); | ||
|
||
describe('isNativeToken', () => { | ||
it('returns true for the native token', () => { | ||
expect(nativeTokenService.isNativeToken(nativeTokenService.nativeToken)).toEqual(true); | ||
}); | ||
|
||
it('returns false for the wrapped token', () => { | ||
expect(nativeTokenService.isNativeToken(nativeTokenService.wrappedToken)).toEqual(false); | ||
}); | ||
|
||
it('returns false for a normal ERC20 token', () => { | ||
expect(nativeTokenService.isNativeToken(FUN_TEST_TOKEN)).toEqual(false); | ||
}); | ||
}); | ||
}); |
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,34 @@ | ||
import { | ||
Amount, Coin, ERC20, Native, | ||
} from 'types'; | ||
import { newAmount } from './utils'; | ||
|
||
export class NativeTokenService { | ||
constructor(readonly nativeToken: Native, readonly wrappedToken: ERC20) {} | ||
|
||
wrapAmount(amount: Amount<Native>): Amount<ERC20> { | ||
return newAmount(amount.value, this.wrappedToken); | ||
} | ||
|
||
unwrapAmount(amount: Amount<ERC20>): Amount<Native> { | ||
if (amount.token !== this.wrappedToken) { | ||
throw new Error(`token ${amount.token.address} is not wrapped`); | ||
} | ||
return newAmount(amount.value, this.nativeToken); | ||
} | ||
|
||
maybeWrapToken(token: Coin): ERC20 { | ||
if (this.isNativeToken(token)) { | ||
return this.wrappedToken; | ||
} | ||
return token as ERC20; | ||
} | ||
|
||
maybeWrapAmount(amount: Amount<Coin>): Amount<ERC20> { | ||
return newAmount(amount.value, this.maybeWrapToken(amount.token)); | ||
} | ||
|
||
isNativeToken(token: Coin): token is Native { | ||
return token === this.nativeToken; | ||
} | ||
} |
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.