Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
a4e08a6
Support omni-bridge token wrapping
BonelessImpl Mar 20, 2025
78801c2
Appease clippy
BonelessImpl Mar 20, 2025
b4d41e8
Minor
BonelessImpl Mar 20, 2025
37b5564
Use a separate parser for the prefix
BonelessImpl Mar 23, 2025
ae067ff
Minor
BonelessImpl Mar 23, 2025
24e10a7
Minor
BonelessImpl Mar 23, 2025
28a0d3c
Some improvements and initial impl of WrappedToken
BonelessImpl Mar 24, 2025
d79226b
Minor corner cases handled
BonelessImpl Mar 24, 2025
ddee05b
Fix unwrap function
BonelessImpl Mar 24, 2025
40a4b53
Minor
BonelessImpl Mar 24, 2025
e29b033
Remove some unnecessary impls
BonelessImpl Mar 24, 2025
44dad0b
Remove wtoken and add improvements to PoA token
BonelessImpl Mar 25, 2025
2088291
Minor fixes
BonelessImpl Mar 25, 2025
b2b358b
Updates based on comments + remove the independent parser
BonelessImpl Mar 26, 2025
0662811
Use an enum for the contract state
BonelessImpl Mar 26, 2025
6f7f3cd
Establish testing for PoA token contract
BonelessImpl Mar 27, 2025
88740f8
Fix env var for poa token
BonelessImpl Mar 27, 2025
903649c
Complete simpler transfer test
BonelessImpl Mar 27, 2025
4decb11
Create a dir for PoA token tests
BonelessImpl Mar 27, 2025
f132cdb
Rename simple_transfer to transfer
BonelessImpl Mar 27, 2025
081b3e4
Add deposit test after wrapping
BonelessImpl Mar 27, 2025
d9b39c4
Implement syncing metadata for the wrapped token
BonelessImpl Mar 28, 2025
d8d747c
Minor fix
BonelessImpl Mar 28, 2025
2203aa7
Add tests for metadata syncing
BonelessImpl Mar 28, 2025
7877d82
Fix metadata repeated setting
BonelessImpl Mar 28, 2025
51f34f8
Fix metadata function, add attached deposit check for it, and a few o…
BonelessImpl Apr 2, 2025
92535b7
Establish ft_transfer_call tests for PoA token
BonelessImpl Apr 2, 2025
0b83884
Minor
BonelessImpl Apr 3, 2025
492c9b3
Return promise + minor fixes
BonelessImpl Apr 3, 2025
0fff004
Finish ft_transfer_call tests
BonelessImpl Apr 3, 2025
951027d
Add tests for upgrading unversioned contract to versioned
BonelessImpl Apr 4, 2025
e52f965
Fix clippy issue with wasm file not built
BonelessImpl Apr 4, 2025
94ce4a1
Many fixes from PR comments
BonelessImpl Apr 4, 2025
5f940fb
Updating parsing algo
BonelessImpl Apr 4, 2025
941ea1c
Require instead of if
BonelessImpl Apr 4, 2025
ef37507
Add storage handling when setting the wrapped token in PoA token cont…
BonelessImpl Apr 7, 2025
43340b9
Remove unnecessary artifacts - defuse
BonelessImpl Apr 7, 2025
6ee154d
Enforce pausing/unpausing before being able to wrap a token with the …
BonelessImpl Apr 7, 2025
19f965a
Fix wrapping issue by using Lazy to store the token.
BonelessImpl Apr 7, 2025
1c5cec3
Add test for insufficient attached deposit when wrapping a token
BonelessImpl Apr 8, 2025
05e500c
Use custom, simplified logic for pausing, instead of Pausable trait
BonelessImpl Apr 8, 2025
9b96252
Implement ControllerUpgradable trait for PoA token
BonelessImpl Apr 8, 2025
d31ab3c
Implement ControllerUpgradable for PoA factor + some refactoring
BonelessImpl Apr 8, 2025
4e9cfec
Rename old-artifacts to unversioned-artifacts
BonelessImpl Apr 8, 2025
13ecc94
Remove deploy vanilla contract from FtExt trait
BonelessImpl Apr 8, 2025
0a9f37c
Use FtExt more
BonelessImpl Apr 8, 2025
3bf0184
Move TestLog outside and use it in ft_transfer in FtExt trait
BonelessImpl Apr 8, 2025
98c4a70
Minor
BonelessImpl Apr 8, 2025
e653f9b
Minor
BonelessImpl Apr 8, 2025
2ef1b0a
Remove unnecessary args
BonelessImpl Apr 9, 2025
d686a9d
Add is_locked() in the PoA token contract and test for it
BonelessImpl Apr 9, 2025
898dedf
Minor
BonelessImpl Apr 9, 2025
d550d68
Add upgrade function to PoA factory and refactor some tests
BonelessImpl Apr 9, 2025
16bf71b
More refactoring of PoA factory tests
BonelessImpl Apr 9, 2025
72d8a9c
Make some asserts more specific to errors
BonelessImpl Apr 9, 2025
efab06f
Minor var rename
BonelessImpl Apr 9, 2025
df3b3b9
Correct artifacts dir name
BonelessImpl Apr 9, 2025
624c2b6
Added some unused tests for poa factory
BonelessImpl Apr 9, 2025
6f137da
Add last tests for ft_transfer_call with special messages
BonelessImpl Apr 9, 2025
3678f4e
Revert "Added some unused tests for poa factory"
BonelessImpl Apr 9, 2025
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
3 changes: 3 additions & 0 deletions Cargo.lock

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

21 changes: 19 additions & 2 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ skip_core_tasks = true
[env]
TARGET_DIR = "${PWD}/res"
POA_TOKEN_WASM = "${TARGET_DIR}/defuse_poa_token.wasm"
POA_TOKEN_WITH_DEPOSIT_DIR = "${TARGET_DIR}/poa-token-with-deposit"

[tasks.default]
alias = "build"

[tasks.clippy]
dependencies = ["add-cache-dir-tag", "build-poa-token"]
dependencies = ["add-cache-dir-tag"]
command = "cargo"
args = ["clippy", "--workspace", "--all-targets", "--no-deps"]

Expand All @@ -34,7 +35,7 @@ args = [
]

[tasks.build-poa-factory]
dependencies = ["add-cache-dir-tag", "build-poa-token"]
dependencies = ["add-cache-dir-tag", "build-poa-token", "build-poa-token-with-deposits"]
command = "cargo"
args = [
"near",
Expand Down Expand Up @@ -65,6 +66,22 @@ args = [
"--no-embed-abi",
]

[tasks.build-poa-token-with-deposits]
dependencies = ["add-cache-dir-tag"]
command = "cargo"
args = [
"near",
"build",
"non-reproducible-wasm",
"--manifest-path",
"./poa-token/Cargo.toml",
"--features",
"contract,deposits",
"--out-dir",
"${POA_TOKEN_WITH_DEPOSIT_DIR}",
"--no-embed-abi",
]

[tasks.test]
alias = "tests"

Expand Down
5 changes: 5 additions & 0 deletions near-utils/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ impl<T> Lock<T> {
self.locked = false;
&mut self.value
}

#[inline]
pub fn ignore_lock(&self) -> &T {
&self.value
}
}

impl<T> From<T> for Lock<T> {
Expand Down
12 changes: 12 additions & 0 deletions near-utils/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ impl<T> UnwrapOrPanic<T> for Option<T> {
}
}

pub trait UnwrapOrPanicCtx<T> {
fn unwrap_or_panic_ctx(self, ctx: &str) -> T;
}

impl<T> UnwrapOrPanicCtx<T> for Option<T> {
#[inline]
#[track_caller]
fn unwrap_or_panic_ctx(self, ctx: &str) -> T {
self.unwrap_or_else(|| env::panic_str(ctx))
}
}

impl<T, E> UnwrapOrPanic<T> for Result<T, E>
where
E: FunctionError,
Expand Down
1 change: 1 addition & 0 deletions poa-factory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ workspace = true

[dependencies]
defuse-admin-utils.workspace = true
defuse-controller.workspace = true
defuse-near-utils.workspace = true
defuse-poa-token.workspace = true

Expand Down
67 changes: 65 additions & 2 deletions poa-factory/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use core::iter;
use std::collections::{HashMap, HashSet};

use defuse_admin_utils::full_access_keys::FullAccessKeys;
use defuse_near_utils::{CURRENT_ACCOUNT_ID, UnwrapOrPanicError, gas_left};
use defuse_controller::ControllerUpgradable;
use defuse_near_utils::{CURRENT_ACCOUNT_ID, UnwrapOrPanicError, gas_left, method_name};
use defuse_poa_token::ext_poa_fungible_token;
use near_contract_standards::fungible_token::{core::ext_ft_core, metadata::FungibleTokenMetadata};
use near_plugins::{
Expand All @@ -21,6 +22,9 @@ use near_sdk::{

use crate::PoaFactory;

const STATE_MIGRATE_FUNCTION: &str = method_name!(Contract::state_migrate);
const STATE_MIGRATE_DEFAULT_GAS: Gas = Gas::from_tgas(5);

#[cfg(not(clippy))]
const POA_TOKEN_WASM: &[u8] = include_bytes!(std::env!(
"POA_TOKEN_WASM",
Expand All @@ -29,12 +33,14 @@ const POA_TOKEN_WASM: &[u8] = include_bytes!(std::env!(
#[cfg(clippy)]
const POA_TOKEN_WASM: &[u8] = b"";

const POA_TOKEN_INIT_BALANCE: NearToken = NearToken::from_near(3);
const POA_TOKEN_INIT_BALANCE: NearToken = NearToken::from_near(5);
const POA_TOKEN_NEW_GAS: Gas = Gas::from_tgas(10);
const POA_TOKEN_FT_DEPOSIT_GAS: Gas = Gas::from_tgas(10);
/// Copied from `near_contract_standards::fungible_token::core_impl::GAS_FOR_FT_TRANSFER_CALL`
const POA_TOKEN_FT_TRANSFER_CALL_MIN_GAS: Gas = Gas::from_tgas(30);

const POA_TOKEN_UPRADE_GAS: Gas = Gas::from_tgas(10);

#[derive(AccessControlRole, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[near(serializers = [json])]
pub enum Role {
Expand Down Expand Up @@ -92,6 +98,39 @@ impl Contract {
);
contract
}

#[pause]
#[access_control_any(roles(Role::DAO, Role::TokenDeployer))]
#[payable]
pub fn upgrade_token(&mut self, token: String, self_public_key: PublicKey) -> Promise {
// FIXME: Check what conditions for atomicity, safety and repeatability are needed
Promise::new(Self::token_id(token))
.function_call(
"add_full_access_key".to_string(),
serde_json::to_vec(&json!({
"public_key": self_public_key,
}))
.unwrap_or_panic_display(),
NearToken::from_near(0),
POA_TOKEN_UPRADE_GAS,
)
.deploy_contract(POA_TOKEN_WASM.to_vec())
.function_call(
"upgrade_to_versioned".to_string(),
serde_json::to_vec(&json!({})).unwrap_or_panic_display(),
NearToken::from_yoctonear(1), // FIXME: amount
POA_TOKEN_UPRADE_GAS,
)
.function_call(
"delete_key".to_string(),
serde_json::to_vec(&json!({
"public_key": self_public_key,
}))
.unwrap_or_panic_display(),
NearToken::from_near(0),
POA_TOKEN_UPRADE_GAS,
)
}
}

#[near]
Expand Down Expand Up @@ -225,6 +264,30 @@ impl FullAccessKeys for Contract {
}
}

#[near]
impl ControllerUpgradable for Contract {
#[access_control_any(roles(Role::DAO, Role::TokenDeployer))]
#[payable]
fn upgrade(
&mut self,
#[serializer(borsh)] code: Vec<u8>,
#[serializer(borsh)] state_migration_gas: Option<Gas>,
) -> Promise {
assert_one_yocto();
Promise::new(CURRENT_ACCOUNT_ID.clone())
.deploy_contract(code)
.function_call(
STATE_MIGRATE_FUNCTION.into(),
Vec::new(),
NearToken::from_yoctonear(0),
state_migration_gas.unwrap_or(STATE_MIGRATE_DEFAULT_GAS),
)
}

#[private]
fn state_migrate(&mut self) {}
}

#[derive(BorshSerialize, BorshStorageKey)]
#[borsh(crate = "::near_sdk::borsh")]
enum Prefix {
Expand Down
3 changes: 2 additions & 1 deletion poa-factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ pub mod contract;
use std::collections::HashMap;

use defuse_admin_utils::full_access_keys::FullAccessKeys;
use defuse_controller::ControllerUpgradable;
use near_contract_standards::fungible_token::metadata::FungibleTokenMetadata;
use near_plugins::AccessControllable;
use near_sdk::{AccountId, Promise, ext_contract, json_types::U128};

#[ext_contract(ext_poa_factory)]
pub trait PoaFactory: AccessControllable + FullAccessKeys {
pub trait PoaFactory: AccessControllable + FullAccessKeys + ControllerUpgradable {
/// Deploys new token to `token.<CURRENT_ACCOUNT_ID>`.
/// Requires to attach enough Ⓝ to cover storage costs.
fn deploy_token(&mut self, token: String, metadata: Option<FungibleTokenMetadata>) -> Promise;
Expand Down
2 changes: 2 additions & 0 deletions poa-token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ workspace = true

[dependencies]
defuse-admin-utils.workspace = true
defuse-controller.workspace = true
defuse-near-utils.workspace = true

near-contract-standards.workspace = true
Expand All @@ -19,3 +20,4 @@ near-sdk = { workspace = true, features = ["unstable"] }

[features]
contract = []
deposits = []
Loading
Loading