Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
5984c19
cherrypick part of changes
Dec 1, 2021
613a469
create indexer.py
Dec 1, 2021
c0d0e6c
remove solana_receipts_update.py
Dec 1, 2021
3f6b5c4
Cherry pick files from old branch
Dec 1, 2021
0790298
add requirement
Dec 1, 2021
340c854
fix refactoring issues
Dec 1, 2021
7449b38
Fix inspection issues
Dec 1, 2021
b3dacfa
fix last issue
Dec 1, 2021
a50cd47
Merge branch '336_indexer_refactoring' into 337_сreate_base_airdroppe…
Dec 1, 2021
2b8f879
Merge remote-tracking branch 'origin/develop' into 337_сreate_base_ai…
Dec 2, 2021
f51f2ed
simplify tests
Dec 2, 2021
6678924
add test
Dec 2, 2021
5d454b7
Merge remote-tracking branch 'origin/develop' into 337_сreate_base_ai…
Dec 3, 2021
add136a
add price provider
Dec 1, 2021
9a4be44
fix PriceProvider, add test
Dec 1, 2021
07aaca8
Add tests. Check worn on all nets
Dec 1, 2021
7a46c12
refactoring
Dec 1, 2021
2fc1424
integrate price_provider into airdropper
Dec 2, 2021
d157d67
integrate price provider
Dec 2, 2021
8a6abfd
use new faucet method
Dec 3, 2021
3d4dec9
add new parameter to airdropper main
Dec 3, 2021
5c29832
Test discriptions for airdropper
Dec 3, 2021
6a4efdc
Comments for price provider tests
Dec 3, 2021
14aeeed
remove unnecessary comment
Dec 3, 2021
bd35791
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
Dec 3, 2021
ff1f557
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
Dec 6, 2021
c28ca8e
fix error
Dec 6, 2021
07dd112
copy from erc20 contract test
Dec 5, 2021
023993d
Merge remote-tracking branch 'origin/develop' into 344_сreate_integra…
Dec 7, 2021
8c68755
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
Dec 7, 2021
f372b08
Merge branch '338_create_sol_price_provider' into 344_сreate_integrat…
Dec 7, 2021
ae5bddb
create integration tests
Dec 7, 2021
7f4c62c
fix oneline
Dec 7, 2021
b9cf0a6
revert changes on test_neon_faucet
Dec 7, 2021
1012a6b
fix some errors
Dec 7, 2021
dab9c2a
not working
Dec 7, 2021
0a4a2a0
add old variant
Dec 8, 2021
941f5ba
add helper functions
Dec 8, 2021
e223c46
transaction works!
Dec 8, 2021
24bc1f1
prepare refactoring
Dec 8, 2021
2401784
remove unnecessary code
Dec 8, 2021
5325895
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
Dec 8, 2021
9743411
Merge branch '338_create_sol_price_provider' into 344_сreate_integrat…
Dec 8, 2021
0f5c559
add account creation methods
Dec 9, 2021
b3cda28
first test almost ready
Dec 9, 2021
8e74e86
fix tests
Dec 9, 2021
18d88c7
fir airdropper startup
Dec 9, 2021
9ec00c9
One test is completely ready!
Dec 9, 2021
89714f6
add complex test case
Dec 9, 2021
48a96e7
add services to docker-compose file
Dec 9, 2021
89b951a
improve tests
Dec 10, 2021
9acbb38
add price update interval parameter
Dec 10, 2021
726cb1c
remove unnecessary imports
Dec 10, 2021
a1cbad2
fix airdropper run
Dec 10, 2021
5167613
Merge branch '338_create_sol_price_provider' into 344_сreate_integrat…
Dec 10, 2021
34dcedf
prepare for CI
Dec 10, 2021
4ec437f
remove commented
Dec 10, 2021
5a03842
fix compose-file
Dec 10, 2021
6119fe5
create wrapper class
Dec 13, 2021
1ac8877
make tests work!
Dec 13, 2021
0f3d41b
fix naming
Dec 13, 2021
50faa45
refactor test_erc20_wrapper_contract
Dec 13, 2021
4ed6d78
remove duplicated code
Dec 15, 2021
c3ce59b
Merge remote-tracking branch 'origin/develop' into 338_create_sol_pri…
Dec 15, 2021
e7d92fb
Merge branch '338_create_sol_price_provider' into 344_сreate_integrat…
Dec 15, 2021
090a7ec
Changes in base algorithm
Dec 16, 2021
ac839d6
simplify deployment
Dec 16, 2021
bbbf4d7
fix price estimation
Dec 16, 2021
36178d0
Merge remote-tracking branch 'origin/develop' into 344_сreate_integra…
Dec 16, 2021
768f33f
Merge branch '344_сreate_integration_tests_for_airdropper' into br_im…
Dec 16, 2021
ecad137
add test
Dec 17, 2021
c1b9ad9
edit assertions
Dec 17, 2021
d4b991d
fix tests
Dec 17, 2021
15e2505
comments
Dec 19, 2021
f57e474
Merge branch 'develop' into 390_improve_base_indexer_algorithm_perfor…
Dec 22, 2021
b06e55c
Merge branch 'develop' into 390_improve_base_indexer_algorithm_perfor…
Dec 23, 2021
6384d8d
Merge branch 'develop' into 390_improve_base_indexer_algorithm_perfor…
Jan 3, 2022
9b828cc
Fix after merge and rename methods
Jan 3, 2022
fc25fcb
remove unnecessary code
Jan 3, 2022
1d442d6
Fix tests
Jan 3, 2022
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
Prev Previous commit
Next Next commit
create wrapper class
  • Loading branch information
ivanl committed Dec 13, 2021
commit 6119fe553507593315a913192434c67f9242e0af
205 changes: 205 additions & 0 deletions proxy/common_neon/erc20_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
from solcx import install_solc
from web3 import Web3
from spl.token.client import Token
from spl.token.constants import TOKEN_PROGRAM_ID
from eth_account.signers.local import LocalAccount as NeonAccount
from solana.rpc.api import Account as SolanaAccount
from solana.publickey import PublicKey
from solana.transaction import AccountMeta, TransactionInstruction
from solana.system_program import SYS_PROGRAM_ID
from solana.sysvar import SYSVAR_RENT_PUBKEY
from spl.token.instructions import create_associated_token_account
from solana.rpc.types import TxOpts, RPCResponse, Commitment
from typing import Union, Dict
import struct

install_solc(version='0.7.6')
from solcx import compile_source

# Standard interface of ERC20 contract to generate ABI for wrapper
ERC20_INTERFACE_SOURCE = '''
pragma solidity >=0.7.0;

interface IERC20 {
function decimals() external view returns (uint8);
function totalSupply() external view returns (uint256);
function balanceOf(address who) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);

event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);


function approveSolana(bytes32 spender, uint64 value) external returns (bool);
event ApprovalSolana(address indexed owner, bytes32 indexed spender, uint64 value);
}
'''

# Copy of contract: https://github.com/neonlabsorg/neon-evm/blob/develop/evm_loader/SPL_ERC20_Wrapper.sol
ERC20_CONTRACT_SOURCE = '''
// SPDX-License-Identifier: MIT

pragma solidity >=0.5.12;


interface IERC20 {
function decimals() external view returns (uint8);
function totalSupply() external view returns (uint256);
function balanceOf(address who) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);

event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);


function approveSolana(bytes32 spender, uint64 value) external returns (bool);
event ApprovalSolana(address indexed owner, bytes32 indexed spender, uint64 value);
}



/*abstract*/ contract NeonERC20Wrapper /*is IERC20*/ {
address constant NeonERC20 = 0xff00000000000000000000000000000000000001;

string public name;
string public symbol;
bytes32 public tokenMint;

constructor(
string memory _name,
string memory _symbol,
bytes32 _tokenMint
) {
name = _name;
symbol = _symbol;
tokenMint = _tokenMint;
}

fallback() external {
bytes memory call_data = abi.encodePacked(tokenMint, msg.data);
(bool success, bytes memory result) = NeonERC20.delegatecall(call_data);

require(success, string(result));

assembly {
return(add(result, 0x20), mload(result))
}
}
}
'''

class ERC20Wrapper:
proxy: Web3
name: str
symbol: str
token: Token
admin: NeonAccount
mint_authority: SolanaAccount
evm_loader_id: PublicKey
neon_contract_address: str
solana_contract_address: PublicKey
interface: Dict
wrapper: Dict

def __init__(self, proxy: Web3,
name: str, symbol: str,
token: Token,
admin: NeonAccount,
mint_authority: SolanaAccount,
evm_loader_id: PublicKey):
self.proxy = proxy
self.name = name
self.symbol = symbol
self.token = token
self.admin = admin
self.mint_authority = mint_authority
self.evm_loader_id = evm_loader_id

def get_neon_account_address(self, neon_account_address: str) -> PublicKey:
neon_account_addressbytes = bytes.fromhex(neon_account_address[2:])
return PublicKey.find_program_address([b"\1", neon_account_addressbytes], self.evm_loader_id)[0]

def deploy_wrapper(self):
compiled_interface = compile_source(ERC20_INTERFACE_SOURCE)
interface_id, interface = compiled_interface.popitem()
self.interface = interface

compiled_wrapper = compile_source(ERC20_CONTRACT_SOURCE)
wrapper_id, wrapper_interface = compiled_wrapper.popitem()
self.wrapper = wrapper_interface

erc20 = self.proxy.eth.contract(abi=self.wrapper['abi'], bytecode=wrapper_interface['bin'])
nonce = self.proxy.eth.get_transaction_count(self.proxy.eth.default_account)
tx = {'nonce': nonce}
tx_constructor = erc20.constructor(self.name, self.symbol, bytes(self.token.pubkey)).buildTransaction(tx)
tx_deploy = self.proxy.eth.account.sign_transaction(tx_constructor, self.admin.key)
tx_deploy_hash = self.proxy.eth.send_raw_transaction(tx_deploy.rawTransaction)
print('tx_deploy_hash:', tx_deploy_hash.hex())
tx_deploy_receipt = self.proxy.eth.wait_for_transaction_receipt(tx_deploy_hash)
print('tx_deploy_receipt:', tx_deploy_receipt)
print('deploy status:', tx_deploy_receipt.status)
self.neon_contract_address = tx_deploy_receipt.contractAddress
self.solana_contract_address = self.get_neon_account_address(self.neon_contract_address)

def get_neon_erc20_account_address(self, neon_account_address: str):
neon_contract_address_bytes = bytes.fromhex(self.neon_contract_address[2:])
neon_account_address_bytes = bytes.fromhex(neon_account_address[2:])
seeds = [b"\1", b"ERC20Balance",
bytes(self.token.pubkey),
neon_contract_address_bytes,
neon_account_address_bytes]
return PublicKey.find_program_address(seeds, self.evm_loader_id)[0]

def create_accociated_account_instruction(self, payer: PublicKey, owner: PublicKey):
return create_associated_token_account(payer, owner, self.token.pubkey)

def create_neon_erc20_account_instruction(self, payer: PublicKey, eth_address: str):
return TransactionInstruction(
program_id=self.evm_loader_id,
data=bytes.fromhex('0F'),
keys=[
AccountMeta(pubkey=payer, is_signer=True, is_writable=True),
AccountMeta(pubkey=self.get_neon_erc20_account_address(eth_address), is_signer=False, is_writable=True),
AccountMeta(pubkey=self.get_neon_account_address(eth_address), is_signer=False, is_writable=True),
AccountMeta(pubkey=self.solana_contract_address, is_signer=False, is_writable=True),
AccountMeta(pubkey=self.token.pubkey, is_signer=False, is_writable=True),
AccountMeta(pubkey=SYS_PROGRAM_ID, is_signer=False, is_writable=False),
AccountMeta(pubkey=TOKEN_PROGRAM_ID, is_signer=False, is_writable=False),
AccountMeta(pubkey=SYSVAR_RENT_PUBKEY, is_signer=False, is_writable=False),
]
)

def create_input_liquidity_instruction(self, payer: PublicKey, from_address: PublicKey, to_address: str, amount: int):
return TransactionInstruction(
program_id=TOKEN_PROGRAM_ID,
data=b'\3' + struct.pack('<Q', amount),
keys=[
AccountMeta(pubkey=from_address, is_signer=False, is_writable=True),
AccountMeta(pubkey=self.get_neon_erc20_account_address(to_address), is_signer=False, is_writable=True),
AccountMeta(pubkey=payer, is_signer=True, is_writable=False)
]
)

def mint_to(self, destination: Union[PublicKey, str], amount: int) -> RPCResponse:
"""
Method mints given amount of tokens to a given address - either in NEON or Solana format
NOTE: destination account must be previously created using instructions
create_accociated_account_instruction or create_neon_erc20_account_instruction
"""
if isinstance(destination, str):
destination = self.get_neon_erc20_account_address(destination)
return self.token.mint_to(destination, self.mint_authority, amount,
opts=TxOpts(skip_preflight=True, skip_confirmation=False))

def get_balance(self, address: Union[PublicKey, str]) -> int:
if isinstance(address, PublicKey):
return int(self.token.get_balance(address, Commitment('confirmed'))['result']['value']['amount'])

erc20 = self.proxy.eth.contract(address=self.neon_contract_address, abi=self.interface['abi'])
return erc20.functions.balanceOf(address).call()