From b4c0d7cfabde00f65f31fd16d8ef9cf7adc4c5c0 Mon Sep 17 00:00:00 2001 From: William Law Date: Mon, 29 Apr 2024 16:16:50 -0400 Subject: [PATCH] add multiple transfers in 1 tx --- chain/transaction.go | 5 +- .../tests/integration/integration_test.go | 53 ++++++++++++++++++- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/chain/transaction.go b/chain/transaction.go index 1dc772def9..e97c55fcb9 100644 --- a/chain/transaction.go +++ b/chain/transaction.go @@ -422,8 +422,9 @@ func (t *Transaction) Execute( refund := maxFee - feeRequired if refund > 0 { ts.DisableAllocation() - defer ts.EnableAllocation() - if err := s.Refund(ctx, t.Auth.Sponsor(), ts, refund); err != nil { + err = s.Refund(ctx, t.Auth.Sponsor(), ts, refund) + ts.EnableAllocation() + if err != nil { return handleRevert(err) } } diff --git a/examples/tokenvm/tests/integration/integration_test.go b/examples/tokenvm/tests/integration/integration_test.go index 004db64b14..b930186a14 100644 --- a/examples/tokenvm/tests/integration/integration_test.go +++ b/examples/tokenvm/tests/integration/integration_test.go @@ -85,7 +85,7 @@ func init() { flag.IntVar( &vms, "vms", - 3, + 4, "number of VMs to create", ) } @@ -101,6 +101,11 @@ var ( rsender2 codec.Address sender2 string + priv3 ed25519.PrivateKey + factory3 *auth.ED25519Factory + rsender3 codec.Address + sender3 string + asset1 []byte asset1Symbol []byte asset1Decimals uint8 @@ -161,6 +166,17 @@ var _ = ginkgo.BeforeSuite(func() { zap.String("pk", hex.EncodeToString(priv2[:])), ) + priv3, err = ed25519.GeneratePrivateKey() + gomega.Ω(err).Should(gomega.BeNil()) + factory3 = auth.NewED25519Factory(priv3) + rsender3 = auth.NewED25519Address(priv3.PublicKey()) + sender3 = codec.MustAddressBech32(tconsts.HRP, rsender3) + log.Debug( + "generated key", + zap.String("addr", sender3), + zap.String("pk", hex.EncodeToString(priv3[:])), + ) + asset1 = []byte("1") asset1Symbol = []byte("s1") asset1Decimals = uint8(1) @@ -1648,6 +1664,41 @@ var _ = ginkgo.Describe("[Tx Processing]", func() { gomega.Ω(err).Should(gomega.BeNil()) gomega.Ω(orders).Should(gomega.HaveLen(0)) }) + + ginkgo.It("transfer to multiple accounts in a single tx", func() { + parser, err := instances[3].tcli.Parser(context.Background()) + gomega.Ω(err).Should(gomega.BeNil()) + submit, _, _, err := instances[3].cli.GenerateTransaction( + context.Background(), + parser, + []chain.Action{ + &actions.Transfer{ + To: rsender2, + Value: 101, + }, + &actions.Transfer{ + To: rsender3, + Value: 50, + }, + }, + factory, + ) + gomega.Ω(err).Should(gomega.BeNil()) + gomega.Ω(submit(context.Background())).Should(gomega.BeNil()) + time.Sleep(2 * time.Second) // for replay test + accept := expectBlk(instances[3]) + results := accept(false) + gomega.Ω(results).Should(gomega.HaveLen(1)) + gomega.Ω(results[0].Success).Should(gomega.BeTrue()) + + balance2, err := instances[3].tcli.Balance(context.Background(), sender2, codec.EmptyAddress) + gomega.Ω(err).To(gomega.BeNil()) + gomega.Ω(balance2).To(gomega.Equal(uint64(101))) + + balance3, err := instances[3].tcli.Balance(context.Background(), sender3, codec.EmptyAddress) + gomega.Ω(err).To(gomega.BeNil()) + gomega.Ω(balance3).To(gomega.Equal(uint64(50))) + }) }) func expectBlk(i instance) func(bool) []*chain.Result {