-
Notifications
You must be signed in to change notification settings - Fork 1
/
accountupdate.go
82 lines (69 loc) · 2.85 KB
/
accountupdate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package walletmanager
import (
"context"
"time"
"github.com/iotaledger/evil-tools/pkg/models"
"github.com/iotaledger/evil-tools/pkg/utils"
"github.com/iotaledger/hive.go/ierrors"
iotago "github.com/iotaledger/iota.go/v4"
"github.com/iotaledger/iota.go/v4/builder"
)
func (m *Manager) allot(ctx context.Context, params *AllotAccountParams) error {
w, err := m.GetWallet(params.Alias)
if err != nil {
return ierrors.Wrapf(err, "could not get wallet and account for alias %s", params.Alias)
}
account, err := m.GetAccount(params.Alias)
if err != nil {
return ierrors.Wrapf(err, "could not get account for alias %s", params.Alias)
}
inputs, err := m.RequestManaAndFundsFromTheFaucet(ctx, w, params.Amount, iotago.BaseToken(0))
if err != nil {
return ierrors.Wrapf(err, "failed to request mana and funds from the faucet for alias %s", params.Alias)
}
congestionResp, issuanceResp, version, err := m.RequestBlockIssuanceData(ctx, m.Client, account.Account)
if err != nil {
return ierrors.Wrapf(err, "failed to request block issuance data for alias %s", params.Alias)
}
signedTx, err := m.createAllotTransaction(inputs, w, account.Account.ID())
if err != nil {
return ierrors.Wrapf(err, "failed to create transaction with allotment to alias %s", params.Alias)
}
blockID, err := m.PostWithBlock(ctx, m.Client, signedTx, m.GenesisAccount(), congestionResp, issuanceResp, version)
if err != nil {
return ierrors.Wrapf(err, "failed to post transaction with allotment to alias %s", params.Alias)
}
m.LogInfof("Posted transaction with blockID %s: allotment to alias %s", blockID.ToHex(), params.Alias)
return nil
}
func (m *Manager) createAllotTransaction(inputs []*models.OutputData, w *Wallet, accountID iotago.AccountID) (*iotago.SignedTransaction, error) {
currentTime := time.Now()
currentSlot := m.API.TimeProvider().SlotFromTime(currentTime)
apiForSlot := m.Client.APIForSlot(currentSlot)
// transaction signer
addrSigner, err := w.GetAddrSignerForIndexes(inputs...)
if err != nil {
return nil, ierrors.Wrap(err, "failed to get address signer")
}
txBuilder := builder.NewTransactionBuilder(apiForSlot, addrSigner)
for _, output := range inputs {
txBuilder.AddInput(&builder.TxInput{
UnlockTarget: output.Address,
InputID: output.OutputID,
Input: output.OutputStruct,
})
}
totalBalance := utils.SumOutputsBalance(inputs)
outputAddr, _, _ := w.getAddress(iotago.AddressEd25519)
output := builder.NewBasicOutputBuilder(outputAddr, totalBalance).MustBuild()
txBuilder.
AddOutput(output).
SetCreationSlot(currentSlot).
//WithTransactionCapabilities(iotago.TransactionCapabilitiesBitMaskWithCapabilities(iotago.WithTransactionCanDoAnything()))
AllotAllMana(txBuilder.CreationSlot(), accountID, 0)
signedTx, err := txBuilder.Build()
if err != nil {
return nil, ierrors.Wrap(err, "failed to build tx")
}
return signedTx, nil
}