Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add slend token #9

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"apps/oracle",
# Contracts (types)
"contracts/market",
"contracts/src-20",
"contracts/pyth-mock",
"contracts/token",
# Libs
Expand Down
13 changes: 13 additions & 0 deletions Forc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ dependencies = [
"std",
]

[[package]]
name = "src-20"
source = "member"
dependencies = [
"standards git+https://github.com/FuelLabs/sway-standards?tag=v0.5.2#270350e69bd7455b7e99f0aae2e29a94d42324bd",
"std",
]

[[package]]
name = "standards"
source = "git+https://github.com/FuelLabs/sway-standards?tag=v0.4.4#a001d3c248595112aae67e5633a06ef9bc0536ae"
Expand All @@ -57,6 +65,11 @@ name = "standards"
source = "git+https://github.com/FuelLabs/sway-standards?tag=v0.5.1#e2d5ac40a1d11a9e38e0a662d35141076515319f"
dependencies = ["std"]

[[package]]
name = "standards"
source = "git+https://github.com/FuelLabs/sway-standards?tag=v0.5.2#270350e69bd7455b7e99f0aae2e29a94d42324bd"
dependencies = ["std"]

[[package]]
name = "std"
source = "git+https://github.com/fuellabs/sway?tag=v0.62.0#efda0397c7bee77de73bd726ec0b732d57614973"
Expand Down
1 change: 1 addition & 0 deletions Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"abis/market_abi",
# Contracts
"contracts/market",
"contracts/src-20",
"contracts/token",
"contracts/test",
"contracts/pyth-mock",
Expand Down
16 changes: 16 additions & 0 deletions contracts/src-20/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "src-20"
description = "A cargo-generate template for Rust + Sway integration testing."
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"

[[bin]]
name = "deploy"
path = "scripts/deploy.rs"

[dependencies]
fuels = { workspace = true }
dotenv = { workspace = true }
tokio = { workspace = true }
rand = { workspace = true }
7 changes: 7 additions & 0 deletions contracts/src-20/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
entry = "main.sw"
license = "Apache-2.0"
name = "src-20"

[dependencies]
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.5.2" }
75 changes: 75 additions & 0 deletions contracts/src-20/scripts/deploy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use dotenv::dotenv;
use fuels::{
accounts::{provider::Provider, wallet::WalletUnlocked},
macros::abigen,
programs::contract::{Contract, LoadConfiguration},
types::{transaction::TxPolicies, transaction_builders::VariableOutputPolicy, AssetId},
};
use rand::Rng;
use std::{error::Error, path::PathBuf};

abigen!(Contract(
name = "Token",
abi = "contracts/src-20/out/release/src-20-abi.json",
));

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
dotenv().ok();

// setup fuel provider
let rpc = std::env::var("RPC").unwrap();
let provider = Provider::connect(rpc).await.unwrap();

// setup wallet
let secret = std::env::var("SECRET").unwrap();
let wallet =
WalletUnlocked::new_from_private_key(secret.parse().unwrap(), Some(provider.clone()));

// deploy token
let configurables = TokenConfigurables::default();
let root = PathBuf::from(env!("CARGO_WORKSPACE_DIR"));
let bin_path = root.join("contracts/src-20/out/release/src-20.bin");
let config = LoadConfiguration::default().with_configurables(configurables);

let mut rng = rand::thread_rng();
let salt = rng.gen::<[u8; 32]>();

let id = Contract::load_from(bin_path, config)?
.with_salt(salt)
.deploy(&wallet, TxPolicies::default())
.await?;
let instance = Token::new(id.clone(), wallet.clone());
instance
.methods()
.constructor(wallet.address().into())
.call()
.await?;

println!("Token deployed at: 0x{}", instance.contract_id().hash());

let asset_id = instance.methods().asset_id().simulate().await?.value;
println!("Asset id: 0x{}", asset_id);

// mint the whole supply to the deployer
let sub_id = AssetId::default();

let max_supply = instance
.methods()
.max_supply(asset_id)
.simulate()
.await?
.value
.unwrap();

instance
.methods()
.mint(wallet.address().into(), sub_id.into(), max_supply)
.with_variable_output_policy(VariableOutputPolicy::Exactly(1))
.call()
.await?;

println!("Minted {} tokens to {}", max_supply, wallet.address());

Ok(())
}
143 changes: 143 additions & 0 deletions contracts/src-20/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// ERC20 equivalent in Sway.
contract;

use standards::src3::SRC3;
use standards::src5::{SRC5, State, AccessError};
use standards::src20::SRC20;
use std::{
asset::{
burn,
mint_to,
},
call_frames::msg_asset_id,
constants::DEFAULT_SUB_ID,
context::msg_amount,
string::String,
};

configurable {
DECIMALS: u8 = 9u8,
NAME: str[8] = __to_str_array("SwayLend"),
SYMBOL: str[5] = __to_str_array("SLEND"),
MAX_SUPPLY: u64 = 1_000_000_000_000_000_000u64, // 1 billion
}

storage {
total_supply: u64 = 0,
owner: State = State::Uninitialized,
}

abi SingleAsset {
#[storage(read, write)]
fn constructor(owner_: Identity);

fn max_supply(asset: AssetId) -> Option<u64>;

fn asset_id() -> AssetId;
}

impl SingleAsset for Contract {
#[storage(read, write)]
fn constructor(owner_: Identity) {
require(storage.owner.read() == State::Uninitialized, "owner-initialized");
storage.owner.write(State::Initialized(owner_));
}

fn max_supply(asset: AssetId) -> Option<u64> {
if asset == AssetId::default() {
Some(MAX_SUPPLY)
} else {
None
}
}

fn asset_id() -> AssetId {
AssetId::default()
}
}

// Native Asset Standard
impl SRC20 for Contract {
#[storage(read)]
fn total_assets() -> u64 {
1_u64
}

#[storage(read)]
fn total_supply(asset: AssetId) -> Option<u64> {
if asset == AssetId::default() {
Some(storage.total_supply.read())
} else {
None
}
}

#[storage(read)]
fn name(asset: AssetId) -> Option<String> {
if asset == AssetId::default() {
Some(String::from_ascii_str(from_str_array(NAME)))
} else {
None
}
}

#[storage(read)]
fn symbol(asset: AssetId) -> Option<String> {
if asset == AssetId::default() {
Some(String::from_ascii_str(from_str_array(SYMBOL)))
} else {
None
}
}

#[storage(read)]
fn decimals(asset: AssetId) -> Option<u8> {
if asset == AssetId::default() {
Some(DECIMALS)
} else {
None
}
}
}

// Ownership Standard
impl SRC5 for Contract {
#[storage(read)]
fn owner() -> State {
storage.owner.read()
}
}

// Mint and Burn Standard
impl SRC3 for Contract {
#[storage(read, write)]
fn mint(recipient: Identity, sub_id: SubId, amount: u64) {
require(sub_id == DEFAULT_SUB_ID, "incorrect-sub-id");
require(
storage.owner.read() == State::Initialized(msg_sender().unwrap()),
AccessError::NotOwner,
);
require(storage.total_supply.read() + amount <= MAX_SUPPLY, "max-supply-reached");

storage
.total_supply
.write(amount + storage.total_supply.read());
mint_to(recipient, DEFAULT_SUB_ID, amount);
}

#[payable]
#[storage(read, write)]
fn burn(sub_id: SubId, amount: u64) {
require(sub_id == DEFAULT_SUB_ID, "incorrect-sub-id");
require(msg_amount() >= amount, "incorrect-amount-provided");
require(
msg_asset_id() == AssetId::default(),
"incorrect-asset-provided",
);

storage
.total_supply
.write(storage.total_supply.read() - amount);
burn(DEFAULT_SUB_ID, amount);
}
}