Created
January 12, 2019 08:16
-
-
Save zapkub/afada6dfe924e081457af90c2c935528 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"fmt" | |
"github.com/stellar/go/build" | |
"github.com/stellar/go/clients/horizon" | |
"github.com/stellar/go/keypair" | |
"net/http" | |
) | |
func panicErr(err error) { | |
if err != nil { | |
if e, ok := err.(*horizon.Error); ok { | |
b, _ := e.Problem.Extras["result_codes"].MarshalJSON() | |
fmt.Println(string(b)) | |
} else { | |
panic(err) | |
} | |
} | |
} | |
func main() { | |
fmt.Println("Hello world") | |
masterAccountKeypair, _ := keypair.Random() | |
distributorAccountKeypair, _ := keypair.Random() | |
v4bpAsset := build.CreditAsset("V4BP", masterAccountKeypair.Address()) | |
{ | |
url := fmt.Sprintf("https://friendbot.stellar.org?addr=%s", masterAccountKeypair.Address()) | |
resp, err := http.Get(url) | |
if err != nil { | |
panic(err) | |
} | |
if resp.StatusCode == http.StatusOK { | |
fmt.Printf("Account has been created ! %s\n", masterAccountKeypair.Address()) | |
} | |
} | |
candidateNames := []string{ | |
"lisa", "jennie", "jisoo", "rose", | |
} | |
// สร้าง Operation | |
operations := []build.TransactionMutator{ | |
build.SourceAccount{ | |
AddressOrSeed: masterAccountKeypair.Address(), | |
}, | |
build.TestNetwork, | |
build.AutoSequence{ | |
SequenceProvider: horizon.DefaultTestNetClient, | |
}, | |
build.CreateAccount( | |
build.Destination{ | |
AddressOrSeed: distributorAccountKeypair.Address(), | |
}, | |
build.NativeAmount{ | |
Amount: "50", | |
}, | |
), | |
build.Trust( | |
v4bpAsset.Code, | |
v4bpAsset.Issuer, | |
build.SourceAccount{ | |
AddressOrSeed: distributorAccountKeypair.Address(), | |
}, | |
), | |
build.Payment( | |
build.Destination{ | |
AddressOrSeed: distributorAccountKeypair.Address(), | |
}, | |
build.CreditAmount{ | |
Issuer: v4bpAsset.Issuer, | |
Code: v4bpAsset.Code, | |
Amount: "300000", | |
}, | |
build.SourceAccount{ | |
AddressOrSeed: masterAccountKeypair.Address(), | |
}, | |
), | |
} | |
candidateSecrets := []string{} | |
for _, name := range candidateNames { | |
candidateKeypair, _ := keypair.Random() | |
fmt.Printf("Generate wallet for %s: %s\n", name, candidateKeypair.Address()) | |
candidateSecrets = append(candidateSecrets, candidateKeypair.Seed()) | |
operations = append(operations, build.CreateAccount( | |
build.Destination{ | |
AddressOrSeed: candidateKeypair.Address(), | |
}, | |
build.NativeAmount{ | |
Amount: "50", | |
}, | |
)) | |
operations = append(operations, build.Trust( | |
v4bpAsset.Code, | |
v4bpAsset.Issuer, | |
build.SourceAccount{ | |
AddressOrSeed: candidateKeypair.Address(), | |
}, | |
)) | |
} | |
// สร้าง Transaction | |
tx, err := build.Transaction(operations...) | |
if err != nil { | |
panic(err) | |
} | |
// Sign transaction | |
candidateSecrets = append(candidateSecrets, masterAccountKeypair.Seed()) | |
candidateSecrets = append(candidateSecrets, distributorAccountKeypair.Seed()) | |
txe, err := tx.Sign(candidateSecrets...) | |
if err != nil { | |
panic(err) | |
} | |
txe64, err := txe.Base64() | |
// Submit transaction | |
resp, err := horizon.DefaultTestNetClient.SubmitTransaction(txe64) | |
if err != nil { | |
panicErr(err) | |
} | |
fmt.Printf("Issuer addr: %s\n", masterAccountKeypair.Address()) | |
fmt.Printf("Distributor addr: %s\nsecret: %s\n", distributorAccountKeypair.Address(), distributorAccountKeypair.Seed()) | |
fmt.Printf("Tx hash: %s\n", resp.Hash) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment