Skip to content

Commit

Permalink
refactor: match statement
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanvdplas committed Jul 31, 2024
1 parent 5aec185 commit 51c38cc
Showing 1 changed file with 22 additions and 44 deletions.
66 changes: 22 additions & 44 deletions pop-api/integration-tests/src/local_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,21 +441,17 @@ fn total_supply(addr: AccountId32, asset_id: AssetId) -> Result<Balance, Error>
let function = function_selector("total_supply");
let params = [function, asset_id.encode()].concat();
let result = bare_call(addr, params, 0).expect("should work");
match decoded::<Result<Balance, Error>>(result) {
Ok(x) => x,
Err(result) => panic!("Contract reverted: {:?}", result),
}
decoded::<Result<Balance, Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

// Call balance_of contract message.
fn balance_of(addr: AccountId32, asset_id: AssetId, owner: AccountId32) -> Result<Balance, Error> {
let function = function_selector("balance_of");
let params = [function, asset_id.encode(), owner.encode()].concat();
let result = bare_call(addr, params, 0).expect("should work");
match decoded::<Result<Balance, Error>>(result) {
Ok(x) => x,
Err(result) => panic!("Contract reverted: {:?}", result),
}
decoded::<Result<Balance, Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

// Call allowance contract message.
Expand All @@ -468,43 +464,35 @@ fn allowance(
let function = function_selector("allowance");
let params = [function, asset_id.encode(), owner.encode(), spender.encode()].concat();
let result = bare_call(addr, params, 0).expect("should work");
match decoded::<Result<Balance, Error>>(result) {
Ok(x) => x,
Err(result) => panic!("Contract reverted: {:?}", result),
}
decoded::<Result<Balance, Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

// Call token_name contract message.
fn token_name(addr: AccountId32, asset_id: AssetId) -> Result<Vec<u8>, Error> {
let function = function_selector("token_name");
let params = [function, asset_id.encode()].concat();
let result = bare_call(addr, params, 0).expect("should work");
match decoded::<Result<Vec<u8>, Error>>(result) {
Ok(x) => x,
Err(result) => panic!("Contract reverted: {:?}", result),
}
decoded::<Result<Vec<u8>, Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

// Call token_symbol contract message.
fn token_symbol(addr: AccountId32, asset_id: AssetId) -> Result<Vec<u8>, Error> {
let function = function_selector("token_symbol");
let params = [function, asset_id.encode()].concat();
let result = bare_call(addr, params, 0).expect("should work");
match decoded::<Result<Vec<u8>, Error>>(result) {
Ok(x) => x,
Err(result) => panic!("Contract reverted: {:?}", result),
}
decoded::<Result<Vec<u8>, Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

// Call token_decimals contract message.
fn token_decimals(addr: AccountId32, asset_id: AssetId) -> Result<u8, Error> {
let function = function_selector("token_decimals");
let params = [function, asset_id.encode()].concat();
let result = bare_call(addr, params, 0).expect("should work");
match decoded::<Result<u8, Error>>(result) {
Ok(x) => x,
Err(result) => panic!("Contract reverted: {:?}", result),
}
decoded::<Result<u8, Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

fn transfer(
Expand All @@ -516,10 +504,8 @@ fn transfer(
let function = function_selector("transfer");
let params = [function, asset_id.encode(), to.encode(), value.encode()].concat();
let result = bare_call(addr, params, 0).expect("should work");
match decoded::<Result<(), Error>>(result) {
Ok(x) => x,
Err(result) => panic!("Contract reverted: {:?}", result),
}
decoded::<Result<(), Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

fn transfer_from(
Expand All @@ -535,10 +521,8 @@ fn transfer_from(
[function, asset_id.encode(), from.encode(), to.encode(), value.encode(), data.encode()]
.concat();
let result = bare_call(addr, params, 0).expect("should work");
match decoded::<Result<(), Error>>(result) {
Ok(x) => x,
Err(result) => panic!("Contract reverted: {:?}", result),
}
decoded::<Result<(), Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

fn approve(
Expand All @@ -550,10 +534,8 @@ fn approve(
let function = function_selector("approve");
let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat();
let result = bare_call(addr, params, 0).expect("should work");
match decoded::<Result<(), Error>>(result) {
Ok(x) => x,
Err(result) => panic!("Contract reverted: {:?}", result),
}
decoded::<Result<(), Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

fn increase_allowance(
Expand All @@ -565,10 +547,8 @@ fn increase_allowance(
let function = function_selector("increase_allowance");
let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat();
let result = bare_call(addr, params, 0).expect("should work");
match decoded::<Result<(), Error>>(result) {
Ok(x) => x,
Err(result) => panic!("Contract reverted: {:?}", result),
}
decoded::<Result<(), Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

fn decrease_allowance(
Expand All @@ -580,10 +560,8 @@ fn decrease_allowance(
let function = function_selector("decrease_allowance");
let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat();
let result = bare_call(addr, params, 0).expect("should work");
match decoded::<Result<(), Error>>(result) {
Ok(x) => x,
Err(result) => panic!("Contract reverted: {:?}", result),
}
decoded::<Result<(), Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

// fn asset_exists(addr: AccountId32, asset_id: AssetId) -> bool {
Expand Down

0 comments on commit 51c38cc

Please sign in to comment.