Skip to content

Commit

Permalink
refactor: change 'withdraw' to 'claimPayment'
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriel Mihura committed Feb 6, 2024
1 parent 04ecc8f commit 5e58bed
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 74 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ jobs:
run: |
make starknet-build
# Deploy PaymentRegistry, Escrow, set escrow, set withdraw selector and test complete flow
# Deploy PaymentRegistry, Escrow, set escrow, set claimPayment selector and test complete flow
- name: Deploy and test complete flow
run: |
export ETH_RPC_URL=${{vars.ETH_RPC_URL}}
Expand All @@ -165,13 +165,13 @@ jobs:
. ./contracts/cairo/deploy.sh
. ./contracts/solidity/set_escrow.sh
. ./contracts/solidity/set_withdraw_selector.sh
. ./contracts/solidity/set_claim_payment_selector.sh
export AMOUNT=1000000000000000000
. ./.github/workflows/scripts/set_order.sh
. ./.github/workflows/scripts/transfer.sh
. ./.github/workflows/scripts/withdraw.sh
. ./.github/workflows/scripts/claim_payment.sh
. ./.github/workflows/scripts/assert.sh
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ ethereum-upgrade: ethereum-build
ethereum-set-escrow:
@. ./contracts/solidity/.env && . ./contracts/solidity/set_escrow.sh

ethereum-set-withdraw-selector:
@. ./contracts/solidity/.env && . ./contracts/cairo/.env && . ./contracts/solidity/set_withdraw_selector.sh
ethereum-set-claim-payment-selector:
@. ./contracts/solidity/.env && . ./contracts/cairo/.env && . ./contracts/solidity/set_claim_payment_selector.sh

starknet-clean:
@cd ./contracts/cairo/ && scarb clean
Expand Down Expand Up @@ -61,7 +61,7 @@ starknet-deploy-and-connect: starknet-build
@. ./contracts/solidity/.env && . ./contracts/cairo/.env
@. ./contracts/cairo/deploy.sh
@. ./contracts/solidity/set_escrow.sh
@. ./contracts/solidity/set_withdraw_selector.sh
@. ./contracts/solidity/set_claim_payment_selector.sh

.ONESHELL:
deploy-all:
Expand All @@ -71,5 +71,5 @@ deploy-all:
@make starknet-build
@. ./contracts/cairo/deploy.sh
@. ./contracts/solidity/set_escrow.sh
@. ./contracts/solidity/set_withdraw_selector.sh
@. ./contracts/solidity/set_claim_payment_selector.sh
@. ./contracts/utils/display_info.sh
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ For this, you will need to:
SN_RPC_URL = Infura or Alchemy RPC URL
SN_ESCROW_OWNER = Public address of the owner of the Escrow contract
MM_SN_WALLET_ADDR = Starknet wallet of the MarketMaker
WITHDRAW_NAME = Exact name of the withdraw function that is called from L1, case sensitive. Example: withdraw_fallback
CLAIM_PAYMENT_NAME = Exact name of the claimPayment function that is called from L1, case sensitive. Example: claimPayment
MM_ETHEREUM_WALLET = Ethereum wallet of the MarketMaker
NATIVE_TOKEN_ETH_STARKNET = Ethereum's erc20 token handler contract in Starknet, this value is automatically updated after solidity/deploy.sh is run
PAYMENT_REGISTRY_PROXY_ADDRESS = Address of ETH smart contract Proxy
Expand All @@ -184,7 +184,7 @@ For this, you will need to:
1. make starknet-build; builds the project
2. make starknet-deploy; deploys the smart contract on the blockchain
3. make ethereum-set-escrow; sets the newly created Starknet contract address on the Ethereum smart contract, so that the L1 contract can communicate with the L2 contract
4. make ethereum-set-withdraw-selector; sets the Starknet _withdraw_ function name on the Ethereum smart contract, so that the L1 contract can communicate with the L2 contract
4. make ethereum-set-claim-payment-selector; sets the Starknet _claimPayment_ function name on the Ethereum smart contract, so that the L1 contract can communicate with the L2 contract

### Second alternative: manual deploy and connect of Escrow and PaymentRegistry

Expand All @@ -210,16 +210,16 @@ This may be better suited for you if you plan to change some of the automaticall

This script uses the previously set .env variable, **ESCROW_CONTRACT_ADDRESS**

3. Setting _EscrowWithdrawSelector_
3. Setting _EscrowClaimPaymentSelector_

Ethereum's smart contract has another variable that must be configured, _EscrowWithdrawSelector_, which is for specifying the _withdraw_ function's name in the Starknet Escrow smart contract.
You can set and change Ethereum's _EscrowWithdrawSelector_ variable, doing the following:
Ethereum's smart contract has another variable that must be configured, _EscrowClaimPaymentSelector_, which is for specifying the _claimPayment_ function's name in the Starknet Escrow smart contract.
You can set and change Ethereum's _EscrowClaimPaymentSelector_ variable, doing the following:
```bash
make ethereum-set-withdraw-selector
make ethereum-set-claim-payment-selector
```
This script uses the WITHDRAW_NAME .env variable to automatically generate the selector in the necesary format
This script uses the CLAIM_PAYMENT_NAME .env variable to automatically generate the selector in the necesary format
### Note on Starknet Smart Contract
Expand Down
2 changes: 1 addition & 1 deletion contracts/cairo/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ SN_RPC_URL=<starknet_rpc_url>
## Required for Escrow Contract
SN_ESCROW_OWNER=<starknet_escrow_owner> #in lowercase hexa with the 0x prefix
MM_SN_WALLET_ADDR=<MarketMaker_starknet_contract_address> #in lowercase hexa with the 0x prefix
WITHDRAW_NAME=<withdraw_function_name> #must match the exact name of the function that withdraws from the starknet smart contract
CLAIM_PAYMENT_NAME=<claim_payment_function_name> #must match the exact name of the function to claim the payment from the starknet smart contract
MM_ETHEREUM_WALLET=<MarketMaker_ethereum_contract_address> #in lowercase hexa with the 0x prefix
NATIVE_TOKEN_ETH_STARKNET=<eth_erc20_in_starknet> #in lowercase hexa with the 0x prefix
2 changes: 1 addition & 1 deletion contracts/cairo/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ STARKNET_PRIVATE_KEY=0x180000000030000018000000000003000000000000300600180000660
SN_RPC_URL=http://0.0.0.0:5050
SN_ESCROW_OWNER=0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973
MM_SN_WALLET_ADDR=0x5686a647a9cdd63ade617e0baf3b364856b813b508f03903eb58a7e622d5855
WITHDRAW_NAME=withdraw
CLAIM_PAYMENT_NAME=claimPayment
MM_ETHEREUM_WALLET=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
NATIVE_TOKEN_ETH_STARKNET=0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7
2 changes: 1 addition & 1 deletion contracts/cairo/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ echo "\nIf you now wish to finish the configuration of this deploy, you will nee
echo "export PAYMENT_REGISTRY_PROXY_ADDRESS=$PAYMENT_REGISTRY_PROXY_ADDRESS"
echo "export ESCROW_CONTRACT_ADDRESS=$ESCROW_CONTRACT_ADDRESS"
echo "make ethereum-set-escrow"
echo "make ethereum-set-withdraw-selector"
echo "make ethereum-set-claim-payment-selector"
10 changes: 5 additions & 5 deletions contracts/cairo/src/escrow.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ mod Escrow {
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
Withdraw: Withdraw,
ClaimPayment: ClaimPayment,
SetOrder: SetOrder,
#[flat]
OwnableEvent: OwnableComponent::Event,
Expand All @@ -94,7 +94,7 @@ mod Escrow {
}

#[derive(Drop, starknet::Event)]
struct Withdraw {
struct ClaimPayment {
order_id: u256,
address: ContractAddress,
amount: u256,
Expand Down Expand Up @@ -233,7 +233,7 @@ mod Escrow {
}

#[l1_handler]
fn withdraw(
fn claimPayment(
ref self: ContractState,
from_address: felt252,
order_id: u256,
Expand All @@ -243,7 +243,7 @@ mod Escrow {
self.pausable.assert_not_paused();
let eth_transfer_contract_felt: felt252 = self.eth_transfer_contract.read().into();
assert(from_address == eth_transfer_contract_felt, 'Only PAYMENT_REGISTRY_CONTRACT');
assert(self.orders_pending.read(order_id), 'Order withdrawn or nonexistent');
assert(self.orders_pending.read(order_id), 'Order claimed or nonexistent');

let order = self.orders.read(order_id);
assert(order.recipient_address == recipient_address, 'recipient_address not match L1');
Expand All @@ -255,6 +255,6 @@ mod Escrow {
IERC20Dispatcher { contract_address: self.native_token_eth_starknet.read() }
.transfer(self.mm_starknet_wallet.read(), payment_amount);

self.emit(Withdraw { order_id, address: self.mm_starknet_wallet.read(), amount });
self.emit(ClaimPayment { order_id, address: self.mm_starknet_wallet.read(), amount });
}
}
4 changes: 2 additions & 2 deletions contracts/cairo/src/tests/test_escrow_allowance.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ mod Escrow {

let mut l1_handler = L1HandlerTrait::new(
contract_address: escrow.contract_address,
function_name: 'withdraw'
function_name: 'claimPayment'
);

let mut payload_buffer: Array<felt252> = ArrayTrait::new();
Expand Down Expand Up @@ -181,7 +181,7 @@ mod Escrow {
data.serialize(ref payload_buffer);
let mut l1_handler = L1HandlerTrait::new(
contract_address: escrow.contract_address,
function_name: 'withdraw',
function_name: 'claimPayment',
);
l1_handler.from_address = ETH_USER().into();

Expand Down
2 changes: 1 addition & 1 deletion contracts/cairo/src/tests/test_escrow_pause.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ mod Escrow {
data.serialize(ref payload_buffer);
let mut l1_handler = L1HandlerTrait::new(
contract_address: escrow.contract_address,
function_name: 'withdraw',
function_name: 'claimPayment',
);
l1_handler.from_address = ETH_TRANSFER_CONTRACT().into();
l1_handler.payload = payload_buffer.span();
Expand Down
4 changes: 2 additions & 2 deletions contracts/solidity/script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ contract Deploy is Script {

address snMessagingAddress = vm.envAddress("SN_MESSAGING_ADDRESS");
uint256 snEscrowAddress = 0x0; // this value is set in a call to the smart contract, once deployed
uint256 snWithdrawSelector = 0x0; // this value is set in a call to the smart contract, once deployed
uint256 snClaimPaymentSelector = 0x0; // this value is set in a call to the smart contract, once deployed
address marketMaker = vm.envAddress("MM_ETHEREUM_WALLET");

PaymentRegistry yab = new PaymentRegistry();
ERC1967Proxy proxy = new ERC1967Proxy(address(yab), "");
PaymentRegistry(address(proxy)).initialize(snMessagingAddress, snEscrowAddress, snWithdrawSelector, marketMaker);
PaymentRegistry(address(proxy)).initialize(snMessagingAddress, snEscrowAddress, snClaimPaymentSelector, marketMaker);

vm.stopBroadcast();

Expand Down
22 changes: 22 additions & 0 deletions contracts/solidity/set_claim_payment_selector.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
. contracts/utils/colors.sh #for ANSI colors

if [ -z "$PAYMENT_REGISTRY_PROXY_ADDRESS" ]; then
printf "\n${RED}ERROR:${COLOR_RESET}\n"
echo "PAYMENT_REGISTRY_PROXY_ADDRESS Variable is empty. Aborting execution.\n"
exit 1
fi
if [ -z "$CLAIM_PAYMENT_NAME" ]; then
printf "\n${RED}ERROR:${COLOR_RESET}\n"
echo "CLAIM_PAYMENT_NAME Variable is empty. Aborting execution.\n"
exit 1
fi

printf "${GREEN}\n=> [ETH] Setting Starknet ClaimPayment Selector on ETH Smart Contract${COLOR_RESET}\n"
echo "Smart contract being modified:" $PAYMENT_REGISTRY_PROXY_ADDRESS

CLAIM_PAYMENT_SELECTOR=$(starkli selector $CLAIM_PAYMENT_NAME)
echo "New ClaimPayment Selector: ${CLAIM_PAYMENT_SELECTOR}"

cast send --rpc-url $ETH_RPC_URL --private-key $ETH_PRIVATE_KEY $PAYMENT_REGISTRY_PROXY_ADDRESS "setEscrowClaimPaymentSelector(uint256)" "${CLAIM_PAYMENT_SELECTOR}" | grep "transactionHash"
echo "Done setting ClaimPayment selector"
22 changes: 0 additions & 22 deletions contracts/solidity/set_withdraw_selector.sh

This file was deleted.

18 changes: 9 additions & 9 deletions contracts/solidity/src/PaymentRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ contract PaymentRegistry is Initializable, OwnableUpgradeable, UUPSUpgradeable {

event Transfer(uint256 indexed orderId, address srcAddress, TransferInfo transferInfo);
event ModifiedEscrowAddress(uint256 newEscrowAddress);
event ModifiedEscrowWithdrawSelector(uint256 newEscrowWithdrawSelector);
event ModifiedEscrowClaimPaymentSelector(uint256 newEscrowClaimPaymentSelector);

mapping(bytes32 => TransferInfo) public transfers;
address private _marketMaker;
IStarknetMessaging private _snMessaging;
uint256 private _snEscrowAddress;
uint256 private _snEscrowWithdrawSelector;
uint256 private _snEscrowClaimPaymentSelector;

constructor() {
_disableInitializers();
Expand All @@ -31,14 +31,14 @@ contract PaymentRegistry is Initializable, OwnableUpgradeable, UUPSUpgradeable {
function initialize(
address snMessaging,
uint256 snEscrowAddress,
uint256 snEscrowWithdrawSelector,
uint256 snEscrowClaimPaymentSelector,
address marketMaker) public initializer {
__Ownable_init(msg.sender);
__UUPSUpgradeable_init();

_snMessaging = IStarknetMessaging(snMessaging);
_snEscrowAddress = snEscrowAddress;
_snEscrowWithdrawSelector = snEscrowWithdrawSelector;
_snEscrowClaimPaymentSelector = snEscrowClaimPaymentSelector;
_marketMaker = marketMaker;
}

Expand All @@ -59,7 +59,7 @@ contract PaymentRegistry is Initializable, OwnableUpgradeable, UUPSUpgradeable {
emit Transfer(orderId, msg.sender, transfers[index]);
}

function withdraw(uint256 orderId, uint256 destAddress, uint256 amount) external payable onlyOwnerOrMM {
function claimPayment(uint256 orderId, uint256 destAddress, uint256 amount) external payable onlyOwnerOrMM {
bytes32 index = keccak256(abi.encodePacked(orderId, destAddress, amount));
TransferInfo storage transferInfo = transfers[index];
require(transferInfo.isUsed == true, "Transfer not found.");
Expand All @@ -73,7 +73,7 @@ contract PaymentRegistry is Initializable, OwnableUpgradeable, UUPSUpgradeable {

_snMessaging.sendMessageToL2{value: msg.value}(
_snEscrowAddress,
_snEscrowWithdrawSelector,
_snEscrowClaimPaymentSelector,
payload);
}

Expand All @@ -82,9 +82,9 @@ contract PaymentRegistry is Initializable, OwnableUpgradeable, UUPSUpgradeable {
emit ModifiedEscrowAddress(snEscrowAddress);
}

function setEscrowWithdrawSelector(uint256 snEscrowWithdrawSelector) external onlyOwner {
_snEscrowWithdrawSelector = snEscrowWithdrawSelector;
emit ModifiedEscrowWithdrawSelector(snEscrowWithdrawSelector);
function setEscrowClaimPaymentSelector(uint256 snEscrowClaimPaymentSelector) external onlyOwner {
_snEscrowClaimPaymentSelector = snEscrowClaimPaymentSelector;
emit ModifiedEscrowClaimPaymentSelector(snEscrowClaimPaymentSelector);
}


Expand Down
4 changes: 2 additions & 2 deletions contracts/solidity/test/MM_ACL.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ contract TransferTest is Test {
PaymentRegistry public yab_caller;

address SN_MESSAGING_ADDRESS = 0xde29d060D45901Fb19ED6C6e959EB22d8626708e;
uint256 SN_ESCROW_WITHDRAW_SELECTOR = 0x15511cc3694f64379908437d6d64458dc76d02482052bfb8a5b33a72c054c77;
uint256 SN_ESCROW_CLAIM_PAYMENT_SELECTOR = 0x15511cc3694f64379908437d6d64458dc76d02482052bfb8a5b33a72c054c77;

function setUp() public {
vm.startPrank(deployer);

yab = new PaymentRegistry();
proxy = new ERC1967Proxy(address(yab), "");
yab_caller = PaymentRegistry(address(proxy));
yab_caller.initialize(SN_MESSAGING_ADDRESS, snEscrowAddress, SN_ESCROW_WITHDRAW_SELECTOR, marketMaker);
yab_caller.initialize(SN_MESSAGING_ADDRESS, snEscrowAddress, SN_ESCROW_CLAIM_PAYMENT_SELECTOR, marketMaker);

vm.stopPrank();
}
Expand Down
Loading

0 comments on commit 5e58bed

Please sign in to comment.