Skip to content

Commit b46c318

Browse files
committed
feat: add wallet entity and wallet factory
Get current user balances (Eth, POLY, or any other ERC20 token)
1 parent cc1b21a commit b46c318

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

src/entities/Wallet.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Factory } from './Factory';
2+
import { Context } from '../../Context';
3+
import { Wallet, Params, UniqueIdentifier } from '../Wallet';
4+
5+
export class WalletFactory extends Factory<Wallet, Params, UniqueIdentifier> {
6+
protected generateProperties = async (uid: string) => {
7+
const { address } = Wallet.unserialize(uid);
8+
9+
return {
10+
address,
11+
};
12+
};
13+
14+
constructor(context: Context) {
15+
super(Wallet, context);
16+
}
17+
}

src/entities/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ export { UsdTieredSto } from './UsdTieredSto';
1515
export { Investment } from './Investment';
1616
export { Sto } from './Sto';
1717
export { Shareholder } from './Shareholder';
18+
export { Wallet } from './Wallet';

0 commit comments

Comments
 (0)