GitLab's annual major release is around the corner. Along with a lot of new and exciting features, there will be a few breaking changes. Learn more here.

script.go 6.4 KB
Newer Older
ttt's avatar
ttt committed
1 2 3 4 5 6 7 8 9 10 11 12 13
package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"time"

	"gitlab.com/tzsuite/rpc"
	"gitlab.com/tzsuite/tzutil"
)

ttt's avatar
ttt committed
14
var seed = flag.String("seed", "", "specify a seed for the keys that will fund the setup")
ttt's avatar
ttt committed
15 16 17 18 19 20 21 22 23

var rpcURL = flag.String("rpcurl", "", "specify the base url of the tezos-node RPC")

func main() {
	flag.Parse()

	ch := make(chan error)
	shutdown := make(chan struct{})

ttt's avatar
ttt committed
24
	r, err := rpc.New(rpc.ConfAlphanet(*rpcURL), nil, shutdown, ch)
25 26 27
	if err != nil {
		log.Fatalf("setting up RPC client: %v", err)
	}
ttt's avatar
ttt committed
28

29 30
	go func(chan struct{}) {
		setup(r)
ttt's avatar
ttt committed
31
		close(shutdown)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	}(shutdown)

	for {
		select {
		case _, more := <-shutdown:
			if !more {
				time.Sleep(time.Millisecond * 500)
				return
			}
		case err := <-ch:
			fmt.Println("error from goroutine:", err)
			close(shutdown)
		}
	}
}
ttt's avatar
ttt committed
47

48
func setup(r *rpc.RPC) {
ttt's avatar
ttt committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	moneySeed := *seed
	moneyKeys, err := tzutil.KeysFromStringSeed(moneySeed)
	if err != nil {
		log.Fatalf("regenerating money keys: %v", err)
	}

	oracleKeys, err := tzutil.KeysWithoutSeed()
	if err != nil {
		log.Fatalf("generating oracle keys: %v", err)
	}

	providerKeys, err := tzutil.KeysWithoutSeed()
	if err != nil {
		log.Fatalf("generating oracle keys: %v", err)
	}

	fmt.Printf("Oracle Identity: %#v\n", oracleKeys)
	fmt.Printf("Provider Identity: %#v\n", providerKeys)

	done, err := r.BatchTransfer(moneyKeys, 10000000, moneyKeys.PKH, oracleKeys.PKH, nil)
ttt's avatar
ttt committed
69 70 71
	if err != nil {
		log.Fatal(err)
	}
ttt's avatar
ttt committed
72 73 74 75 76 77 78 79
	go func(done <-chan string) {
		opHash := <-done
		fmt.Println("transfer to oracle injected in operation:", opHash)
		blockHash := <-done
		fmt.Println("transfer to oracle included in block:", blockHash)
	}(done)

	done, err = r.BatchTransfer(moneyKeys, 10000000, moneyKeys.PKH, providerKeys.PKH, nil)
ttt's avatar
ttt committed
80 81 82
	if err != nil {
		log.Fatal(err)
	}
ttt's avatar
ttt committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	go func(done <-chan string) {
		opHash := <-done
		fmt.Println("transfer to provider injected in operation:", opHash)
		blockHash := <-done
		fmt.Println("transfer to provider included in block:", blockHash)
	}(done)

	// oracle contract
	done, oracleDone, err := r.BatchOriginate(moneyKeys, oracleKeys.PKH, 0, moneyKeys.PKH, prepareOracleScript(oracleKeys.PKH), false, false, "")
	if err != nil {
		log.Fatal(err)
	}
	included := make(chan string)
	go func(done <-chan string, included chan<- string) {
		opHash := <-done
		fmt.Println("origination of oracle contract injected in operation:", opHash)
		blockHash := <-done
		fmt.Println("origination of oracle contract included in block:", blockHash)
		included <- blockHash
	}(done, included)

	// placeholder proxy contract
	done, placeholderDone, err := r.BatchOriginate(moneyKeys, providerKeys.PKH, 0, moneyKeys.PKH, json.RawMessage(placeholderScript), false, false, "")
	if err != nil {
		log.Fatal(err)
	}
	go func(done <-chan string, included chan<- string) {
		opHash := <-done
		fmt.Println("origination of placeholder proxy contract injected in operation:", opHash)
		blockHash := <-done
		fmt.Println("origination of placeholder proxy contract included in block:", blockHash)
		included <- blockHash
	}(done, included)

	oracle := <-oracleDone
	placeholder := <-placeholderDone
	// make sure both originations made it into a block before continuing
	<-included
	<-included

	fmt.Println("oracle address is:", oracle)
	fmt.Println("placeholder address is:", placeholder)

	// provider contract
	// we need the previous two contracts for this one
	done, providerDone, err := r.BatchOriginate(moneyKeys, providerKeys.PKH, 20000000, moneyKeys.PKH, prepareProviderContract(providerKeys.PublicStr, oracle, placeholder), false, false, "")
	if err != nil {
		log.Fatal(err)
	}
	go func(done <-chan string, included chan<- string) {
		opHash := <-done
		fmt.Println("origination of provider contract injected in operation:", opHash)
		blockHash := <-done
		fmt.Println("origination of provider contract included in block:", blockHash)
		included <- blockHash
	}(done, included)

	provider := <-providerDone
	<-included

ttt's avatar
ttt committed
143
	fmt.Println("provider address is:", provider)
ttt's avatar
ttt committed
144

ttt's avatar
ttt committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	// real proxy contract
	// we need the oracle PKH and provider contract for this one
	done, proxyDone, err := r.BatchOriginate(moneyKeys, providerKeys.PKH, 0, moneyKeys.PKH, prepareProxyContract(oracleKeys.PKH, provider), false, false, "")
	if err != nil {
		log.Fatal(err)
	}
	go func(done <-chan string, included chan<- string) {
		opHash := <-done
		fmt.Println("origination of real proxy contract injected in operation:", opHash)
		blockHash := <-done
		fmt.Println("origination of real proxy contract included in block:", blockHash)
		included <- blockHash
	}(done, included)

	proxy := <-proxyDone
	<-included

	_, sig, err := r.HashAndSignData(prepareHDI(proxy), providerKeys)
	if err != nil {
		log.Fatal(err)
	}
	// initialize the provider contract
	// we need to sign the proxy contract address and send it along with the signature
	done, err = r.BatchTransfer(moneyKeys, 10000000, moneyKeys.PKH, provider, initParams(proxy, sig))
	if err != nil {
		log.Fatal(err)
	}
	opHash := <-done
	fmt.Println("initialization of provider injected in operation:", opHash)
	blockHash := <-done
	fmt.Println("initialization of provider included in block:", blockHash)

	fmt.Println("It works.")
	fmt.Printf("Oracle Identity: %#v\n", oracleKeys)
	fmt.Printf("Oracle Contract: %v\n", oracle)
	fmt.Printf("Provider Identity: %#v\n", providerKeys)
	fmt.Printf("Provider Contract: %v\n", provider)
	fmt.Printf("Proxy Contract: %v\n", proxy)

	close(included)
}

func prepareOracleScript(oraclePKH string) json.RawMessage {
	return json.RawMessage(oracleScript + oracleScriptInitStorage(oraclePKH))
}

func prepareProviderContract(providerPubKey, oracleContract, placeholderContract string) json.RawMessage {
	return json.RawMessage(providerScript + providerScriptInitStorage(providerPubKey, oracleContract, placeholderContract))
}

func prepareProxyContract(oraclePKH, providerContract string) json.RawMessage {
	return json.RawMessage(proxyScript + proxyScriptInitStorage(oraclePKH, providerContract))
}

func prepareHDI(proxyContract string) rpc.HashDataInput {
	return rpc.HashDataInput{
		Data: json.RawMessage(fmt.Sprintf(`{"string":"%s"}`, proxyContract)),
		Type: json.RawMessage(`{"prim":"contract","args":[{"prim":"pair","args":[{"prim":"bytes"},{"prim":"nat"}]}]}`),
	}
}

func initParams(proxyContract, sig string) json.RawMessage {
	return json.RawMessage(fmt.Sprintf(`{"prim":"Left","args":[{"prim":"Right","args":[{"prim":"Right","args":[{"prim":"Right","args":[{"prim":"Pair","args":[{"string":"%s"},{"string":"%s"}]}]}]}]}]}`, sig, proxyContract))
}