/
contract.rs
144 lines (125 loc) · 4.32 KB
/
contract.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use cosmwasm_std::{
entry_point, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult,
};
use crate::coin_helpers::assert_sent_sufficient_coin;
use crate::error::ContractError;
use crate::msg::{ConfigResponse, ExecuteMsg, InstantiateMsg, QueryMsg, ResolveRecordResponse};
use crate::state::{Config, NameRecord, CONFIG, NAME_RESOLVER};
const MIN_NAME_LENGTH: u64 = 3;
const MAX_NAME_LENGTH: u64 = 64;
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, StdError> {
let config = Config {
purchase_price: msg.purchase_price,
transfer_price: msg.transfer_price,
};
CONFIG.save(deps.storage, &config)?;
Ok(Response::default())
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Register { name } => execute_register(deps, env, info, name),
ExecuteMsg::Transfer { name, to } => execute_transfer(deps, env, info, name, to),
}
}
pub fn execute_register(
deps: DepsMut,
_env: Env,
info: MessageInfo,
name: String,
) -> Result<Response, ContractError> {
// we only need to check here - at point of registration
validate_name(&name)?;
let config = CONFIG.load(deps.storage)?;
assert_sent_sufficient_coin(&info.funds, config.purchase_price)?;
let key = name.as_bytes();
let record = NameRecord { owner: info.sender };
if (NAME_RESOLVER.may_load(deps.storage, key)?).is_some() {
// name is already taken
return Err(ContractError::NameTaken { name });
}
// name is available
NAME_RESOLVER.save(deps.storage, key, &record)?;
Ok(Response::default())
}
pub fn execute_transfer(
deps: DepsMut,
_env: Env,
info: MessageInfo,
name: String,
to: String,
) -> Result<Response, ContractError> {
let config = CONFIG.load(deps.storage)?;
assert_sent_sufficient_coin(&info.funds, config.transfer_price)?;
let new_owner = deps.api.addr_validate(&to)?;
let key = name.as_bytes();
NAME_RESOLVER.update(deps.storage, key, |record| {
if let Some(mut record) = record {
if info.sender != record.owner {
return Err(ContractError::Unauthorized {});
}
record.owner = new_owner.clone();
Ok(record)
} else {
Err(ContractError::NameNotExists { name: name.clone() })
}
})?;
Ok(Response::default())
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::ResolveRecord { name } => query_resolver(deps, env, name),
QueryMsg::Config {} => to_binary::<ConfigResponse>(&CONFIG.load(deps.storage)?.into()),
}
}
fn query_resolver(deps: Deps, _env: Env, name: String) -> StdResult<Binary> {
let key = name.as_bytes();
let address = match NAME_RESOLVER.may_load(deps.storage, key)? {
Some(record) => Some(String::from(&record.owner)),
None => None,
};
let resp = ResolveRecordResponse { address };
to_binary(&resp)
}
// let's not import a regexp library and just do these checks by hand
fn invalid_char(c: char) -> bool {
let is_valid =
c.is_ascii_digit() || c.is_ascii_lowercase() || (c == '.' || c == '-' || c == '_');
!is_valid
}
/// validate_name returns an error if the name is invalid
/// (we require 3-64 lowercase ascii letters, numbers, or . - _)
fn validate_name(name: &str) -> Result<(), ContractError> {
let length = name.len() as u64;
if (name.len() as u64) < MIN_NAME_LENGTH {
Err(ContractError::NameTooShort {
length,
min_length: MIN_NAME_LENGTH,
})
} else if (name.len() as u64) > MAX_NAME_LENGTH {
Err(ContractError::NameTooLong {
length,
max_length: MAX_NAME_LENGTH,
})
} else {
match name.find(invalid_char) {
None => Ok(()),
Some(bytepos_invalid_char_start) => {
let c = name[bytepos_invalid_char_start..].chars().next().unwrap();
Err(ContractError::InvalidCharacter { c })
}
}
}
}