Skip to content

Commit

Permalink
Test script cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrug committed Oct 24, 2024
1 parent 5b71460 commit ed1544f
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions playground/test_playground.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ SELLTOKEN="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
BUYTOKEN="0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
RECEIVER="0xa0Ee7A142d267C1f36714E4a8F75612F20a79720"
AMOUNT="1000000000000000000"
PRIVATEKEY="0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6" # cast wallet new
PRIVATEKEY="0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6"
APPDATA='{"version":"1.3.0","metadata":{}}'

# Run test flow

# Calculate AppData hash
app_data_hash=$(cast keccak $APPDATA)

echo "Request price qoute for buying USDC for WETH"
quote_reponse=$( curl --fail-with-body -s -X 'POST' \
"http://$HOST/api/v1/quote" \
Expand All @@ -34,17 +38,18 @@ quote_reponse=$( curl --fail-with-body -s -X 'POST' \
"kind": "sell",
"sellAmountBeforeFee": "'$AMOUNT'"
}')
sellAmount=$(jq -r --args '.quote.sellAmount' <<< "${quote_reponse}")

buyAmount=$(jq -r --args '.quote.buyAmount' <<< "${quote_reponse}")
feeAmount=$(jq -r --args '.quote.feeAmount' <<< "${quote_reponse}")
#echo -e $sellAmount"\n"$buyAmount"\n"$feeAmount
validTo=$(($(date +%s) + 120)) # validity time: now + 2 minutes
#order="0x"$(printf '%s' "$quote_reponse" | hexdump -ve '/1 "%02X"') #"$quote_reponse #(jq -r --args '.quote' <<< "${quote_reponse}")

echo $quote_reponse

# Filter out unneeded fields
order_proposal=$(jq -r --args '.quote|=(.appData="0xa872cd1c41362821123e195e2dc6a3f19502a451e1fb2a1f861131526e98fdc7") | del(.quote.appDataHash) | .quote|=(.sellAmount="'$AMOUNT'") | .quote|=(.feeAmount="0") | .quote|=(.validTo="'$validTo'") | .quote' <<< "${quote_reponse}")
# Prepare EIP712 message
eip712_message=$(jq -r --args '
.quote|=(.appData="'$app_data_hash'") |
del(.quote.appDataHash) |
.quote|=(.sellAmount="'$AMOUNT'") |
.quote|=(.feeAmount="0") |
.quote|=(.validTo='$validTo') |
.quote' <<< "${quote_reponse}")

# Prepare EIP-712 typed struct
eip712_typed_struct='{
Expand Down Expand Up @@ -77,46 +82,34 @@ eip712_typed_struct='{
"chainId": 1,
"verifyingContract": "0x9008D19f58AAbD9eD0D60971565AA8510560ab41"
},
"message": '$order_proposal'
"message": '$eip712_message'
}'

echo .
echo $eip712_typed_struct
echo .

# Validate if json is well formatted and compact it
# Check if json is well formatted and compact it
eip712_typed_struct=$(jq -r -c <<< "${eip712_typed_struct}")

# Dump to file as there are some spaces in field values
echo $eip712_typed_struct > tmp.json

# sign quote_reponse with private key
# Sign quote_reponse with private key
signature=$(cast wallet sign --private-key $PRIVATEKEY --data --from-file tmp.json)
echo "Intent signature:" $signature

app_data=${APPDATA//\"/\\\"} # escape quotes for json field

# Update EIP712 message with additional fields required for order submit
order_proposal=$(jq -r -c --args '
.from="'$RECEIVER'" |
.appData|="'$app_data'" |
.appDataHash="'$app_data_hash'" |
.signature="'$signature'"' <<< "${eip712_message}")

echo "Submit an order"
orderUid=$( curl -v -X 'POST' \
"http://$HOST/api/v1/orders" \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"sellToken": "'$SELLTOKEN'",
"buyToken": "'$BUYTOKEN'",
"receiver": "'$RECEIVER'",
"sellAmount": "'$AMOUNT'",
"buyAmount": "'$buyAmount'",
"validTo": '$validTo',
"feeAmount": "0",
"kind": "sell",
"partiallyFillable": false,
"sellTokenBalance": "erc20",
"buyTokenBalance": "erc20",
"signingScheme": "eip712",
"signature": "'$signature'",
"from": "'$RECEIVER'",
"appData": "{\"version\":\"1.3.0\",\"metadata\":{}}",
"appDataHash": "0xa872cd1c41362821123e195e2dc6a3f19502a451e1fb2a1f861131526e98fdc7"
}')
-d "${order_proposal}")
orderUid=${orderUid:1:-1} # remove quotes
echo "Order UID: $orderUid"

Expand Down

0 comments on commit ed1544f

Please sign in to comment.