Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests/defitx #89

Merged
merged 8 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions defichain/transactions/defitx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from .modules.governance import *
from .modules.loans import *
from .modules.masternode import *
from .modules.oracles import *
from .modules.pool import *
from .modules.token import *
from .modules.vault import *

8 changes: 0 additions & 8 deletions defichain/transactions/defitx/modules/governance.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
from ..builddefitx import BuildDefiTx


class SetGovernance(BaseDefiTx):
pass


class SetGovernanceHeight(BaseDefiTx):
pass


class CreateCfp(BaseDefiTx):
pass

Expand Down
24 changes: 0 additions & 24 deletions defichain/transactions/defitx/modules/loans.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,6 @@
from .basedefitx import BaseDefiTx


class SetLoanScheme(BaseDefiTx):
pass


class DestroyLoanScheme(BaseDefiTx):
pass


class SetDefaultLoanScheme(BaseDefiTx):
pass


class SetCollateralToken(BaseDefiTx):
pass


class SetLoanToken(BaseDefiTx):
pass


class UpdateLoanToken(BaseDefiTx):
pass


class TakeLoan(BaseDefiTx):
"""
Builds the defi transaction: Take Loans
Expand Down
17 changes: 0 additions & 17 deletions defichain/transactions/defitx/modules/oracles.py

This file was deleted.

14 changes: 3 additions & 11 deletions defichain/transactions/defitx/modules/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def deserialize(network: Any, hex: str) -> "PoolSwap":

return PoolSwap(addressFrom.get_address(), tokenFrom, amountFrom, addressTo.get_address(), tokenTo, maxPrice)

def __init__(self, addressFrom: str, tokenFrom: int, amountFrom: int, addressTo: str, tokenTo: int,
def __init__(self, addressFrom: str, tokenFrom: "int | str", amountFrom: int, addressTo: str, tokenTo: "int | str",
maxPrice: int):

self._addressFrom, self._tokenFrom, self._amountFrom, self._addressTo, self._tokenTo, self._maxPrice = None, \
Expand Down Expand Up @@ -206,8 +206,8 @@ def deserialize(network: Any, hex: str) -> "CompositeSwap":
return CompositeSwap(poolSwap.get_addressFrom(), poolSwap.get_tokenFrom(), poolSwap.get_amountFrom(),
poolSwap.get_addressTo(), poolSwap.get_tokenTo(), poolSwap.get_maxPrice(), pools)

def __init__(self, addressFrom: str, tokenFrom: int, amountFrom: int, addressTo: str, tokenTo: int, maxPrice: int,
pools: [str]):
def __init__(self, addressFrom: str, tokenFrom: "int | str", amountFrom: int, addressTo: str,
tokenTo: "int | str", maxPrice: int, pools: [str]):
super().__init__(addressFrom, tokenFrom, amountFrom, addressTo, tokenTo, maxPrice)
self._pools = None
self.set_pools(pools)
Expand Down Expand Up @@ -436,11 +436,3 @@ def set_addressFrom(self, addressFrom: str):

def set_amount(self, amount: str):
self._amount = amount


class CreatePoolPair(BaseDefiTx):
pass


class UpdatePoolPair(BaseDefiTx):
pass
17 changes: 0 additions & 17 deletions defichain/transactions/defitx/modules/token.py

This file was deleted.

158 changes: 158 additions & 0 deletions tests/transactions/defitx/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import copy
from defichain.networks import DefichainMainnet
from defichain import Wallet
from defichain.transactions.utils import BuildAddressAmounts


class Keys:
mnemonic = "zone myth seat market badge hamster puppy worry mind attend heavy churn upgrade heart scan address " \
"across mix spread add boss armed desert mixed"
wallet: Wallet = Wallet(DefichainMainnet)
wallet.from_mnemonic(mnemonic)
privateKey: str = wallet.private_key()
wif: str = wallet.wif()


class Addresses:
P2PKH: str = Keys.wallet.p2pkh_address()
P2SH: str = Keys.wallet.p2sh_address()
P2WPKH: str = Keys.wallet.p2wpkh_address()


class TestAccounts:
value: int = 1000
tokenId: int = 0
mintingOutputsStart: int = 2

addressAmounts = BuildAddressAmounts()
addressAmounts.add(Addresses.P2PKH, 0, 1)
addressAmounts.add(Addresses.P2PKH, "BTC", 1)
addressAmounts.add(Addresses.P2PKH, "TSLA", 1)
addressAmounts = addressAmounts.build()

utxo_to_account_p2pkh_serialized: str = "6a2d4466547855011976a914ad56321e69b7e2d30aeca9f49979ffc53084296f88ac0100" \
"000000e803000000000000"
utxo_to_account_p2sh_serialized: str = "6a2b44665478550117a91493a457d0e4cc789beb65eb77742d35297652dafe87010000000" \
"0e803000000000000"
utxo_to_account_p2wpkh_serialized: str = "6a2a446654785501160014ad56321e69b7e2d30aeca9f49979ffc53084296f010000000" \
"0e803000000000000"

account_to_utxo_p2pkh_serialized: str = "6a2d44665478621976a914ad56321e69b7e2d30aeca9f49979ffc53084296f88ac010000" \
"0000e80300000000000002"
account_to_utxo_p2sh_serialized: str = "6a2b446654786217a91493a457d0e4cc789beb65eb77742d35297652dafe870100000000e" \
"80300000000000002"
account_to_utxo_p2wpkh_serialized: str = "6a2a4466547862160014ad56321e69b7e2d30aeca9f49979ffc53084296f0100000000e" \
"80300000000000002"

account_to_account_p2pkh_serialized: str = "6a4c5f44665478421976a914ad56321e69b7e2d30aeca9f49979ffc53084296f88ac0" \
"11976a914ad56321e69b7e2d30aeca9f49979ffc53084296f88ac0300000000010000" \
"0000000000020000000100000000000000710000000100000000000000"
account_to_account_p2sh_serialized: str = "6a4c5d446654784217a91493a457d0e4cc789beb65eb77742d35297652dafe87011976" \
"a914ad56321e69b7e2d30aeca9f49979ffc53084296f88ac0300000000010000000000" \
"0000020000000100000000000000710000000100000000000000"
account_to_account_p2wpkh_serialized: str = "6a4c5c4466547842160014ad56321e69b7e2d30aeca9f49979ffc53084296f011976" \
"a914ad56321e69b7e2d30aeca9f49979ffc53084296f88ac03000000000100000000" \
"000000020000000100000000000000710000000100000000000000"


class TestGovernance:
proposalId: str = "1f4039ab714cf73ff1d92b58608ed20a73a799cde88c103b679bdafd83eb472b"
masternodeId: str = "d590154d5b106d910cfc07850830b2ddd6f12094d89c2ed69a6e1a836ec7a0fd"
decision: () = ("yes", "No", "neutral")

vote_yes_serialized: str = "6a46446654784f2b47eb83fdda9b673b108ce8cd99a7730ad28e60582bd9f13ff74c71ab39401ffda0c76" \
"e831a6e9ad62e9cd89420f1d6ddb230088507fc0c916d105b4d1590d501"
vote_no_serialized: str = "6a46446654784f2b47eb83fdda9b673b108ce8cd99a7730ad28e60582bd9f13ff74c71ab39401ffda0c76e" \
"831a6e9ad62e9cd89420f1d6ddb230088507fc0c916d105b4d1590d502"
vote_neutral_serialized: str = "6a46446654784f2b47eb83fdda9b673b108ce8cd99a7730ad28e60582bd9f13ff74c71ab39401ffda" \
"0c76e831a6e9ad62e9cd89420f1d6ddb230088507fc0c916d105b4d1590d503"


class TestLoans:
vaultId: str = "ce39e5134cc16f4d2afc73c93098c88289e7f92d4d380909ac85c7a02d783200"
amounts: [] = ["1@66", "1@TSLA"]

takeloan_p2pkh_serialized: str = "6a4c5844665478580032782da0c785ac0909384d2df9e78982c89830c973fc2a4d6fc14c13e539c" \
"e1976a914ad56321e69b7e2d30aeca9f49979ffc53084296f88ac02420000000100000000000000" \
"710000000100000000000000"
takeloan_p2sh_serialized: str = "6a4c5644665478580032782da0c785ac0909384d2df9e78982c89830c973fc2a4d6fc14c13e539ce" \
"17a91493a457d0e4cc789beb65eb77742d35297652dafe8702420000000100000000000000710000" \
"000100000000000000"
takeloan_p2wpkh_serialized: str = "6a4c5544665478580032782da0c785ac0909384d2df9e78982c89830c973fc2a4d6fc14c13e539" \
"ce160014ad56321e69b7e2d30aeca9f49979ffc53084296f024200000001000000000000007100" \
"00000100000000000000"

paybackloan_p2pkh_serialized: str = "6a4c5844665478480032782da0c785ac0909384d2df9e78982c89830c973fc2a4d6fc14c13e5" \
"39ce1976a914ad56321e69b7e2d30aeca9f49979ffc53084296f88ac02420000000100000000" \
"000000710000000100000000000000"
paybackloan_p2sh_serialized: str = "6a4c5644665478480032782da0c785ac0909384d2df9e78982c89830c973fc2a4d6fc14c13e53" \
"9ce17a91493a457d0e4cc789beb65eb77742d35297652dafe8702420000000100000000000000" \
"710000000100000000000000"
paybackloan_p2wpkh_serialized: str = "6a4c5544665478480032782da0c785ac0909384d2df9e78982c89830c973fc2a4d6fc14c13e" \
"539ce160014ad56321e69b7e2d30aeca9f49979ffc53084296f024200000001000000000000" \
"00710000000100000000000000"


class TestMasternode:
mn_id: str = "995915f151be03337b5d0021d1cf0a5f8589f2d318db375f76625f550b135596"
operatorAddress: str = "8WZsy6xubp4sn3CiMBQCz6CzBN3JdtpVNj"
ownerAddress: str = "8VzWANbjh6GUZ48xorXuLg1ygh81w8ZSD9"
rewardAddress: str = "df1qv2ld40h77h6jyc8v8z89hlgz82th08vfdvece3"
timeLock: () = (0, 5, 10)

create_massternode_timelock_0_serialized: str = "6a1a446654784301a9d692734b0130b71f39d1705aca21868b4950e4"
create_massternode_timelock_5_serialized: str = "6a1c446654784301a9d692734b0130b71f39d1705aca21868b4950e40401"
create_massternode_timelock_10_serialized: str = "6a1c446654784301a9d692734b0130b71f39d1705aca21868b4950e40802"

resign_masternode_serialized: str = "6a2544665478529655130b555f62765f37db18d3f289855f0acfd121005d7b3303be51f1155999"

update_masternode_serialized: str = "6a4c6b446654786d9655130b555f62765f37db18d3f289855f0acfd121005d7b3303be51f115" \
"599903010114a386a4920ac131a0f506dea101d9f4a699f0da49020114a9d692734b0130b71f" \
"39d1705aca21868b4950e403041462bedabefef5f52260ec388e5bfd023a97779d89"


class TestPool:
addressFrom: str = Addresses.P2WPKH
tokenFrom: str = "BTC"
amountFrom: int = 1000
addressTo: str = Addresses.P2PKH
tokenTo: str = 0
tokenTo2: str = 66
maxPrice: int = 999999999
pools: [] = ["BTC-DFI", "DFI-DUSD", "DUSD-MSTR"]

poolswap_serialized: str = "6a4c504466547873160014ad56321e69b7e2d30aeca9f49979ffc53084296f02e8030000000000001976a" \
"914ad56321e69b7e2d30aeca9f49979ffc53084296f88ac000900000000000000ffe0f50500000000"
compositeswap_serialized: str = "6a4c544466547869160014ad56321e69b7e2d30aeca9f49979ffc53084296f02e803000000000000" \
"1976a914ad56321e69b7e2d30aeca9f49979ffc53084296f88ac420900000000000000ffe0f50500" \
"00000003051147"

addressAmount: {} = {Addresses.P2WPKH: ["100000000@DFI", "1070@BTC"]}

addpoolliquidity_serialized: str = "6a4c4d446654786c01160014ad56321e69b7e2d30aeca9f49979ffc53084296f020000000000e" \
"1f50500000000020000002e04000000000000160014ad56321e69b7e2d30aeca9f49979ffc530" \
"84296f"

removeAmount = "1@BTC-DFI"

removepoolliquidity_serialized: str = "6a254466547872160014ad56321e69b7e2d30aeca9f49979ffc53084296f05010000000000" \
"0000"


class TestVaults:
ownerAddress: str = Addresses.P2WPKH
schemeId: str = "MIN150"

createvault_serialized: str = "6a234466547856160014ad56321e69b7e2d30aeca9f49979ffc53084296f064d494e313530"

vaultId: str = "5cbe99407674a689fa9b8a522462b7a4b3e7893f61453ce3fa77f1307f7d0600"
addressFrom: str = Addresses.P2WPKH
amount: str = "1@DUSD"

deposittovault_serialized: str = "6a45446654785300067d7f30f177fae33c45613f89e7b3a4b76224528a9bfa89a674764099be5c1" \
"60014ad56321e69b7e2d30aeca9f49979ffc53084296f0f0100000000000000"

addressTo: str = Addresses.P2WPKH

withdrawfromvault_serialized: str = "6a45446654784a00067d7f30f177fae33c45613f89e7b3a4b76224528a9bfa89a674764099be" \
"5c160014ad56321e69b7e2d30aeca9f49979ffc53084296f0f0100000000000000"
70 changes: 70 additions & 0 deletions tests/transactions/defitx/test_accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import pytest

from defichain.networks import DefichainMainnet
from defichain.transactions.defitx import DefiTx, UtxosToAccount, AccountToUtxos, AccountToAccount, AnyAccountToAccount, \
SetFutureSwap

from . import Addresses, TestAccounts


@pytest.mark.transactions
def test_utxo_to_account(): # 01
utxo_to_account_p2pkh: UtxosToAccount = UtxosToAccount(address=Addresses.P2PKH, amount=TestAccounts.value,
tokenId=TestAccounts.tokenId)
utxo_to_account_p2sh: UtxosToAccount = UtxosToAccount(address=Addresses.P2SH, amount=TestAccounts.value,
tokenId=TestAccounts.tokenId)
utxo_to_account_p2wpkh: UtxosToAccount = UtxosToAccount(address=Addresses.P2WPKH, amount=TestAccounts.value,
tokenId=TestAccounts.tokenId)

assert utxo_to_account_p2pkh.serialize() == TestAccounts.utxo_to_account_p2pkh_serialized
assert utxo_to_account_p2sh.serialize() == TestAccounts.utxo_to_account_p2sh_serialized
assert utxo_to_account_p2wpkh.serialize() == TestAccounts.utxo_to_account_p2wpkh_serialized

assert DefiTx.deserialize(DefichainMainnet, TestAccounts.utxo_to_account_p2pkh_serialized).serialize() == \
TestAccounts.utxo_to_account_p2pkh_serialized
assert DefiTx.deserialize(DefichainMainnet, TestAccounts.utxo_to_account_p2sh_serialized).serialize() == \
TestAccounts.utxo_to_account_p2sh_serialized
assert DefiTx.deserialize(DefichainMainnet, TestAccounts.utxo_to_account_p2wpkh_serialized).serialize() == \
TestAccounts.utxo_to_account_p2wpkh_serialized


@pytest.mark.transactions
def test_account_to_utxo(): # 02
account_to_utxo_p2pkh: AccountToUtxos = AccountToUtxos(addressFrom=Addresses.P2PKH, value=TestAccounts.value,
mintingOutputsStart=TestAccounts.mintingOutputsStart)
account_to_utxo_p2sh: AccountToUtxos = AccountToUtxos(addressFrom=Addresses.P2SH, value=TestAccounts.value,
mintingOutputsStart=TestAccounts.mintingOutputsStart)
account_to_utxo_p2wpkh: AccountToUtxos = AccountToUtxos(addressFrom=Addresses.P2WPKH, value=TestAccounts.value,
mintingOutputsStart=TestAccounts.mintingOutputsStart)

assert account_to_utxo_p2pkh.serialize() == TestAccounts.account_to_utxo_p2pkh_serialized
assert account_to_utxo_p2sh.serialize() == TestAccounts.account_to_utxo_p2sh_serialized
assert account_to_utxo_p2wpkh.serialize() == TestAccounts.account_to_utxo_p2wpkh_serialized

assert DefiTx.deserialize(DefichainMainnet, TestAccounts.account_to_utxo_p2pkh_serialized).serialize() == \
TestAccounts.account_to_utxo_p2pkh_serialized
assert DefiTx.deserialize(DefichainMainnet, TestAccounts.account_to_utxo_p2sh_serialized).serialize() == \
TestAccounts.account_to_utxo_p2sh_serialized
assert DefiTx.deserialize(DefichainMainnet, TestAccounts.account_to_utxo_p2wpkh_serialized).serialize() == \
TestAccounts.account_to_utxo_p2wpkh_serialized


@pytest.mark.transactions
def test_account_to_account(): # 03
account_to_account_p2pkh: AccountToAccount = AccountToAccount(addressFrom=Addresses.P2PKH,
addressAmountTo=TestAccounts.addressAmounts)
account_to_account_p2sh: AccountToAccount = AccountToAccount(addressFrom=Addresses.P2SH,
addressAmountTo=TestAccounts.addressAmounts)
account_to_account_p2wpkh: AccountToAccount = AccountToAccount(addressFrom=Addresses.P2WPKH,
addressAmountTo=TestAccounts.addressAmounts)

assert account_to_account_p2pkh.serialize() == TestAccounts.account_to_account_p2pkh_serialized
assert account_to_account_p2sh.serialize() == TestAccounts.account_to_account_p2sh_serialized
assert account_to_account_p2wpkh.serialize() == TestAccounts.account_to_account_p2wpkh_serialized

assert DefiTx.deserialize(DefichainMainnet, TestAccounts.account_to_account_p2pkh_serialized).serialize() == \
TestAccounts.account_to_account_p2pkh_serialized
assert DefiTx.deserialize(DefichainMainnet, TestAccounts.account_to_account_p2sh_serialized).serialize() == \
TestAccounts.account_to_account_p2sh_serialized
assert DefiTx.deserialize(DefichainMainnet, TestAccounts.account_to_account_p2wpkh_serialized).serialize() == \
TestAccounts.account_to_account_p2wpkh_serialized
27 changes: 27 additions & 0 deletions tests/transactions/defitx/test_governance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest

from defichain.networks import DefichainMainnet
from defichain.transactions.defitx import DefiTx, CreateCfp, CreateVoc, Vote
from . import TestGovernance


@pytest.mark.transactions
def test_vote(): # 01
vote_yes: Vote = Vote(proposalId=TestGovernance.proposalId, masternodeId=TestGovernance.masternodeId,
decision=TestGovernance.decision[0])
vote_no: Vote = Vote(proposalId=TestGovernance.proposalId, masternodeId=TestGovernance.masternodeId,
decision=TestGovernance.decision[1])
vote_neutral: Vote = Vote(proposalId=TestGovernance.proposalId, masternodeId=TestGovernance.masternodeId,
decision=TestGovernance.decision[2])

assert vote_yes.serialize() == TestGovernance.vote_yes_serialized
assert vote_no.serialize() == TestGovernance.vote_no_serialized
assert vote_neutral.serialize() == TestGovernance.vote_neutral_serialized

assert DefiTx.deserialize(DefichainMainnet, TestGovernance.vote_yes_serialized).serialize() == \
TestGovernance.vote_yes_serialized
assert DefiTx.deserialize(DefichainMainnet, TestGovernance.vote_no_serialized).serialize() == \
TestGovernance.vote_no_serialized
assert DefiTx.deserialize(DefichainMainnet, TestGovernance.vote_neutral_serialized).serialize() == \
TestGovernance.vote_neutral_serialized

Loading