From c6e938c95ba54b72a4cc89afac482dc0287341a7 Mon Sep 17 00:00:00 2001 From: Aleksey Suleymanov Date: Wed, 11 Oct 2017 08:41:22 +0000 Subject: [PATCH] Add Operation: LimitOrderCancel LimitOrderCreate Convert TransferToVesting WithdrawVesting --- apis/database/api.go | 4 +- client/functions.go | 87 +++++++++++++++++++++++++++-------- transactions/sign_the_shit.go | 6 +-- types/operations.go | 57 ++++++++++++++++++++++- 4 files changed, 129 insertions(+), 25 deletions(-) diff --git a/apis/database/api.go b/apis/database/api.go index 11a5c72..0704867 100644 --- a/apis/database/api.go +++ b/apis/database/api.go @@ -740,7 +740,7 @@ func (api *API) GetWitnessByAccount(author string) (*Witness, error) { //get_witnesses_by_vote func (api *API) GetWitnessByVote(author string, limit uint) ([]*Witness, error) { if limit > 1000 { - return nil, errors.New("GetOrderBook: limit must not exceed 1000") + return nil, errors.New("GetWitnessByVote: limit must not exceed 1000") } raw, err := api.Raw("get_witnesses_by_vote", []interface{}{author, limit}) if err != nil { @@ -756,7 +756,7 @@ func (api *API) GetWitnessByVote(author string, limit uint) ([]*Witness, error) //lookup_witness_accounts func (api *API) LookupWitnessAccounts(author string, limit uint) ([]string, error) { if limit > 1000 { - return nil, errors.New("GetOrderBook: limit must not exceed 1000") + return nil, errors.New("LookupWitnessAccounts: limit must not exceed 1000") } raw, err := api.Raw("lookup_witness_accounts", []interface{}{author, limit}) if err != nil { diff --git a/client/functions.go b/client/functions.go index 979fcfa..e2f854c 100644 --- a/client/functions.go +++ b/client/functions.go @@ -519,39 +519,88 @@ func (api *Client) Login(user_name, key string) bool { } } -func (api *Client) FeedPublish(username, base, quote string) error { +func (api *Client) LimitOrderCancel(owner string, orderid uint32) error { - ExchR := types.ExchRate{Base: base, Quote: quote} - tx := &types.FeedPublishOperation{ - Publisher: username, - ExchangeRate: ExchR, + tx := &types.LimitOrderCancelOperation{ + Owner: owner, + OrderID: orderid, } - resp, err := api.Send_Trx(username, tx) + resp, err := api.Send_Trx(owner, tx) if err != nil { - return errors.Wrapf(err, "Error FeedPublish: ") + return errors.Wrapf(err, "Error LimitOrderCancel: ") } else { - log.Println("[FeedPublish] Block -> ", resp.BlockNum, " FeedPublish user -> ", username) + log.Println("[LimitOrderCancel] Block -> ", resp.BlockNum, " LimitOrderCancel user -> ", owner) return nil } } -func (api *Client) WitnessUpdate(username, url, signKey, acfee, fee string, blocksize uint32, sbdir uint16) error { +func (api *Client) LimitOrderCreate(owner, sell, buy string, orderid uint32) error { - chprop := types.ChainProperties{AccountCreationFee: acfee, MaximumBlockSize: blocksize, SBDInterestRate: sbdir} - tx := &types.WitnessUpdateOperation{ - Owner: username, - Url: url, - BlockSigningKey: signKey, - Props: &chprop, - Fee: fee, + expiration := time.Now().Add(3600000 * time.Second).UTC() + fok := false + + tx := &types.LimitOrderCreateOperation{ + Owner: owner, + OrderID: orderid, + AmountToSell: sell, + MinToReceive: buy, + FillOrKill: fok, + Expiration: &types.Time{&expiration}, + } + + resp, err := api.Send_Trx(owner, tx) + if err != nil { + return errors.Wrapf(err, "Error LimitOrderCreate: ") + } else { + log.Println("[LimitOrderCreate] Block -> ", resp.BlockNum, " LimitOrderCreate user -> ", owner) + return nil + } +} + +func (api *Client) Convert(owner, amount string, requestid uint32) error { + tx := &types.ConvertOperation{ + Owner: owner, + RequestID: requestid, + Amount: amount, + } + + resp, err := api.Send_Trx(owner, tx) + if err != nil { + return errors.Wrapf(err, "Error Convert: ") + } else { + log.Println("[Convert] Block -> ", resp.BlockNum, " Convert user -> ", owner) + return nil + } +} + +func (api *Client) TransferToVesting(from, to, amount string) error { + tx := &types.TransferToVestingOperation{ + From: from, + To: to, + Amount: amount, + } + + resp, err := api.Send_Trx(from, tx) + if err != nil { + return errors.Wrapf(err, "Error TransferToVesting: ") + } else { + log.Println("[TransferToVesting] Block -> ", resp.BlockNum, " TransferToVesting user -> ", from) + return nil + } +} + +func (api *Client) WithdrawVesting(account, vshares string) error { + tx := &types.WithdrawVestingOperation{ + Account: account, + VestingShares: vshares, } - resp, err := api.Send_Trx(username, tx) + resp, err := api.Send_Trx(account, tx) if err != nil { - return errors.Wrapf(err, "Error WitnessUpdate: ") + return errors.Wrapf(err, "Error WithdrawVesting: ") } else { - log.Println("[WitnessUpdate] Block -> ", resp.BlockNum, " WitnessUpdate user -> ", username) + log.Println("[WithdrawVesting] Block -> ", resp.BlockNum, " WithdrawVesting user -> ", account) return nil } } diff --git a/transactions/sign_the_shit.go b/transactions/sign_the_shit.go index 64c04ef..ce05315 100644 --- a/transactions/sign_the_shit.go +++ b/transactions/sign_the_shit.go @@ -4,7 +4,7 @@ import ( "crypto/ecdsa" "crypto/elliptic" "crypto/sha256" - _ "encoding/hex" + "encoding/hex" "errors" "fmt" //secp256k1 "github.com/btcsuite/btcd/btcec" @@ -30,7 +30,7 @@ func (tx *SignedTransaction) Sign_Single(priv_b []byte, data []byte) []byte { func signBuffer(buf []byte, private_key *ecdsa.PrivateKey) []byte { //Debug info - //log.Println("signBuffer buf=", hex.EncodeToString(buf)) + log.Println("signBuffer buf=", hex.EncodeToString(buf)) // Hash a message. alg := sha256.New() alg.Write(buf) @@ -42,7 +42,7 @@ func signBuffer(buf []byte, private_key *ecdsa.PrivateKey) []byte { func signBufferSha256(buf_sha256 []byte, private_key *ecdsa.PrivateKey) []byte { // *secp256k1.Signature //Debug info - //log.Println("signBufferSha256 buf_sha256=", hex.EncodeToString(buf_sha256)) + log.Println("signBufferSha256 buf_sha256=", hex.EncodeToString(buf_sha256)) var buf_sha256_clone = make([]byte, len(buf_sha256)) copy(buf_sha256_clone, buf_sha256) diff --git a/types/operations.go b/types/operations.go index f13e424..6b44043 100644 --- a/types/operations.go +++ b/types/operations.go @@ -44,6 +44,15 @@ func (op *ConvertOperation) Data() interface{} { return op } +func (op *ConvertOperation) MarshalTransaction(encoder *transaction.Encoder) error { + enc := transaction.NewRollingEncoder(encoder) + enc.EncodeUVarint(uint64(TypeConvert.Code())) + enc.Encode(op.Owner) + enc.Encode(op.RequestID) + enc.EncodeMoney(op.Amount) + return enc.Err() +} + // FC_REFLECT( steemit::chain::feed_publish_operation, // (publisher) // (exchange_rate) ) @@ -226,6 +235,15 @@ func (op *TransferToVestingOperation) Data() interface{} { return op } +func (op *TransferToVestingOperation) MarshalTransaction(encoder *transaction.Encoder) error { + enc := transaction.NewRollingEncoder(encoder) + enc.EncodeUVarint(uint64(TypeTransferToVesting.Code())) + enc.Encode(op.From) + enc.Encode(op.To) + enc.EncodeMoney(op.Amount) + return enc.Err() +} + // FC_REFLECT( steemit::chain::withdraw_vesting_operation, // (account) // (vesting_shares) ) @@ -243,6 +261,14 @@ func (op *WithdrawVestingOperation) Data() interface{} { return op } +func (op *WithdrawVestingOperation) MarshalTransaction(encoder *transaction.Encoder) error { + enc := transaction.NewRollingEncoder(encoder) + enc.EncodeUVarint(uint64(TypeWithdrawVesting.Code())) + enc.Encode(op.Account) + enc.EncodeMoney(op.VestingShares) + return enc.Err() +} + // FC_REFLECT( steemit::chain::set_withdraw_vesting_route_operation, // (from_account) // (to_account) @@ -406,6 +432,18 @@ func (op *LimitOrderCreateOperation) Data() interface{} { return op } +func (op *LimitOrderCreateOperation) MarshalTransaction(encoder *transaction.Encoder) error { + enc := transaction.NewRollingEncoder(encoder) + enc.EncodeUVarint(uint64(TypeLimitOrderCreate.Code())) + enc.Encode(op.Owner) + enc.Encode(op.OrderID) + enc.EncodeMoney(op.AmountToSell) + enc.EncodeMoney(op.MinToReceive) + enc.EncodeBool(op.FillOrKill) + enc.Encode(op.Expiration) + return enc.Err() +} + // FC_REFLECT( steemit::chain::limit_order_cancel_operation, // (owner) // (orderid) ) @@ -423,6 +461,14 @@ func (op *LimitOrderCancelOperation) Data() interface{} { return op } +func (op *LimitOrderCancelOperation) MarshalTransaction(encoder *transaction.Encoder) error { + enc := transaction.NewRollingEncoder(encoder) + enc.EncodeUVarint(uint64(TypeLimitOrderCancel.Code())) + enc.Encode(op.Owner) + enc.Encode(op.OrderID) + return enc.Err() +} + // FC_REFLECT( steemit::chain::delete_comment_operation, // (author) // (permlink) ) @@ -616,7 +662,7 @@ func (op *ProveAuthorityOperation) Data() interface{} { type RequestAccountRecoveryOperation struct { RecoveryAccount string `json:"recovery_account"` AccountToRecover string `json:"account_to_recover"` - NewOwnerAuthority string `json:"new_owner_authority"` + NewOwnerAuthority []interface{} `json:"new_owner_authority"` Extensions []interface{} `json:"extensions"` } @@ -657,6 +703,15 @@ func (op *ChangeRecoveryAccountOperation) Data() interface{} { return op } +func (op *ChangeRecoveryAccountOperation) MarshalTransaction(encoder *transaction.Encoder) error { + enc := transaction.NewRollingEncoder(encoder) + enc.EncodeUVarint(uint64(TypeChangeRecoveryAccount.Code())) + enc.Encode(op.AccountToRecover) + enc.Encode(op.NewRecoveryAccount) + enc.Encode(byte(0)) + return enc.Err() +} + type EscrowTransferOperation struct { From string `json:"from"` To string `json:"to"`