OSDN Git Service

add TestCompileLockContract
authorChengcheng Zhang <943420582@qq.com>
Mon, 26 Aug 2019 07:27:56 +0000 (15:27 +0800)
committerChengcheng Zhang <943420582@qq.com>
Mon, 26 Aug 2019 07:27:56 +0000 (15:27 +0800)
main.go
swap/contract.go
swap/contract_test.go [new file with mode: 0644]
swap/request.go

diff --git a/main.go b/main.go
index e61c151..8af7a49 100644 (file)
--- a/main.go
+++ b/main.go
@@ -8,27 +8,29 @@ import (
 
 func main() {
        balances := swap.ListBalances("a1")
-       fmt.Println(balances)
+       fmt.Println("balances:", balances)
 
        accounts := swap.ListAccounts()
-       fmt.Println(accounts)
+       fmt.Println("accounts:", accounts)
 
        addresses := swap.ListAddresses("a1")
-       fmt.Println(addresses)
+       fmt.Println("addresses:", addresses)
 
        pubkeyInfo := swap.ListPubkeys("a1")
        fmt.Println(pubkeyInfo)
 
+       assetRequested := "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
        seller := "00145dd7b82556226d563b6e7d573fe61d23bd461c1f"
        cancelKey := "3e5d7d52d334964eef173021ef6a04dc0807ac8c41700fe718f5a80c2109f79e"
-       contractInfo := swap.Compile(seller, cancelKey)
-       fmt.Println(contractInfo)
+       amountRequested := uint64(1000000000)
+       contractInfo := swap.CompileLockContract(assetRequested, seller, cancelKey, amountRequested)
+       fmt.Println("contract info:", contractInfo)
 
        assetID := "bae7e17bb8f5d0cfbfd87a92f3204da082d388d4c9b10e8dcd36b3d0a18ceb3a"
        amount := uint64(20000000000)
        controlProgram := "203e5d7d52d334964eef173021ef6a04dc0807ac8c41700fe718f5a80c2109f79e1600145dd7b82556226d563b6e7d573fe61d23bd461c1f0400ca9a3b20ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff741a547a6413000000007b7b51547ac1631a000000547a547aae7cac00c0"
        tx := swap.BuildTransaction(assetID, controlProgram, amount)
-       fmt.Println(tx)
+       fmt.Println("tx:", tx)
 
        password := "12345"
        signedTransaction := swap.SignTransaction(password, string(tx))
index 3b1e0dd..fa90c06 100644 (file)
@@ -25,11 +25,11 @@ func ListAccounts() []Account {
        data := []byte(`{}`)
        body := request(listAccountsURL, data)
 
-       accounts := new(AccountsResponse)
-       if err := json.Unmarshal(body, accounts); err != nil {
+       accountsResp := new(AccountsResponse)
+       if err := json.Unmarshal(body, accountsResp); err != nil {
                fmt.Println(err)
        }
-       return accounts.Data
+       return accountsResp.Data
 }
 
 type Address struct {
@@ -71,11 +71,11 @@ func ListBalances(accountAlias string) []Balance {
        data := []byte(`{"account_alias": "` + accountAlias + `"}`)
        body := request(listBalancesURL, data)
 
-       balances := new(BalancesResponse)
-       if err := json.Unmarshal(body, balances); err != nil {
+       balancesResp := new(BalancesResponse)
+       if err := json.Unmarshal(body, balancesResp); err != nil {
                fmt.Println(err)
        }
-       return balances.Data
+       return balancesResp.Data
 }
 
 type PubkeyInfo struct {
@@ -97,11 +97,11 @@ func ListPubkeys(accountAlias string) KeyInfo {
        data := []byte(`{"account_alias": "` + accountAlias + `"}`)
        body := request(listPubkeysURL, data)
 
-       pubkeys := new(PubkeysResponse)
-       if err := json.Unmarshal(body, pubkeys); err != nil {
+       pubkeysResp := new(PubkeysResponse)
+       if err := json.Unmarshal(body, pubkeysResp); err != nil {
                fmt.Println(err)
        }
-       return pubkeys.Data
+       return pubkeysResp.Data
 }
 
 type ContractInfo struct {
@@ -113,15 +113,15 @@ type ContractResponse struct {
        Data   ContractInfo `json:"data"`
 }
 
-func Compile(seller, cancelKey string) ContractInfo {
+func CompileLockContract(assetRequested, seller, cancelKey string, amountRequested uint64) ContractInfo {
        data := []byte(`{
                "contract":"contract TradeOffer(assetRequested: Asset, amountRequested: Amount, seller: Program, cancelKey: PublicKey) locks valueAmount of valueAsset { clause trade() { lock amountRequested of assetRequested with seller unlock valueAmount of valueAsset } clause cancel(sellerSig: Signature) { verify checkTxSig(cancelKey, sellerSig) unlock valueAmount of valueAsset}}",
                "args":[
                        {
-                               "string":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
+                               "string":"` + assetRequested + `"
                        },
                        {
-                               "integer":1000000000
+                               "integer":` + strconv.FormatUint(amountRequested, 10) + `
                        },
                        {
                                "string":"` + seller + `"
diff --git a/swap/contract_test.go b/swap/contract_test.go
new file mode 100644 (file)
index 0000000..a2850ea
--- /dev/null
@@ -0,0 +1,39 @@
+package swap
+
+import (
+       "reflect"
+       "testing"
+)
+
+var (
+       assetRequested  = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
+       seller          = "00145dd7b82556226d563b6e7d573fe61d23bd461c1f"
+       cancelKey       = "3e5d7d52d334964eef173021ef6a04dc0807ac8c41700fe718f5a80c2109f79e"
+       amountRequested = uint64(1000000000)
+)
+
+func TestCompileLockContract(t *testing.T) {
+       var tests = []struct {
+               assetRequested  string
+               seller          string
+               cancelKey       string
+               amountRequested uint64
+               want            string
+       }{
+               {
+                       assetRequested:  "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
+                       seller:          "00145dd7b82556226d563b6e7d573fe61d23bd461c1f",
+                       cancelKey:       "3e5d7d52d334964eef173021ef6a04dc0807ac8c41700fe718f5a80c2109f79e",
+                       amountRequested: uint64(1000000000),
+                       want:            "203e5d7d52d334964eef173021ef6a04dc0807ac8c41700fe718f5a80c2109f79e1600145dd7b82556226d563b6e7d573fe61d23bd461c1f0400ca9a3b20ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff741a547a6413000000007b7b51547ac1631a000000547a547aae7cac00c0",
+               },
+       }
+
+       for i, tt := range tests {
+               got := CompileLockContract(tt.assetRequested, tt.seller, tt.cancelKey, tt.amountRequested).Program
+
+               if !reflect.DeepEqual(got, tt.want) {
+                       t.Errorf("CompileLockContract %d: content mismatch: have %x, want %x", i, got, tt.want)
+               }
+       }
+}
index db41ef9..6be8b3b 100644 (file)
@@ -38,6 +38,6 @@ func request(URL string, data []byte) []byte {
        if err != nil {
                fmt.Println(err)
        }
-       fmt.Println("response Body:", string(body))
+       // fmt.Println("response Body:", string(body))
        return body
 }