sender.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package terra
  2. import (
  3. "context"
  4. "encoding/json"
  5. "io/ioutil"
  6. "time"
  7. "github.com/terra-project/terra.go/client"
  8. "github.com/terra-project/terra.go/key"
  9. "github.com/terra-project/terra.go/msg"
  10. "github.com/terra-project/terra.go/tx"
  11. "github.com/certusone/wormhole/node/pkg/vaa"
  12. )
  13. type submitVAAMsg struct {
  14. Params submitVAAParams `json:"submit_v_a_a"`
  15. }
  16. type submitVAAParams struct {
  17. VAA []byte `json:"vaa"`
  18. }
  19. // SubmitVAA prepares transaction with signed VAA and sends it to the Terra blockchain
  20. func SubmitVAA(ctx context.Context, urlLCD string, chainID string, contractAddress string, feePayer string, signed *vaa.VAA) (*client.TxResponse, error) {
  21. // Serialize VAA
  22. vaaBytes, err := signed.Marshal()
  23. if err != nil {
  24. return nil, err
  25. }
  26. // Derive Raw Private Key
  27. privKey, err := key.DerivePrivKey(feePayer, key.CreateHDPath(0, 0))
  28. if err != nil {
  29. return nil, err
  30. }
  31. // Generate StdPrivKey
  32. tmKey, err := key.StdPrivKeyGen(privKey)
  33. if err != nil {
  34. return nil, err
  35. }
  36. // Generate Address from Public Key
  37. addr := msg.AccAddress(tmKey.PubKey().Address())
  38. // Create LCDClient
  39. LCDClient := client.NewLCDClient(
  40. urlLCD,
  41. chainID,
  42. msg.NewDecCoinFromDec("uusd", msg.NewDecFromIntWithPrec(msg.NewInt(15), 2)), // 0.15uusd
  43. msg.NewDecFromIntWithPrec(msg.NewInt(15), 1), tmKey, time.Second*15,
  44. )
  45. contract, err := msg.AccAddressFromBech32(contractAddress)
  46. if err != nil {
  47. return nil, err
  48. }
  49. // Create tx
  50. contractCall, err := json.Marshal(submitVAAMsg{
  51. Params: submitVAAParams{
  52. VAA: vaaBytes,
  53. }})
  54. if err != nil {
  55. return nil, err
  56. }
  57. executeContract := msg.NewExecuteContract(addr, contract, contractCall, msg.NewCoins())
  58. transaction, err := LCDClient.CreateAndSignTx(ctx, client.CreateTxOptions{
  59. Msgs: []msg.Msg{
  60. executeContract,
  61. },
  62. Fee: tx.StdFee{
  63. Gas: msg.NewInt(0),
  64. Amount: msg.NewCoins(),
  65. },
  66. })
  67. if err != nil {
  68. return nil, err
  69. }
  70. // Broadcast
  71. return LCDClient.Broadcast(ctx, transaction)
  72. }
  73. // ReadKey reads file and returns its content as a string
  74. func ReadKey(path string) (string, error) {
  75. b, err := ioutil.ReadFile(path)
  76. if err != nil {
  77. return "", err
  78. }
  79. return string(b), nil
  80. }