Skip to content

Commit

Permalink
Fixes estimate_fee
Browse files Browse the repository at this point in the history
  • Loading branch information
madnesspie committed Jun 25, 2020
1 parent 3ca88a6 commit 8a28223
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion django_obm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__version__ = "0.3.3"
__version__ = "0.3.4"
15 changes: 14 additions & 1 deletion django_obm/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Meta:


class TransactionSerializer(serializers.ModelSerializer):
from_address = serializers.CharField(required=False)
from_address = serializers.CharField(required=False, allow_null=True)
to_address = serializers.CharField(required=True)
password = serializers.CharField(required=False)
currency = serializers.SlugRelatedField(
Expand Down Expand Up @@ -140,6 +140,8 @@ def create(self, validated_data):
# pylint: disable=abstract-method
class CurrencyEstimateFeeSerializer(serializers.Serializer):
currency = serializers.CharField(required=True)
to_address = serializers.CharField(required=False, allow_null=True)
amount = serializers.FloatField(required=False, allow_null=True)

def validate(self, attrs):
currency_name = attrs["currency"]
Expand All @@ -150,5 +152,16 @@ def validate(self, attrs):
raise serializers.ValidationError(
f"Currency '{currency.name}'' does not exist."
)
if currency.name == "ethereum":
to_address = attrs.get("to_address")
if to_address is None:
raise serializers.ValidationError(
"Field 'to_address' is required for ethereum."
)
amount = attrs.get("amount")
if amount is None:
raise serializers.ValidationError(
f"Field 'amount' is required for ethereum."
)
attrs["currency"] = currency
return attrs
29 changes: 14 additions & 15 deletions django_obm/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,19 @@ def estimate_fee(self, request):
data=request.data
)
assert serializer.is_valid(raise_exception=True)
currency = serializer.validated_data["currency"]
node = currency.default_node
estimated_fee = node.estimate_fee(
from_address=request.query_params.get("from_address"),
to_address=request.query_params.get("to_address"),
amount=request.query_params.get("amount"),
fee={
"gas": request.query_params.get("gas"),
"gas_price": request.query_params.get("gas_price"),
},
data=request.query_params.get("data"),
conf_target=request.query_params.get("conf_target", 1),
)
node.close()
data = serializer.validated_data
currency = data["currency"]
to_address = data["to_address"]
amount = data["amount"]
with currency.default_node as node:
estimated_fee = node.estimate_fee(
from_address=None,
to_address=to_address,
amount=amount,
)
return response.Response(
{"currency": currency.name, "estimated_fee": estimated_fee,}
{
"currency": currency.name,
"estimated_fee": estimated_fee,
}
)
2 changes: 1 addition & 1 deletion tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_get_estimate_fee(client, node):
ethereum_tx = {
"currency": node.connector.currency,
"to_address": str(os.environ.get("GETH_IN_WALLET_ADDRESS")),
"amount": 10,
"amount": "10",
}
response = client.post(
urls.reverse("currency-estimate-fee"),
Expand Down

0 comments on commit 8a28223

Please sign in to comment.