-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from xssnick/wallet
Added wallet functionality & fixes
- Loading branch information
Showing
22 changed files
with
2,876 additions
and
119 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
File renamed without changes.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"strings" | ||
|
||
"github.com/xssnick/tonutils-go/address" | ||
"github.com/xssnick/tonutils-go/liteclient" | ||
"github.com/xssnick/tonutils-go/liteclient/tlb" | ||
"github.com/xssnick/tonutils-go/ton" | ||
"github.com/xssnick/tonutils-go/ton/wallet" | ||
) | ||
|
||
func main() { | ||
client := liteclient.NewClient() | ||
|
||
// connect to mainnet lite server | ||
err := client.Connect(context.Background(), "135.181.140.212:13206", "K0t3+IWLOXHYMvMcrGZDPs+pn58a17LFbnXoQkKc2xw=") | ||
if err != nil { | ||
log.Fatalln("connection err: ", err.Error()) | ||
return | ||
} | ||
|
||
api := ton.NewAPIClient(client) | ||
|
||
// seed words of account, you can generate them with any wallet or using wallet.NewSeed() method | ||
words := strings.Split("birth pattern then forest walnut then phrase walnut fan pumpkin pattern then cluster blossom verify then forest velvet pond fiction pattern collect then then", " ") | ||
|
||
w, err := wallet.FromSeed(api, words, wallet.V3) | ||
if err != nil { | ||
log.Fatalln("FromPrivateKey err:", err.Error()) | ||
return | ||
} | ||
|
||
log.Println("wallet address:", w.Address()) | ||
|
||
block, err := api.GetBlockInfo(context.Background()) | ||
if err != nil { | ||
log.Fatalln("get block err:", err.Error()) | ||
return | ||
} | ||
|
||
balance, err := w.GetBalance(context.Background(), block) | ||
if err != nil { | ||
log.Fatalln("GetBalance err:", err.Error()) | ||
return | ||
} | ||
|
||
if balance.NanoTON().Uint64() >= 3000000 { | ||
addr := address.MustParseAddr("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N") | ||
err = w.Transfer(context.Background(), addr, new(tlb.Grams).MustFromTON("0.003"), "Hey bro, happy birthday!") | ||
if err != nil { | ||
log.Fatalln("Transfer err:", err.Error()) | ||
return | ||
} | ||
|
||
log.Println("transaction sent, balance left:", balance.TON()) | ||
|
||
return | ||
} | ||
|
||
log.Println("not enough balance:", balance.TON()) | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,51 @@ | ||
package tlb | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"math/big" | ||
) | ||
|
||
type Grams struct { | ||
val *big.Int | ||
} | ||
type Grams big.Int | ||
|
||
func (g Grams) TON() string { | ||
f := new(big.Float).SetInt(g.val) | ||
f := new(big.Float).SetInt((*big.Int)(&g)) | ||
t := new(big.Float).Quo(f, new(big.Float).SetUint64(1000000000)) | ||
|
||
return t.String() | ||
} | ||
|
||
func (g Grams) NanoTON() *big.Int { | ||
return g.val | ||
return (*big.Int)(&g) | ||
} | ||
|
||
func (g *Grams) FromNanoTON(val *big.Int) *Grams { | ||
*g = Grams(*val) | ||
return g | ||
} | ||
|
||
func (g *Grams) MustFromTON(val string) *Grams { | ||
v, err := g.FromTON(val) | ||
if err != nil { | ||
panic(err) | ||
return nil | ||
} | ||
return v | ||
} | ||
|
||
func (g *Grams) FromTON(val string) (*Grams, error) { | ||
f, ok := new(big.Float).SetString(val) | ||
if !ok { | ||
return nil, errors.New("invalid string") | ||
} | ||
|
||
f = f.Mul(f, new(big.Float).SetUint64(1000000000)) | ||
i, _ := f.Int(new(big.Int)) | ||
|
||
*g = Grams(*i) | ||
return g, nil | ||
} | ||
|
||
func (g *Grams) MarshalJSON() ([]byte, error) { | ||
return []byte(fmt.Sprintf("%q", g.TON())), nil | ||
} |
Oops, something went wrong.