Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate boilerplate or no code deploy common contracts #77 #78

Merged
merged 10 commits into from
Mar 13, 2019
6 changes: 3 additions & 3 deletions abi_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"strings"

abis "github.com/gochain-io/web3/abi"
"github.com/gochain-io/web3/assets"

"github.com/gochain-io/gochain/v3/accounts/abi"
)
Expand Down Expand Up @@ -34,5 +34,5 @@ func readAbi(reader io.Reader) (*abi.ABI, error) {
}

var bundledContracts = map[string]string{
"erc20": abis.ERC20,
"erc721": abis.ERC721}
"erc20": assets.ERC20,
"erc721": assets.ERC721}
File renamed without changes.
29 changes: 28 additions & 1 deletion abi/erc20.go → assets/erc20.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
package abis
package assets

import (
"math/big"
)

type Erc20Params struct {
Symbol string
TokenName string
Decimals int
TotalSupply *big.Int
}

const ERC20_CAPPED_PAUSABLE_TEMPLATE = `pragma solidity ^0.5.2;
r-gochain marked this conversation as resolved.
Show resolved Hide resolved

import "./lib/oz/contracts/token/ERC20/ERC20Pausable.sol";
import "./lib/oz/contracts/token/ERC20/ERC20Capped.sol";
import "./lib/oz/contracts/token/ERC20/ERC20Detailed.sol";

contract CappedToken is ERC20Detailed, ERC20Capped, ERC20Pausable {

// ------------------------------------------------------------------------
// Constructor
// ------------------------------------------------------------------------
constructor()
ERC20Detailed("{{.TokenName}}", "{{.Symbol}}", {{.Decimals}})
ERC20Capped({{.TotalSupply}})
public {}
}`
const ERC20 = `[
r-gochain marked this conversation as resolved.
Show resolved Hide resolved
{
"constant": true,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion abi/erc721.go → assets/erc721.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package abis
package assets

const ERC721 = `[
{
Expand Down
65 changes: 65 additions & 0 deletions cmd/web3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"context"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"math/big"
"os"
"os/exec"
"os/signal"
"sort"
"strconv"
"strings"
"syscall"
"time"
Expand All @@ -20,6 +23,7 @@ import (
"github.com/gochain-io/gochain/v3/common/hexutil"
"github.com/gochain-io/gochain/v3/core/types"
"github.com/gochain-io/web3"
"github.com/gochain-io/web3/assets"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -360,6 +364,19 @@ func main() {
}
},
},
{
Name: "generate",
Usage: "Generate a contract",
Subcommands: []cli.Command{
{
Name: "erc20",
Usage: "Generate a erc20 contract(use the following list of arguments after the command - SYMBOL TOKEN_NAME DECIMALS TOTAL_SUPPLY)",
Action: func(c *cli.Context) {
GenerateContract(ctx, "erc20", c.Args())
r-gochain marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
},
}
app.Run(os.Args)
}
Expand Down Expand Up @@ -871,6 +888,54 @@ func Send(ctx context.Context, rpcURL, privateKey, toAddress, amount string) {
fmt.Println("Transaction address:", tx.Hash.Hex())
}

func GenerateContract(ctx context.Context, contractType string, args []string) {
if _, err := os.Stat("lib/oz"); os.IsNotExist(err) {
cmd := exec.Command("git", "clone", "--depth", "1", "--branch", "master", "https://github.com/OpenZeppelin/openzeppelin-solidity", "lib/oz")
log.Printf("Cloning OpenZeppeling repo...")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatalf("Cloning finished with error: %v", err)
}
}
if contractType == "erc20" {
if len(args) != 4 {
log.Fatalln("wrong amount of the parameters, the following parameters are mandatory SYMBOL TOKEN_NAME DECIMALS TOTAL_SUPPLY")
}
decimals, err := strconv.Atoi(args[2])
if err != nil {
log.Fatalln("Cannot parse DECIMALS", err)
}
totalSupply, ok := new(big.Int).SetString(args[3], 10)
if !ok {
log.Fatalln("Cannot parse total supply")
}
totalSupply.Mul(totalSupply, new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimals)), nil))
params := assets.Erc20Params{
Symbol: args[0],
TokenName: args[1],
Decimals: decimals,
TotalSupply: totalSupply,
}
tmpl, err := template.New("contract").Parse(assets.ERC20_CAPPED_PAUSABLE_TEMPLATE)
if err != nil {
log.Fatalf("Cannot parse the template: %v", err)
}
f, err := os.Create(params.TokenName + ".sol")
if err != nil {
log.Fatalf("Cannot create the file: %v", err)
return
}
err = tmpl.Execute(f, params)
if err != nil {
log.Fatalf("Cannot execute the template: %v", err)
return
}
fmt.Println("The sample contract has been successfully written to", params.TokenName+".sol", "file")
}
}

func printReceiptDetails(r *web3.Receipt, myabi *abi.ABI) {
var logs []web3.Event
var err error
Expand Down