Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit d78609d

Browse files
committed
Allow to specify minted amount and max supply of native tokens
1 parent aa6a228 commit d78609d

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

Diff for: pkg/tests/combined_account_transition_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func createNativetoken(ts *testsuite.TestSuite) {
252252
node1 := ts.Node("node1")
253253
node2 := ts.Node("node2")
254254

255-
tx := wallet.CreateNativeTokenFromInput("TX7", "TX5:0", "TX4:0")
255+
tx := wallet.CreateNativeTokenFromInput("TX7", "TX5:0", "TX4:0", 50_000, 100_000)
256256
ts.IssueBasicBlockWithOptions("block6", wallet, tx)
257257

258258
ts.AssertTransactionsExist(wallet.Transactions("TX7"), true, node1)

Diff for: pkg/testsuite/mock/wallet_transactions.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/iotaledger/hive.go/lo"
1111
"github.com/iotaledger/hive.go/runtime/options"
1212
"github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger"
13+
"github.com/iotaledger/iota-core/pkg/testsuite/depositcalculator"
1314
iotago "github.com/iotaledger/iota.go/v4"
1415
"github.com/iotaledger/iota.go/v4/builder"
1516
"github.com/iotaledger/iota.go/v4/tpkg"
@@ -666,10 +667,18 @@ func (w *Wallet) CreateNFTFromInput(transactionName string, inputName string, op
666667
}
667668

668669
//nolint:forcetypeassert
669-
func (w *Wallet) CreateNativeTokenFromInput(transactionName string, inputName string, accountOutputName string) *iotago.SignedTransaction {
670+
func (w *Wallet) CreateNativeTokenFromInput(transactionName string, inputName string, accountOutputName string, mintedAmount iotago.BaseToken, maxSupply iotago.BaseToken) *iotago.SignedTransaction {
670671
input := w.Output(inputName)
671672
accountOutput := w.AccountOutput(accountOutputName)
672-
mintedAmount := input.BaseTokenAmount()
673+
674+
minMintedAmount := lo.PanicOnErr(depositcalculator.MinDeposit(w.Node.Protocol.CommittedAPI().ProtocolParameters(), iotago.OutputFoundry, depositcalculator.WithHasNativeToken()))
675+
mintedAmount = lo.Max(minMintedAmount, mintedAmount)
676+
maxSupply = lo.Max(maxSupply, mintedAmount)
677+
678+
if mintedAmount > input.BaseTokenAmount() {
679+
panic(fmt.Sprintf("input %s does not have enough funds to mint native token", inputName))
680+
}
681+
remainderAmount := input.BaseTokenAmount() - mintedAmount
673682

674683
// transition account output, increase foundry counter by 1, the amount of account stays the same
675684
accID := accountOutput.Output().(*iotago.AccountOutput).AccountID
@@ -681,7 +690,7 @@ func (w *Wallet) CreateNativeTokenFromInput(transactionName string, inputName st
681690
foundryID, _ := iotago.FoundryIDFromAddressAndSerialNumberAndTokenScheme(accAddr, accTransitionOutput.FoundryCounter, iotago.TokenSchemeSimple)
682691
tokenScheme := &iotago.SimpleTokenScheme{
683692
MintedTokens: big.NewInt(int64(mintedAmount)),
684-
MaximumSupply: big.NewInt(int64(mintedAmount)),
693+
MaximumSupply: big.NewInt(int64(maxSupply)),
685694
MeltedTokens: big.NewInt(0),
686695
}
687696

@@ -691,10 +700,22 @@ func (w *Wallet) CreateNativeTokenFromInput(transactionName string, inputName st
691700
Amount: big.NewInt(int64(mintedAmount)),
692701
}).MustBuild()
693702

703+
outputStates := iotago.Outputs[iotago.Output]{accTransitionOutput, foundryOutput}
704+
// prepare remainder output if needed
705+
if remainderAmount > 0 {
706+
outputStates = append(outputStates, &iotago.BasicOutput{
707+
Amount: remainderAmount,
708+
UnlockConditions: iotago.BasicOutputUnlockConditions{
709+
&iotago.AddressUnlockCondition{Address: w.Address()},
710+
},
711+
Features: iotago.BasicOutputFeatures{},
712+
})
713+
}
714+
694715
return w.createSignedTransactionWithOptions(
695716
transactionName,
696717
WithInputs(utxoledger.Outputs{accountOutput, input}),
697-
WithOutputs(iotago.Outputs[iotago.Output]{accTransitionOutput, foundryOutput}),
718+
WithOutputs(outputStates),
698719
WithBlockIssuanceCreditInput(&iotago.BlockIssuanceCreditInput{
699720
AccountID: accID,
700721
}),

Diff for: tools/docker-network/tests/accounttransition_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ func Test_AccountTransitions(t *testing.T) {
5555

5656
// create native token
5757
fmt.Println("\nCreating native token")
58-
account2 = d.CreateNativeToken(account2)
58+
account2 = d.CreateNativeToken(account2, 5000, 10000)
5959
}

Diff for: tools/docker-network/tests/dockerframework.go

+30-11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/iotaledger/hive.go/lo"
2323
"github.com/iotaledger/hive.go/runtime/options"
2424
"github.com/iotaledger/iota-core/pkg/protocol"
25+
"github.com/iotaledger/iota-core/pkg/testsuite/depositcalculator"
2526
"github.com/iotaledger/iota-core/pkg/testsuite/snapshotcreator"
2627
iotago "github.com/iotaledger/iota.go/v4"
2728
"github.com/iotaledger/iota.go/v4/api"
@@ -484,28 +485,35 @@ func (d *DockerTestFramework) AllotManaTo(from *Account, to *Account, manaToAllo
484485
}
485486

486487
// CreateNativeToken request faucet funds then use it to create native token for the account, and returns the updated Account.
487-
func (d *DockerTestFramework) CreateNativeToken(from *Account) (updatedAccount *Account) {
488+
func (d *DockerTestFramework) CreateNativeToken(from *Account, mintedAmount iotago.BaseToken, maxSupply iotago.BaseToken) (updatedAccount *Account) {
488489
// requesting faucet funds for native token creation
489490
ctx := context.TODO()
490491
fundsAddr, privateKey := d.getAddress(iotago.AddressEd25519)
491492
fundsOutputID, fundsUTXOOutput := d.RequestFaucetFunds(ctx, fundsAddr)
492493

493-
mintedAmount := fundsUTXOOutput.BaseTokenAmount()
494-
495494
clt := d.Node("V1").Client
496495
currentSlot := clt.LatestAPI().TimeProvider().SlotFromTime(time.Now())
497496
apiForSlot := clt.APIForSlot(currentSlot)
498497

498+
minMintedAmount, err := depositcalculator.MinDeposit(apiForSlot.ProtocolParameters(), iotago.OutputFoundry, depositcalculator.WithHasNativeToken())
499+
require.NoError(d.Testing, err)
500+
501+
mintedAmount = lo.Max(minMintedAmount, mintedAmount)
502+
require.GreaterOrEqual(d.Testing, mintedAmount, fundsUTXOOutput.BaseTokenAmount())
503+
maxSupply = lo.Max(maxSupply, mintedAmount)
504+
505+
remainderAmount := fundsUTXOOutput.BaseTokenAmount() - mintedAmount
506+
499507
// increase foundry counter
500508
accTransitionOutput := builder.NewAccountOutputBuilderFromPrevious(from.AccountOutput).
501509
FoundriesToGenerate(1).MustBuild()
502510

503-
// build foundry output, consume all amount from the UTXO output
511+
// build foundry output
504512
foundryID, err := iotago.FoundryIDFromAddressAndSerialNumberAndTokenScheme(from.AccountAddress, accTransitionOutput.FoundryCounter, iotago.TokenSchemeSimple)
505513
require.NoError(d.Testing, err)
506514
tokenScheme := &iotago.SimpleTokenScheme{
507515
MintedTokens: big.NewInt(int64(mintedAmount)),
508-
MaximumSupply: big.NewInt(int64(mintedAmount)),
516+
MaximumSupply: big.NewInt(int64(maxSupply)),
509517
MeltedTokens: big.NewInt(0),
510518
}
511519

@@ -515,17 +523,28 @@ func (d *DockerTestFramework) CreateNativeToken(from *Account) (updatedAccount *
515523
Amount: big.NewInt(int64(mintedAmount)),
516524
}).MustBuild()
517525

518-
// prepare transaction
526+
addressKeys := []iotago.AddressKeys{
527+
iotago.NewAddressKeysForEd25519Address(fundsAddr.(*iotago.Ed25519Address), privateKey),
528+
iotago.NewAddressKeysForEd25519Address(from.AccountOutput.UnlockConditionSet().Address().Address.(*iotago.Ed25519Address), from.BlockIssuerKey),
529+
}
530+
531+
signedTxBuilder := builder.NewTransactionBuilder(apiForSlot)
532+
533+
// prepare remainder output if needed
534+
if remainderAmount > 0 {
535+
receiveAddr, privKey := d.getAddress(iotago.AddressEd25519)
536+
remainderOutput := builder.NewBasicOutputBuilder(receiveAddr, remainderAmount).MustBuild()
537+
addressKeys = append(addressKeys, iotago.NewAddressKeysForEd25519Address(receiveAddr.(*iotago.Ed25519Address), privKey))
538+
signedTxBuilder = signedTxBuilder.AddOutput(remainderOutput)
539+
}
540+
519541
issuerResp, err := clt.BlockIssuance(ctx)
520542
require.NoError(d.Testing, err)
521543

522544
congestionResp, err := clt.Congestion(ctx, from.AccountAddress, lo.PanicOnErr(issuerResp.LatestCommitment.ID()))
523545
require.NoError(d.Testing, err)
524546

525-
signer := iotago.NewInMemoryAddressSigner(iotago.NewAddressKeysForEd25519Address(fundsAddr.(*iotago.Ed25519Address), privateKey),
526-
iotago.NewAddressKeysForEd25519Address(from.AccountOutput.UnlockConditionSet().Address().Address.(*iotago.Ed25519Address), from.BlockIssuerKey))
527-
528-
signedTx, err := builder.NewTransactionBuilder(apiForSlot).
547+
signedTx, err := signedTxBuilder.
529548
AddInput(&builder.TxInput{
530549
UnlockTarget: fundsAddr,
531550
InputID: fundsOutputID,
@@ -543,7 +562,7 @@ func (d *DockerTestFramework) CreateNativeToken(from *Account) (updatedAccount *
543562
AddCommitmentInput(&iotago.CommitmentInput{CommitmentID: lo.Return1(issuerResp.LatestCommitment.ID())}).
544563
WithTransactionCapabilities(iotago.TransactionCapabilitiesBitMaskWithCapabilities(iotago.WithTransactionCanDoAnything())).
545564
AllotAllMana(currentSlot, from.AccountID).
546-
Build(signer)
565+
Build(iotago.NewInMemoryAddressSigner(addressKeys...))
547566
require.NoError(d.Testing, err)
548567

549568
blkID := d.SubmitPayload(ctx, signedTx, wallet.NewEd25519Account(from.AccountID, from.BlockIssuerKey), congestionResp, issuerResp)

0 commit comments

Comments
 (0)