95 lines
1.9 KiB
Go
95 lines
1.9 KiB
Go
package apt
|
|
|
|
import (
|
|
"github.com/aptos-labs/aptos-go-sdk"
|
|
"github.com/aptos-labs/aptos-go-sdk/crypto"
|
|
"github.com/zeromicro/go-zero/core/threading"
|
|
)
|
|
|
|
type Apt struct {
|
|
client *aptos.Client
|
|
sender *aptos.Account
|
|
|
|
payloads chan aptos.TransactionBuildPayload
|
|
results chan aptos.TransactionSubmissionResponse
|
|
}
|
|
|
|
func NewApt(privateKeyStr string, isTest bool, resultHandler func(aptos.TransactionSubmissionResponse)) (*Apt, error) {
|
|
var networkConfig aptos.NetworkConfig
|
|
if isTest {
|
|
networkConfig = aptos.TestnetConfig
|
|
} else {
|
|
networkConfig = aptos.MainnetConfig
|
|
}
|
|
client, err := aptos.NewClient(networkConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
privateKey := &crypto.Ed25519PrivateKey{}
|
|
err = privateKey.FromHex(privateKeyStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sender, err := aptos.NewAccountFromSigner(privateKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
payloads := make(chan aptos.TransactionBuildPayload, 100)
|
|
results := make(chan aptos.TransactionSubmissionResponse, 100)
|
|
|
|
threading.GoSafe(func() {
|
|
client.BuildSignAndSubmitTransactions(sender, payloads, results)
|
|
})
|
|
|
|
a := &Apt{
|
|
client: client,
|
|
sender: sender,
|
|
payloads: payloads,
|
|
results: results,
|
|
}
|
|
|
|
threading.GoSafe(func() {
|
|
for result := range results {
|
|
a.handleResult(result)
|
|
}
|
|
})
|
|
|
|
return a, nil
|
|
}
|
|
|
|
func (a *Apt) transferUsdt(id uint64, toAddress string, amount uint64) error {
|
|
receiver := aptos.AccountAddress{}
|
|
err := receiver.ParseStringRelaxed(toAddress)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
p, err := aptos.CoinTransferPayload(nil, receiver, amount)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
a.payloads <- aptos.TransactionBuildPayload{
|
|
Id: id,
|
|
Type: aptos.TransactionSubmissionTypeSingle,
|
|
Inner: aptos.TransactionPayload{Payload: p},
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *Apt) handleResult(result aptos.TransactionSubmissionResponse) {
|
|
|
|
}
|
|
|
|
func (a *Apt) Start() {
|
|
|
|
}
|
|
|
|
func (a *Apt) Stop() {
|
|
close(a.payloads)
|
|
close(a.results)
|
|
}
|