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
+48-2Lines changed: 48 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,52 @@ const val = instance.getValue();
310
310
// since the contract returns that value.
311
311
```
312
312
313
+
#### Overloaded functions
314
+
315
+
You may find yourself in the situation of having a Truffle contract object that has multiple functions with the same name.
316
+
You can call these "overloaded functions" just like you would a normal contract function. Truffle contract instances
317
+
actually wrap web3's contract abstraction (`web3.eth.Contract`) and so when you call an overloaded function, it
318
+
uses the same function resolution that web3 uses. However, we must give a warning that this overloaded function
319
+
resolution is a bit dodgy and can resolve to the wrong function when you call it. So be careful!
320
+
321
+
The more unambiguous way of calling these types of functions is to use the `methods` namespace on Truffle contract
322
+
objects. Consider the following contrived code sample:
323
+
324
+
```solidity
325
+
contract MyContract {
326
+
uint256 public myUint;
327
+
string public myString;
328
+
329
+
function setValue(uint val) {
330
+
myUint = val;
331
+
}
332
+
333
+
function setValue(string str) {
334
+
myString = str;
335
+
}
336
+
}
337
+
```
338
+
339
+
This Solidity contract contains two functions named `setValue`, each taking a different type of input. In your JavaScript you
340
+
might do something like the following:
341
+
342
+
```javascript
343
+
awaitinstance.setValue("this is not string cheese");
344
+
```
345
+
346
+
Truffle contract (internally using web3's overloaded function resolution) will see that you input a string and
347
+
call the second method in the contract to set `myString`. Again however, sometimes when your contract is more
348
+
complex there might be some ambiguousness and it might fail to resolve to the correct function. In cases where
349
+
you want to explicitly specify which function you would like, you should do something similar to the following:
350
+
351
+
```javascript
352
+
awaitinstance.methods["setValue(string)"]("this is not string cheese");
353
+
```
354
+
355
+
The `methods` property is an object whose keys are strings that correspond to the contract function's names
356
+
and signatures. The values are the functions themselves. We recommend using this method of calling overloaded
357
+
functions since it will unambiguously resolve to the correct function.
358
+
313
359
#### Processing transaction results
314
360
315
361
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