Skip to content

Commit

Permalink
Merge pull request #29 from yanlevi/main
Browse files Browse the repository at this point in the history
Updating to use latest requirements
  • Loading branch information
yarinvak authored May 1, 2023
2 parents 3476d81 + 99803cb commit 59bd06f
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 70 deletions.
2 changes: 0 additions & 2 deletions fireblocks_defi_sdk_py/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,3 @@ class Chain(Enum):
OPTIMISM_KOVAN = "optimistic ethereum kovan"
RONIN = "ronin"
RONIN_TEST = "ronin_test"
ARBITRUM = "arbitrum"
ARBITRUM_GOERLI = "arbitrum_goerli"
2 changes: 1 addition & 1 deletion fireblocks_defi_sdk_py/examples/basic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
w3 = Web3(Web3.EthereumTesterProvider())
print(w3.isConnected())
CHAIN = Chain.ROPSTEN
CONTRACT_ADDRESS = Web3.toChecksumAddress("0xcbe74e21b070a979b9d6426b11e876d4cb618daf")
CONTRACT_ADDRESS = Web3.to_checksum_address("0xcbe74e21b070a979b9d6426b11e876d4cb618daf")
EIP20_ABI = json.loads('[{"constant":false,"inputs":[{"name":"_greeting","type":"string"}],"name":"greet","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getGreeting","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]')
GREETING = "Hello web3"

Expand Down
2 changes: 1 addition & 1 deletion fireblocks_defi_sdk_py/examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fireblocks-defi-sdk==1.0.2
fireblocks-defi-sdk==1.1.0
eth-tester==0.8.0b3
py-evm==0.6.1a2
10 changes: 5 additions & 5 deletions fireblocks_defi_sdk_py/examples/uniswap_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def approve_withdrawal_input_token():
external_wallet_address=erc20_input_token_proxy_addr,
chain=CHAIN)
input_token_abi = json.loads(erc20_input_token_abi)
contract = w3.eth.contract(address=Web3.toChecksumAddress(erc20_input_token_addr), abi=input_token_abi)
contract = w3.eth.contract(address=Web3.to_checksum_address(erc20_input_token_addr), abi=input_token_abi)
approve_tx = contract.functions.approve(uniswap_router_addr, 999999999).buildTransaction()
tx_res = w3bridge.send_transaction(approve_tx, note="contract call approve")
print(tx_res)
Expand All @@ -48,13 +48,13 @@ def call_uniswap_action():
external_wallet_address=uniswap_router_addr,
chain=CHAIN)
uniswap_abi = json.loads(uniswap_router_abi)
contract = w3.eth.contract(address=Web3.toChecksumAddress(uniswap_router_addr), abi=uniswap_abi)
contract = w3.eth.contract(address=Web3.to_checksum_address(uniswap_router_addr), abi=uniswap_abi)
deadline = (datetime.now() + timedelta(seconds=180)).timestamp()
deadline = int(deadline)
uniswap_call_tx = contract.functions.swapTokensForExactTokens(1000000000000, 100000000000,
[Web3.toChecksumAddress(erc20_input_token_proxy_addr),
Web3.toChecksumAddress(output_token_addr)],
Web3.toChecksumAddress(addr_to_get_output_tokens),
[Web3.to_checksum_address(erc20_input_token_proxy_addr),
Web3.to_checksum_address(output_token_addr)],
Web3.to_checksum_address(addr_to_get_output_tokens),
deadline).buildTransaction()
tx_res = w3bridge.send_transaction(uniswap_call_tx, note="Uniswap contract call")
print(tx_res)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@
FILE_PATH, MODE = "some/path/to/my/contract", "r"

if __name__ == "__main__":
# We start by building a bridge to the Ropsten network, including initiating a CustomToken object which will
# We start by building a bridge to the Goerli network, including initiating a CustomToken object which will
# represent our custom contract. In order to initiate a CustomToken, we will need a contract ABI. This can be read
# from another file, passed as a variable declared locally, or fetched from EtherScan using fetch_abi(address). We
# will use the first.
ropsten_bridge = Web3Bridge(SDK, VAULT_ID, CONTRACT_ADDRESS, Chain.ROPSTEN, WHITELISTED_CONTRACT_UUID)
goerli_bridge = Web3Bridge(SDK, VAULT_ID, Chain.GOERLI, CONTRACT_ADDRESS, WHITELISTED_CONTRACT_UUID)
with open(FILE_PATH, MODE) as file:
contract_abi = file.read()
custom_contract_bridge = CustomToken(ropsten_bridge, contract_abi)
custom_contract_bridge = CustomToken(goerli_bridge, contract_abi)

# Let's assume our ABI holds a transferOwnership write function, and a contractOwner function (returning a boolean).
# We will first validate we own the contract and then transfer it to another vault in our account.
checked_address = custom_contract_bridge.web_provider.toChecksumAddress(custom_contract_bridge.wallet_address)
checked_address = custom_contract_bridge.web_provider.to_checksum_address(custom_contract_bridge.wallet_address)
if custom_contract_bridge.call_read_function("contractOwner", checked_address):
# We will initiate another bridge, as we will need the wallet address, and we will later on transfer it back.
secondary_ropsten_bridge = Web3Bridge(SDK, SECONDARY_VAULT_ID, CONTRACT_ADDRESS, Chain.ROPSTEN,
WHITELISTED_CONTRACT_UUID)
secondary_contract_bridge = CustomToken(secondary_ropsten_bridge, contract_abi)
secondary_goerli_brdige = Web3Bridge(SDK, SECONDARY_VAULT_ID, Chain.GOERLI, CONTRACT_ADDRESS,
WHITELISTED_CONTRACT_UUID)
secondary_contract_bridge = CustomToken(secondary_goerli_brdige, contract_abi)
building_params = {"from": checked_address}
secondary_address = secondary_contract_bridge.web_provider.toChecksumAddress(
secondary_address = secondary_contract_bridge.web_provider.to_checksum_address(
secondary_contract_bridge.wallet_address)
transfer_ownership_raw_transaction = custom_contract_bridge. \
call_write_function("transferOwnership",
secondary_address,
building_params=building_params)

transfer_ownership_transaction = custom_contract_bridge.submit_transaction(transfer_ownership_raw_transaction)
if ropsten_bridge.check_tx_is_completed(transfer_ownership_transaction['id']) == TRANSACTION_STATUS_COMPLETED:
if goerli_bridge.check_tx_is_completed(transfer_ownership_transaction['id']) == TRANSACTION_STATUS_COMPLETED:
print("Successfully transferred ownership from first wallet to the second one.")
# We will now transfer the ownership back to our original wallet.
building_params['from'] = secondary_address
Expand All @@ -49,7 +49,7 @@
checked_address,
building_params=building_params)
ownership_transaction = secondary_contract_bridge.submit_transaction(ownership_raw_transaction, "Returning")
if secondary_ropsten_bridge.check_tx_is_completed(ownership_transaction['id']) == \
if secondary_goerli_brdige.check_tx_is_completed(ownership_transaction['id']) == \
TRANSACTION_STATUS_COMPLETED:
print("Successfully transferred ownership from second wallet to the first one.")
else:
Expand Down
12 changes: 6 additions & 6 deletions fireblocks_defi_sdk_py/tokenization/examples/erc20_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
TOKEN_AMOUNT = 10

if __name__ == "__main__":
# We start by building a bridge to the Ropsten network, including initiating a CustomToken object which will
# We start by building a bridge to the Goerli network, including initiating a CustomToken object which will
# represent our ERC20 contract. In order to initiate a CustomToken, we will need a contract ABI. This can be read
# from another file, passed as a variable declared locally, or fetched from EtherScan using fetch_abi(address). We
# will use the latter.
ropsten_bridge = Web3Bridge(SDK, VAULT_ID, CONTRACT_ADDRESS, Chain.ROPSTEN)
goerli_bridge = Web3Bridge(SDK, VAULT_ID, Chain.GOERLI, CONTRACT_ADDRESS)
contract_abi = fetch_abi(CONTRACT_ADDRESS)
erc20_contract_bridge = CustomToken(ropsten_bridge, contract_abi)
erc20_contract_bridge = CustomToken(goerli_bridge, contract_abi)

# We build functions through passing the function name exactly as it appears on the ABI, following by its arguments.
# It's important to differentiate between a read (view) and write function. We will demonstrate both.
Expand All @@ -31,23 +31,23 @@

# We will mint a token (function receives address & amount) to our own address and then send it to another address.
# We will also have to add the sender under the building_params in the following format:
checked_address = erc20_contract_bridge.web_provider.toChecksumAddress(erc20_contract_bridge.wallet_address)
checked_address = erc20_contract_bridge.web_provider.to_checksum_address(erc20_contract_bridge.wallet_address)
# Web3 only accepts checkedSum addresses.
building_params = {"from": checked_address}
mint_raw_transaction = erc20_contract_bridge.call_write_function("mint", checked_address,
TOKEN_AMOUNT,
building_params=building_params)
# We will now submit the transaction to Fireblocks.
mint_transaction = erc20_contract_bridge.submit_transaction(mint_raw_transaction)
if ropsten_bridge.check_tx_is_completed(mint_transaction['id']) == TRANSACTION_STATUS_COMPLETED:
if goerli_bridge.check_tx_is_completed(mint_transaction['id']) == TRANSACTION_STATUS_COMPLETED:
print(f"Successfully minted {TOKEN_AMOUNT} to {erc20_contract_bridge.wallet_address}")
transfer_raw_transaction = erc20_contract_bridge.call_write_function("transferFrom",
checked_address,
RECEIVER_ADDRESS, TOKEN_AMOUNT,
building_params=building_params)
transfer_transaction = erc20_contract_bridge.submit_transaction(transfer_raw_transaction,
"Transferring 10 minted tokens.")
if ropsten_bridge.check_tx_is_completed(transfer_transaction['id']):
if goerli_bridge.check_tx_is_completed(transfer_transaction['id']):
print(
f"Successfully transferred {TOKEN_AMOUNT} from {erc20_contract_bridge.wallet_address} to "
f"{RECEIVER_ADDRESS}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
TOKEN_IDS = [1, 2]

if __name__ == "__main__":
# We start by building a bridge to the Kovan network, including initiating a ERC1155 object.
kovan_bridge = Web3Bridge(SDK, VAULT_ID, CONTRACT, Chain.KOVAN)
multi_token_contract_bridge = ERC1155(kovan_bridge)
# We start by building a bridge to the Goerli network, including initiating a ERC1155 object.
goerli_bridge = Web3Bridge(SDK, VAULT_ID, Chain.GOERLI, CONTRACT)
multi_token_contract_bridge = ERC1155(goerli_bridge)

# ERC1155 implements all the basic capabilities mentioned in ERC721:
# https://ethereum.org/en/developers/docs/standards/tokens/erc-1155/
Expand All @@ -31,7 +31,7 @@
transfer_tokens = multi_token_contract_bridge.safe_batch_transfer_from(RECEIVER_ADDRESSES, TOKEN_IDS,
tokens_balance,
note="Transfer to another vault.")
transfer_result = kovan_bridge.check_tx_is_completed(transfer_tokens['id'])
transfer_result = goerli_bridge.check_tx_is_completed(transfer_tokens['id'])

if transfer_result == TRANSACTION_STATUS_COMPLETED:
print(f"{RECEIVER_ADDRESSES} now owns {tokens_balance} of tokens: {TOKEN_IDS}")
Expand Down
8 changes: 4 additions & 4 deletions fireblocks_defi_sdk_py/tokenization/examples/nft_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
TOKEN_ID = 1

if __name__ == "__main__":
# We start by building a bridge to the Ropsten network, including initiating a ERC721 object.
ropsten_bridge = Web3Bridge(SDK, VAULT_ID, CONTRACT_ADDRESS, Chain.ROPSTEN)
nft_contract_bridge = ERC721(ropsten_bridge)
# We start by building a bridge to the Goerli network, including initiating a ERC721 object.
goerli_bridge = Web3Bridge(SDK, VAULT_ID, Chain.GOERLI, CONTRACT_ADDRESS)
nft_contract_bridge = ERC721(goerli_bridge)

# ERC721 implements all the basic capabilities mentioned in ERC721:
# https://ethereum.org/en/developers/docs/standards/tokens/erc-721/
Expand All @@ -40,7 +40,7 @@
receiver_initial_balance = nft_contract_bridge.balance_of(RECEIVER_ADDRESS)
# safe_transfer_from returns a dictionary with a status and id.
transaction = nft_contract_bridge.safe_transfer_from(RECEIVER_ADDRESS, TOKEN_ID)
transaction_result = ropsten_bridge.check_tx_is_completed(transaction['id'])
transaction_result = goerli_bridge.check_tx_is_completed(transaction['id'])

if transaction_result == TRANSACTION_STATUS_COMPLETED:
# After the transaction has been completed we check whether the balance of the receiver has been updated
Expand Down
6 changes: 3 additions & 3 deletions fireblocks_defi_sdk_py/tokenization/tokens/base_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, web3_bridge: Web3Bridge):
self.web_provider = web3_bridge.web_provider
self.abi = None
self.contract: contract = self.web_provider.eth.contract(
address=self.web_provider.toChecksumAddress(self.web3_bridge.external_wallet_address)
address=self.web_provider.to_checksum_address(self.web3_bridge.external_wallet_address)
)

def __str__(self):
Expand All @@ -42,11 +42,11 @@ def call_write_function(self, abi_function: str, *args, building_params: dict =
"""
if not building_params:
building_params = {}
return self.contract.get_function_by_name(abi_function)(*args).buildTransaction(building_params)
return self.contract.get_function_by_name(abi_function)(*args).build_transaction(building_params)

def submit_transaction(self, transaction: dict, note: str = "") -> dict:
"""
Takes a ready transaction after being built (using web3 buildTransaction()) and transmits it to Fireblocks.
Takes a ready transaction after being built (using web3 build_transaction()) and transmits it to Fireblocks.
:param note:
:param transaction:
:return:
Expand Down
2 changes: 1 addition & 1 deletion fireblocks_defi_sdk_py/tokenization/tokens/custom_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ def __init__(self, web3_bridge: Web3Bridge, custom_abi: str):
super().__init__(web3_bridge)
self.abi = custom_abi
self.contract = self.web_provider.eth.contract(
address=self.web_provider.toChecksumAddress(self.web3_bridge.external_wallet_address),
address=self.web_provider.to_checksum_address(self.web3_bridge.external_wallet_address),
abi=self.abi
)
26 changes: 13 additions & 13 deletions fireblocks_defi_sdk_py/tokenization/tokens/erc1155.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(self, web3_bridge: Web3Bridge):
super().__init__(web3_bridge)
self.abi = ERC1155_ABI
self.contract: contract = self.web_provider.eth.contract(
address=self.web_provider.toChecksumAddress(self.web3_bridge.external_wallet_address),
address=self.web_provider.to_checksum_address(self.web3_bridge.external_wallet_address),
abi=self.abi
)

Expand All @@ -22,7 +22,7 @@ def set_approval_for_all(self, operator_address: str, is_approved: bool, note: s
:param note: (Optional) Add a note to the transaction.
:return: None
"""
checked_op_adr = self.web_provider.toChecksumAddress(operator_address)
checked_op_adr = self.web_provider.to_checksum_address(operator_address)
return self.submit_transaction(self.call_write_function("setApprovalForAll", checked_op_adr, is_approved), note)

def safe_transfer_from(self, to_address: str, token_id: int, amount: int, from_address: str = "",
Expand All @@ -38,17 +38,17 @@ def safe_transfer_from(self, to_address: str, token_id: int, amount: int, from_a
"""
if not from_address:
from_address = self.wallet_address
address_dict = {"from": self.web_provider.toChecksumAddress(from_address)}
checked_from_adr = self.web_provider.toChecksumAddress(from_address)
checked_to_adr = self.web_provider.toChecksumAddress(to_address)
address_dict = {"from": self.web_provider.to_checksum_address(from_address)}
checked_from_adr = self.web_provider.to_checksum_address(from_address)
checked_to_adr = self.web_provider.to_checksum_address(to_address)

transaction = self.contract.functions.safeTransferFrom(
checked_from_adr,
checked_to_adr,
token_id,
amount,
data
).buildTransaction(address_dict)
).build_transaction(address_dict)

return self.submit_transaction(transaction, note)

Expand All @@ -71,9 +71,9 @@ def safe_batch_transfer_from(self, to_address: str, token_ids: List[int], values
"""
if not from_address:
from_address = self.wallet_address
address_dict = {"from": self.web_provider.toChecksumAddress(from_address)}
checked_from_adr = self.web_provider.toChecksumAddress(from_address)
checked_to_adr = self.web_provider.toChecksumAddress(to_address)
address_dict = {"from": self.web_provider.to_checksum_address(from_address)}
checked_from_adr = self.web_provider.to_checksum_address(from_address)
checked_to_adr = self.web_provider.to_checksum_address(to_address)
if len(token_ids) != len(values):
raise ValueError("Length of token_ids and values must match!")

Expand All @@ -83,7 +83,7 @@ def safe_batch_transfer_from(self, to_address: str, token_ids: List[int], values
token_ids,
values,
data
).buildTransaction(address_dict)
).build_transaction(address_dict)

return self.submit_transaction(transaction, note)

Expand Down Expand Up @@ -117,7 +117,7 @@ def balance_of_batch(self, id_list: List[int], owners_list=None) -> List[int]:
"""
if not owners_list:
owners_list = [self.wallet_address] * len(id_list)
checked_addresses = [self.web_provider.toChecksumAddress(address) for address in owners_list]
checked_addresses = [self.web_provider.to_checksum_address(address) for address in owners_list]
return self.call_read_function("balanceOfBatch", checked_addresses, id_list)

def is_approved_for_all(self, operator_address: str, owner_address: str = "") -> bool:
Expand All @@ -129,8 +129,8 @@ def is_approved_for_all(self, operator_address: str, owner_address: str = "") ->
"""
if not owner_address:
owner_address = self.wallet_address
owner_checked_address = self.web_provider.toChecksumAddress(owner_address)
operator_checked_address = self.web_provider.toChecksumAddress(operator_address)
owner_checked_address = self.web_provider.to_checksum_address(owner_address)
operator_checked_address = self.web_provider.to_checksum_address(operator_address)
return self.call_read_function("isApprovedForAll", owner_checked_address, operator_checked_address)

def uri(self, token_id: int) -> str:
Expand Down
Loading

0 comments on commit 59bd06f

Please sign in to comment.