Create application side code to invoke smartcode

We created the castvote.go as a client to invoke castvote contract in voternetchaincode.

  • First create a wallet for any registered user (user1 in this case). a wallet is nothing but a file containing the users certs,msp info which will be used by the client app to invoke transaction .It represents the identity which invokes the transaction.

  • gateway is responsible for setting up the network configuration for the transaction . Amongst other things it uses service discovery to get all the endorsers info for submitting the transaction.

  • The minimal network info is provided in the config file connection.yaml

  • finally get the contract from the channel and invoke the transaction

castvote.go

wallet, err := gateway.NewFileSystemWallet("wallet")

gw, err := gateway.Connect(
   gateway.WithConfig(config.FromFile(configFilePath)),
   gateway.WithIdentity(wallet, "user"),
)

network, err := gw.GetNetwork("votingchannel")

contract := network.GetContract("voternetchaincode")
result, err := contract.SubmitTransaction("Cast", voter, time.Now().String(), candidate, uuid.New().String())

Last updated

Was this helpful?