This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
erc20_fixedsupply.go
76 lines (56 loc) · 2.45 KB
/
erc20_fixedsupply.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package erc20
import (
"fmt"
"github.com/s7techlab/cckit/extensions/owner"
"github.com/s7techlab/cckit/router"
p "github.com/s7techlab/cckit/router/param"
)
const SymbolKey = `symbol`
const NameKey = `name`
const TotalSupplyKey = `totalSupply`
func NewErc20FixedSupply() *router.Chaincode {
r := router.New(`erc20fixedSupply`).Use(p.StrictKnown).
// Chaincode init function, initiates token smart contract with token symbol, name and totalSupply
Init(invokeInitFixedSupply, p.String(`symbol`), p.String(`name`), p.Int(`totalSupply`)).
// Get token symbol
Query(`symbol`, querySymbol).
// Get token name
Query(`name`, queryName).
// Get the total token supply
Query(`totalSupply`, queryTotalSupply).
// get account balance
Query(`balanceOf`, queryBalanceOf, p.String(`mspId`), p.String(`certId`)).
//Send value amount of tokens
Invoke(`transfer`, invokeTransfer, p.String(`toMspId`), p.String(`toCertId`), p.Int(`amount`)).
// Allow spender to withdraw from your account, multiple times, up to the _value amount.
// If this function is called again it overwrites the current allowance with _value
Invoke(`approve`, invokeApprove, p.String(`spenderMspId`), p.String(`spenderCertId`), p.Int(`amount`)).
// Returns the amount which _spender is still allowed to withdraw from _owner
Query(`allowance`, queryAllowance, p.String(`ownerMspId`), p.String(`ownerCertId`),
p.String(`spenderMspId`), p.String(`spenderCertId`)).
// Send amount of tokens from owner account to another
Invoke(`transferFrom`, invokeTransferFrom, p.String(`fromMspId`), p.String(`fromCertId`),
p.String(`toMspId`), p.String(`toCertId`), p.Int(`amount`))
return router.NewChaincode(r)
}
func invokeInitFixedSupply(c router.Context) (interface{}, error) {
ownerIdentity, err := owner.SetFromCreator(c)
if err != nil {
return nil, fmt.Errorf(`set chaincode owner: %w`, err)
}
// save token configuration in state
if err = c.State().Insert(SymbolKey, c.ParamString(`symbol`)); err != nil {
return nil, err
}
if err = c.State().Insert(NameKey, c.ParamString(`name`)); err != nil {
return nil, err
}
if err = c.State().Insert(TotalSupplyKey, c.ParamInt(`totalSupply`)); err != nil {
return nil, err
}
// set token owner initial balance
if err = setBalance(c, ownerIdentity.GetMSPIdentifier(), ownerIdentity.GetID(), c.ParamInt(`totalSupply`)); err != nil {
return nil, fmt.Errorf(`set owner initial balance: %w`, err)
}
return ownerIdentity, nil
}