Skip to content

Commit af0cce4

Browse files
authored
Merge pull request #3 from yakubido/master
Refactor encode methods
2 parents a1190ee + 2d77089 commit af0cce4

File tree

1 file changed

+5
-48
lines changed

1 file changed

+5
-48
lines changed

src/Coder.php

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -152,44 +152,23 @@ public function getAllowance(string $ownerAddress, string $spenderAddress): stri
152152
}
153153

154154
/**
155+
* @param string $method
155156
* @param array $params
156157
* @return string
157158
* @throws CoderException
158159
*/
159-
public function getCodeTransferFrom(array $params): string
160-
{
161-
$function = $this->contract->functions['transferFrom'];
162-
if (count($params) !== count($function['inputs']))
163-
{
164-
throw new CoderException('Incorrect number parameters for method transferFrom.');
165-
}
166-
167-
$data = $this->ethAbi->encodeParameters($function, $params);
168-
169-
$functionSignature = $this->encodeFunctionSignature('transferFrom');
170-
171-
172-
$code = $functionSignature . Utils::stripZero($data);
173-
174-
return $code;
175-
}
176-
177-
/**
178-
* @param array $params
179-
* @return string
180-
* @throws CoderException
181-
*/
182-
public function getCodeTransfer(array $params): string
160+
public function encodeMethod(string $method, array $params): string
183161
{
184-
$function = $this->contract->functions['transfer'];
162+
$methodName = strtolower($method);
163+
$function = $this->contract->functions[$methodName];
185164
if (count($params) !== count($function['inputs']))
186165
{
187166
throw new CoderException('Incorrect number parameters for method transferFrom.');
188167
}
189168

190169
$data = $this->ethAbi->encodeParameters($function, $params);
191170

192-
$functionSignature = $this->encodeFunctionSignature('transfer');
171+
$functionSignature = $this->encodeFunctionSignature($methodName);
193172

194173

195174
$code = $functionSignature . Utils::stripZero($data);
@@ -216,26 +195,4 @@ private function encodeFunctionSignature(string $method)
216195

217196
return substr(Utils::sha3($rawSignature), 0, 10);
218197
}
219-
220-
/**
221-
* @param array $params
222-
* @return string
223-
* @throws CoderException
224-
*/
225-
public function getCodeApprove(array $params): string
226-
{
227-
$function = $this->contract->functions['approve'];
228-
if (count($params) !== count($function['inputs']))
229-
{
230-
throw new CoderException('Incorrect number parameters for method approve.');
231-
}
232-
$data = $this->ethAbi->encodeParameters($function, $params);
233-
234-
$functionSignature = $this->encodeFunctionSignature('approve');
235-
236-
237-
$code = $functionSignature . Utils::stripZero($data);
238-
239-
return $code;
240-
}
241198
}

0 commit comments

Comments
 (0)