Created
May 13, 2020 09:50
-
-
Save lostpg/ee1b2f50a4276840198150e3372e2728 to your computer and use it in GitHub Desktop.
Deploy a ckb contract with ckb-sdk-go.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// File: secbit.io/deployer/txutil/collect-inputs.go | |
package txutil | |
import ( | |
"encoding/hex" | |
"fmt" | |
"github.com/ququzone/ckb-sdk-go/rpc" | |
"github.com/ququzone/ckb-sdk-go/types" | |
"github.com/ququzone/ckb-sdk-go/utils" | |
) | |
// CollectInputCells helps to collect sufficient live cells according to the given lock args | |
func CollectInputCells(client rpc.Client, receiverLockArg []byte, expense uint64) (cells []*types.Cell, capacity uint64, err error) { | |
collector := utils.NewCellCollector(client, &types.Script{ | |
CodeHash: types.HexToHash("0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"), | |
HashType: types.HashTypeType, | |
Args: receiverLockArg, | |
}, utils.NewCapacityCellProcessor(expense)) | |
result, err := collector.Collect() | |
if err != nil { | |
return nil, 0, err | |
} | |
cells, capacity = result.Cells, result.Capacity | |
return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"math" | |
"secbit.io/deployer/txutil" | |
"github.com/ququzone/ckb-sdk-go/crypto/secp256k1" | |
"github.com/ququzone/ckb-sdk-go/rpc" | |
"github.com/ququzone/ckb-sdk-go/transaction" | |
"github.com/ququzone/ckb-sdk-go/types" | |
"github.com/ququzone/ckb-sdk-go/utils" | |
) | |
func main() { | |
client, err := rpc.Dial("http://127.0.0.1:8114") | |
if err != nil { | |
log.Fatalf("create rpc client error: %v", err) | |
} | |
// Import private key from env variable | |
// hexPrivKey := os.Getenv("CKB_PRIVKEY") | |
hexPrivKey := "PRIVATE_KEY" | |
privKey, err := secp256k1.HexToKey(hexPrivKey) | |
if err != nil { | |
log.Fatalf("import private key error: %v", err) | |
} | |
systemScripts, err := utils.NewSystemScripts(client) | |
if err != nil { | |
log.Fatalf("load system script error: %v", err) | |
} | |
// `senderArgs, _ := blake2b.Blake160(privKey.PubKey())` | |
changeScript, _ := privKey.Script(systemScripts) | |
// Read contract executable | |
contractFilePath := "PATH_TO_CONTRACT_BINARY" | |
content, err := ioutil.ReadFile(contractFilePath) | |
if err != nil { | |
log.Fatalf("Read contract binary failed: %v", err) | |
} | |
// Totalize the expense during this transfer | |
transfering := uint64(423000 * math.Pow10(8)) | |
minerFee := uint64(math.Pow10(6)) | |
// Amount of ckb to be consumed. | |
expense := transfering + minerFee | |
// Collect sufficient input cells for this transaction | |
inputCells, capacity, err := txutil.CollectInputCells(client, changeScript.Args, expense) | |
if err != nil { | |
log.Fatalf("Collecting input cells error: %v", err) | |
} | |
changeAmount := int(capacity) - int(expense) | |
if changeAmount < 0 { | |
log.Fatalf("Not enough CKB, needs %d, has %d, change: %d", expense, capacity, changeAmount) | |
} | |
// Create a new single signature transaction. | |
tx := transaction.NewSecp256k1SingleSigTx(systemScripts) | |
tx.Outputs = append(tx.Outputs, &types.CellOutput{ | |
Capacity: transfering, | |
Lock: &types.Script{ | |
CodeHash: types.HexToHash("0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"), | |
HashType: types.HashTypeType, | |
Args: changeScript.Args, | |
}, | |
}) | |
tx.Outputs = append(tx.Outputs, &types.CellOutput{ | |
Capacity: uint64(changeAmount), | |
Lock: &types.Script{ | |
CodeHash: types.HexToHash("0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"), | |
HashType: types.HashTypeType, | |
Args: changeScript.Args, | |
}, | |
}) | |
tx.OutputsData = [][]byte{content, {}} | |
group, witnessArgs, err := transaction.AddInputsForTransaction(tx, inputCells) | |
if err != nil { | |
log.Fatalf("add inputs to transaction error: %v", err) | |
} | |
err = transaction.SingleSignTransaction(tx, group, witnessArgs, privKey) | |
if err != nil { | |
log.Fatalf("sign transaction error: %v", err) | |
} | |
fmt.Println(tx.ComputeHash()) | |
txutil.PrintTx(tx, inputCells) | |
// Send transaction and print tx hash | |
hash, err := client.SendTransaction(context.Background(), tx) | |
if err != nil { | |
log.Fatalf("send transaction error: %v", err) | |
} | |
fmt.Println(hash.String()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment