Skip to content

Commit

Permalink
Merge pull request #319 from Build-Squad/mudit/eng-240-fix-pyxfluencer
Browse files Browse the repository at this point in the history
ENG 240 Pyxfluencer Issues
  • Loading branch information
muditmahajan authored Apr 17, 2024
2 parents d7f1480 + d155f45 commit 27cc0a6
Show file tree
Hide file tree
Showing 9 changed files with 340 additions and 185 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy_api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
source venv/bin/activate
pip3 install -r requirements.txt
pip3 uninstall pyxfluencer -y
pip3 install pyxfluencer-1.2.1-py3-none-any.whl
latest_file=$(ls -v | grep 'pyxfluencer-[0-9.]*-py3-none-any.whl' | tail -n 1) && pip install $latest_file
python3 manage.py migrate
python3 manage.py collectstatic -link --noinput
echo "Stopping previous processes..."
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy_prod_api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
source venv/bin/activate
pip3 install -r requirements.txt
pip3 uninstall pyxfluencer -y
pip3 install pyxfluencer-1.2.1-py3-none-any.whl
latest_file=$(ls -v | grep 'pyxfluencer-[0-9.]*-py3-none-any.whl' | tail -n 1) && pip install $latest_file
python3 manage.py migrate
python3 manage.py collectstatic -link --noinput
echo "Stopping previous processes..."
Expand Down
Binary file not shown.
47 changes: 11 additions & 36 deletions solana-python/launch_validate.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import asyncio

from solders.pubkey import Pubkey
from solana.rpc.types import TxOpts

from pyxfluencer.instructions import validate_escrow_sol
from pyxfluencer.utils import get_local_keypair_pubkey
from pyxfluencer.utils import sign_and_send_transaction
Expand All @@ -28,55 +25,33 @@ async def main(target_state):
print(len(msg)*"*")

configuration = load_configuration()
network = configuration["network"]
network = configuration["rpc"]["mainnet"]

print("nework -->", network)

keypair_paths = KeypairPaths()

validation_authority, validation_authority_pk \
= get_local_keypair_pubkey(path=keypair_paths.validation_authority)
_, business_pk = get_local_keypair_pubkey(path=keypair_paths.bussines_keypair)
_, business_pk = get_local_keypair_pubkey() #path=keypair_paths.bussines_keypair)
_, influencer_pk = get_local_keypair_pubkey(path=keypair_paths.influencer_keypair)

# check configuration matches local keypairs
assert str(validation_authority_pk) == configuration["platform"]
assert str(business_pk) == configuration["business"]["pubkey"]
assert str(influencer_pk) == configuration["influencer"]["pubkey"]


order_code = configuration["order_code"]
SEEDS = [b"escrow",
bytes(business_pk),
bytes(influencer_pk),
bytes(str(order_code),"UTF-8")
]

escrow_pda, _ = Pubkey.find_program_address(SEEDS, PROGRAM_ID)
print("Escrow SOL PDA",escrow_pda)

# state 1 = unlock funds so business can re-fund
# state 2 = unlock funds so influencer can claim
# percentage_fee is passed is passed always (both cases cancel and deliver)

args = {"target_state":target_state.value, "percentage_fee": 0 }
from pyxfluencer import EscrowValidator

accounts = {
"validation_authority": validation_authority_pk,
"influencer":influencer_pk,
"business":business_pk,
"escrow_account":escrow_pda
}

opts = TxOpts(skip_confirmation = True,
skip_preflight = True,
preflight_commitment="processed")
escrow_validator = EscrowValidator(validation_authority, business_pk, influencer_pk, order_code, network)
if target_state == TargetState.CANCEL:
res = await escrow_validator.cancel()
else:
res = await escrow_validator.deliver()

ix = validate_escrow_sol(args, accounts, program_id=PROGRAM_ID)

signers = [validation_authority]

await sign_and_send_transaction(ix, signers, opts, network)
print("Results from Validator",res)


import argparse
Expand All @@ -93,5 +68,5 @@ async def main(target_state):
exit()


asyncio.run(main(TargetState.DELIVERY))
asyncio.run(main(target_state))

158 changes: 96 additions & 62 deletions solana-python/pyxfluencer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@
from .utils import sign_and_send_transaction
from .program_id import PROGRAM_ID

xfluencer_solana_python_client_version="1.2.1"
xfluencer_solana_python_client_version = "1.2.2"

###################
# Version: 1.2.2
# Bump: Patch
# Updated: 16.04.2024
# - add re-try logic
# - add priority_ffes instruction
# - add compute unit instruction
# - add custom error treatment on simulation
###################
# Version: 1.2.1
# Bump: Patch
Expand Down Expand Up @@ -71,12 +79,13 @@ class EscrowState(Enum):
@dataclass
class EscrowValidator:
validator_authority: Keypair
business_address: Pubkey
influencer_address: Pubkey
business_address: str | Pubkey
influencer_address: str | Pubkey
order_code: int
network: str = "https://api.devnet.solana.com"
percentage_fee: int = 0
processing_spl_escrow: bool = False
priority_fees: int = 0

async def cancel(self):
return await validate_escrow(self.validator_authority,
Expand All @@ -85,26 +94,32 @@ async def cancel(self):
EscrowState.CANCEL,
self.order_code,
self.network,
self.percentage_fee,
self.processing_spl_escrow)
percentage_fee = self.percentage_fee,
processing_spl_escrow = self.processing_spl_escrow,
priority_fees = self.priority_fees
)
async def deliver(self):
return await validate_escrow(self.validator_authority,
self.business_address,
self.influencer_address,
EscrowState.DELIVERED,
self.order_code,
self.network,
self.percentage_fee,
self.processing_spl_escrow)
percentage_fee = self.percentage_fee,
processing_spl_escrow = self.processing_spl_escrow,
priority_fees = self.priority_fees
)


# NON SPL escrow
async def validate_escrow_to_cancel(validator_authority: Keypair,
business_address: str,
influencer_address: str,
business_address: str, #@TODO: Accept Pubkey as well
influencer_address: str, #@TODO: Accept Pubkey as well
order_code:int,
network: str = "https://api.devnet.solana.com",
percentage_fee: int = 0):
percentage_fee: int = 0,
priority_fees: int = 0
):
"""
Args:
Expand All @@ -119,21 +134,25 @@ async def validate_escrow_to_cancel(validator_authority: Keypair,
"""

return await validate_escrow(validator_authority,
business_address,
influencer_address,
EscrowState.CANCEL,
order_code,
network,
percentage_fee,
processing_spl_escrow=False)
business_address,
influencer_address,
EscrowState.CANCEL,
order_code,
network,
percentage_fee=percentage_fee,
processing_spl_escrow=False,
priority_fees=priority_fees
)

# NON SPL escrow
async def validate_escrow_to_delivered(validator_authority: Keypair,
business_address: str,
influencer_address: str,
business_address: str, #@TODO: Accept Pubkey as well
influencer_address: str, #@TODO: Accept Pubkey as well
order_code:int,
network: str = "https://api.devnet.solana.com",
percentage_fee: int = 0):
percentage_fee: int = 0,
priority_fees: int = 0
):
"""_summary_
Args:
Expand All @@ -153,68 +172,83 @@ async def validate_escrow_to_delivered(validator_authority: Keypair,
EscrowState.DELIVERED,
order_code,
network,
percentage_fee,
processing_spl_escrow=False)
percentage_fee=percentage_fee,
processing_spl_escrow=False,
priority_fees=priority_fees
)


async def validate_escrow(validation_authority: Keypair,
business_address: str,
influencer_address: str,
target_escrow_state: EscrowState,
business_address: str | Pubkey,
influencer_address: str | Pubkey,
target_escrow_state: EscrowState,
order_code: int,
network: str = "https://api.devnet.solana.com",
percentage_fee: int = 0,
processing_spl_escrow: bool = False):
processing_spl_escrow: bool = False,
priority_fees: int = 0):

business_pk = Pubkey.from_string(business_address)
influencer_pk = Pubkey.from_string(influencer_address)
if isinstance(business_address,str):
business_pk = Pubkey.from_string(business_address)
else:
business_pk = business_address

opts = TxOpts(skip_confirmation = True,
skip_preflight = True,
preflight_commitment="processed")

args = {"target_state":target_escrow_state.value, "percentage_fee": percentage_fee}

signers = [validation_authority]

if not processing_spl_escrow:
if isinstance(influencer_address,str):
influencer_pk = Pubkey.from_string(influencer_address)
else:
influencer_pk = influencer_address


args = {"target_state": target_escrow_state.value,
"percentage_fee": percentage_fee}

signers = [validation_authority]

if not processing_spl_escrow:
print("Processing Escrow SOL case")
SEEDS = [b"escrow", bytes(business_pk), bytes(influencer_pk),
bytes(str(order_code),"UTF-8")]
bytes(str(order_code), "UTF-8")]

escrow_pda, _ = Pubkey.find_program_address(SEEDS, PROGRAM_ID)

accounts = {
"validation_authority": validation_authority.pubkey(),
"influencer":influencer_pk,
"business":business_pk,
"escrow_account":escrow_pda
}
"validation_authority": validation_authority.pubkey(),
"influencer": influencer_pk,
"business": business_pk,
"escrow_account": escrow_pda
}

ix = validate_escrow_sol(args, accounts, program_id=PROGRAM_ID)

#return await sign_and_send_transaction(ix, signers, opts, network)
# return await sign_and_send_transaction(ix, signers, opts, network)

else:
print("Processing Escrow SPL case")
# find vault and escrows pdas

# find vault and escrows pdas
vault_account_pda, _ = \
Pubkey.find_program_address([b"token-seed",
bytes(str(order_code),"UTF-8")], PROGRAM_ID)
Pubkey.find_program_address([b"token-seed",
bytes(str(order_code), "UTF-8")], PROGRAM_ID)

escrow_account_pda, _ = \
Pubkey.find_program_address([b"escrow-data",
bytes(str(order_code),"UTF-8")], PROGRAM_ID)
Pubkey.find_program_address([b"escrow-data",
bytes(str(order_code), "UTF-8")], PROGRAM_ID)

accounts = {
"validation_authority": validation_authority.pubkey(),
"validation_authority": validation_authority.pubkey(),
"vault_account": vault_account_pda,
"influencer":influencer_pk,
"business":business_pk,
"escrow_account":escrow_account_pda
}
"influencer": influencer_pk,
"business": business_pk,
"escrow_account": escrow_account_pda
}

ix = validate_escrow_spl(args, accounts, program_id=PROGRAM_ID)

return await sign_and_send_transaction(ix, signers, opts, network)

try:
tx_resp = await sign_and_send_transaction(ix, signers, network, priority_fees=priority_fees)
if tx_resp:
print(f"Escrow validation status: {tx_resp}")
return tx_resp
except Exception as e:
print(f"Error validating escrow {str(e)}")
raise Exception(f"Error validating escrow {e}")
Loading

0 comments on commit 27cc0a6

Please sign in to comment.