Skip to content

Commit

Permalink
feat: ContractCallTx, not working yet
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed May 27, 2019
1 parent 74ac97a commit bb74df6
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 9 deletions.
20 changes: 11 additions & 9 deletions aeternity/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ type AensConfig struct {

// ContractConfig configurations for contracts
type ContractConfig struct {
Gas uint64 `json:"gas" yaml:"gas" mapstructure:"gas"`
GasPrice uint64 `json:"gas_price" yaml:"gas_price" mapstructure:"gas_price"`
Deposit uint64 `json:"deposit" yaml:"deposit" mapstructure:"deposit"`
VMVersion uint64 `json:"vm_version" yaml:"vm_version" mapstructure:"vm_version"`
Gas uint64 `json:"gas" yaml:"gas" mapstructure:"gas"`
GasPrice uint64 `json:"gas_price" yaml:"gas_price" mapstructure:"gas_price"`
Deposit uint64 `json:"deposit" yaml:"deposit" mapstructure:"deposit"`
VMVersion uint64 `json:"vm_version" yaml:"vm_version" mapstructure:"vm_version"`
ABIVersion uint64 `json:"abi_version" yaml:"abi_version" mapstructure:"abi_version"`
}

// OracleConfig configurations for contracts
Expand Down Expand Up @@ -100,11 +101,12 @@ var Config = ProfileConfig{
ClaimFee: *utils.RequireBigIntFromString("100000000000000"),
UpdateFee: *utils.RequireBigIntFromString("100000000000000"),
},
Contracts: ContractConfig{ // UNUSED
Gas: 1e9,
GasPrice: 1,
Deposit: 0,
VMVersion: 0,
Contracts: ContractConfig{
Gas: 1e9,
GasPrice: 1e9,
Deposit: 0,
VMVersion: 0,
ABIVersion: 2,
},
Oracles: OracleConfig{
QueryFee: *utils.NewBigIntFromUint64(0),
Expand Down
62 changes: 62 additions & 0 deletions aeternity/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,3 +842,65 @@ func NewContractCreateTx(OwnerID string, AccountNonce uint64, Code string, VMVer
CallData: CallData,
}
}

type ContractCallTx struct {
CallerID string
AccountNonce uint64
ContractID string
Amount utils.BigInt
Gas uint64
GasPrice uint64
AbiVersion uint64
VMVersion uint64
CallData string
TxFee utils.BigInt
TxTTL uint64
}

func (tx *ContractCallTx) JSON() (string, error) {
swaggerT := models.ContractCallTx{
CallerID: models.EncodedHash(tx.CallerID),
Nonce: tx.AccountNonce,
ContractID: models.EncodedHash(tx.ContractID),
Amount: tx.Amount,
Gas: &tx.Gas,
GasPrice: &tx.GasPrice,
AbiVersion: &tx.AbiVersion,
VMVersion: &tx.VMVersion,
CallData: models.EncodedByteArray(tx.CallData),
Fee: tx.TxFee,
TTL: &tx.TxTTL,
}
output, err := swaggerT.MarshalBinary()
return string(output), err
}

func (tx *ContractCallTx) RLP() (rlpRawMsg []byte, err error) {
cID, err := buildIDTag(IDTagAccount, tx.CallerID)
if err != nil {
return
}
ctID, err := buildIDTag(IDTagContract, tx.ContractID)
if err != nil {
return
}
callDataBinary, err := Decode(tx.CallData)
if err != nil {
return
}

rlpRawMsg, err = buildRLPMessage(
ObjectTagContractCallTransaction,
rlpMessageVersion,
cID,
tx.AccountNonce,
ctID,
tx.TxFee.Int,
tx.TxTTL,
tx.Amount,
tx.Gas,
tx.GasPrice,
callDataBinary,
)
return
}
70 changes: 70 additions & 0 deletions aeternity/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,3 +804,73 @@ func TestContractCreateTx_RLP(t *testing.T) {
})
}
}

func TestContractCallTx_RLP(t *testing.T) {
type fields struct {
CallerID string
AccountNonce uint64
ContractID string
Amount utils.BigInt
Gas uint64
GasPrice uint64
AbiVersion uint64
VMVersion uint64
CallData string
TxFee utils.BigInt
TxTTL uint64
}
testCases := []struct {
name string
fields fields
wantTx string
wantErr bool
}{
{
name: "Basic contract call tx",
fields: fields{
CallerID: "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
AccountNonce: uint64(1),
ContractID: "ct_2pfWWzeRzWSdm68HXZJn61KhxdsBA46wzYgvo1swkdJZij1rKm",
Amount: *utils.NewBigIntFromUint64(10),
Gas: uint64(10),
GasPrice: uint64(10),
AbiVersion: uint64(0),
VMVersion: uint64(0),
CallData: "cb_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACDiIx1s38k5Ft5Ms6mFe/Zc9A/CVvShSYs/fnyYDBmTRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACo7j+li",
TxFee: Config.Client.Fee,
TxTTL: Config.Client.TTL,
},
wantTx: "you tell me",
wantErr: false,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
tx := &ContractCallTx{
CallerID: tt.fields.CallerID,
AccountNonce: tt.fields.AccountNonce,
ContractID: tt.fields.ContractID,
Amount: tt.fields.Amount,
Gas: tt.fields.Gas,
GasPrice: tt.fields.GasPrice,
AbiVersion: tt.fields.AbiVersion,
VMVersion: tt.fields.VMVersion,
CallData: tt.fields.CallData,
TxFee: tt.fields.TxFee,
TxTTL: tt.fields.TxTTL,
}
txJSON, _ := tx.JSON()
fmt.Println(txJSON)

gotTx, err := BaseEncodeTx(tx)
if (err != nil) != tt.wantErr {
t.Errorf("ContractCallTx.RLP() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotTx, tt.wantTx) {
gotTxRawBytes, wantTxRawBytes := getRLPSerialized(gotTx, tt.wantTx)
t.Errorf("ContractCallTx.RLP() = \n%v\n%v, want \n%v\n%v", gotTx, gotTxRawBytes, tt.wantTx, wantTxRawBytes)
}
})
}
}

0 comments on commit bb74df6

Please sign in to comment.