|
| 1 | +import { BigNumber, ContractWrapper } from '@polymathnetwork/contract-wrappers'; |
| 2 | +import { Entity } from './Entity'; |
| 3 | +import { serialize, unserialize } from '../utils'; |
| 4 | +import { PolymathError } from '../PolymathError'; |
| 5 | +import { ErrorCode } from '../types'; |
| 6 | +import { Context } from '../Context'; |
| 7 | +import { PolymathBase } from '../PolymathBase'; |
| 8 | + |
| 9 | +export interface UniqueIdentifier { |
| 10 | + address: string; |
| 11 | +} |
| 12 | + |
| 13 | +function isUniqueIdentifier(identifier: any): identifier is UniqueIdentifier { |
| 14 | + const { address } = identifier; |
| 15 | + |
| 16 | + return typeof address === 'string'; |
| 17 | +} |
| 18 | + |
| 19 | +export interface Params extends UniqueIdentifier {} |
| 20 | + |
| 21 | +export class Wallet extends Entity<Params> { |
| 22 | + public static generateId({ address }: UniqueIdentifier) { |
| 23 | + return serialize('wallet', { |
| 24 | + address, |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + public static unserialize(serialized: string) { |
| 29 | + const unserialized = unserialize(serialized); |
| 30 | + |
| 31 | + if (!isUniqueIdentifier(unserialized)) { |
| 32 | + throw new PolymathError({ |
| 33 | + code: ErrorCode.InvalidUuid, |
| 34 | + message: 'Wrong Wallet ID format.', |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + return unserialized; |
| 39 | + } |
| 40 | + |
| 41 | + public uid: string; |
| 42 | + |
| 43 | + public address: string; |
| 44 | + |
| 45 | + private contractWrappers: PolymathBase; |
| 46 | + |
| 47 | + constructor(params: Params & UniqueIdentifier, context: Context) { |
| 48 | + super(); |
| 49 | + |
| 50 | + const { address } = params; |
| 51 | + const { contractWrappers } = context; |
| 52 | + |
| 53 | + this.address = address; |
| 54 | + this.contractWrappers = contractWrappers; |
| 55 | + this.uid = Wallet.generateId({ |
| 56 | + address, |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + public toPojo() { |
| 61 | + const { uid, address } = this; |
| 62 | + |
| 63 | + return { |
| 64 | + uid, |
| 65 | + address, |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + public _refresh(params: Partial<Params>) { |
| 70 | + const { address } = params; |
| 71 | + |
| 72 | + if (address) { |
| 73 | + this.address = address; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public getPolyBalance = async (): Promise<BigNumber> => { |
| 78 | + const { address } = this; |
| 79 | + return await this.contractWrappers.getBalance({ address }); |
| 80 | + }; |
| 81 | + |
| 82 | + public getEthBalance = async (): Promise<BigNumber> => { |
| 83 | + return await this.contractWrappers.polyToken.balanceOf(); |
| 84 | + }; |
| 85 | + |
| 86 | + public getErc20Balance = async (tokenAddress: string): Promise<BigNumber> => { |
| 87 | + const erc20Wrapper = await this.contractWrappers.getERC20TokenWrapper({ |
| 88 | + address: tokenAddress, |
| 89 | + }); |
| 90 | + |
| 91 | + const { address } = this; |
| 92 | + return await erc20Wrapper.balanceOf({ owner: address }); |
| 93 | + }; |
| 94 | +} |
0 commit comments