Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
#272 create and delete account on every ethereum trx
  • Loading branch information
otselnik committed Nov 9, 2021
commit 3182ee86a8e8f4b280ac3300be79f8e9c69e87c0
28 changes: 24 additions & 4 deletions proxy/plugin/solana_rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
import traceback
import threading
from .solana_rest_api_tools import EthereumAddress, create_account_with_seed, getTokens, \
getAccountInfo, call_signed, call_emulated, \
Trx, EthereumError, create_collateral_pool_address, getTokenAddr, STORAGE_SIZE, neon_config_load, MINIMAL_GAS_PRICE
getAccountInfo, call_signed, call_emulated, create_multiple_accounts_with_seed, \
Trx, EthereumError, create_collateral_pool_address, getTokenAddr, STORAGE_SIZE, \
neon_config_load, MINIMAL_GAS_PRICE, refund_accounts
from solana.rpc.commitment import Commitment, Confirmed
from web3 import Web3
import logging
Expand Down Expand Up @@ -56,18 +57,27 @@ def __init__(self, client, signer):

logger.debug("LOCK RESOURCES {}".format(self.proxy_id))

self.client = client
self.signer = signer
self.operator = signer.public_key()
self.operator_token = getTokenAddr(self.operator)

proxy_id_bytes = self.proxy_id.to_bytes((self.proxy_id.bit_length() + 7) // 8, 'big')

storage_seed = keccak_256(b"storage" + proxy_id_bytes).hexdigest()[:32]
storage_seed = bytes(storage_seed, 'utf8')
self.storage = create_account_with_seed(client, funding=signer, base=signer, seed=storage_seed, storage_size=STORAGE_SIZE)

holder_seed = keccak_256(b"holder" + proxy_id_bytes).hexdigest()[:32]
holder_seed = bytes(holder_seed, 'utf8')
self.holder = create_account_with_seed(client, funding=signer, base=signer, seed=holder_seed, storage_size=STORAGE_SIZE)

accounts = create_multiple_accounts_with_seed(
client,
funding=signer,
base=signer,
seeds=[storage_seed, holder_seed],
sizes=[STORAGE_SIZE, STORAGE_SIZE])
self.storage = accounts[0]
self.holder = accounts[1]

collateral_pool_index = self.proxy_id % 10
self.collateral_pool_index_buf = collateral_pool_index.to_bytes(4, 'little')
Expand All @@ -79,6 +89,16 @@ def __del__(self):
with proxy_used_id_glob.get_lock():
if proxy_used_id_glob[self.proxy_id] == 1:
proxy_used_id_glob[self.proxy_id] = 0

proxy_id_bytes = self.proxy_id.to_bytes((self.proxy_id.bit_length() + 7) // 8, 'big')

storage_seed = keccak_256(b"storage" + proxy_id_bytes).hexdigest()[:32]
storage_seed = bytes(storage_seed, 'utf8')

holder_seed = keccak_256(b"holder" + proxy_id_bytes).hexdigest()[:32]
holder_seed = bytes(holder_seed, 'utf8')

refund_accounts(self.client, self.signer, [storage_seed, holder_seed])
else:
raise Exception("Proxy resource id ({}) is locked.".format(self.proxy_id))

Expand Down
41 changes: 41 additions & 0 deletions proxy/plugin/solana_rest_api_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,47 @@ def create_account_with_seed(client, funding, base, seed, storage_size):
return account


def create_multiple_accounts_with_seed(client, funding, base, seeds, sizes):
accounts = []
trx = Transaction()

for seed, storage_size in zip(seeds, sizes):
account = accountWithSeed(base.public_key(), seed, PublicKey(evm_loader_id))
accounts.append(account)

if client.get_balance(account, commitment=Confirmed)['result']['value'] == 0:
minimum_balance = client.get_minimum_balance_for_rent_exemption(storage_size, commitment=Confirmed)["result"]
logger.debug("Minimum balance required for account {}".format(minimum_balance))

trx.add(createAccountWithSeedTrx(funding.public_key(), base.public_key(), seed, minimum_balance, storage_size, PublicKey(evm_loader_id)))

if len(trx.instructions) > 0:
send_transaction(client, trx, funding)

return accounts


def refund_accounts(client, owner, seeds):
trx = Transaction()
for seed in seeds:
account = accountWithSeed(owner.public_key(), seed, PublicKey(evm_loader_id))
if client.get_balance(account, commitment=Confirmed)['result']['value'] != 0:
trx.add(make_refund_tx(account, owner, seed))

if len(trx.instructions) > 0:
send_transaction(client, trx, owner)


def make_refund_tx(del_key, owner, seed):
return TransactionInstruction(
program_id=PublicKey(evm_loader_id),
data=bytearray.fromhex("10") + bytes(seed, 'utf8'),
keys=[
AccountMeta(pubkey=del_key, is_signer=False, is_writable=True),
AccountMeta(pubkey=owner.public_key(), is_signer=True, is_writable=True),
])


def make_keccak_instruction_data(check_instruction_index, msg_len, data_start):
if check_instruction_index > 255 and check_instruction_index < 0:
raise Exception("Invalid index for instruction - {}".format(check_instruction_index))
Expand Down