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
[FAB-18228] Add ERC20 fungible token sample for chaincode javascript (hyperledger#327)
This PR adds ERC20 capabilities to the token-account-based sample.
It includes javascript Chaincode and the updated README to explain
how to use tokens in the Fabric test-network.
Signed-off-by: Yuki Kondo <[email protected]>
Copy file name to clipboardExpand all lines: token-account-based/README.md
+150
Original file line number
Diff line number
Diff line change
@@ -30,10 +30,17 @@ The -ca flag is used to deploy the network using certificate authorities. This a
30
30
## Deploy the smart contract to the channel
31
31
32
32
You can use the test network script to deploy the account-based token contract to the channel that was just created. Deploy the smart contract to `mychannel` using the following command:
The above command deploys the go chaincode with short name `token_account`. The smart contract will use the default endorsement policy of majority of channel members.
38
45
Since the channel has two members, this implies that we'll need to get peer endorsements from 2 out of the 2 channel members.
39
46
@@ -140,6 +147,8 @@ Using the Org2 terminal, the Org2 recipient user can retrieve their own account
The `Transfer` function validates that the account associated with the calling client ID has sufficient funds for the transfer.
165
190
It will then debit the caller's account and credit the recipient's account. Note that the sample contract will automatically create an account with zero balance for the recipient, if one does not yet exist.
166
191
@@ -186,6 +211,131 @@ The function queries the balance of the account associated with the recipient cl
186
211
187
212
Congratulations, you've transferred 100 tokens! The Org2 recipient can now transfer tokens to other registered users in the same manner.
188
213
214
+
## Another scenario (for JavaScript contract only)
215
+
216
+
This sample has another transfer method called `transferFrom`, which allows an approved spender to transfer fungible tokens on behalf of the account owner. The second scenario demonstrates how to approve the spender and transfer fungible tokens.
217
+
218
+
In this tutorial, you will approve the spender and transfer tokens as follows:
219
+
220
+
- A minter has already created tokens according to the scenario above.
221
+
- The same minter client uses the `approve` function to set the allowance of tokens a spender client can transfer on behalf of the minter. It is assumed that the spender has provided their client ID to the `approve` caller out of band.
222
+
- The spender client will then use the `transferFrom` function to transfer the requested number of tokens to the recipient's account on behalf of the minter. It is assumed that the recipient has provided their client ID to the `transferFrom` caller out of band.
223
+
224
+
## Register identities
225
+
226
+
You have already brought up the network and deployed the smart contract to the channel. We will use the same network and smart contract.
227
+
228
+
We will use the Org1 CA to create the spender identity.
229
+
Back in the Org1 terminal, you can register a new spender client identity using the `fabric-ca-client` tool:
The minter intends to approve 500 tokens to be transferred by the spender, but first the spender needs to provide their own client ID as the payment address.
247
+
248
+
Open a 3rd terminal to represent the spender in Org1 and navigate to fabric-samples/test-network. Set the the environment variables for the Org1 spender user.
The approve function added that the spender client can consume 500 tokens on behalf of the minter. We can check the spender client's allowance from the minter by calling the `allowance` function.
280
+
281
+
Let's request the spender's allowance from the minter:
The function queries the allowance associated with the spender client ID and returns:
288
+
```
289
+
500
290
+
```
291
+
292
+
## TransferFrom tokens
293
+
294
+
The spender intends to transfer 100 tokens to the Org2 recipient on behalf of the minter. The spender has already got the minter client Id and the recipient client ID.
295
+
296
+
Back in the 3rd terminal, request the transfer of 100 tokens to the recipient account:
The `TransferFrom` function has three args: sender, recipient, amount. The function validates that the account associated with the sender has sufficient funds for the transfer. The function also validates if the allowance associated with the calling client ID exceeds funds to be transferred.
304
+
It will then debit the sender's account and credit the recipient's account. It will also decrease the spender's allowance approved by the minter. Note that the sample contract will automatically create an account with zero balance for the recipient, if one does not yet exist.
305
+
306
+
While still in the 3rd terminal, let's request the minter's account balance again:
The function queries the balance of the account associated with the recipient client ID and returns:
333
+
```
334
+
200
335
+
```
336
+
337
+
Congratulations, you've transferred 100 tokens! The Org2 recipient can now transfer tokens to other registered users in the same manner.
338
+
189
339
## Clean up
190
340
191
341
When you are finished, you can bring down the test network. The command will remove all the nodes of the test network, and delete any ledger data that you created:
0 commit comments