summaryrefslogtreecommitdiff
path: root/tools/block-generator/generator/make_transactions.go
blob: 558dfd956fb5bf6e4bc8d575554c153fb795e2fb (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package generator

import (
	"encoding/binary"

	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/data/basics"
	txn "github.com/algorand/go-algorand/data/transactions"
	"github.com/algorand/go-algorand/data/txntest"
	"github.com/algorand/go-algorand/protocol"
)

// ---- header / boilerplate ----

func (g *generator) makeTxnHeader(sender basics.Address, round, intra uint64) txn.Header {
	note := make([]byte, 8)
	binary.LittleEndian.PutUint64(note, g.txnCounter+intra)

	return txn.Header{
		Sender:      sender,
		Fee:         basics.MicroAlgos{Raw: g.params.MinTxnFee},
		FirstValid:  basics.Round(round),
		LastValid:   basics.Round(round + 1000),
		GenesisID:   g.genesisID,
		GenesisHash: g.genesisHash,
		Note:        note,
	}
}

// makeTestTxn creates and populates the flat txntest.Txn structure with the given values.
func (g *generator) makeTestTxn(sender basics.Address, round, intra uint64) txntest.Txn {
	note := make([]byte, 8)
	binary.LittleEndian.PutUint64(note, g.txnCounter+intra)

	return txntest.Txn{
		Sender:      sender,
		Fee:         basics.MicroAlgos{Raw: g.params.MinTxnFee},
		FirstValid:  basics.Round(round),
		LastValid:   basics.Round(round + 1000),
		GenesisID:   g.genesisID,
		GenesisHash: g.genesisHash,
		Note:        note,
	}
}

// ---- payments ----

func (g *generator) makePaymentTxn(header txn.Header, receiver basics.Address, amount uint64, closeRemainderTo basics.Address) txn.Transaction {
	return txn.Transaction{
		Type:   protocol.PaymentTx,
		Header: header,
		PaymentTxnFields: txn.PaymentTxnFields{
			Receiver:         receiver,
			Amount:           basics.MicroAlgos{Raw: amount},
			CloseRemainderTo: closeRemainderTo,
		},
	}
}

// ---- asset transactions ----

func (g *generator) makeAssetCreateTxn(header txn.Header, total uint64, defaultFrozen bool, assetName string) txn.Transaction {
	return txn.Transaction{
		Type:   protocol.AssetConfigTx,
		Header: header,
		AssetConfigTxnFields: txn.AssetConfigTxnFields{
			AssetParams: basics.AssetParams{
				Total:         total,
				DefaultFrozen: defaultFrozen,
				AssetName:     assetName,
				Manager:       header.Sender,
				Freeze:        header.Sender,
				Clawback:      header.Sender,
				Reserve:       header.Sender,
			},
		},
	}
}

func (g *generator) makeAssetDestroyTxn(header txn.Header, index uint64) txn.Transaction {
	return txn.Transaction{
		Type:   protocol.AssetConfigTx,
		Header: header,
		AssetConfigTxnFields: txn.AssetConfigTxnFields{
			ConfigAsset: basics.AssetIndex(index),
		},
	}
}

func (g *generator) makeAssetTransferTxn(header txn.Header, receiver basics.Address, amount uint64, closeAssetsTo basics.Address, index uint64) txn.Transaction {
	return txn.Transaction{
		Type:   protocol.AssetTransferTx,
		Header: header,
		AssetTransferTxnFields: txn.AssetTransferTxnFields{
			XferAsset:     basics.AssetIndex(index),
			AssetAmount:   amount,
			AssetReceiver: receiver,
			AssetCloseTo:  closeAssetsTo,
		},
	}
}

func (g *generator) makeAssetAcceptanceTxn(header txn.Header, index uint64) txn.Transaction {
	return g.makeAssetTransferTxn(header, header.Sender, 0, basics.Address{}, index)
}

// ---- application transactions ----

func (g *generator) makeAppCreateTxn(kind appKind, sender basics.Address, round, intra uint64, futureAppId uint64) []txn.SignedTxn {
	var approval, clear interface{}
	if kind == appKindSwap {
		approval, clear = approvalSwapBytes, clearSwapBytes
	} else {
		approval, clear = approvalBoxesBytes, clearBoxesBytes
	}

	createTxn := g.makeTestTxn(sender, round, intra)

	createTxn.Type = protocol.ApplicationCallTx
	createTxn.ApprovalProgram = approval
	createTxn.ClearStateProgram = clear

	// max out local/global state usage but split
	// 50% between bytes/uint64
	createTxn.LocalStateSchema = basics.StateSchema{
		NumUint:      8,
		NumByteSlice: 8,
	}
	createTxn.GlobalStateSchema = basics.StateSchema{
		NumUint:      32,
		NumByteSlice: 32,
	}

	createTxFee := g.params.MinTxnFee
	senderIndex := accountToIndex(sender)

	// TODO: should check for min balance
	g.balances[senderIndex] -= createTxFee
	if kind != appKindBoxes {
		return txntest.Group(&createTxn)
	}

	// also group in a pay txn to fund the app
	pstFee := uint64(1_000)
	pstAmt := uint64(2_000_000)

	paySibTxn := g.makeTestTxn(sender, round, intra)
	paySibTxn.Type = protocol.PaymentTx
	paySibTxn.Receiver = basics.AppIndex(futureAppId).Address()
	paySibTxn.Fee = basics.MicroAlgos{Raw: pstFee}
	paySibTxn.Amount = uint64(pstAmt)

	// TODO: should check for min balance}
	g.balances[senderIndex] -= (pstFee + pstAmt)

	return txntest.Group(&createTxn, &paySibTxn)
}

// makeAppOptinTxn currently only works for the boxes app
func (g *generator) makeAppOptinTxn(sender basics.Address, round, intra uint64, kind appKind, appIndex uint64) []txn.SignedTxn {
	if kind != appKindBoxes {
		panic("makeAppOptinTxn only works for the boxes app currently")
	}

	optInTxn := g.makeTestTxn(sender, round, intra)
	/* all 0 values but keep around for reference
	optInTxn.ApplicationArgs = nil
	optInTxn.ForeignApps = nil
	optInTxn.ForeignAssets = nil
	optInTxn.ExtraProgramPages = 0
	*/

	optInTxn.Type = protocol.ApplicationCallTx
	optInTxn.ApplicationID = basics.AppIndex(appIndex)
	optInTxn.OnCompletion = txn.OptInOC
	// the first inner sends some algo to the creator:
	optInTxn.Accounts = []basics.Address{indexToAccount(g.appMap[kind][appIndex].sender)}
	optInTxn.Boxes = []txn.BoxRef{
		{Name: crypto.Digest(sender).ToSlice()},
	}

	// TODO: these may not make sense for the swap optin

	pstFee := uint64(2_000)
	pstAmt := uint64(2_000_000)

	paySibTxn := g.makeTestTxn(sender, round, intra)
	paySibTxn.Type = protocol.PaymentTx
	paySibTxn.Receiver = basics.AppIndex(appIndex).Address()
	paySibTxn.Fee = basics.MicroAlgos{Raw: pstFee}
	paySibTxn.Amount = uint64(pstAmt)

	senderIndex := accountToIndex(sender)
	// TODO: should check for min balance}
	// TODO: for the case of boxes, should refund 0.76 algo
	g.balances[senderIndex] -= (pstFee + pstAmt)

	return txntest.Group(&optInTxn, &paySibTxn)
}

// makeAppCallTxn currently only works for the boxes app
func (g *generator) makeAppCallTxn(sender basics.Address, round, intra, appIndex uint64) txn.Transaction {
	callTxn := g.makeTestTxn(sender, round, intra)
	callTxn.Type = protocol.ApplicationCallTx
	callTxn.ApplicationID = basics.AppIndex(appIndex)
	callTxn.OnCompletion = txn.NoOpOC // redundant for clarity
	callTxn.ApplicationArgs = [][]byte{
		{0xe1, 0xf9, 0x3f, 0x1d}, // the method selector for getting a box
	}

	callTxn.Boxes = []txn.BoxRef{
		{Name: crypto.Digest(sender).ToSlice()},
	}

	// TODO: should check for min balance
	appCallTxFee := g.params.MinTxnFee
	senderIndex := accountToIndex(sender)
	g.balances[senderIndex] -= appCallTxFee

	return callTxn.Txn()
}