diff --git a/ApolloBuilder.go b/ApolloBuilder.go index 3c65c10..92daca6 100644 --- a/ApolloBuilder.go +++ b/ApolloBuilder.go @@ -166,6 +166,7 @@ func (b *Apollo) ConsumeUTxO(utxo UTxO.UTxO, payments ...PaymentI) *Apollo { panic("selected value is negative") } b.payments = append(b.payments, payments...) + selectedValue = selectedValue.RemoveZeroAssets() p := NewPaymentFromValue(utxo.Output.GetAddress(), selectedValue) b.payments = append(b.payments, p) return b @@ -194,6 +195,7 @@ func (b *Apollo) ConsumeAssetsFromUtxo(utxo UTxO.UTxO, payments ...PaymentI) *Ap return b } b.payments = append(b.payments, payments...) + selectedValue = selectedValue.RemoveZeroAssets() p := NewPaymentFromValue(utxo.Output.GetAddress(), selectedValue) b.payments = append(b.payments, p) return b @@ -783,6 +785,15 @@ Returns: func (b *Apollo) setCollateral() (*Apollo, error) { if len(b.collaterals) > 0 { + collateral_amount := 5_000_000 + for _, utxo := range b.collaterals { + if int(utxo.Output.GetValue().GetCoin()) >= collateral_amount+1_000_000 && len(utxo.Output.GetValue().GetAssets()) <= 5 { + b.totalCollateral = collateral_amount + return_amount := utxo.Output.GetValue().GetCoin() - int64(collateral_amount) + returnOutput := TransactionOutput.SimpleTransactionOutput(b.inputAddresses[0], Value.SimpleValue(return_amount, utxo.Output.GetValue().GetAssets())) + b.collateralReturn = &returnOutput + } + } return b, nil } witnesses := b.buildWitnessSet() @@ -1198,7 +1209,7 @@ func (b *Apollo) addChangeAndFee() (*Apollo, error) { TransactionOutput.SimpleTransactionOutput(b.inputAddresses[0], Value.SimpleValue(0, change.GetAssets())), b.Context, ) { - if len(b.preselectedUtxos) == 0 { + if len(b.getAvailableUtxos()) == 0 { return b, errors.New("No Remaining UTxOs") } sortedUtxos := SortUtxos(b.getAvailableUtxos()) diff --git a/ApolloBuilder_test.go b/ApolloBuilder_test.go index de9787f..c9d45a6 100644 --- a/ApolloBuilder_test.go +++ b/ApolloBuilder_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/Salvionied/apollo" + "github.com/Salvionied/apollo/serialization" "github.com/Salvionied/apollo/serialization/Address" "github.com/Salvionied/apollo/serialization/Asset" "github.com/Salvionied/apollo/serialization/AssetName" @@ -565,3 +566,575 @@ func TestMintPlutusWithPayment(t *testing.T) { t.Error("Tx is not correct", hex.EncodeToString(txBytes)) } } + +func TestGetWallet(t *testing.T) { + cc := BlockFrostChainContext.NewBlockfrostChainContext(BLOCKFROST_BASE_URL_MAINNET, int(MAINNET), "mainnetVueasSgKfYhM4PQBq0UGipAyHBpbX4oT") + apollob := apollo.New(&cc) + wall := apollob.GetWallet() + if wall != nil { + t.Error("Wallet should be nil") + } + apollob = apollob.SetWalletFromBech32("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + wallet := apollob.GetWallet() + if wallet.GetAddress().String() != "addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu" { + t.Error("Wallet address is not correct") + } +} + +func TestAddInputs(t *testing.T) { + cc := BlockFrostChainContext.NewBlockfrostChainContext(BLOCKFROST_BASE_URL_MAINNET, int(MAINNET), "mainnetVueasSgKfYhM4PQBq0UGipAyHBpbX4oT") + decoded_addr, _ := Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + testUtxo := UTxO.UTxO{ + Input: TransactionInput.TransactionInput{ + TransactionId: []byte("d5d1f7c223dc88bb41474af23b685e0247307e94e715ef5e62f325ac94f73056"), + Index: 0, + }, + Output: TransactionOutput.SimpleTransactionOutput( + decoded_addr, + Value.SimpleValue(15000000, nil)), + } + apollob := apollo.New(&cc) + apollob = apollob.AddInput(testUtxo).SetChangeAddress(decoded_addr) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if !built.GetTx().TransactionBody.Inputs[0].EqualTo(testUtxo.Input) { + t.Error("Tx is not correct") + } + +} + +func TestConsumeUtxo(t *testing.T) { + cc := BlockFrostChainContext.NewBlockfrostChainContext(BLOCKFROST_BASE_URL_MAINNET, int(MAINNET), "mainnetVueasSgKfYhM4PQBq0UGipAyHBpbX4oT") + decoded_addr, _ := Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + testUtxo := UTxO.UTxO{ + Input: TransactionInput.TransactionInput{ + TransactionId: []byte("d5d1f7c223dc88bb41474af23b685e0247307e94e715ef5e62f325ac94f73056"), + Index: 0, + }, + Output: TransactionOutput.SimpleTransactionOutput( + decoded_addr, + Value.SimpleValue(15_000_000, nil)), + } + biAdaUtxo := UTxO.UTxO{ + Input: TransactionInput.TransactionInput{ + TransactionId: []byte("d5d1f7c223dc88bb41474af23b685e0247307e94e715ef5e62f325ac94f73056"), + Index: 1, + }, + Output: TransactionOutput.SimpleTransactionOutput( + decoded_addr, + Value.SimpleValue(15_000_000, nil)), + } + + apollob := apollo.New(&cc) + apollob = apollob.SetChangeAddress(decoded_addr). + ConsumeUTxO(testUtxo, + apollo.NewPayment("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu", 2_000_000, nil), + apollo.NewPayment("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu", 2_000_000, nil)).AddLoadedUTxOs(biAdaUtxo) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if !built.GetTx().TransactionBody.Inputs[0].EqualTo(testUtxo.Input) { + t.Error("Tx is not correct") + } + if len(built.GetTx().TransactionBody.Outputs) != 4 { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.Outputs[0].Lovelace() != 2_000_000 { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.Outputs[1].Lovelace() != 2_000_000 { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.Outputs[2].Lovelace() != 11_000_000 { + t.Error("Tx is not correct") + } +} + +func TestConsumeAssetsFromUtxo(t *testing.T) { + cc := BlockFrostChainContext.NewBlockfrostChainContext(BLOCKFROST_BASE_URL_MAINNET, int(MAINNET), "mainnetVueasSgKfYhM4PQBq0UGipAyHBpbX4oT") + decoded_addr, _ := Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + testUtxo := UTxO.UTxO{ + Input: TransactionInput.TransactionInput{ + TransactionId: []byte("d5d1f7c223dc88bb41474af23b685e0247307e94e715ef5e62f325ac94f73056"), + Index: 0, + }, + Output: TransactionOutput.SimpleTransactionOutput( + decoded_addr, + Value.SimpleValue(15_000_000, MultiAsset.MultiAsset[int64]{ + Policy.PolicyId{Value: "279c909f348e533da5808898f87f9a14bb2c3dfbbacccd631d927a3f"}: Asset.Asset[int64]{ + AssetName.NewAssetNameFromString("TEST"): 1, + }, + })), + } + biAdaUtxo := UTxO.UTxO{ + Input: TransactionInput.TransactionInput{ + TransactionId: []byte("d5d1f7c223dc88bb41474af23b685e0247307e94e715ef5e62f325ac94f73056"), + Index: 1, + }, + Output: TransactionOutput.SimpleTransactionOutput( + decoded_addr, + Value.SimpleValue(15_000_000, nil)), + } + + apollob := apollo.New(&cc) + apollob = apollob.SetChangeAddress(decoded_addr). + ConsumeAssetsFromUtxo(testUtxo, + apollo.NewPayment("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu", 2_000_000, []apollo.Unit{apollo.NewUnit("279c909f348e533da5808898f87f9a14bb2c3dfbbacccd631d927a3f", "TEST", 1)})).AddLoadedUTxOs(biAdaUtxo) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if !built.GetTx().TransactionBody.Inputs[0].EqualTo(testUtxo.Input) { + t.Error("Tx is not correct") + } + if len(built.GetTx().TransactionBody.Outputs) != 3 { + t.Error("Tx is not correct") + } + + if len(built.GetTx().TransactionBody.Outputs[0].GetValue().GetAssets()) != 1 { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.Outputs[1].Lovelace() != 15_000_000 { + t.Error("Tx is not correct") + } + if len(built.GetTx().TransactionBody.Outputs[1].GetValue().GetAssets()) != 0 { + t.Error("Tx is not correct") + } +} + +var decoded_addr, _ = Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + +var InputUtxo = UTxO.UTxO{ + Input: TransactionInput.TransactionInput{ + TransactionId: []byte("d5d1f7c223dc88bb41474af23b685e0247307e94e715ef5e62f325ac94f73056"), + Index: 1, + }, + Output: TransactionOutput.SimpleTransactionOutput( + decoded_addr, + Value.SimpleValue(15_000_000, nil)), +} + +func TestPayToContract(t *testing.T) { + cc := BlockFrostChainContext.NewBlockfrostChainContext(BLOCKFROST_BASE_URL_MAINNET, int(MAINNET), "mainnetVueasSgKfYhM4PQBq0UGipAyHBpbX4oT") + decoded_addr, _ := Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + datum := PlutusData.PlutusData{ + TagNr: 121, + PlutusDataType: PlutusData.PlutusBytes, + Value: []byte("Hello, World!")} + + apollob := apollo.New(&cc) + apollob = apollob.SetChangeAddress(decoded_addr).AddLoadedUTxOs(InputUtxo). + PayToContract(decoded_addr, &datum, 1_000_000, false). + PayToContract(decoded_addr, &datum, 1_000_000, true) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if len(built.GetTx().TransactionBody.Outputs) != 3 { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.Outputs[0].Lovelace() != 1_000_000 { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.Outputs[1].Lovelace() != 1_000_000 { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.Outputs[0].IsPostAlonzo { + t.Error("Tx is not correct") + } + if !built.GetTx().TransactionBody.Outputs[1].IsPostAlonzo { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.Outputs[0].GetDatumHash() == nil { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.Outputs[1].GetDatumHash() != nil { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionWitnessSet.PlutusData == nil { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.Outputs[1].GetDatum() == nil { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.Outputs[0].GetDatum() != nil { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.Outputs[1].GetDatum().TagNr != 121 { + t.Error("Tx is not correct", built.GetTx().TransactionBody.Outputs[1].GetDatum().TagNr) + } + txBytes, _ := built.GetTx().Bytes() + if hex.EncodeToString(txBytes) != "84a4008182584064356431663763323233646338386262343134373461663233623638356530323437333037653934653731356566356536326633323561633934663733303536010183835839010a59337f7b3a913424d7f7a151401e052642b68e948d8cacadc6372016a9999419cc5a61ca62da81e378d7538213a3715a6b858c948c69c91a000f4240582037ead362f4ab7844a8416b045caa46a91066d391c16ae4d4a81557f14f7a0984a3005839010a59337f7b3a913424d7f7a151401e052642b68e948d8cacadc6372016a9999419cc5a61ca62da81e378d7538213a3715a6b858c948c69c9011a000f4240028201d81850d8794d48656c6c6f2c20576f726c6421825839010a59337f7b3a913424d7f7a151401e052642b68e948d8cacadc6372016a9999419cc5a61ca62da81e378d7538213a3715a6b858c948c69c91a00c371ff021a0002eb410b5820e3ba8d5e90fa0152fc652eb7e2e8dfd9efa06ce2abd002e69c7b5f8a51be8cd7a1049fd8794d48656c6c6f2c20576f726c6421fff5f6" { + t.Error("Tx is not correct", hex.EncodeToString(txBytes)) + } + +} + +func TestRequiredSigner(t *testing.T) { + cc := BlockFrostChainContext.NewBlockfrostChainContext(BLOCKFROST_BASE_URL_MAINNET, int(MAINNET), "mainnetVueasSgKfYhM4PQBq0UGipAyHBpbX4oT") + decoded_addr, _ := Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + apollob := apollo.New(&cc) + apollob = apollob.SetChangeAddress(decoded_addr).AddLoadedUTxOs(InputUtxo). + AddRequiredSignerFromAddress(decoded_addr, true, true) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if len(built.GetTx().TransactionBody.RequiredSigners) != 2 { + t.Error("Tx is not correct") + } + if hex.EncodeToString(built.GetTx().TransactionBody.RequiredSigners[0][:]) != hex.EncodeToString(decoded_addr.PaymentPart) { + t.Error("Tx is not correct") + } + if hex.EncodeToString(built.GetTx().TransactionBody.RequiredSigners[1][:]) != hex.EncodeToString(decoded_addr.StakingPart) { + t.Error("Tx is not correct") + } + apollob = apollo.New(&cc) + apollob = apollob.SetChangeAddress(decoded_addr).AddLoadedUTxOs(InputUtxo). + AddRequiredSignerFromBech32(decoded_addr.String(), true, true) + built, err = apollob.Complete() + if err != nil { + t.Error(err) + } + if len(built.GetTx().TransactionBody.RequiredSigners) != 2 { + t.Error("Tx is not correct") + } + if hex.EncodeToString(built.GetTx().TransactionBody.RequiredSigners[0][:]) != hex.EncodeToString(decoded_addr.PaymentPart) { + t.Error("Tx is not correct") + } + if hex.EncodeToString(built.GetTx().TransactionBody.RequiredSigners[1][:]) != hex.EncodeToString(decoded_addr.StakingPart) { + t.Error("Tx is not correct") + } + + apollob = apollo.New(&cc) + apollob = apollob.SetChangeAddress(decoded_addr).AddLoadedUTxOs(InputUtxo). + AddRequiredSigner(serialization.PubKeyHash(decoded_addr.PaymentPart)) + built, err = apollob.Complete() + if err != nil { + t.Error(err) + } + if len(built.GetTx().TransactionBody.RequiredSigners) != 1 { + t.Error("Tx is not correct") + } + if hex.EncodeToString(built.GetTx().TransactionBody.RequiredSigners[0][:]) != hex.EncodeToString(decoded_addr.PaymentPart) { + t.Error("Tx is not correct") + } +} + +var collateralUtxo = UTxO.UTxO{ + Input: TransactionInput.TransactionInput{ + TransactionId: []byte("d5d1f7c223dc88bb41474af23b685e0247307e94e715ef5e62f325ac94f73056"), + Index: 1, + }, + Output: TransactionOutput.SimpleTransactionOutput( + decoded_addr, + Value.SimpleValue(5_000_000, nil))} + +var collateralUtxo2 = UTxO.UTxO{ + Input: TransactionInput.TransactionInput{ + TransactionId: []byte("d5d1f7c223dc88bb41474af23b685e0247307e94e715ef5e62f325ac94f73056"), + Index: 1, + }, + Output: TransactionOutput.SimpleTransactionOutput( + decoded_addr, + Value.SimpleValue(10_000_000, nil))} + +func TestFeePadding(t *testing.T) { + cc := BlockFrostChainContext.NewBlockfrostChainContext(BLOCKFROST_BASE_URL_MAINNET, int(MAINNET), "mainnetVueasSgKfYhM4PQBq0UGipAyHBpbX4oT") + decoded_addr, _ := Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + apollob := apollo.New(&cc) + apollob = apollob.SetChangeAddress(decoded_addr).AddLoadedUTxOs(InputUtxo).PayToContract(decoded_addr, nil, 1_000_000, false).SetFeePadding(500_000) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if built.GetTx().TransactionBody.Fee != 683641 { + t.Error("Tx is not correct", built.GetTx().TransactionBody.Fee) + } + if built.GetTx().TransactionBody.Outputs[0].Lovelace() != 1_000_000 { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.Outputs[1].Lovelace() != 13316359 { + t.Error("Tx is not correct", built.GetTx().TransactionBody.Outputs[1].Lovelace()) + } + if built.GetTx().TransactionBody.Outputs[0].IsPostAlonzo && built.GetTx().TransactionBody.Outputs[0].GetDatumHash() != nil { + t.Error("Tx is not correct", built.GetTx().TransactionBody.Outputs[0].GetDatumHash(), built.GetTx().TransactionBody.Outputs[0].IsPostAlonzo) + } +} + +func TestSetCollateral(t *testing.T) { + // full 5 ada collateral + cc := BlockFrostChainContext.NewBlockfrostChainContext(BLOCKFROST_BASE_URL_MAINNET, int(MAINNET), "mainnetVueasSgKfYhM4PQBq0UGipAyHBpbX4oT") + decoded_addr, _ := Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + apollob := apollo.New(&cc) + apollob = apollob.SetChangeAddress(decoded_addr).AddLoadedUTxOs(InputUtxo).PayToContract(decoded_addr, nil, 1_000_000, false).SetFeePadding(500_000).AddCollateral(collateralUtxo) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if !built.GetTx().TransactionBody.Collateral[0].EqualTo(collateralUtxo.Input) { + t.Error("Tx is not correct") + } +} + +func TestCollateralwithReturn(t *testing.T) { + // full 5 ada collateral + cc := BlockFrostChainContext.NewBlockfrostChainContext(BLOCKFROST_BASE_URL_MAINNET, int(MAINNET), "mainnetVueasSgKfYhM4PQBq0UGipAyHBpbX4oT") + decoded_addr, _ := Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + apollob := apollo.New(&cc) + apollob = apollob.SetChangeAddress(decoded_addr).AddLoadedUTxOs(InputUtxo).PayToContract(decoded_addr, nil, 1_000_000, false).SetFeePadding(500_000).AddCollateral(collateralUtxo2) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if built.GetTx().TransactionBody.TotalCollateral != 5_000_000 { + t.Error("Tx is not correct") + } + if built.GetTx().TransactionBody.CollateralReturn.Lovelace() != 5_000_000 { + t.Error("Tx is not correct", built.GetTx().TransactionBody.CollateralReturn) + } + if !built.GetTx().TransactionBody.Collateral[0].EqualTo(collateralUtxo2.Input) { + t.Error("Tx is not correct") + } +} + +func TestRedeemerCollect(t *testing.T) { + cc := apollo.NewEmptyBackend() + decoded_addr, _ := Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + apollob := apollo.New(&cc) + redeemer := Redeemer.Redeemer{ + Tag: Redeemer.SPEND, + Index: 0, + Data: PlutusData.PlutusData{ + TagNr: 121, + PlutusDataType: PlutusData.PlutusBytes, + Value: []byte("Hello, World!")}, + } + datum := PlutusData.PlutusData{ + TagNr: 121, + PlutusDataType: PlutusData.PlutusBytes, + Value: []byte("Hello, World!")} + + utxos := testutils.InitUtxosDifferentiated() + apollob = apollob.SetChangeAddress(decoded_addr).AddLoadedUTxOs(utxos...). + CollectFrom(InputUtxo, redeemer).AttachDatum(&datum).AttachV1Script([]byte("Hello, World!")).SetEstimationExUnitsRequired() + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if !built.GetTx().TransactionBody.Inputs[0].EqualTo(InputUtxo.Input) { + t.Error("Tx is not correct") + } + wts := built.GetTx().TransactionWitnessSet + if wts.Redeemer[0].Tag != Redeemer.SPEND { + t.Error("Tx is not correct") + } + if wts.Redeemer[0].Index != 0 { + t.Error("Tx is not correct") + } + if wts.Redeemer[0].Data.TagNr != 121 { + t.Error("Tx is not correct") + } + if wts.Redeemer[0].Data.PlutusDataType != PlutusData.PlutusBytes { + t.Error("Tx is not correct") + } + if string(wts.Redeemer[0].Data.Value.([]byte)) != "Hello, World!" { + t.Error("Tx is not correct") + } + if wts.PlutusData == nil { + t.Error("Tx is not correct") + } + if wts.PlutusData[0].TagNr != 121 { + t.Error("Tx is not correct") + } + if wts.PlutusData[0].PlutusDataType != PlutusData.PlutusBytes { + t.Error("Tx is not correct") + } + if string(wts.PlutusData[0].Value.([]byte)) != "Hello, World!" { + t.Error("Tx is not correct") + } + + if string(wts.PlutusV1Script[0]) != "Hello, World!" { + t.Error("Tx is not correct") + } + + if wts.Redeemer[0].ExUnits.Mem == 0 { + t.Error("Tx is not correct", wts.Redeemer[0].ExUnits.Mem) + } + if wts.Redeemer[0].ExUnits.Steps == 0 { + t.Error("Tx is not correct", wts.Redeemer[0].ExUnits.Steps) + } + if built.GetTx().TransactionBody.Fee != 228771 { + t.Error("Tx is not correct", built.GetTx().TransactionBody.Fee) + } + if built.GetTx().TransactionBody.Collateral == nil { + t.Error("Tx is not correct") + } + +} + +func TestAddSameScriptTwiceV1(t *testing.T) { + cc := apollo.NewEmptyBackend() + utxos := testutils.InitUtxosDifferentiated() + decoded_addr, _ := Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + apollob := apollo.New(&cc) + apollob = apollob.AttachV1Script([]byte("Hello, World!")).AttachV1Script([]byte("Hello, World!")) + apollob = apollob.SetChangeAddress(decoded_addr).AddLoadedUTxOs(utxos...) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + + } + if len(built.GetTx().TransactionWitnessSet.PlutusV1Script) != 1 { + t.Error("Tx is not correct") + } +} + +func TestAddSameScriptTwiceV2(t *testing.T) { + cc := apollo.NewEmptyBackend() + utxos := testutils.InitUtxosDifferentiated() + decoded_addr, _ := Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + apollob := apollo.New(&cc) + apollob = apollob.AttachV2Script([]byte("Hello, World!")).AttachV2Script([]byte("Hello, World!")) + apollob = apollob.SetChangeAddress(decoded_addr).AddLoadedUTxOs(utxos...) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if len(built.GetTx().TransactionWitnessSet.PlutusV2Script) != 1 { + t.Error("Tx is not correct") + } +} + +func TestSetChangeAddressBech32(t *testing.T) { + cc := apollo.NewEmptyBackend() + apollob := apollo.New(&cc) + apollob = apollob.SetChangeAddressBech32("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu").AddInput(InputUtxo) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if built.GetTx().TransactionBody.Outputs[0].GetAddress().String() != "addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu" { + t.Error("Tx is not correct") + } +} + +func TestSetWalletFromBech32(t *testing.T) { + cc := apollo.NewEmptyBackend() + apollob := apollo.New(&cc) + apollob = apollob.SetWalletFromBech32("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu").SetWalletAsChangeAddress().AddInput(InputUtxo) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if built.GetTx().TransactionBody.Outputs[0].GetAddress().String() != "addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu" { + t.Error("Tx is not correct") + } +} + +func TestRefInput(t *testing.T) { + cc := apollo.NewEmptyBackend() + apollob := apollo.New(&cc) + apollob = apollob.SetWalletFromBech32("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu").SetWalletAsChangeAddress().AddInput(InputUtxo). + AddReferenceInput(hex.EncodeToString(InputUtxo.Input.TransactionId), 0).AddCollateral(collateralUtxo) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if hex.EncodeToString(built.GetTx().TransactionBody.ReferenceInputs[0].TransactionId) != hex.EncodeToString(InputUtxo.Input.TransactionId) { + t.Error("Tx is not correct") + } +} + +func TestExactComplete(t *testing.T) { + cc := apollo.NewEmptyBackend() + decoded_addr, _ := Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + apollob := apollo.New(&cc) + redeemer := Redeemer.Redeemer{ + Tag: Redeemer.SPEND, + Index: 0, + Data: PlutusData.PlutusData{ + TagNr: 121, + PlutusDataType: PlutusData.PlutusBytes, + Value: []byte("Hello, World!")}, + } + datum := PlutusData.PlutusData{ + TagNr: 121, + PlutusDataType: PlutusData.PlutusBytes, + Value: []byte("Hello, World!")} + + utxos := testutils.InitUtxosDifferentiated() + apollob = apollob.SetChangeAddress(decoded_addr).AddLoadedUTxOs(utxos...). + CollectFrom(InputUtxo, redeemer).AttachDatum(&datum).AttachV1Script([]byte("Hello, World!")).SetEstimationExUnitsRequired() + built, err := apollob.CompleteExact(200_000) + if err != nil { + t.Error(err) + } + if !built.GetTx().TransactionBody.Inputs[0].EqualTo(InputUtxo.Input) { + t.Error("Tx is not correct") + } + wts := built.GetTx().TransactionWitnessSet + if wts.Redeemer[0].Tag != Redeemer.SPEND { + t.Error("Tx is not correct") + } + if wts.Redeemer[0].Index != 0 { + t.Error("Tx is not correct") + } + if wts.Redeemer[0].Data.TagNr != 121 { + t.Error("Tx is not correct") + } + if wts.Redeemer[0].Data.PlutusDataType != PlutusData.PlutusBytes { + t.Error("Tx is not correct") + } + if string(wts.Redeemer[0].Data.Value.([]byte)) != "Hello, World!" { + t.Error("Tx is not correct") + } + if wts.PlutusData == nil { + t.Error("Tx is not correct") + } + if wts.PlutusData[0].TagNr != 121 { + t.Error("Tx is not correct") + } + if wts.PlutusData[0].PlutusDataType != PlutusData.PlutusBytes { + t.Error("Tx is not correct") + } + if string(wts.PlutusData[0].Value.([]byte)) != "Hello, World!" { + t.Error("Tx is not correct") + } + + if string(wts.PlutusV1Script[0]) != "Hello, World!" { + t.Error("Tx is not correct") + } + + if wts.Redeemer[0].ExUnits.Mem == 0 { + t.Error("Tx is not correct", wts.Redeemer[0].ExUnits.Mem) + } + if wts.Redeemer[0].ExUnits.Steps == 0 { + t.Error("Tx is not correct", wts.Redeemer[0].ExUnits.Steps) + } + if built.GetTx().TransactionBody.Fee != 200_000 { + t.Error("Tx is not correct", built.GetTx().TransactionBody.Fee) + } + if built.GetTx().TransactionBody.Collateral == nil { + t.Error("Tx is not correct") + } +} + +func TestCongestedBuild(t *testing.T) { + cc := apollo.NewEmptyBackend() + decoded_addr, _ := Address.DecodeAddress("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seyu") + apollob := apollo.New(&cc) + utxos := testutils.InitUtxosCongested() + apollob = apollob.SetChangeAddress(decoded_addr).AddLoadedUTxOs(utxos...). + AddPayment(apollo.NewPayment("addr1qy99jvml0vafzdpy6lm6z52qrczjvs4k362gmr9v4hrrwgqk4xvegxwvtfsu5ck6s83h346nsgf6xu26dwzce9yvd8ysd2seya", 150_000_000, nil)) + built, err := apollob.Complete() + if err != nil { + t.Error(err) + } + if len(built.GetTx().TransactionBody.Outputs) == 2 { + t.Error("Tx is not correct") + } +} diff --git a/coverage.html b/coverage.html index dea55a6..30fe649 100644 --- a/coverage.html +++ b/coverage.html @@ -55,13 +55,13 @@