Transaction

Ethereum transaction in blockchain network.

class swap.providers.ethereum.transaction.Transaction(network: str = 'mainnet', provider: str = 'http', token: Optional[str] = None)

Ethereum Transaction.

Parameters
  • network (str) – Ethereum network, defaults to mainnet.

  • provider (str) – Ethereum network provider, defaults to http.

  • token (str) – Infura API endpoint token, defaults to 4414fea5f7454211956b1627621450b4.

Returns

Transaction – Ethereum transaction instance.

Note

Ethereum has only five networks, mainnet, ropsten, kovan, rinkeby and testnet.

fee(unit: str = 'Wei')Union[Wei, int, float]

Get Ethereum transaction fee.

Parameters

unit (str) – Ethereum unit, default to Wei.

Returns

Wei, int, float – Ethereum transaction fee.

>>> from swap.providers.ethereum.htlc import HTLC
>>> from swap.providers.ethereum.transaction import FundTransaction
>>> from swap.utils import sha256, get_current_timestamp
>>> htlc: HTLC = HTLC(network="mainnet")
>>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_address="0xd77E0d2Eef905cfB39c3C4b952Ed278d58f96E1f", sender_address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", endtime=get_current_timestamp(plus=3600))
>>> fund_transaction: FundTransaction = FundTransaction(network="mainnet")
>>> fund_transaction.build_transaction(address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", htlc=htlc, amount=100_000_000)
>>> fund_transaction.fee(unit="Wei")
1532774
hash()Optional[str]

Get Ethereum transaction hash.

Returns

str – Ethereum transaction hash.

>>> from swap.providers.ethereum.transaction import WithdrawTransaction
>>> from swap.providers.ethereum.solver import WithdrawSolver
>>> withdraw_transaction: WithdrawTransaction = WithdrawTransaction(network="mainnet")
>>> withdraw_transaction.build_transaction(transaction_hash="0xe49ff507739f8d916ae2c9fd51dd63764658ffa42a5288a49d93bc70a933edc4", secret_key="Hello Meheret!", address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", contract_address="0x67324d402ffc103d061dAfA9096ff639f0676378")
>>> withdraw_solver: WithdrawSolver = WithdrawSolver(xprivate_key="xprv9s21ZrQH143K3Y3pdbkbjreZQ9RVmqTLhRgf86uZyCJk2ou36YdUJt5frjwihGWmV1fQEDioiGZXWXUbHLy3kQf5xmhvhp8dZ2tfn6tgGUj", address=1)
>>> withdraw_transaction.sign(solver=withdraw_solver)
>>> withdraw_transaction.hash()
"0x9bbf83e56fea4cd9d23e000e8273551ba28317e4d3c311a49be919b305feb711"
json()dict

Get Ethereum transaction fee.

Returns

Wei, int, float – Ethereum transaction fee.

>>> from swap.providers.ethereum.htlc import HTLC
>>> from swap.providers.ethereum.transaction import FundTransaction
>>> from swap.utils import sha256, get_current_timestamp
>>> htlc: HTLC = HTLC(network="mainnet")
>>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_address="0xd77E0d2Eef905cfB39c3C4b952Ed278d58f96E1f", sender_address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", endtime=get_current_timestamp(plus=3600))
>>> fund_transaction: FundTransaction = FundTransaction(network="mainnet")
>>> fund_transaction.build_transaction(address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", htlc=htlc, amount=100_000_000)
>>> fund_transaction.json()
{'chainId': 1337, 'from': '0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C', 'value': 3000000000000000000, 'nonce': 0, 'gas': 22488, 'gasPrice': 20000000000, 'to': '0xeaEaC81da5E386E8Ca4De1e64d40a10E468A5b40', 'data': '0xf4fd30623a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb000000000000000000000000d77e0d2eef905cfb39c3c4b952ed278d58f96e1f00000000000000000000000069e04fe16c9a6a83076b3c2dc4b4bc21b5d9a20c0000000000000000000000000000000000000000000000000000000060ce0ab6'}
raw()Optional[str]

Get Ethereum transaction hash.

Returns

str – Ethereum transaction hash.

>>> from swap.providers.ethereum.transaction import RefundTransaction
>>> from swap.providers.ethereum.solver import RefundSolver
>>> refund_transaction: RefundTransaction = RefundTransaction(network="mainnet")
>>> refund_transaction.build_transaction(transaction_hash="0xe49ff507739f8d916ae2c9fd51dd63764658ffa42a5288a49d93bc70a933edc4", address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", contract_address="0x67324d402ffc103d061dAfA9096ff639f0676378")
>>> refund_solver: RefundSolver = RefundSolver(xprivate_key="xprv9s21ZrQH143K3Y3pdbkbjreZQ9RVmqTLhRgf86uZyCJk2ou36YdUJt5frjwihGWmV1fQEDioiGZXWXUbHLy3kQf5xmhvhp8dZ2tfn6tgGUj", address=0)
>>> refund_transaction.sign(solver=refund_solver)
>>> refund_transaction.hash()
"0x9bbf83e56fea4cd9d23e000e8273551ba28317e4d3c311a49be919b305feb711"
type()str

Get Ethereum transaction hash.

Returns

str – Ethereum transaction hash.

>>> from swap.providers.ethereum.transaction import WithdrawTransaction
>>> from swap.providers.ethereum.solver import WithdrawSolver
>>> withdraw_transaction: WithdrawTransaction = WithdrawTransaction(network="mainnet")
>>> withdraw_transaction.build_transaction(transaction_hash="0xe49ff507739f8d916ae2c9fd51dd63764658ffa42a5288a49d93bc70a933edc4", secret_key="Hello Meheret!", address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", contract_address="0x67324d402ffc103d061dAfA9096ff639f0676378")
>>> withdraw_transaction.type()
"ethereum_withdraw_unsigned"
signature()dict

Get Ethereum transaction hash.

Returns

str – Ethereum transaction hash.

>>> from swap.providers.ethereum.transaction import RefundTransaction
>>> from swap.providers.ethereum.solver import RefundSolver
>>> refund_transaction: RefundTransaction = RefundTransaction(network="mainnet")
>>> refund_transaction.build_transaction(transaction_hash="0xe49ff507739f8d916ae2c9fd51dd63764658ffa42a5288a49d93bc70a933edc4", address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", contract_address="0x67324d402ffc103d061dAfA9096ff639f0676378")
>>> refund_solver: RefundSolver = RefundSolver(xprivate_key="xprv9s21ZrQH143K3Y3pdbkbjreZQ9RVmqTLhRgf86uZyCJk2ou36YdUJt5frjwihGWmV1fQEDioiGZXWXUbHLy3kQf5xmhvhp8dZ2tfn6tgGUj", address=0)
>>> refund_transaction.sign(solver=refund_solver)
>>> refund_transaction.signature()
{'hash': '0x120241e6e89b54d90dc3a3f73d6353f83818c3d404c991d3b74691f000583396', 'rawTransaction': '0xf8f4018504a817c80083021cd094eaeac81da5e386e8ca4de1e64d40a10e468a5b408829a2241af62c0000b884f4fd30623a26da82ead15a80533a02696656b14b5dbfd84eb14790f2e1be5e9e45820eeb000000000000000000000000d77e0d2eef905cfb39c3c4b952ed278d58f96e1f00000000000000000000000069e04fe16c9a6a83076b3c2dc4b4bc21b5d9a20c0000000000000000000000000000000000000000000000000000000060ce40e8820a95a05d598fe47b96ef59b2a5b62a2793f499f1abce31938dc494b496b20969656cf4a063d515ee2a84d323a7f232eae4196e2e449a010eef52e6125b639b0b52fd2d2f', 'r': 42223337416619984402386667584480976881779168344975798352755076934920973937908, 's': 45155461792159514883067068644058913853180508583163102385805265017506142956847, 'v': 2709}
transaction_raw()str

Get Ethereum fund transaction raw.

Returns

str – Ethereum fund transaction raw.

>>> from swap.providers.ethereum.htlc import HTLC
>>> from swap.providers.ethereum.transaction import FundTransaction
>>> from swap.utils import sha256, get_current_timestamp
>>> htlc: HTLC = HTLC(network="mainnet")
>>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_address="0xd77E0d2Eef905cfB39c3C4b952Ed278d58f96E1f", sender_address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", endtime=get_current_timestamp(plus=3600))
>>> fund_transaction: FundTransaction = FundTransaction(network="mainnet")
>>> fund_transaction.build_transaction(address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", htlc=htlc, amount=100_000_000)
>>> fund_transaction.transaction_raw()
"eyJmZWUiOiAxMzg0NDgsICJ0cmFuc2FjdGlvbiI6IHsiY2hhaW5JZCI6IDEzMzcsICJmcm9tIjogIjB4NjllMDRmZTE2YzlBNkE4MzA3NkIzYzJkYzRiNEJjMjFiNWQ5QTIwQyIsICJ2YWx1ZSI6IDMwMDAwMDAwMDAwMDAwMDAwMDAsICJub25jZSI6IDEsICJnYXMiOiAxMzg0NDgsICJnYXNQcmljZSI6IDIwMDAwMDAwMDAwLCAidG8iOiAiMHhlYUVhQzgxZGE1RTM4NkU4Q2E0RGUxZTY0ZDQwYTEwRTQ2OEE1YjQwIiwgImRhdGEiOiAiMHhmNGZkMzA2MjNhMjZkYTgyZWFkMTVhODA1MzNhMDI2OTY2NTZiMTRiNWRiZmQ4NGViMTQ3OTBmMmUxYmU1ZTllNDU4MjBlZWIwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDBkNzdlMGQyZWVmOTA1Y2ZiMzljM2M0Yjk1MmVkMjc4ZDU4Zjk2ZTFmMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwNjllMDRmZTE2YzlhNmE4MzA3NmIzYzJkYzRiNGJjMjFiNWQ5YTIwYzAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwNjBjZTQwZTgifSwgInNpZ25hdHVyZSI6IG51bGwsICJuZXR3b3JrIjogInRlc3RuZXQiLCAidHlwZSI6ICJldGhlcmV1bV9mdW5kX3Vuc2lnbmVkIn0"

FundTransaction

class swap.providers.ethereum.transaction.FundTransaction(network: str = 'mainnet', provider: str = 'http', token: Optional[str] = None)

Ethereum Fund transaction.

Parameters
  • network (str) – Ethereum network, defaults to mainnet.

  • provider (str) – Ethereum network provider, defaults to http.

  • token (str) – Infura API endpoint token, defaults to 4414fea5f7454211956b1627621450b4.

Returns

FundTransaction – Ethereum fund transaction instance.

Warning

Do not forget to build transaction after initialize fund transaction.

build_transaction(address: str, htlc: swap.providers.ethereum.htlc.HTLC, amount: Union[Wei, int], unit: str = 'Wei')swap.providers.ethereum.transaction.FundTransaction

Build Ethereum fund transaction.

Parameters
  • htlc (ethereum.htlc.HTLC) – Ethereum HTLC instance.

  • address (str) – Ethereum sender address.

  • amount (Wei, int) – Ethereum amount.

  • unit (str) – Ethereum unit, default to Wei.

Returns

FundTransaction – Ethereum fund transaction instance.

>>> from swap.providers.ethereum.htlc import HTLC
>>> from swap.providers.ethereum.transaction import FundTransaction
>>> from swap.utils import sha256, get_current_timestamp
>>> htlc: HTLC = HTLC(network="mainnet")
>>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_address="0xd77E0d2Eef905cfB39c3C4b952Ed278d58f96E1f", sender_address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", endtime=get_current_timestamp(plus=3600))
>>> fund_transaction: FundTransaction = FundTransaction(network="mainnet")
>>> fund_transaction.build_transaction(address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", htlc=htlc, amount=100_000_000)
<swap.providers.ethereum.transaction.FundTransaction object at 0x0409DAF0>
sign(solver: swap.providers.ethereum.solver.FundSolver)swap.providers.ethereum.transaction.FundTransaction

Sign Ethereum fund transaction.

Parameters

solver (ethereum.solver.FundSolver) – Ethereum fund solver.

Returns

FundTransaction – Ethereum fund transaction instance.

>>> from swap.providers.ethereum.htlc import HTLC
>>> from swap.providers.ethereum.transaction import FundTransaction
>>> from swap.providers.ethereum.solver import FundSolver
>>> from swap.utils import sha256, get_current_timestamp
>>> htlc: HTLC = HTLC(network="mainnet")
>>> htlc.build_htlc(secret_hash=sha256("Hello Meheret!"), recipient_address="0xd77E0d2Eef905cfB39c3C4b952Ed278d58f96E1f", sender_address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", endtime=get_current_timestamp(plus=3600))
>>> fund_transaction: FundTransaction = FundTransaction(network="mainnet")
>>> fund_transaction.build_transaction(address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", htlc=htlc, amount=100_000_000)
>>> fund_solver: FundSolver = FundSolver(xprivate_key="xprv9s21ZrQH143K3Y3pdbkbjreZQ9RVmqTLhRgf86uZyCJk2ou36YdUJt5frjwihGWmV1fQEDioiGZXWXUbHLy3kQf5xmhvhp8dZ2tfn6tgGUj", address=0)
>>> fund_transaction.sign(solver=fund_solver)
<swap.providers.ethereum.transaction.FundTransaction object at 0x0409DAF0>

WithdrawTransaction

class swap.providers.ethereum.transaction.WithdrawTransaction(network: str = 'mainnet', provider: str = 'http', token: Optional[str] = None)

Ethereum Withdraw transaction.

Parameters
  • network (str) – Ethereum network, defaults to mainnet.

  • provider (str) – Ethereum network provider, defaults to http.

  • token (str) – Infura API endpoint token, defaults to 4414fea5f7454211956b1627621450b4.

Returns

WithdrawTransaction – Ethereum withdraw transaction instance.

Warning

Do not forget to build transaction after initialize withdraw transaction.

build_transaction(transaction_hash: str, address: str, secret_key: str, contract_address: Optional[str] = None)swap.providers.ethereum.transaction.WithdrawTransaction

Build Ethereum withdraw transaction.

Parameters
  • transaction_hash (str) – Ethereum HTLC funded transaction hash.

  • address (str) – Ethereum recipient address.

  • secret_key (str) – Secret password/passphrase.

  • contract_address (str) – Ethereum HTLC contract address, defaults to None.

Returns

WithdrawTransaction – Ethereum withdraw transaction instance.

>>> from swap.providers.ethereum.transaction import WithdrawTransaction
>>> withdraw_transaction: WithdrawTransaction = WithdrawTransaction(network="mainnet")
>>> withdraw_transaction.build_transaction(transaction_hash="0xe49ff507739f8d916ae2c9fd51dd63764658ffa42a5288a49d93bc70a933edc4", secret_key="Hello Meheret!", address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", contract_address="0x67324d402ffc103d061dAfA9096ff639f0676378")
<swap.providers.ethereum.transaction.WithdrawTransaction object at 0x0409DAF0>
sign(solver: swap.providers.ethereum.solver.WithdrawSolver)swap.providers.ethereum.transaction.WithdrawTransaction

Sign Ethereum withdraw transaction.

Parameters

solver (ethereum.solver.WithdrawSolver) – Ethereum withdraw solver.

Returns

WithdrawTransaction – Ethereum withdraw transaction instance.

>>> from swap.providers.ethereum.transaction import WithdrawTransaction
>>> from swap.providers.ethereum.solver import WithdrawSolver
>>> withdraw_transaction: WithdrawTransaction = WithdrawTransaction(network="mainnet")
>>> withdraw_transaction.build_transaction(transaction_hash="0xe49ff507739f8d916ae2c9fd51dd63764658ffa42a5288a49d93bc70a933edc4", secret_key="Hello Meheret!", address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", contract_address="0x67324d402ffc103d061dAfA9096ff639f0676378")
>>> withdraw_solver: WithdrawSolver = WithdrawSolver(xprivate_key="xprv9s21ZrQH143K3Y3pdbkbjreZQ9RVmqTLhRgf86uZyCJk2ou36YdUJt5frjwihGWmV1fQEDioiGZXWXUbHLy3kQf5xmhvhp8dZ2tfn6tgGUj", address=1)
>>> withdraw_transaction.sign(solver=withdraw_solver)
<swap.providers.ethereum.transaction.WithdrawTransaction object at 0x0409DAF0>

RefundTransaction

class swap.providers.ethereum.transaction.RefundTransaction(network: str = 'mainnet', provider: str = 'http', token: Optional[str] = None)

Ethereum Refund transaction.

Parameters
  • network (str) – Ethereum network, defaults to mainnet.

  • provider (str) – Ethereum network provider, defaults to http.

  • token (str) – Infura API endpoint token, defaults to 4414fea5f7454211956b1627621450b4.

Returns

RefundTransaction – Ethereum refund transaction instance.

Warning

Do not forget to build transaction after initialize refund transaction.

build_transaction(transaction_hash: str, address: str, contract_address: Optional[str] = None)swap.providers.ethereum.transaction.RefundTransaction

Build Ethereum refund transaction.

Parameters
  • transaction_hash (str) – Ethereum HTLC funded transaction hash.

  • address (str) – Ethereum sender address.

  • contract_address (str) – Ethereum HTLC contract address, defaults to None.

Returns

RefundTransaction – Ethereum refund transaction instance.

>>> from swap.providers.ethereum.transaction import RefundTransaction
>>> refund_transaction: RefundTransaction = RefundTransaction(network="mainnet")
>>> refund_transaction.build_transaction(transaction_hash="0xe49ff507739f8d916ae2c9fd51dd63764658ffa42a5288a49d93bc70a933edc4", address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", contract_address="0x67324d402ffc103d061dAfA9096ff639f0676378")
<swap.providers.ethereum.transaction.RefundTransaction object at 0x0409DAF0>
sign(solver: swap.providers.ethereum.solver.RefundSolver)swap.providers.ethereum.transaction.RefundTransaction

Sign Ethereum refund transaction.

Parameters

solver (ethereum.solver.RefundSolver) – Ethereum refund solver.

Returns

RefundTransaction – Ethereum refund transaction instance.

>>> from swap.providers.ethereum.transaction import RefundTransaction
>>> from swap.providers.ethereum.solver import RefundSolver
>>> refund_transaction: RefundTransaction = RefundTransaction(network="mainnet")
>>> refund_transaction.build_transaction(transaction_hash="0xe49ff507739f8d916ae2c9fd51dd63764658ffa42a5288a49d93bc70a933edc4", address="0x69e04fe16c9A6A83076B3c2dc4b4Bc21b5d9A20C", contract_address="0x67324d402ffc103d061dAfA9096ff639f0676378")
>>> refund_solver: RefundSolver = RefundSolver(xprivate_key="xprv9s21ZrQH143K3Y3pdbkbjreZQ9RVmqTLhRgf86uZyCJk2ou36YdUJt5frjwihGWmV1fQEDioiGZXWXUbHLy3kQf5xmhvhp8dZ2tfn6tgGUj", address=0)
>>> refund_transaction.sign(solver=refund_solver)
<swap.providers.ethereum.transaction.RefundTransaction object at 0x0409DAF0>