-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mitsosf/irc2
IRC2 support
- Loading branch information
Showing
9 changed files
with
334 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "icon-sdk" | ||
version = "1.1.0" | ||
version = "1.2.0" | ||
edition = "2021" | ||
description = "ICON(ICX) SDK for Rust" | ||
authors = ["Dimitris Frangiadakis <[email protected]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
use std::error::Error; | ||
use std::str::FromStr; | ||
use rust_decimal::Decimal; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::{json, Value}; | ||
use crate::icon_service::IconService; | ||
use crate::transaction_builder::TransactionBuilder; | ||
use crate::utils::helpers::icx_to_hex; | ||
use crate::wallet::Wallet; | ||
|
||
#[derive(Default, Serialize, Deserialize)] | ||
pub struct IRC2 { | ||
icon_service: IconService, | ||
contract_address: String, | ||
} | ||
|
||
impl IRC2 { | ||
pub fn new(icon_service: IconService, contract_address: String) -> Self { | ||
Self { | ||
icon_service, | ||
contract_address, | ||
} | ||
} | ||
|
||
pub async fn name(&self) -> Result<Value, Box<dyn Error>> { | ||
let transaction = TransactionBuilder::new(&self.icon_service) | ||
.method("icx_call") | ||
.to(&self.contract_address) | ||
.call( | ||
json!({ | ||
"method": "name", | ||
}) | ||
) | ||
.build(); | ||
|
||
let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?; | ||
|
||
Ok(response) | ||
} | ||
|
||
pub async fn symbol(&self) -> Result<Value, Box<dyn Error>> { | ||
let transaction = TransactionBuilder::new(&self.icon_service) | ||
.method("icx_call") | ||
.to(&self.contract_address) | ||
.call( | ||
json!({ | ||
"method": "symbol", | ||
}) | ||
) | ||
.build(); | ||
|
||
let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?; | ||
|
||
Ok(response) | ||
} | ||
|
||
pub async fn decimals(&self) -> Result<Value, Box<dyn Error>> { | ||
let transaction = TransactionBuilder::new(&self.icon_service) | ||
.method("icx_call") | ||
.to(&self.contract_address) | ||
.call( | ||
json!({ | ||
"method": "decimals", | ||
}) | ||
) | ||
.build(); | ||
|
||
let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?; | ||
|
||
Ok(response) | ||
} | ||
|
||
pub async fn total_supply(&self) -> Result<Value, Box<dyn Error>> { | ||
let transaction = TransactionBuilder::new(&self.icon_service) | ||
.method("icx_call") | ||
.to(&self.contract_address) | ||
.call( | ||
json!({ | ||
"method": "totalSupply", | ||
}) | ||
) | ||
.build(); | ||
|
||
let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?; | ||
|
||
Ok(response) | ||
} | ||
|
||
pub async fn balance_of(&self, account: String) -> Result<Value, Box<dyn Error>> { | ||
let transaction = TransactionBuilder::new(&self.icon_service) | ||
.method("icx_call") | ||
.to(&self.contract_address) | ||
.call( | ||
json!({ | ||
"method": "balanceOf", | ||
"params": { | ||
"_owner": account, | ||
} | ||
}) | ||
) | ||
.build(); | ||
|
||
let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?; | ||
|
||
Ok(response) | ||
} | ||
|
||
pub async fn transfer(&self, wallet: Wallet, to: &str, value: &str, version: &str, nid: &str, nonce: &str, step_limit: &str) -> Result<Value, Box<dyn Error>> { | ||
let mut parsed_value = value.to_string(); | ||
|
||
if !parsed_value.starts_with("0x") { | ||
match icx_to_hex(Decimal::from_str(value).expect("Invalid value")) { | ||
Some(v) => { | ||
parsed_value = v; | ||
} | ||
None => panic!("Failed to convert value to hex"), | ||
} | ||
} | ||
|
||
let transaction = TransactionBuilder::new(&self.icon_service) | ||
.method("icx_sendTransaction") | ||
.from(wallet.get_public_address().as_str()) | ||
.to(&self.contract_address) | ||
.version(version) | ||
.nid(nid) | ||
.timestamp() | ||
.nonce(nonce) | ||
.step_limit(step_limit) | ||
.call( | ||
json!({ | ||
"method": "transfer", | ||
"params": { | ||
"_to": to, | ||
"_value": parsed_value, | ||
} | ||
}) | ||
) | ||
.sign(wallet.get_private_key().as_str()) | ||
.build(); | ||
|
||
let response: Value = transaction.send().await.map_err(|e| Box::new(e) as Box<dyn Error>)?; | ||
|
||
Ok(response) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::str::FromStr; | ||
use rust_decimal::Decimal; | ||
use icon_sdk::utils::helpers; | ||
|
||
#[tokio::test] | ||
async fn test_hex_to_icx() -> Result<(), ()> { | ||
let res = helpers::hex_to_icx("0x63b5429420c741b16a10f"); | ||
match res { | ||
Some(response) => { | ||
assert_eq!(response.to_string(), "7533727.039631672546337039"); | ||
}, | ||
None => panic!("Error"), | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_icx_to_hex() -> Result<(), ()> { | ||
let res = helpers::icx_to_hex(Decimal::from_str("7533727.039631672546337039").unwrap()); | ||
match res { | ||
Some(response) => { | ||
assert_eq!(response, "0x63b5429420c741b16a10f"); | ||
println!("{:?}", response); | ||
}, | ||
None => panic!("Error"), | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.