@leofcoin/standards 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ export declare const restoreBalances: (balances: any) => {};
2
+ export declare const restoreApprovals: (approvals: any) => {};
@@ -3,11 +3,15 @@ export declare type TokenState = {
3
3
  roles: {
4
4
  [index: string]: address[];
5
5
  };
6
- holders: number;
6
+ holders: BigNumberish;
7
7
  balances: {
8
- [index: string]: BigNumberish;
8
+ [address: address]: BigNumberish;
9
+ };
10
+ approvals: {
11
+ [owner: address]: {
12
+ [operator: address]: BigNumberish;
13
+ };
9
14
  };
10
- approvals: {};
11
15
  totalSupply: BigNumberish;
12
16
  };
13
17
  export default class Token extends Roles {
package/exports/token.js CHANGED
@@ -1,5 +1,24 @@
1
1
  import Roles from './roles.js';
2
2
 
3
+ // when state is stored it get encoded as a string to so we need to reformat balances back to BigNumbers
4
+ const restoreBalances = (balances) => {
5
+ const _balances = {};
6
+ for (const address in balances) {
7
+ _balances[address] = BigNumber['from'](balances[address]);
8
+ }
9
+ return _balances;
10
+ };
11
+ const restoreApprovals = (approvals) => {
12
+ const _approvals = {};
13
+ for (const owner in approvals) {
14
+ _approvals[owner] = {};
15
+ for (const operator in approvals[owner]) {
16
+ _approvals[owner][operator] = BigNumber['from'](approvals[owner][operator]);
17
+ }
18
+ }
19
+ return _approvals;
20
+ };
21
+
3
22
  class Token extends Roles {
4
23
  /**
5
24
  * string
@@ -31,10 +50,10 @@ class Token extends Roles {
31
50
  throw new Error(`symbol undefined`);
32
51
  super(state?.roles);
33
52
  if (state) {
53
+ this.#balances = restoreBalances(state.balances);
54
+ this.#approvals = restoreApprovals(state.approvals);
34
55
  this.#holders = BigNumber['from'](state.holders);
35
- this.#balances = state.balances;
36
- this.#approvals = state.approvals;
37
- this.#totalSupply = state.totalSupply;
56
+ this.#totalSupply = BigNumber['from'](state.totalSupply);
38
57
  }
39
58
  else {
40
59
  this.#name = name;
@@ -116,7 +135,7 @@ class Token extends Roles {
116
135
  const owner = msg.sender;
117
136
  if (!this.#approvals[owner])
118
137
  this.#approvals[owner] = {};
119
- this.#approvals[owner][operator] = amount;
138
+ this.#approvals[owner][operator] = BigNumber['from'](amount);
120
139
  }
121
140
  approved(owner, operator, amount) {
122
141
  return this.#approvals[owner][operator] === amount;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/standards",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Contract standards",
5
5
  "type": "module",
6
6
  "exports": {
package/src/helpers.ts ADDED
@@ -0,0 +1,19 @@
1
+ // when state is stored it get encoded as a string to so we need to reformat balances back to BigNumbers
2
+ export const restoreBalances = (balances) => {
3
+ const _balances = {}
4
+ for (const address in balances) {
5
+ _balances[address] = BigNumber['from'](balances[address])
6
+ }
7
+ return _balances
8
+ }
9
+
10
+ export const restoreApprovals = (approvals) => {
11
+ const _approvals = {}
12
+ for (const owner in approvals) {
13
+ _approvals[owner] = {}
14
+ for (const operator in approvals[owner]) {
15
+ _approvals[owner][operator] = BigNumber['from'](approvals[owner][operator])
16
+ }
17
+ }
18
+ return _approvals
19
+ }
package/src/token.ts CHANGED
@@ -1,10 +1,11 @@
1
+ import { restoreApprovals, restoreBalances } from './helpers.js'
1
2
  import Roles from './roles.js'
2
3
 
3
4
  export declare type TokenState = {
4
5
  roles: { [index: string]: address[] }
5
- holders: number
6
- balances: { [index: string]: BigNumberish }
7
- approvals: {}
6
+ holders: BigNumberish
7
+ balances: { [address: address]: BigNumberish }
8
+ approvals: { [owner: address]: { [operator: address]: BigNumberish } }
8
9
  totalSupply: BigNumberish
9
10
  }
10
11
 
@@ -28,11 +29,11 @@ export default class Token extends Roles {
28
29
  /**
29
30
  * Object => Object => uint
30
31
  */
31
- #approvals = {}
32
+ #approvals: { [owner: string]: { [operator: string]: typeof BigNumber } } = {}
32
33
 
33
34
  #decimals = 18
34
35
 
35
- #totalSupply = BigNumber['from'](0)
36
+ #totalSupply: typeof BigNumber = BigNumber['from'](0)
36
37
 
37
38
  // this.#privateField2 = 1
38
39
  constructor(name: string, symbol: string, decimals: number = 18, state?: TokenState) {
@@ -42,10 +43,10 @@ export default class Token extends Roles {
42
43
  super(state?.roles)
43
44
 
44
45
  if (state) {
46
+ this.#balances = restoreBalances(state.balances)
47
+ this.#approvals = restoreApprovals(state.approvals)
45
48
  this.#holders = BigNumber['from'](state.holders)
46
- this.#balances = state.balances
47
- this.#approvals = state.approvals
48
- this.#totalSupply = state.totalSupply
49
+ this.#totalSupply = BigNumber['from'](state.totalSupply)
49
50
  } else {
50
51
  this.#name = name
51
52
  this.#symbol = symbol
@@ -138,7 +139,7 @@ export default class Token extends Roles {
138
139
  setApproval(operator: address, amount: BigNumberish) {
139
140
  const owner = msg.sender
140
141
  if (!this.#approvals[owner]) this.#approvals[owner] = {}
141
- this.#approvals[owner][operator] = amount
142
+ this.#approvals[owner][operator] = BigNumber['from'](amount)
142
143
  }
143
144
 
144
145
  approved(owner: address, operator: address, amount: BigNumberish): boolean {