github.com/s7techlab/cckit@v0.10.5/examples/erc20/erc20_fixedsupply.go (about)

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