Skip to content

Commit

Permalink
Merge pull request #9 from mdehoog/refcell/client_block_tests
Browse files Browse the repository at this point in the history
Feat: Client Blocks Test Happy Path
  • Loading branch information
refcell authored Jan 31, 2023
2 parents 19d072a + b93347d commit 073bd09
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 1 deletion.
94 changes: 94 additions & 0 deletions configs/optimism/goerli-erc20.ros
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
request_funds(1){
find_account{
currency = {"symbol":"ETH", "decimals":18};
random_account = find_balance({
"minimum_balance":{
"value": "0",
"currency": {{currency}}
},
"create_limit":1
});
},
// Create a separate scenario to request funds so that
// the address we are using to request funds does not
// get rolled back if funds do not yet exist.
request{
loaded_account = find_balance({
"account_identifier": {{random_account.account_identifier}},
"minimum_balance":{
"value": "10000000000000",
"currency": {{currency}}
}
});
}
}
transfer(1){
transfer{
transfer.network = {"network":"Goerli", "blockchain":"Optimism"};
currency = {"symbol":"ETH", "decimals":18};
sender = {"address":"0x791Ab321d86Ca11feD3AfDff3e1b6bAD093d1220"};
// Set the recipient_amount as some value <= sender.balance-max_fee
max_fee = "84000000000000";
recipient_amount = random_number({"minimum": "100000000000000", "maximum": "1000000000000000"});
print_message({"recipient_amount":{{recipient_amount}}});
// Find recipient and construct operations
sender_amount = 0 - {{recipient_amount}};
recipient = {"address":"0x622Fbe99b3A378FAC736bf29d7e23B85E18816eB"};
transfer.confirmation_depth = "1";
transfer.operations = [
{
"operation_identifier":{"index":0},
"type":"CALL",
"account":{{sender}},
"amount":{
"value":{{sender_amount}},
"currency":{{currency}}
}
},
{
"operation_identifier":{"index":1},
"type":"CALL",
"account":{{recipient}},
"amount":{
"value":{{recipient_amount}},
"currency":{{currency}}
}
}
];
}
}
erc20_transfer(1){
transfer{
transfer.network = {"network":"Goerli", "blockchain":"Optimism"};
currency = {"symbol":"USDC", "decimals":6,"metadata": {"token_address": "0x62F3712A8A2bF3482F9Aa42F2C8296CF50774DDD"}};
sender = {"address":"0x4cdBd835fE18BD93ccA39A262Cff72dbAC99E24F"};
// Set the recipient_amount as some value <= sender.balance-max_fee
max_fee = "84000000000000";
recipient_amount = "888";
print_message({"recipient_amount":{{recipient_amount}}});
// Find recipient and construct operations
sender_amount = 0 - {{recipient_amount}};
recipient = {"address":"0x622Fbe99b3A378FAC736bf29d7e23B85E18816eB"};
transfer.confirmation_depth = "1";
transfer.operations = [
{
"operation_identifier":{"index":0},
"type":"PAYMENT",
"account":{{sender}},
"amount":{
"value":{{sender_amount}},
"currency":{{currency}}
}
},
{
"operation_identifier":{"index":1},
"type":"PAYMENT",
"account":{{recipient}},
"amount":{
"value":{{recipient_amount}},
"currency":{{currency}}
}
}
];
}
}
83 changes: 82 additions & 1 deletion pkg/client/client_blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,88 @@ func TestClientBlocks(t *testing.T) {
suite.Run(t, new(ClientBlocksTestSuite))
}

// TestGetBlockReceipts tests fetching block receipts from the op client.
func (testSuite *ClientBlocksTestSuite) TestGetBlockReceipts() {
// Construct arguments
ctx := context.Background()
hash := EthCommon.HexToHash("0xb358c6958b1cab722752939cbb92e3fec6b6023de360305910ce80c56c3dad9d")
gasPrice := big.NewInt(10000)
blockNumber := big.NewInt(1)
blockNumberString := blockNumber.String()
to := EthCommon.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87")
myTx := EthTypes.NewTransaction(
0,
to,
big.NewInt(0),
0,
gasPrice,
nil,
)
txs := []SdkClient.RPCTransaction{
{
Tx: myTx,
TxExtraInfo: SdkClient.TxExtraInfo{
BlockNumber: &blockNumberString,
BlockHash: &hash,
From: &to,
TxHash: &hash,
},
},
}
baseFee := big.NewInt(10000)

// Mock the internall call to the mock client
ethReceipt := EthTypes.Receipt{
// Consensus fields: These fields are defined by the Yellow Paper
Type: 0,
PostState: []byte{0x00},
Status: 1,
CumulativeGasUsed: 0,
Bloom: EthTypes.BytesToBloom([]byte{0x00}),
Logs: []*EthTypes.Log{},
// Implementation fields: These fields are added by geth when processing a transaction.
// They are stored in the chain database.
TxHash: hash,
ContractAddress: to,
GasUsed: 0,
// transaction corresponding to this receipt.
BlockHash: hash,
BlockNumber: blockNumber,
TransactionIndex: 0,
// OVM legacy: extend receipts with their L1 price (if a rollup tx)
// IGNORED
}
testSuite.internalClient.On("BatchCallContext", ctx, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
arg := args.Get(1).([]EthRpc.BatchElem)
receipt := arg[0].Result.(**EthTypes.Receipt)
*receipt = &ethReceipt
// arg[0].Result = ethReceipt
arg[0].Error = nil
})

// Perform internal calculations
gasPrice, _ = SdkClient.EffectiveGasPrice(myTx, baseFee)
gasUsed := new(big.Int).SetUint64(ethReceipt.GasUsed)
feeAmount := new(big.Int).Mul(gasUsed, gasPrice)
if ethReceipt.L1Fee != nil {
feeAmount.Add(feeAmount, ethReceipt.L1Fee)
}
receiptJSON, _ := ethReceipt.MarshalJSON()
receipt := &SdkClient.RosettaTxReceipt{
Type: ethReceipt.Type,
GasPrice: gasPrice,
GasUsed: gasUsed,
Logs: ethReceipt.Logs,
RawMessage: receiptJSON,
TransactionFee: feeAmount,
}

// Execute and validate the call
txReceipts, err := testSuite.client.GetBlockReceipts(ctx, hash, txs, baseFee)
testSuite.NoError(err)
testSuite.Equal([]*SdkClient.RosettaTxReceipt{receipt}, txReceipts)
}

// TestGetBlockReceiptsBatchCallErrors tests fetching block receipts from the op client with failing batch calls.
func (testSuite *ClientBlocksTestSuite) TestGetBlockReceiptsBatchCallErrors() {
// Construct arguments
Expand Down Expand Up @@ -97,7 +179,6 @@ func (testSuite *ClientBlocksTestSuite) TestGetBlockReceiptsBatchCallErrors() {
// Execute and validate the call
_, err := testSuite.client.GetBlockReceipts(ctx, hash, txs, baseFee)
testSuite.Equal(fmt.Errorf("error"), err)
// testSuite.Equal([]*SdkClient.RosettaTxReceipt{}, txReceipts)
}

// TestGetBlockReceiptsEmptyTxs tests fetching block receipts from the op client with no transactions.
Expand Down

0 comments on commit 073bd09

Please sign in to comment.