You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: packages/contract/README.md
+76-2Lines changed: 76 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -240,11 +240,11 @@ contract MyContract {
240
240
}
241
241
```
242
242
243
-
From Javascript's point of view, this contract has three functions: `setValue`, `getValue` and `value`. This is because `value` is public and automatically creates a getter function for it.
243
+
From JavaScript's point of view, this contract has three functions: `setValue`, `getValue` and `value`. This is because `value` is public and automatically creates a getter function for it.
244
244
245
245
#### Making a transaction via a contract function
246
246
247
-
When we call `setValue()`, this creates a transaction. From Javascript:
247
+
When we call `setValue()`, this creates a transaction. From JavaScript:
248
248
249
249
```javascript
250
250
constresult=instance.setValue(5);
@@ -310,6 +310,80 @@ const val = instance.getValue();
310
310
// since the contract returns that value.
311
311
```
312
312
313
+
#### Overloaded functions
314
+
315
+
Solidity supports a contract having multiple functions with the same name as long as the
316
+
function argument types differ. And so you may find yourself in the situation of having a
317
+
Truffle contract object with this property.
318
+
You can invoke these "overloaded" functions just like you would a normal contract function. Truffle contract instances
319
+
actually wrap web3's contract abstraction (`web3.eth.Contract`) and when you call an overloaded function, it
320
+
uses the same function resolution that web3 uses. However, we must give a warning that this overloaded function
321
+
resolution is often imprecise and can resolve to the wrong function when you call it.
322
+
323
+
Consider the following contrived code sample:
324
+
325
+
```solidity
326
+
contract MyContract {
327
+
uint256 public myUint;
328
+
string public myString;
329
+
string public myOtherString;
330
+
331
+
function setValues(uint256 val, string str) {
332
+
myUint = val;
333
+
myString = str
334
+
}
335
+
336
+
function setValues(string str, string otherStr) {
337
+
myString = str;
338
+
myOtherString = otherStr;
339
+
}
340
+
341
+
function setValues(uint256 val, string str, string otherStr) {
342
+
myUint = val;
343
+
myString = str;
344
+
myOtherString = otherStr;
345
+
}
346
+
}
347
+
```
348
+
349
+
This Solidity contract contains three functions named `setValues`, each taking a different type of input. In your JavaScript you
350
+
might do something like the following:
351
+
352
+
```javascript
353
+
awaitinstance.setValue(
354
+
666,
355
+
"this is not string cheese",
356
+
"this is string cheese"
357
+
);
358
+
```
359
+
360
+
As mentioned above, Truffle contract wraps web3 `Contract` objects and uses web3's overloaded function resolution.
361
+
To resolve these functions, web3's strategy is to look at the number of arguments that the function was invoked with and try
362
+
one that takes that many arguments. This works for some cases but is not ideal in that it
363
+
can invoke the incorrect function. Take, for example, the case where we do something like the following:
364
+
365
+
```javascript
366
+
awaitinstance.setValue("this is not string cheese", "this is string cheese");
367
+
```
368
+
369
+
We know that in our contract there are two functions named `setValues` that take
370
+
two arguments. In the above case, we cannot be sure whether web3's resolution will resolve to the first or second
371
+
function. The more precise way to reference functions like this is to use
372
+
the `methods` property on the contract instance. The following is an example of how you
373
+
access these methods:
374
+
375
+
```javascript
376
+
awaitinstance.methods["setValue(string,string)"](
377
+
"this is not string cheese",
378
+
"this is string cheese"
379
+
);
380
+
```
381
+
382
+
The `methods` property is an object whose keys are strings that correspond to the contract function's names
383
+
and signatures with no spaces. The values are the functions themselves. You can inspect the
384
+
`methods` property to see the key names in this object and match the function signature.
385
+
We recommend using this method of calling overloaded functions since it will unambiguously resolve to the correct function.
386
+
313
387
#### Processing transaction results
314
388
315
389
When you make a transaction, you're given a `result` object that gives you a wealth of information about the transaction. You're given the transaction hash (`result.tx`), the decoded events (also known as logs; `result.logs`), and a transaction receipt (`result.receipt`). In the below example, you'll receive the `ValueSet()` event because you triggered the event using the `setValue()` function:
0 commit comments