USN
USN is a NEAR-native USD stable coin.
The contract implements fungible token API according to the following standards:
- NEP-141 (ERC-20 fashioned)
- NEP-148
- Fungible Token Event
The specific part of the USN contract is buy
/sell
methods of NEAR/USD exchange with rates taken from the oracle (priceoracle).
Contract Address
Mainnet | Testnet |
---|---|
usn | usdn.testnet |
How It Works
Buy USN for NEAR
Method: buy
sell
API
Sell USN for NEAR with Method: sell
Slippage
Methods buy
and sell
requires the expected exchange rate to avoid slippage. If the price suddenly changes (slips) out of the expected deviation the USN contract aborts the transaction.
Build
First, install prerequisites:
npm install
Then, build.
For local sandbox:
npm run build
For testnet:
npm run build:testnet
For mainnet:
npm run build:mainnet
WARNING: There is a difference in each target. The crucial difference is that they communicate with different oracle addresses:
- Mainnet:
priceoracle.near
- Testnet:
priceoracle.testnet
- Sandbox:
priceoracle.test.near
And all these oracle contracts report prices with different asset names.
Test
Run unit tests
cargo test
Run integration tests
npm run build
npm run deploy
npm run test
Manual testing on the Testnet
Build
npm run build:testnet
Deploy
near deploy --force --wasmFile target/wasm32-unknown-unknown/testnet/usn.wasm --accountId=usdn.testnet --masterAccount=usdn.testnet
Init once
near call usdn.testnet new --args '{"owner_id": "usdn.testnet"}' --accountId=usdn.testnet
Add a guardian
near call usdn.testnet extend_guardians --accountId usdn.testnet --args '{"guardians": ["alice.testnet"]}'
Buy and sell
# Send NEAR, buy USN.
near call usdn.testnet buy --accountId alice.testnet --amount 1 --gas 50000000000000
# Check USN balance.
near call usdn.testnet ft_balance_of --accountId alice.testnet --args '{"account_id": "alice.testnet"}'
# Sell USN, receive NEAR.
near call usdn.testnet sell --accountId alice.testnet --args '{"amount": "118800"}' --amount 0.000000000000000000000001 --gas 50000000000000
# Buy USN with slippage control
near call usdn.testnet buy --args '{"expected": {"multiplier": "111439", "slippage": "10", "decimals": "28" }}' --accountId alice.testnet --amount 1 --gas 50000000000000
# Buy USN and transfer to someone.
near call usdn.testnet buy --args '{"to": "bob.testnet"}' --accountId alice.testnet --amount 1 --gas 50000000000000
DAO
Upgrade the contract via Upgrade Proposal
- Download
usn.mainnet.wasm
from https://github.com/binary-star-near/usn/releases - Create an upgrade proposal:
sputnikdao proposal upgrade usn.mainnet.wasm usn --daoAcc usn --accountId alice.near --network mainnet
API
Buy/sell USN
Send NEAR, receive USN.
pub fn buy(&mut self, expected: Option<ExpectedRate>, to: Option<AccountId>);
Send USN, receive NEAR.
pub fn sell(&mut self, amount: U128, expected: Option<ExpectedRate>) -> Promise;
View methods
pub fn contract_status(&self) -> ContractStatus;
pub fn name(&self) -> String;
pub fn symbol(&self) -> String;
pub fn decimals(&self) -> u8;
pub fn spread(&self, amount: Option<U128>) -> U128;
pub fn version(&self) -> String;
pub fn blacklist_status(&self, account_id: &AccountId) -> BlackListStatus;
pub fn owner(&self);
NEP-141 (ERC-20)
pub fn ft_transfer(&mut self, receiver_id: AccountId, amount: U128, memo: Option<String>);
pub fn ft_transfer_call(
&mut self,
receiver_id: AccountId,
amount: U128,
memo: Option<String>,
msg: String,
) -> PromiseOrValue<U128>;
pub fn ft_total_supply(&self) -> U128;
pub fn ft_balance_of(&self, account_id: AccountId) -> U128;
pub fn ft_metadata(&self) -> FungibleTokenMetadata;
NEP-145: partial storage API
Always returns 125 milliNEAR indicating that user doesn't need to be registered with storage_deposit
.
pub fn storage_balance_of(&self, account_id: AccountId) -> Option<StorageBalance>;
Constructor
pub fn new(owner_id: AccountId) -> Self;
Private setters
For owner only.
pub fn upgrade_name_symbol(&mut self, name: String, symbol: String);
pub fn upgrade_icon(&mut self, data: String);
pub fn add_to_blacklist(&mut self, account_id: &AccountId);
pub fn remove_from_blacklist(&mut self, account_id: &AccountId);
pub fn destroy_black_funds(&mut self, account_id: &AccountId);
pub fn pause(&mut self);
pub fn resume(&mut self);
pub fn set_fixed_spread(&mut self, spread: U128) {
pub fn set_adaptive_spread(&mut self, params: Option<ExponentialSpreadParams>);
pub fn set_owner(&mut self, owner_id: AccountId);
pub fn extend_guardians(&mut self, guardians: Vec<AccountId>);
pub fn remove_guardians(&mut self, guardians: Vec<AccountId>);
Upgradability
pub fn upgrade();
pub fn migrate() -> Self;