Skip to content

Commit e418f66

Browse files
authored
Expose Caller Address in ERC-20 API (#58)
* expose optional caller address on allowance api * expose caller address in erc-20 wrapper + erc-20 api * update method description * update tests for erc-20 api * bump package to 0.1.1
1 parent 1e7c5b7 commit e418f66

File tree

4 files changed

+164
-47
lines changed

4 files changed

+164
-47
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "set.js",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "A javascript library for interacting with the Set Protocol v2",
55
"keywords": [
66
"set.js",

src/api/ERC20API.ts

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ import Assertions from '../assertions';
3434
export default class ERC20API {
3535
private assert: Assertions;
3636
private erc20Wrapper: ERC20Wrapper;
37-
public constructor(
38-
provider: Provider,
39-
assertions?: Assertions
40-
) {
37+
public constructor(provider: Provider, assertions?: Assertions) {
4138
this.erc20Wrapper = new ERC20Wrapper(provider);
4239
this.assert = assertions || new Assertions();
4340
}
@@ -47,61 +44,86 @@ export default class ERC20API {
4744
*
4845
* @param tokenAddress Address of the ERC20 token
4946
* @param userAddress Address of the user
47+
* @param callerAddress Optional. The address of user transferring from.
5048
* @return The balance of the ERC20 token in BigNumber format
5149
*/
52-
public async getBalanceAsync(tokenAddress: Address, userAddress: Address): Promise<BigNumber> {
50+
public async getBalanceAsync(
51+
tokenAddress: Address,
52+
userAddress: Address,
53+
callerAddress: Address = undefined
54+
): Promise<BigNumber> {
5355
this.assert.schema.isValidAddress('tokenAddress', tokenAddress);
5456
this.assert.schema.isValidAddress('userAddress', userAddress);
5557

56-
return this.erc20Wrapper.balanceOf(tokenAddress, userAddress);
58+
return this.erc20Wrapper.balanceOf(
59+
tokenAddress,
60+
userAddress,
61+
callerAddress
62+
);
5763
}
5864

5965
/**
6066
* Gets name of the ERC20 token
6167
*
6268
* @param tokenAddress Address of the ERC20 token
69+
* @param callerAddress Optional. The address of user transferring from.
6370
* @return The name of the ERC20 token
6471
*/
65-
public async getTokenNameAsync(tokenAddress: Address): Promise<string> {
72+
public async getTokenNameAsync(
73+
tokenAddress: Address,
74+
callerAddress: Address = undefined
75+
): Promise<string> {
6676
this.assert.schema.isValidAddress('tokenAddress', tokenAddress);
6777

68-
return this.erc20Wrapper.name(tokenAddress);
78+
return this.erc20Wrapper.name(tokenAddress, callerAddress);
6979
}
7080

7181
/**
7282
* Gets symbol of the ERC20 token
7383
*
7484
* @param tokenAddress Address of the ERC20 token
85+
* @param callerAddress Optional. The address of user transferring from.
7586
* @return The symbol of the ERC20 token
7687
*/
77-
public async getTokenSymbolAsync(tokenAddress: Address): Promise<string> {
88+
public async getTokenSymbolAsync(
89+
tokenAddress: Address,
90+
callerAddress: Address = undefined
91+
): Promise<string> {
7892
this.assert.schema.isValidAddress('tokenAddress', tokenAddress);
7993

80-
return this.erc20Wrapper.symbol(tokenAddress);
94+
return this.erc20Wrapper.symbol(tokenAddress, callerAddress);
8195
}
8296

8397
/**
8498
* Gets the total supply of the ERC20 token
8599
*
86100
* @param tokenAddress Address of the ERC20 token
101+
* @param callerAddress Optional. The address of user transferring from.
87102
* @return The total supply of ERC-20 in BigNumber format
88103
*/
89-
public async getTotalSupplyAsync(tokenAddress: Address): Promise<BigNumber> {
104+
public async getTotalSupplyAsync(
105+
tokenAddress: Address,
106+
callerAddress: Address = undefined
107+
): Promise<BigNumber> {
90108
this.assert.schema.isValidAddress('tokenAddress', tokenAddress);
91109

92-
return this.erc20Wrapper.totalSupply(tokenAddress);
110+
return this.erc20Wrapper.totalSupply(tokenAddress, callerAddress);
93111
}
94112

95113
/**
96114
* Gets decimals of the ERC20 token
97115
*
98116
* @param tokenAddress Address of the ERC20 token
117+
* @param callerAddress Optional. The address of user transferring from.
99118
* @return The decimals of the ERC20 token
100119
*/
101-
public async getDecimalsAsync(tokenAddress: Address): Promise<BigNumber> {
120+
public async getDecimalsAsync(
121+
tokenAddress: Address,
122+
callerAddress: Address = undefined
123+
): Promise<BigNumber> {
102124
this.assert.schema.isValidAddress('tokenAddress', tokenAddress);
103125

104-
return this.erc20Wrapper.decimals(tokenAddress);
126+
return this.erc20Wrapper.decimals(tokenAddress, callerAddress);
105127
}
106128

107129
/**
@@ -110,18 +132,25 @@ export default class ERC20API {
110132
* @param tokenAddress Address of the token
111133
* @param ownerAddress Address of the owner
112134
* @param spenderAddress Address of the spender
135+
* @param callerAddress Optional. The address of user transferring from.
113136
* @return The allowance of the spender in BigNumber format
114137
*/
115138
public async getAllowanceAsync(
116139
tokenAddress: Address,
117140
ownerAddress: Address,
118141
spenderAddress: Address,
142+
callerAddress: Address = undefined
119143
): Promise<BigNumber> {
120144
this.assert.schema.isValidAddress('tokenAddress', tokenAddress);
121145
this.assert.schema.isValidAddress('ownerAddress', ownerAddress);
122146
this.assert.schema.isValidAddress('spenderAddress', spenderAddress);
123147

124-
return this.erc20Wrapper.allowance(tokenAddress, ownerAddress, spenderAddress);
148+
return this.erc20Wrapper.allowance(
149+
tokenAddress,
150+
ownerAddress,
151+
spenderAddress,
152+
callerAddress
153+
);
125154
}
126155

127156
/**
@@ -140,13 +169,19 @@ export default class ERC20API {
140169
to: Address,
141170
value: BigNumber,
142171
callerAddress: Address = undefined,
143-
txOpts: TransactionOverrides = {},
172+
txOpts: TransactionOverrides = {}
144173
): Promise<ContractTransaction> {
145174
this.assert.schema.isValidAddress('tokenAddress', tokenAddress);
146175
this.assert.schema.isValidAddress('toAddress', to);
147176
this.assert.schema.isValidNumber('value', value);
148177

149-
return await this.erc20Wrapper.transfer(tokenAddress, to, value, callerAddress, txOpts);
178+
return await this.erc20Wrapper.transfer(
179+
tokenAddress,
180+
to,
181+
value,
182+
callerAddress,
183+
txOpts
184+
);
150185
}
151186

152187
/**
@@ -170,7 +205,13 @@ export default class ERC20API {
170205
this.assert.schema.isValidAddress('spenderAddress', spenderAddress);
171206
this.assert.schema.isValidNumber('value', value);
172207

173-
return this.erc20Wrapper.approve(tokenAddress, spenderAddress, value, callerAddress, txOpts);
208+
return this.erc20Wrapper.approve(
209+
tokenAddress,
210+
spenderAddress,
211+
value,
212+
callerAddress,
213+
txOpts
214+
);
174215
}
175216

176217
/**
@@ -190,13 +231,20 @@ export default class ERC20API {
190231
to: Address,
191232
value: BigNumber,
192233
callerAddress: Address = undefined,
193-
txOpts: TransactionOverrides = {},
234+
txOpts: TransactionOverrides = {}
194235
): Promise<ContractTransaction> {
195236
this.assert.schema.isValidAddress('tokenAddress', tokenAddress);
196237
this.assert.schema.isValidAddress('toAddress', to);
197238
this.assert.schema.isValidAddress('fromAddress', from);
198239
this.assert.schema.isValidNumber('value', value);
199240

200-
return this.erc20Wrapper.transferFrom(tokenAddress, from, to, value, callerAddress, txOpts);
241+
return this.erc20Wrapper.transferFrom(
242+
tokenAddress,
243+
from,
244+
to,
245+
value,
246+
callerAddress,
247+
txOpts
248+
);
201249
}
202250
}

src/wrappers/set-protocol-v2/ERC20Wrapper.ts

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,18 @@ export default class ERC20Wrapper {
4545
*
4646
* @param tokenAddress Address of the ERC20 token
4747
* @param userAddress Address of the user
48+
* @param callerAddress Address of the method caller
4849
* @return The balance of the ERC20 token
4950
*/
50-
public async balanceOf(tokenAddress: Address, userAddress: Address): Promise<BigNumber> {
51-
const tokenInstance = await this.contracts.loadERC20Async(tokenAddress);
51+
public async balanceOf(
52+
tokenAddress: Address,
53+
userAddress: Address,
54+
callerAddress: Address = undefined
55+
): Promise<BigNumber> {
56+
const tokenInstance = await this.contracts.loadERC20Async(
57+
tokenAddress,
58+
callerAddress
59+
);
5260

5361
return await tokenInstance.balanceOf(userAddress);
5462
}
@@ -57,10 +65,17 @@ export default class ERC20Wrapper {
5765
* Gets name of the ERC20 token
5866
*
5967
* @param tokenAddress Address of the ERC20 token
68+
* @param callerAddress Address of the method caller
6069
* @return The name of the ERC20 token
6170
*/
62-
public async name(tokenAddress: Address): Promise<string> {
63-
const tokenInstance = await this.contracts.loadERC20Async(tokenAddress);
71+
public async name(
72+
tokenAddress: Address,
73+
callerAddress: Address = undefined
74+
): Promise<string> {
75+
const tokenInstance = await this.contracts.loadERC20Async(
76+
tokenAddress,
77+
callerAddress
78+
);
6479

6580
return await tokenInstance.name();
6681
}
@@ -69,10 +84,17 @@ export default class ERC20Wrapper {
6984
* Gets balance of the ERC20 token
7085
*
7186
* @param tokenAddress Address of the ERC20 token
87+
* @param callerAddress Address of the method caller
7288
* @return The symbol of the ERC20 token
7389
*/
74-
public async symbol(tokenAddress: Address): Promise<string> {
75-
const tokenInstance = await this.contracts.loadERC20Async(tokenAddress);
90+
public async symbol(
91+
tokenAddress: Address,
92+
callerAddress: Address = undefined
93+
): Promise<string> {
94+
const tokenInstance = await this.contracts.loadERC20Async(
95+
tokenAddress,
96+
callerAddress
97+
);
7698

7799
return await tokenInstance.symbol();
78100
}
@@ -81,10 +103,17 @@ export default class ERC20Wrapper {
81103
* Gets the total supply of the ERC20 token
82104
*
83105
* @param tokenAddress Address of the ERC20 token
106+
* @param callerAddress Address of the method caller
84107
* @return The symbol of the ERC20 token
85108
*/
86-
public async totalSupply(tokenAddress: Address): Promise<BigNumber> {
87-
const tokenInstance = await this.contracts.loadERC20Async(tokenAddress);
109+
public async totalSupply(
110+
tokenAddress: Address,
111+
callerAddress: Address = undefined
112+
): Promise<BigNumber> {
113+
const tokenInstance = await this.contracts.loadERC20Async(
114+
tokenAddress,
115+
callerAddress
116+
);
88117

89118
return await tokenInstance.totalSupply();
90119
}
@@ -93,11 +122,17 @@ export default class ERC20Wrapper {
93122
* Gets decimals of the ERC20 token
94123
*
95124
* @param tokenAddress Address of the ERC20 token
96-
* @param userAddress Address of the user
125+
* @param callerAddress Address of the method caller
97126
* @return The decimals of the ERC20 token
98127
*/
99-
public async decimals(tokenAddress: Address): Promise<BigNumber> {
100-
const tokenInstance = await this.contracts.loadERC20Async(tokenAddress);
128+
public async decimals(
129+
tokenAddress: Address,
130+
callerAddress: Address = undefined
131+
): Promise<BigNumber> {
132+
const tokenInstance = await this.contracts.loadERC20Async(
133+
tokenAddress,
134+
callerAddress
135+
);
101136

102137
return await tokenInstance.decimals();
103138
}
@@ -108,14 +143,19 @@ export default class ERC20Wrapper {
108143
* @param tokenAddress Address of the token
109144
* @param ownerAddress Address of the owner
110145
* @param spenderAddress Address of the spender
146+
* @param callerAddress Address of the method caller
111147
* @return The allowance of the spender
112148
*/
113149
public async allowance(
114150
tokenAddress: Address,
115151
ownerAddress: Address,
116152
spenderAddress: Address,
153+
callerAddress: Address = undefined
117154
): Promise<BigNumber> {
118-
const tokenInstance = await this.contracts.loadERC20Async(tokenAddress);
155+
const tokenInstance = await this.contracts.loadERC20Async(
156+
tokenAddress,
157+
callerAddress
158+
);
119159

120160
return await tokenInstance.allowance(ownerAddress, spenderAddress);
121161
}
@@ -136,10 +176,13 @@ export default class ERC20Wrapper {
136176
to: Address,
137177
value: BigNumber,
138178
callerAddress: Address = undefined,
139-
txOpts?: TransactionOverrides,
179+
txOpts?: TransactionOverrides
140180
): Promise<ContractTransaction> {
141181
const txOptions = await generateTxOpts(txOpts);
142-
const tokenInstance = await this.contracts.loadERC20Async(tokenAddress, callerAddress);
182+
const tokenInstance = await this.contracts.loadERC20Async(
183+
tokenAddress,
184+
callerAddress
185+
);
143186

144187
return await tokenInstance.transfer(to, value, txOptions);
145188
}
@@ -162,9 +205,12 @@ export default class ERC20Wrapper {
162205
to: Address,
163206
value: BigNumber,
164207
callerAddress: Address = undefined,
165-
txOpts?: TransactionOverrides,
208+
txOpts?: TransactionOverrides
166209
): Promise<ContractTransaction> {
167-
const tokenInstance = await this.contracts.loadERC20Async(tokenAddress, callerAddress);
210+
const tokenInstance = await this.contracts.loadERC20Async(
211+
tokenAddress,
212+
callerAddress
213+
);
168214
const txOptions = await generateTxOpts(txOpts);
169215

170216
return await tokenInstance.transferFrom(from, to, value, txOptions);
@@ -185,10 +231,13 @@ export default class ERC20Wrapper {
185231
spenderAddress: Address,
186232
value: BigNumber,
187233
callerAddress: Address = undefined,
188-
txOpts?: TransactionOverrides,
234+
txOpts?: TransactionOverrides
189235
): Promise<ContractTransaction> {
190236
const txOptions = await generateTxOpts(txOpts);
191-
const tokenInstance = await this.contracts.loadERC20Async(tokenAddress, callerAddress);
237+
const tokenInstance = await this.contracts.loadERC20Async(
238+
tokenAddress,
239+
callerAddress
240+
);
192241

193242
return await tokenInstance.approve(spenderAddress, value, txOptions);
194243
}

0 commit comments

Comments
 (0)