func TstCreateTxStore(t *testing.T) (store *wtxmgr.Store, tearDown func()) { dir, err := ioutil.TempDir("", "pool_test_txstore") if err != nil { t.Fatalf("Failed to create txstore dir: %v", err) } db, err := walletdb.Create("bdb", filepath.Join(dir, "txstore.db")) if err != nil { t.Fatalf("Failed to create walletdb: %v", err) } wtxmgrNamespace, err := db.Namespace([]byte("testtxstore")) if err != nil { t.Fatalf("Failed to create walletdb namespace: %v", err) } s, err := wtxmgr.Create(wtxmgrNamespace, &chaincfg.TestNetParams) if err != nil { t.Fatalf("Failed to create txstore: %v", err) } return s, func() { os.RemoveAll(dir) } }
func exampleCreateTxStore() (*wtxmgr.Store, func(), error) { dir, err := ioutil.TempDir("", "pool_test_txstore") if err != nil { return nil, nil, err } db, err := walletdb.Create("bdb", filepath.Join(dir, "txstore.db")) if err != nil { return nil, nil, err } wtxmgrNamespace, err := db.Namespace([]byte("testtxstore")) if err != nil { return nil, nil, err } s, err := wtxmgr.Create(wtxmgrNamespace, &chaincfg.SimNetParams) if err != nil { return nil, nil, err } return s, func() { os.RemoveAll(dir) }, nil }
func Example_basicUsage() { // Open the database. db, dbTeardown, err := testDB() defer dbTeardown() if err != nil { fmt.Println(err) return } // Create or open a db namespace for the transaction store. ns, err := db.Namespace([]byte("txstore")) if err != nil { fmt.Println(err) return } // Create (or open) the transaction store in the provided namespace. err = wtxmgr.Create(ns) if err != nil { fmt.Println(err) return } s, err := wtxmgr.Open(ns, &chaincfg.TestNetParams) if err != nil { fmt.Println(err) return } // Insert an unmined transaction that outputs 10 Coin to a wallet address // at output 0. err = s.InsertTx(exampleTxRecordA, nil) if err != nil { fmt.Println(err) return } defaultAccount := uint32(0) err = s.AddCredit(exampleTxRecordA, nil, 0, false, defaultAccount) if err != nil { fmt.Println(err) return } // Insert a second transaction which spends the output, and creates two // outputs. Mark the second one (5 Coin) as wallet change. err = s.InsertTx(exampleTxRecordB, nil) if err != nil { fmt.Println(err) return } err = s.AddCredit(exampleTxRecordB, nil, 1, true, defaultAccount) if err != nil { fmt.Println(err) return } // Mine each transaction in a block at height 100. err = s.InsertTx(exampleTxRecordA, &exampleBlock100) if err != nil { fmt.Println(err) return } err = s.InsertTx(exampleTxRecordB, &exampleBlock100) if err != nil { fmt.Println(err) return } // Print the one confirmation balance. bal, err := s.Balance(1, 100, wtxmgr.BFBalanceSpendable, true, defaultAccount) if err != nil { fmt.Println(err) return } fmt.Println(bal) // Fetch unspent outputs. utxos, err := s.UnspentOutputs() if err != nil { fmt.Println(err) } expectedOutPoint := wire.OutPoint{Hash: exampleTxRecordB.Hash, Index: 1} for _, utxo := range utxos { fmt.Println(utxo.OutPoint == expectedOutPoint) } // Output: // 5 Coin // true }