Skip to content
Closed
39 changes: 20 additions & 19 deletions cmd/metaclientd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,45 @@ import (
"github.com/rs/zerolog/log"
)

// Post Txin to Metachain, with signature of the signer.
// MetaChain takes this as a vote to the PostTxIn.
func (b *MetachainBridge) PostTxIn(fromAddress string, toAddress string, sourceAsset string, sourceAmount uint64, mBurnt uint64, destAsset string, txHash string, blockHeight uint64) (string, error) {
func (b *MetachainBridge) PostSend(sender string, senderChain string, receiver string, receiverChain string, mBurnt string, mMint string, message string, inTxHash string, inBlockHeight uint64) (string, error) {
signerAddress := b.keys.GetSignerInfo().GetAddress().String()
msg := types.NewMsgCreateTxinVoter(
signerAddress,
txHash, sourceAsset, sourceAmount, mBurnt, destAsset,
fromAddress, toAddress, blockHeight,
)
msg := types.NewMsgSendVoter(signerAddress, sender, senderChain, receiver, receiverChain, mBurnt, mMint, message, inTxHash, inBlockHeight)

metaTxHash, err := b.Broadcast(msg)
if err != nil {
log.Err(err).Msg("PostTxIn broadcast fail")
log.Err(err).Msg("PostSend broadcast fail")
return "", err
}
return metaTxHash, nil
}

// Post Txin to Metachain, with signature of the signer.
// MetaChain takes this as a vote to the PostTxIn.
func (b *MetachainBridge) PostTxoutConfirmation(txoutId uint64, txHash string, mMint uint64, destinationAsset string, destinationAmount uint64, toAddress string, blockHeight uint64) (string, error) {
func (b *MetachainBridge) PostReceiveConfirmation(sendHash string, outTxHash string, outBlockHeight uint64, mMint string) (string, error) {
signerAddress := b.keys.GetSignerInfo().GetAddress().String()
msg := types.NewMsgTxoutConfirmationVoter(signerAddress, txoutId, txHash, mMint, destinationAsset, destinationAmount, toAddress, blockHeight)
msg := types.NewMsgReceiveConfirmation(signerAddress, sendHash, outTxHash, outBlockHeight, mMint)
metaTxHash, err := b.Broadcast(msg)
if err != nil {
log.Err(err).Msg("PostTxoutConfirmation broadcast fail")
log.Err(err).Msg("PostReceiveConfirmation broadcast fail")
return "", err
}
return metaTxHash, nil
}

// Get all current Txout from MetaCore
func (b *MetachainBridge) GetAllTxout() ([]*types.Txout, error){
func (b *MetachainBridge) GetAllSend() ([]*types.Send, error) {
client := types.NewQueryClient(b.grpcConn)
resp, err := client.TxoutAll(context.Background(), &types.QueryAllTxoutRequest{})
resp, err := client.SendAll(context.Background(), &types.QueryAllSendRequest{})
if err != nil {
log.Error().Err(err).Msg("query TxoutAll error")
log.Error().Err(err).Msg("query SendAll error")
return nil, err
}
return resp.Txout, nil
return resp.Send, nil
}

func (b *MetachainBridge) GetAllReceive() ([]*types.Receive, error) {
client := types.NewQueryClient(b.grpcConn)
resp, err := client.ReceiveAll(context.Background(), &types.QueryAllReceiveRequest{})
if err != nil {
log.Error().Err(err).Msg("query SendAll error")
return nil, err
}
return resp.Receive, nil
}
36 changes: 19 additions & 17 deletions cmd/metaclientd/voter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,45 +74,47 @@ func (s *VoterSuite) SetUpTest(c *C) {
}
}

func (s *VoterSuite) TestObservedTxIn(c *C) {
func (s *VoterSuite) TestSendVoter(c *C) {
b1 := s.bridge1
b2 := s.bridge2
//err := b.PostTxIn("ETH.ETH", 2, 4, "ETH.BSC", "0xdeadbeef", "0x1234", 2345)
metaHash, err := b1.PostTxIn("0xfrom", "0xto", "0xsource.ETH", 123456, 23245, "0xdest.BSC",
metaHash, err := b1.PostSend("0xfrom", "Ethereum", "0xto", "BSC", "123456", "23245", "little message",
"0xtxhash", 123123)

c.Assert(err, IsNil)
log.Info().Msgf("PostTxIn metaHash %s", metaHash)
log.Info().Msgf("PostSend metaHash %s", metaHash)

// wait for the next block
timer1 := time.NewTimer(2 * time.Second)
<-timer1.C

metaHash, err = b2.PostTxIn("0xfrom", "0xto", "0xsource.ETH", 123456, 23245, "0xdest.BSC",
metaHash, err = b2.PostSend("0xfrom", "Ethereum", "0xto", "BSC", "123456", "23245", "little message",
"0xtxhash", 123123)
c.Assert(err, IsNil)
log.Info().Msgf("Second PostTxIn metaHash %s", metaHash)
log.Info().Msgf("Second PostSend metaHash %s", metaHash)

// wait for the next block
timer2 := time.NewTimer(2 * time.Second)
<-timer2.C

txouts, err := b1.GetAllTxout()
sends, err := b1.GetAllSend()
c.Assert(err, IsNil)
log.Info().Msgf("sends: %v", sends)
c.Assert(len(sends) >= 1, Equals, true)

send := sends[0]

metaHash, err = b1.PostReceiveConfirmation(send.Index, "0xoutHash", 2123, "23245")
c.Assert(err, IsNil)
log.Info().Msgf("txouts: %v", txouts)
c.Assert(len(txouts) >=1, Equals, true)

txout := txouts[0]
tid := txout.Id
metaHash, err = b1.PostTxoutConfirmation(tid, "0xhashtxout", 1337, "0xnicetoken", 1773, "0xmywallet", 12345)
timer3 := time.NewTimer(2 * time.Second)
<-timer3.C
metaHash, err = b2.PostTxoutConfirmation(tid, "0xhashtxout", 1337, "0xnicetoken", 1773, "0xmywallet", 12345)

timer4 := time.NewTimer(2 * time.Second)
<-timer4.C
metaHash, err = b2.PostReceiveConfirmation(send.Index, "0xoutHash", 2123, "23245")
c.Assert(err, IsNil)

txouts, err = b1.GetAllTxout()
receives, err := b2.GetAllReceive()
c.Assert(err, IsNil)
log.Info().Msgf("txouts: %v", txouts)
log.Info().Msgf("receives: %v", receives)
c.Assert(len(receives), Equals, 1)

}
Loading