Skip to content

Commit

Permalink
Merge pull request #182 from kaleido-io/erc20
Browse files Browse the repository at this point in the history
Make ERC-20 / ERC-721 default
  • Loading branch information
nguyer authored Apr 26, 2022
2 parents dd9c432 + fdc6c78 commit f3ee3c5
Show file tree
Hide file tree
Showing 16 changed files with 248 additions and 119 deletions.
4 changes: 2 additions & 2 deletions cmd/deploy_ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ solc --combined-json abi,bin contract.sol > contract.json
return err
}
}
contractAddress, err := stackManager.DeployContract(filename, selectedContractName, 0, args[2:])
location, err := stackManager.DeployContract(filename, selectedContractName, 0, args[2:])
if err != nil {
return err
}
fmt.Print(contractAddress)
fmt.Print(location)
return nil
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func init() {
initCmd.Flags().IntVarP(&initOptions.ServicesBasePort, "services-base-port", "s", 5100, "Mapped port base of services (100 added for each member)")
initCmd.Flags().StringVarP(&databaseSelection, "database", "d", "sqlite3", fmt.Sprintf("Database type to use. Options are: %v", types.DBSelectionStrings))
initCmd.Flags().StringVarP(&blockchainProviderInput, "blockchain-provider", "b", "geth", fmt.Sprintf("Blockchain provider to use. Options are: %v", types.BlockchainProviderStrings))
initCmd.Flags().StringArrayVarP(&tokenProvidersSelection, "token-providers", "t", []string{"erc1155"}, fmt.Sprintf("Token providers to use. Options are: %v", types.ValidTokenProviders))
initCmd.Flags().StringArrayVarP(&tokenProvidersSelection, "token-providers", "t", []string{"erc20_erc721"}, fmt.Sprintf("Token providers to use. Options are: %v", types.ValidTokenProviders))
initCmd.Flags().IntVarP(&initOptions.ExternalProcesses, "external", "e", 0, "Manage a number of FireFly core processes outside of the docker-compose stack - useful for development and debugging")
initCmd.Flags().StringVarP(&initOptions.FireFlyVersion, "release", "r", "latest", "Select the FireFly release version to use")
initCmd.Flags().StringVarP(&initOptions.ManifestPath, "manifest", "m", "", "Path to a manifest.json file containing the versions of each FireFly microservice to use. Overrides the --release flag.")
Expand Down
6 changes: 5 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ This command will start a stack and run it in the background.
if spin != nil {
spin.Start()
}
if err := stackManager.StartStack(verbose, &startOptions); err != nil {
messages, err := stackManager.StartStack(verbose, &startOptions)
if err != nil {
return err
}
if spin != nil {
spin.Stop()
}
fmt.Print("\n\n")
for _, message := range messages {
fmt.Printf("%s\n\n", message)
}
for _, member := range stackManager.Stack.Members {
fmt.Printf("Web UI for member '%v': http://127.0.0.1:%v/ui\n", member.ID, member.ExposedFireflyPort)
if stackManager.Stack.SandboxEnabled {
Expand Down
4 changes: 2 additions & 2 deletions internal/blockchain/blockchain_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import (
type IBlockchainProvider interface {
WriteConfig(options *types.InitOptions) error
FirstTimeSetup() error
DeployFireFlyContract() (*core.BlockchainConfig, error)
DeployFireFlyContract() (*core.BlockchainConfig, *types.ContractDeploymentResult, error)
PreStart() error
PostStart() error
GetDockerServiceDefinitions() []*docker.ServiceDefinition
GetFireflyConfig(stack *types.Stack, member *types.Member) (blockchainConfig *core.BlockchainConfig, coreConfig *core.OrgConfig)
Reset() error
GetContracts(filename string, extraArgs []string) ([]string, error)
DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (interface{}, error)
DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (*types.ContractDeploymentResult, error)
CreateAccount(args []string) (interface{}, error)
ParseAccount(interface{}) interface{}
}
18 changes: 12 additions & 6 deletions internal/blockchain/ethereum/besu/besu_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (p *BesuProvider) PostStart() error {
return nil
}

func (p *BesuProvider) DeployFireFlyContract() (*core.BlockchainConfig, error) {
func (p *BesuProvider) DeployFireFlyContract() (*core.BlockchainConfig, *types.ContractDeploymentResult, error) {
return ethconnect.DeployFireFlyContract(p.Stack, p.Log, p.Verbose)
}

Expand Down Expand Up @@ -223,7 +223,7 @@ func (p *BesuProvider) Reset() error {
}

func (p *BesuProvider) GetContracts(filename string, extraArgs []string) ([]string, error) {
contracts, err := ethereum.ReadCombinedABIJSON(filename)
contracts, err := ethereum.ReadContractJSON(filename)
if err != nil {
return []string{}, err
}
Expand All @@ -236,14 +236,20 @@ func (p *BesuProvider) GetContracts(filename string, extraArgs []string) ([]stri
return contractNames, err
}

func (p *BesuProvider) DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (interface{}, error) {
func (p *BesuProvider) DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (*types.ContractDeploymentResult, error) {
contractAddres, err := ethconnect.DeployCustomContract(member, filename, contractName)
if err != nil {
return nil, err
}
return map[string]string{
"address": contractAddres,
}, nil
result := &types.ContractDeploymentResult{
DeployedContract: &types.DeployedContract{
Name: contractName,
Location: map[string]string{
"address": contractAddres,
},
},
}
return result, nil
}

func (p *BesuProvider) CreateAccount(args []string) (interface{}, error) {
Expand Down
27 changes: 22 additions & 5 deletions internal/blockchain/ethereum/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ type CompiledContract struct {
}

type truffleCompiledContract struct {
ABI interface{} `json:"abi"`
Bytecode string `json:"bytecode"`
ABI interface{} `json:"abi"`
Bytecode string `json:"bytecode"`
ContractName string `json:"contractName"`
}

func ReadTruffleCompiledContract(filePath string) (*CompiledContract, error) {
func ReadTruffleCompiledContract(filePath string) (*CompiledContracts, error) {
d, _ := ioutil.ReadFile(filePath)
var truffleCompiledContract *truffleCompiledContract
err := json.Unmarshal(d, &truffleCompiledContract)
Expand All @@ -48,10 +49,15 @@ func ReadTruffleCompiledContract(filePath string) (*CompiledContract, error) {
ABI: truffleCompiledContract.ABI,
Bytecode: truffleCompiledContract.Bytecode,
}
return contract, nil
contracts := &CompiledContracts{
Contracts: map[string]*CompiledContract{
truffleCompiledContract.ContractName: contract,
},
}
return contracts, nil
}

func ReadCombinedABIJSON(filePath string) (*CompiledContracts, error) {
func ReadSolcCompiledContract(filePath string) (*CompiledContracts, error) {
d, _ := ioutil.ReadFile(filePath)
var contracts *CompiledContracts
err := json.Unmarshal(d, &contracts)
Expand All @@ -61,6 +67,17 @@ func ReadCombinedABIJSON(filePath string) (*CompiledContracts, error) {
return contracts, nil
}

func ReadContractJSON(filePath string) (*CompiledContracts, error) {
contracts, err := ReadSolcCompiledContract(filePath)
if err != nil {
return nil, err
}
if len(contracts.Contracts) > 0 {
return contracts, nil
}
return ReadTruffleCompiledContract(filePath)
}

func ExtractContracts(containerName, sourceDir, destinationDir string, verbose bool) error {
if err := docker.RunDockerCommand(destinationDir, verbose, verbose, "cp", containerName+":"+sourceDir, destinationDir); err != nil {
return err
Expand Down
33 changes: 21 additions & 12 deletions internal/blockchain/ethereum/ethconnect/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func DeprecatedRegisterContract(member *types.Member, contract *ethereum.Compile
return nil
}

func DeployFireFlyContract(s *types.Stack, log log.Logger, verbose bool) (*core.BlockchainConfig, error) {
func DeployFireFlyContract(s *types.Stack, log log.Logger, verbose bool) (*core.BlockchainConfig, *types.ContractDeploymentResult, error) {
var containerName string
var firstNonExternalMember *types.Member
for _, member := range s.Members {
Expand All @@ -293,42 +293,51 @@ func DeployFireFlyContract(s *types.Stack, log log.Logger, verbose bool) (*core.
}
}
if containerName == "" {
return nil, errors.New("unable to extract contracts from container - no valid firefly core containers found in stack")
return nil, nil, errors.New("unable to extract contracts from container - no valid firefly core containers found in stack")
}
log.Info("extracting smart contracts")

if err := ethereum.ExtractContracts(containerName, "/firefly/contracts", s.RuntimeDir, verbose); err != nil {
return nil, err
return nil, nil, err
}

contracts, err := ethereum.ReadCombinedABIJSON(filepath.Join(s.RuntimeDir, "contracts", "Firefly.json"))
var fireflyContract *ethereum.CompiledContract
contracts, err := ethereum.ReadContractJSON(filepath.Join(s.RuntimeDir, "contracts", "Firefly.json"))
if err != nil {
return nil, err
return nil, nil, err
}

fireflyContract, ok := contracts.Contracts["Firefly.sol:Firefly"]
if !ok {
fireflyContract, err = ethereum.ReadTruffleCompiledContract(filepath.Join(s.RuntimeDir, "contracts", "Firefly.json"))
if err != nil {
return nil, err
fireflyContract, ok = contracts.Contracts["FireFly"]
if !ok {
return nil, nil, fmt.Errorf("unable to find compiled FireFly contract")
}
}

log.Info(fmt.Sprintf("deploying firefly contract via '%s'", firstNonExternalMember.ID))
contractAddress, err := deployContract(firstNonExternalMember, fireflyContract, map[string]string{})
if err != nil {
return nil, err
return nil, nil, err
}
return &core.BlockchainConfig{
blockchainConfig := &core.BlockchainConfig{
Ethereum: &core.EthereumConfig{
Ethconnect: &core.EthconnectConfig{
Instance: contractAddress,
},
},
}, nil
}
result := &types.ContractDeploymentResult{
DeployedContract: &types.DeployedContract{
Name: "FireFly",
Location: map[string]string{"address": contractAddress},
},
}
return blockchainConfig, result, nil
}

func DeployCustomContract(member *types.Member, filename, contractName string) (string, error) {
contracts, err := ethereum.ReadCombinedABIJSON(filename)
contracts, err := ethereum.ReadContractJSON(filename)
if err != nil {
return "", nil
}
Expand Down
19 changes: 13 additions & 6 deletions internal/blockchain/ethereum/geth/geth_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (p *GethProvider) unlockAccount(address, password string) error {
return nil
}

func (p *GethProvider) DeployFireFlyContract() (*core.BlockchainConfig, error) {
func (p *GethProvider) DeployFireFlyContract() (*core.BlockchainConfig, *types.ContractDeploymentResult, error) {
return ethconnect.DeployFireFlyContract(p.Stack, p.Log, p.Verbose)
}

Expand Down Expand Up @@ -205,7 +205,7 @@ func (p *GethProvider) Reset() error {
}

func (p *GethProvider) GetContracts(filename string, extraArgs []string) ([]string, error) {
contracts, err := ethereum.ReadCombinedABIJSON(filename)
contracts, err := ethereum.ReadContractJSON(filename)
if err != nil {
return []string{}, err
}
Expand All @@ -218,14 +218,21 @@ func (p *GethProvider) GetContracts(filename string, extraArgs []string) ([]stri
return contractNames, err
}

func (p *GethProvider) DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (interface{}, error) {
func (p *GethProvider) DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (*types.ContractDeploymentResult, error) {
contractAddres, err := ethconnect.DeployCustomContract(member, filename, contractName)
if err != nil {
return nil, err
}
return map[string]string{
"address": contractAddres,
}, nil

result := &types.ContractDeploymentResult{
DeployedContract: &types.DeployedContract{
Name: contractName,
Location: map[string]string{
"address": contractAddres,
},
},
}
return result, nil
}

func (p *GethProvider) CreateAccount(args []string) (interface{}, error) {
Expand Down
Loading

0 comments on commit f3ee3c5

Please sign in to comment.