-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added tokens interface #7
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8ea43f6
added tokens interface
d08f310
added tests
1a26e57
fix lock
ea870eb
added ganache to ci
c8735ca
fix android build
9cd8b28
fix ci android
948d5a8
review changes
5b6e4a8
Update src/lib/token/BaseToken.ts
chescalante f00b5af
review changes
32860d9
Update package.json
chescalante 76ed63c
refactor and added TRBTC
ec5104c
removed balance div
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,5 +18,6 @@ jobs: | |
with: | ||
node-version: 12.x | ||
- run: yarn | ||
- run: yarn ganache & | ||
- run: yarn test | ||
- run: yarn lint |
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,39 @@ | ||
import { BigNumber, BigNumberish, Signer, ContractTransaction } from 'ethers' | ||
|
||
export type TokenType = 'erc20' | 'rbtc' | ||
|
||
export class BaseToken { | ||
public signer: Signer | ||
public logo: string | ||
|
||
constructor(signer: Signer, logo: string) { | ||
this.signer = signer | ||
this.logo = logo | ||
} | ||
|
||
protected async getAddress() { | ||
return await this.signer.getAddress() | ||
} | ||
} | ||
|
||
export interface ITransferOptions { | ||
gasPrice: BigNumberish | ||
gasLimit: BigNumberish | ||
nonce: BigNumberish | ||
} | ||
|
||
export interface IToken { | ||
getType: () => TokenType | ||
decimals: () => Promise<number> | ||
symbol: () => Promise<string> | ||
balance: () => Promise<BigNumber> | ||
transfer: ( | ||
recipientAddress: string, | ||
amount: BigNumberish, | ||
options?: ITransferOptions, | ||
) => Promise<ContractTransaction> | ||
logo: string | ||
} | ||
|
||
export const ten = BigNumber.from(10) | ||
export const tenPow = (exp: BigNumberish) => ten.pow(exp) |
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,98 @@ | ||
import { providers, BigNumber } from 'ethers' | ||
import { tenPow } from './BaseToken' | ||
import { ERC20Token } from './ERC20Token' | ||
import { ERC20__factory } from './types' | ||
import { ERC677__factory } from './types/factories/ERC677__factory' | ||
|
||
const Config = { | ||
BLOCKCHAIN_HTTP_URL: 'HTTP://127.0.0.1:8545', | ||
} | ||
|
||
const TEST_TOKEN_DECIMALS = 18 | ||
|
||
const getJsonRpcProvider = async (): Promise<providers.JsonRpcProvider> => { | ||
return new providers.JsonRpcProvider(Config.BLOCKCHAIN_HTTP_URL) | ||
} | ||
|
||
const getSigner = async (index: number = 0) => { | ||
const provider = await getJsonRpcProvider() | ||
return provider.getSigner(index) | ||
} | ||
|
||
describe('ERC20 token', () => { | ||
let erc20Token: ERC20Token | null = null | ||
let tokenAddress = '' | ||
|
||
beforeEach(async () => { | ||
const account = await getSigner() | ||
const accountAddress = await account.getAddress() | ||
|
||
// using ERC677__factory that supports ERC20 to set totalSupply (just for testing purpose) | ||
const erc677Factory = new ERC677__factory(account) | ||
const erc20 = (await erc677Factory.deploy( | ||
accountAddress, | ||
BigNumber.from(10).mul(tenPow(TEST_TOKEN_DECIMALS)), | ||
'TEST_ERC20', | ||
'TEST_ERC20', | ||
)) as any | ||
|
||
tokenAddress = erc20.address | ||
|
||
erc20Token = new ERC20Token(tokenAddress, account, 'logo.jpg') | ||
}) | ||
|
||
test('get symbol', async () => { | ||
const symbol = await erc20Token!.symbol() | ||
|
||
expect(symbol).toBe('TEST_ERC20') | ||
}) | ||
|
||
test('get logo', async () => { | ||
const logo = erc20Token!.logo | ||
|
||
expect(logo).toBe('logo.jpg') | ||
}) | ||
|
||
test('get decimals', async () => { | ||
const decimals = await erc20Token!.decimals() | ||
|
||
expect(decimals).toBe(TEST_TOKEN_DECIMALS) | ||
}) | ||
|
||
test('get type', async () => { | ||
const type = erc20Token!.getType() | ||
|
||
expect(type).toBe('erc20') | ||
}) | ||
|
||
test('get balance', async () => { | ||
const result = await erc20Token!.balance() | ||
|
||
expect(result.eq(10)).toBe(true) | ||
}) | ||
|
||
test('transfer', async () => { | ||
const from = await getSigner() | ||
const fromAddress = await from.getAddress() | ||
const to = await getSigner(1) | ||
const toAddress = await to.getAddress() | ||
|
||
const amountToTransfer = BigNumber.from(100) | ||
|
||
const sender = ERC20__factory.connect(tokenAddress, from) | ||
|
||
const balanceSender = await sender.balanceOf(fromAddress) | ||
|
||
expect(balanceSender.gte(amountToTransfer)).toBe(true) | ||
|
||
const transferTx = await erc20Token!.transfer(toAddress, amountToTransfer) | ||
|
||
await transferTx.wait() | ||
|
||
const recipient = ERC20__factory.connect(tokenAddress, to) | ||
|
||
const balanceRecipient = await recipient.balanceOf(toAddress) | ||
|
||
expect(balanceRecipient.eq(amountToTransfer)).toBe(true) | ||
}) | ||
}) |
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,50 @@ | ||
import { BigNumber, BigNumberish, ContractTransaction, Signer } from 'ethers' | ||
import { | ||
BaseToken, | ||
IToken, | ||
ITransferOptions, | ||
tenPow, | ||
TokenType, | ||
} from './BaseToken' | ||
import { ERC20 as ERC20Type, ERC20__factory } from './types' | ||
|
||
class ERC20Token extends BaseToken implements IToken { | ||
private tokenContract: ERC20Type | ||
|
||
constructor(address: string, signer: Signer, logo: string) { | ||
super(signer, logo) | ||
this.tokenContract = ERC20__factory.connect(address, signer) | ||
} | ||
|
||
public getType(): TokenType { | ||
return 'erc20' | ||
} | ||
|
||
public async decimals(): Promise<number> { | ||
return this.tokenContract.decimals() | ||
} | ||
|
||
public async symbol(): Promise<string> { | ||
return this.tokenContract.symbol() | ||
} | ||
|
||
public async balance(): Promise<BigNumber> { | ||
const account = await this.getAddress() | ||
|
||
const decimals = await this.decimals() | ||
|
||
const balance = await this.tokenContract.balanceOf(account) | ||
|
||
return balance.div(tenPow(decimals)) | ||
} | ||
|
||
public async transfer( | ||
recipientAddress: string, | ||
amount: BigNumberish, | ||
options?: ITransferOptions, | ||
): Promise<ContractTransaction> { | ||
return this.tokenContract.transfer(recipientAddress, amount, options ?? {}) | ||
} | ||
} | ||
|
||
export { ERC20Token } |
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,76 @@ | ||
import { providers, BigNumber } from 'ethers' | ||
import { RBTCToken } from './RBTCToken' | ||
|
||
const Config = { | ||
BLOCKCHAIN_HTTP_URL: 'HTTP://127.0.0.1:8545', | ||
} | ||
|
||
const TEST_TOKEN_DECIMALS = 18 | ||
|
||
const getJsonRpcProvider = async (): Promise<providers.JsonRpcProvider> => { | ||
return new providers.JsonRpcProvider(Config.BLOCKCHAIN_HTTP_URL) | ||
} | ||
|
||
const getSigner = async (index: number = 0) => { | ||
const provider = await getJsonRpcProvider() | ||
return provider.getSigner(index) | ||
} | ||
|
||
describe('RBTC token', () => { | ||
let rbtcToken: RBTCToken | null = null | ||
|
||
beforeEach(async () => { | ||
const account = await getSigner() | ||
|
||
rbtcToken = new RBTCToken(account, 'logo.jpg') | ||
}) | ||
|
||
test('get symbol', async () => { | ||
const symbol = await rbtcToken!.symbol() | ||
|
||
expect(symbol).toBe('RBTC') | ||
}) | ||
|
||
test('get logo', async () => { | ||
const logo = rbtcToken!.logo | ||
|
||
expect(logo).toBe('logo.jpg') | ||
}) | ||
|
||
test('get decimals', async () => { | ||
const decimals = await rbtcToken!.decimals() | ||
|
||
expect(decimals).toBe(TEST_TOKEN_DECIMALS) | ||
}) | ||
|
||
test('get type', async () => { | ||
const type = rbtcToken!.getType() | ||
|
||
expect(type).toBe('rbtc') | ||
}) | ||
|
||
test('get balance', async () => { | ||
const result = await rbtcToken!.balance() | ||
|
||
expect(result.gt(0)).toBe(true) | ||
}) | ||
|
||
test('transfer', async () => { | ||
const to = await getSigner(1) | ||
const toAddress = await to.getAddress() | ||
|
||
const balanceRecipientInitial = await to.getBalance() | ||
|
||
const amountToTransfer = BigNumber.from(100) | ||
|
||
const transferTx = await rbtcToken!.transfer(toAddress, amountToTransfer) | ||
|
||
await transferTx.wait() | ||
|
||
const balanceRecipient = await to.getBalance() | ||
|
||
expect( | ||
balanceRecipient.eq(balanceRecipientInitial.add(amountToTransfer)), | ||
).toBe(true) | ||
}) | ||
}) |
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,51 @@ | ||
import { BigNumber, BigNumberish, ContractTransaction, Signer } from 'ethers' | ||
import { | ||
BaseToken, | ||
IToken, | ||
ITransferOptions, | ||
tenPow, | ||
TokenType, | ||
} from './BaseToken' | ||
|
||
class RBTCToken extends BaseToken implements IToken { | ||
constructor(signer: Signer, logo: string) { | ||
super(signer, logo) | ||
} | ||
|
||
public getType(): TokenType { | ||
return 'rbtc' | ||
} | ||
|
||
public async decimals(): Promise<number> { | ||
return 18 | ||
} | ||
|
||
public async symbol(): Promise<string> { | ||
return 'RBTC' | ||
} | ||
ilanolkies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public async balance(): Promise<BigNumber> { | ||
const decimals = await this.decimals() | ||
|
||
const balance = await this.signer.getBalance() | ||
|
||
return balance.div(tenPow(decimals)) | ||
} | ||
|
||
public async transfer( | ||
recipientAddress: string, | ||
amount: BigNumberish, | ||
options?: ITransferOptions, | ||
): Promise<ContractTransaction> { | ||
const account = await this.getAddress() | ||
|
||
return this.signer.sendTransaction({ | ||
from: account, | ||
to: recipientAddress, | ||
value: amount, | ||
...options, | ||
}) | ||
} | ||
} | ||
|
||
export { RBTCToken } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I are only working with testnet now. Let's add
TRBTC
too. I guess it is a different entity, maybe inherits all the same but overriding thesymbol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a few changes regarding this, let me know if it's ok for now.