Skip to content
Merged
Changes from all commits
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
180 changes: 32 additions & 148 deletions internal/migrations/erc20-bridge/001-actions.sql
Original file line number Diff line number Diff line change
@@ -1,164 +1,48 @@
/**
Bridge Mechanism:
1. Call [chain]_admin_lock_tokens(), to lock x amount of tokens from user.
2. Call [chain]_admin_issue_tokens(), to issue the locked tokens to layer-1 (escrow on ethereum).
3. Frontend will call `list_wallet_rewards --namespace [chain]_bridge` to get inclusion proofs.
4. Frontend will call claimReward function on escrow contract using the inclusion proofs given.
5. Receiver wallet will get the tokens after claim is done.
*/
CREATE OR REPLACE ACTION get_erc20_bridge_info()
PUBLIC VIEW RETURNS (
chain TEXT,
escrow TEXT,
epoch_period TEXT,
erc20 TEXT,
decimals INT,
balance NUMERIC(78, 0),
synced BOOLEAN,
synced_at INT8,
enabled BOOLEAN
) {
FOR $row IN sepolia_bridge.info() {
RETURN $row.chain, $row.escrow, $row.epoch_period, $row.erc20, $row.decimals, $row.balance, $row.synced, $row.synced_at, $row.enabled;
}
};

-- TESTNET
CREATE OR REPLACE ACTION sepolia_wallet_balance($wallet_address TEXT) PUBLIC VIEW RETURNS (balance NUMERIC(78, 0)) {
$lower_caller TEXT := LOWER(@caller);

-- Permission Check: Ensure caller has the 'system:erc20_bridge_writer' role.
$has_permission BOOL := false;
for $row in are_members_of('system', 'erc20_bridge_writer', ARRAY[$lower_caller]) {
if $row.wallet = $lower_caller AND $row.is_member {
$has_permission := true;
break;
}
}
if NOT $has_permission {
ERROR('Caller does not have the required system:erc20_bridge_writer role to read balance.');
}

$balance := sepolia_bridge.balance($wallet_address);
return $balance;
};

CREATE OR REPLACE ACTION sepolia_admin_lock_tokens($wallet_address TEXT, $amount TEXT) PUBLIC {
$lower_caller TEXT := LOWER(@caller);

-- Permission Check: Ensure caller has the 'system:erc20_bridge_writer' role.
$has_permission BOOL := false;
for $row in are_members_of('system', 'erc20_bridge_writer', ARRAY[$lower_caller]) {
if $row.wallet = $lower_caller AND $row.is_member {
$has_permission := true;
break;
}
}
if NOT $has_permission {
ERROR('Caller does not have the required system:erc20_bridge_writer role to lock tokens.');
}

sepolia_bridge.lock_admin($wallet_address, $amount::NUMERIC(78, 0));
};

CREATE OR REPLACE ACTION sepolia_admin_unlock_tokens($to_address TEXT, $amount TEXT) PUBLIC {
$lower_caller TEXT := LOWER(@caller);

-- Permission Check: Ensure caller has the 'system:erc20_bridge_writer' role.
$has_permission BOOL := false;
for $row in are_members_of('system', 'erc20_bridge_writer', ARRAY[$lower_caller]) {
if $row.wallet = $lower_caller AND $row.is_member {
$has_permission := true;
break;
}
}
if NOT $has_permission {
ERROR('Caller does not have the required system:erc20_bridge_writer role to unlock tokens.');
}

sepolia_bridge.unlock($to_address, $amount::NUMERIC(78, 0));
};

CREATE OR REPLACE ACTION sepolia_admin_issue_tokens($to_address TEXT, $amount TEXT) PUBLIC {
$lower_caller TEXT := LOWER(@caller);

-- Permission Check: Ensure caller has the 'system:erc20_bridge_writer' role.
$has_permission BOOL := false;
for $row in are_members_of('system', 'erc20_bridge_writer', ARRAY[$lower_caller]) {
if $row.wallet = $lower_caller AND $row.is_member {
$has_permission := true;
break;
}
}
if NOT $has_permission {
ERROR('Caller does not have the required system:erc20_bridge_writer role to issue tokens.');
}

sepolia_bridge.issue($to_address, $amount::NUMERIC(78, 0));
RETURN $balance;
};

CREATE OR REPLACE ACTION sepolia_admin_bridge_tokens($amount TEXT) PUBLIC {
sepolia_bridge.bridge($amount::NUMERIC(78, 0));
-- Calculate 1% fee and lock it in our treasury
$numAmount := $amount::NUMERIC(78, 0);
$fee := $numAmount * 0.01;
sepolia_bridge.lock($fee);

-- Bridge the rest to users
sepolia_bridge.bridge($numAmount - $fee);
};
Comment thread
williamrusdyputra marked this conversation as resolved.

-- MAINNET
CREATE OR REPLACE ACTION mainnet_wallet_balance($wallet_address TEXT) PUBLIC VIEW RETURNS (balance NUMERIC(78, 0)) {
$lower_caller TEXT := LOWER(@caller);

-- Permission Check: Ensure caller has the 'system:erc20_bridge_writer' role.
$has_permission BOOL := false;
for $row in are_members_of('system', 'erc20_bridge_writer', ARRAY[$lower_caller]) {
if $row.wallet = $lower_caller AND $row.is_member {
$has_permission := true;
break;
}
}
if NOT $has_permission {
ERROR('Caller does not have the required system:erc20_bridge_writer role to read balance.');
}

$balance := mainnet_bridge.balance($wallet_address);
return $balance;
};

CREATE OR REPLACE ACTION mainnet_admin_lock_tokens($wallet_address TEXT, $amount TEXT) PUBLIC {
$lower_caller TEXT := LOWER(@caller);

-- Permission Check: Ensure caller has the 'system:erc20_bridge_writer' role.
$has_permission BOOL := false;
for $row in are_members_of('system', 'erc20_bridge_writer', ARRAY[$lower_caller]) {
if $row.wallet = $lower_caller AND $row.is_member {
$has_permission := true;
break;
}
}
if NOT $has_permission {
ERROR('Caller does not have the required system:erc20_bridge_writer role to lock tokens.');
}

mainnet_bridge.lock_admin($wallet_address, $amount::NUMERIC(78, 0));
};

CREATE OR REPLACE ACTION mainnet_admin_unlock_tokens($to_address TEXT, $amount TEXT) PUBLIC {
$lower_caller TEXT := LOWER(@caller);

-- Permission Check: Ensure caller has the 'system:erc20_bridge_writer' role.
$has_permission BOOL := false;
for $row in are_members_of('system', 'erc20_bridge_writer', ARRAY[$lower_caller]) {
if $row.wallet = $lower_caller AND $row.is_member {
$has_permission := true;
break;
}
}
if NOT $has_permission {
ERROR('Caller does not have the required system:erc20_bridge_writer role to unlock tokens.');
}

mainnet_bridge.unlock($to_address, $amount::NUMERIC(78, 0));
};

CREATE OR REPLACE ACTION mainnet_admin_issue_tokens($to_address TEXT, $amount TEXT) PUBLIC {
$lower_caller TEXT := LOWER(@caller);

-- Permission Check: Ensure caller has the 'system:erc20_bridge_writer' role.
$has_permission BOOL := false;
for $row in are_members_of('system', 'erc20_bridge_writer', ARRAY[$lower_caller]) {
if $row.wallet = $lower_caller AND $row.is_member {
$has_permission := true;
break;
}
}
if NOT $has_permission {
ERROR('Caller does not have the required system:erc20_bridge_writer role to issue tokens.');
}

mainnet_bridge.issue($to_address, $amount::NUMERIC(78, 0));
RETURN $balance;
};

CREATE OR REPLACE ACTION mainnet_admin_bridge_tokens($amount TEXT) PUBLIC {
mainnet_bridge.bridge($amount::NUMERIC(78, 0));
-- Calculate 1% fee and lock it in our treasury
$numAmount := $amount::NUMERIC(78, 0);
$fee := $numAmount * 0.01;
mainnet_bridge.lock($fee);

-- Bridge the rest to users
mainnet_bridge.bridge($numAmount - $fee);
};
Comment thread
williamrusdyputra marked this conversation as resolved.
Outdated
Loading