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))
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 {
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 {
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 {
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 + `"
--- /dev/null
+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)
+ }
+ }
+}
if err != nil {
fmt.Println(err)
}
- fmt.Println("response Body:", string(body))
+ // fmt.Println("response Body:", string(body))
return body
}