示例#1
0
func readAbi(root, contract string) ([]byte, error) {
	p := path.Join(root, common.StripHex(contract))
	if _, err := os.Stat(p); err != nil {
		return []byte{}, fmt.Errorf("Abi doesn't exist for =>\t%s", p)
	}

	b, err := ioutil.ReadFile(p)
	if err != nil {
		return []byte{}, err
	}

	return b, nil
}
示例#2
0
func PackArgsABI(abiSpec abi.ABI, data ...string) (string, error) {

	funcName := data[0]
	args := data[1:]

	a := []interface{}{}
	for _, aa := range args {
		bb, _ := hex.DecodeString(common.StripHex(common.CoerceHexAndPad(aa, true)))
		a = append(a, bb)
	}

	packedBytes, err := abiSpec.Pack(funcName, args)
	if err != nil {
		return "", err
	}

	packed := hex.EncodeToString(packedBytes)

	return packed, nil
}
示例#3
0
func DeployJob(deploy *definitions.Deploy, do *definitions.Do) (string, error) {
	// Preprocess variables
	deploy.Source, _ = util.PreProcess(deploy.Source, do)
	deploy.Contract, _ = util.PreProcess(deploy.Contract, do)
	deploy.Amount, _ = util.PreProcess(deploy.Amount, do)
	deploy.Nonce, _ = util.PreProcess(deploy.Nonce, do)
	deploy.Fee, _ = util.PreProcess(deploy.Fee, do)
	deploy.Gas, _ = util.PreProcess(deploy.Gas, do)

	// Use default
	deploy.Source = useDefault(deploy.Source, do.Package.Account)
	deploy.Amount = useDefault(deploy.Amount, do.DefaultAmount)
	deploy.Fee = useDefault(deploy.Fee, do.DefaultFee)
	deploy.Gas = useDefault(deploy.Gas, do.DefaultGas)

	// assemble contract
	var p string
	if _, err := os.Stat(deploy.Contract); err == nil {
		p = deploy.Contract
	} else {
		p = filepath.Join(do.ContractsPath, deploy.Contract)
	}
	logger.Debugf("Contract path =>\t\t%s\n", p)

	// use the proper compiler
	if do.Compiler != "" {
		logger.Debugf("Setting compiler path =>\t%s\n", do.Compiler)
		if err := setCompiler(do, p); err != nil {
			return "", err
		}
	}

	// compile
	bytecode, abiSpec, err := compilers.Compile(p)
	if err != nil {
		return "", err
	}
	logger.Debugf("Abi spec =>\t\t\t%s\n", string(abiSpec))
	contractCode := hex.EncodeToString(bytecode)

	// Don't use pubKey if account override
	var oldKey string
	if deploy.Source != do.Package.Account {
		oldKey = do.PublicKey
		do.PublicKey = ""
	}

	// additional data may be sent along with the contract
	// these are naively added to the end of the contract code using standard
	// mint packing
	if deploy.Data != "" {
		splitout := strings.Split(deploy.Data, " ")
		for _, s := range splitout {
			s, _ = util.PreProcess(s, do)
			addOns := common.LeftPadString(common.StripHex(common.Coerce2Hex(s)), 64)
			logger.Debugf("Contract Code =>\t\t%s\n", contractCode)
			logger.Debugf("\tAdditional Data =>\t%s\n", addOns)
			contractCode = contractCode + addOns
		}
	}

	// Deploy contract
	logger.Infof("Deploying Contract =>\t\t%s:%v\n", deploy.Source, contractCode)
	tx, err := core.Call(do.Chain, do.Signer, do.PublicKey, deploy.Source, "", deploy.Amount, deploy.Nonce, deploy.Gas, deploy.Fee, contractCode)
	if err != nil {
		return "", fmt.Errorf("Error deploying contract %s: %v", p, err)
	}

	// Sign, broadcast, display
	var result string
	result, err = deployFinalize(do, tx, deploy.Wait)

	// Save ABI
	if _, err := os.Stat(do.ABIPath); os.IsNotExist(err) {
		if err := os.Mkdir(do.ABIPath, 0775); err != nil {
			return "", err
		}
	}
	abiLocation := filepath.Join(do.ABIPath, result)
	logger.Debugf("Saving ABI =>\t\t\t%s\n", abiLocation)
	if err := ioutil.WriteFile(abiLocation, []byte(abiSpec), 0664); err != nil {
		return "", err
	}

	// Don't use pubKey if account override
	if deploy.Source != do.Package.Account {
		do.PublicKey = oldKey
	}

	return result, nil
}