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

Fungible token fee #5

Merged
merged 3 commits into from
Dec 22, 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ jobs:
run: cargo build --all-features

- name: Run tests
run: cargo test --all -- --test-threads=1
run: cargo test --all
8 changes: 8 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 = [
"crates/crosschain-indexer-centralized",
"crates/oracle-contract",
"crates/reclaim-gpt-contract",
"crates/test-ft-contract",
]
resolver = "2"

Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ Some data producers may choose to charge a fee for requesting some data using th

`set_fee(fee: ProducerFee)`, ProducerFee is defined in [crates/oracle-contract/src/producer.rs](crates/oracle-contract/src/producer.rs).

The oracle contract uses prepaid balance to optimize the amount of receipts generated, reducing the latency, but may allow per-request pricing in
the future, if someone wants this feature added. To top up your balance, use these methods:
The oracle contract uses prepaid balance to optimize the amount of receipts generated, reducing the latency, and allows per-request pricing. To top up your balance, use these methods:

`deposit_near(account_id: Option<AccountId> default predecessor, producer_id: Option<AccountId> default all)`
`ft_transfer_call` on the token with msg `account_id: Option<AccountId> default predecessor, producer_id: Option<AccountId> default all`
`ft_transfer_call` on the token with msg `account_id: Option<AccountId> default sender, producer_id: Option<AccountId> default all`

Parameters:
- `account_id`: Account to top up. If not specified, defaults to the predecessor account, but you can top up someone else's balance in the contract
Expand Down
7 changes: 0 additions & 7 deletions crates/oracle-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ repository = "https://github.com/INTEARnear/oracle"
license = "MIT OR Apache-2.0"

[package.metadata.near.reproducible_build]
# docker image, descriptor of build environment
image = "sourcescan/cargo-near:0.12.2-rust-1.82.0"
# tag after colon above serves only descriptive purpose; image is identified by digest
image_digest = "sha256:5013a742e19a95c108bdfce085a57bda2f1047248e5eb9f005a8adc1ec8a1e42"
# build command inside of docker container
# if docker image from default gallery is used https://hub.docker.com/r/sourcescan/cargo-near/tags,
# the command may be any combination of flags of `cargo-near`,
# supported by respective version of binary inside the container besides `--no-locked` flag
container_build_command = ["cargo", "near", "build"]

[lib]
crate-type = ["cdylib", "rlib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
near-sdk = "5.5"
near-sdk-contract-tools = "3.0.2"
Expand Down
4 changes: 0 additions & 4 deletions crates/oracle-contract/rust-toolchain.toml

This file was deleted.

18 changes: 6 additions & 12 deletions crates/oracle-contract/src/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use near_sdk::{
env, json_types::U128, log, near, serde_json, AccountId, Gas, NearToken, Promise,
PromiseOrValue,
};
use near_sdk_contract_tools::ft::Nep141Receiver;
use near_sdk_contract_tools::ft::{ext_nep141, Nep141Receiver};

use crate::producer::ProducerId;
#[cfg(feature = "contract")]
Expand Down Expand Up @@ -161,17 +161,11 @@ impl Oracle {
amount = amount.0
);
}
// TODO replace with high-level call when not using git near-sdk
Promise::new(ft_id).function_call(
"ft_transfer".to_string(),
serde_json::to_vec(&serde_json::json!({
"amount": amount,
"receiver_id": account_id,
}))
.unwrap(),
NearToken::from_near(0),
Gas::from_tgas(10),
);

ext_nep141::ext(ft_id)
.with_static_gas(Gas::from_tgas(10))
.with_attached_deposit(NearToken::from_yoctonear(1))
.ft_transfer(account_id, amount, None);
}
}

Expand Down
172 changes: 146 additions & 26 deletions crates/oracle-contract/src/fees.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use near_sdk::{env, json_types::U128, near, NearToken, Promise};
use near_sdk::{env, json_types::U128, near, Gas, NearToken, Promise};
use near_sdk_contract_tools::ft::ext_nep141;

use crate::{balance::FtId, consumer::ConsumerId, producer::ProducerId};
#[cfg(feature = "contract")]
Expand All @@ -24,23 +25,30 @@ pub enum PrepaidFee {
None,
Near {
amount: NearToken,
payment_type: PaymentType,
payment_type: NearPaymentType,
},
FungibleToken {
token: FtId,
amount: U128,
payment_type: PaymentType,
payment_type: FtPaymentType,
},
}

#[near(serializers=[json, borsh])]
#[derive(Debug, Clone, PartialEq)]
pub enum PaymentType {
pub enum NearPaymentType {
ForSpecificProducer,
ForAllProducers,
AttachedToCall,
}

#[near(serializers=[json, borsh])]
#[derive(Debug, Clone, PartialEq)]
pub enum FtPaymentType {
ForSpecificProducer,
ForAllProducers,
}

#[cfg(feature = "contract")]
#[near]
impl Oracle {
Expand Down Expand Up @@ -77,7 +85,7 @@ impl Oracle {
if !env::attached_deposit().is_zero() {
return Some(PrepaidFee::Near {
amount: env::attached_deposit(),
payment_type: PaymentType::AttachedToCall,
payment_type: NearPaymentType::AttachedToCall,
});
}

Expand All @@ -94,7 +102,7 @@ impl Oracle {
}
return Some(PrepaidFee::Near {
amount: *prepaid_amount,
payment_type: PaymentType::ForSpecificProducer,
payment_type: NearPaymentType::ForSpecificProducer,
});
}
}
Expand All @@ -104,16 +112,56 @@ impl Oracle {
consumer.near_balance.checked_sub(*prepaid_amount).unwrap();
return Some(PrepaidFee::Near {
amount: *prepaid_amount,
payment_type: PaymentType::ForAllProducers,
payment_type: NearPaymentType::ForAllProducers,
});
}

None
}
ProducerFee::FungibleToken {
token: _,
prepaid_amount: _,
} => unimplemented!(),
token,
prepaid_amount,
} => {
let consumer = self
.consumers
.get_mut(consumer_id)
.expect("Consumer is not registered");

if let Some(ft_balance) = consumer
.ft_balances_producer
.get_mut(&(producer_id.clone(), token.clone()))
{
if *ft_balance >= *prepaid_amount {
*ft_balance = ft_balance.0.checked_sub(prepaid_amount.0).unwrap().into();
if ft_balance.0 == 0 {
consumer
.ft_balances_producer
.remove(&(producer_id.clone(), token.clone()));
}
return Some(PrepaidFee::FungibleToken {
token: token.clone(),
amount: *prepaid_amount,
payment_type: FtPaymentType::ForSpecificProducer,
});
}
}

if let Some(ft_balance) = consumer.ft_balances.get_mut(token) {
if *ft_balance >= *prepaid_amount {
*ft_balance = ft_balance.0.checked_sub(prepaid_amount.0).unwrap().into();
if ft_balance.0 == 0 {
consumer.ft_balances.remove(token);
}
return Some(PrepaidFee::FungibleToken {
token: token.clone(),
amount: *prepaid_amount,
payment_type: FtPaymentType::ForAllProducers,
});
}
}

None
}
}
}

Expand All @@ -130,7 +178,7 @@ impl Oracle {
amount: _,
payment_type,
} => match payment_type {
PaymentType::ForSpecificProducer => {
NearPaymentType::ForSpecificProducer => {
let consumer = self
.consumers
.get_mut(consumer_id)
Expand All @@ -148,7 +196,7 @@ impl Oracle {
);
}
}
PaymentType::ForAllProducers => {
NearPaymentType::ForAllProducers => {
let consumer = self
.consumers
.get_mut(consumer_id)
Expand All @@ -159,16 +207,46 @@ impl Oracle {
.checked_add(NearToken::from_yoctonear(refund_amount.0))
.unwrap();
}
PaymentType::AttachedToCall => {
NearPaymentType::AttachedToCall => {
Promise::new(consumer_id.clone())
.transfer(NearToken::from_yoctonear(refund_amount.0));
}
},
PrepaidFee::FungibleToken {
token: _,
token,
amount: _,
payment_type: _,
} => unimplemented!(),
payment_type,
} => match payment_type {
FtPaymentType::ForSpecificProducer => {
let consumer = self
.consumers
.get_mut(consumer_id)
.expect("Consumer is not registered");

if let Some(ft_balance) = consumer
.ft_balances_producer
.get_mut(&(producer_id.clone(), token.clone()))
{
*ft_balance = ft_balance.0.checked_add(refund_amount.0).unwrap().into();
} else {
consumer
.ft_balances_producer
.insert((producer_id.clone(), token.clone()), refund_amount);
}
}
FtPaymentType::ForAllProducers => {
let consumer = self
.consumers
.get_mut(consumer_id)
.expect("Consumer is not registered");

if let Some(ft_balance) = consumer.ft_balances.get_mut(token) {
*ft_balance = ft_balance.0.checked_add(refund_amount.0).unwrap().into();
} else {
consumer.ft_balances.insert(token.clone(), refund_amount);
}
}
},
}
}

Expand All @@ -190,7 +268,7 @@ impl Oracle {
.expect("Consumer is not registered");

match payment_type {
PaymentType::ForSpecificProducer => {
NearPaymentType::ForSpecificProducer => {
if let Some(near_balance) =
consumer.near_balance_producer.get_mut(producer_id)
{
Expand All @@ -201,19 +279,49 @@ impl Oracle {
.insert(producer_id.clone(), *amount);
}
}
PaymentType::ForAllProducers => {
NearPaymentType::ForAllProducers => {
consumer.near_balance = consumer.near_balance.checked_add(*amount).unwrap();
}
PaymentType::AttachedToCall => {
NearPaymentType::AttachedToCall => {
Promise::new(consumer_id.clone()).transfer(*amount);
}
}
}
PrepaidFee::FungibleToken {
token: _,
amount: _,
payment_type: _,
} => unimplemented!(),
token,
amount,
payment_type,
} => match payment_type {
FtPaymentType::ForSpecificProducer => {
let consumer = self
.consumers
.get_mut(consumer_id)
.expect("Consumer is not registered");

if let Some(ft_balance) = consumer
.ft_balances_producer
.get_mut(&(producer_id.clone(), token.clone()))
{
*ft_balance = ft_balance.0.checked_add(amount.0).unwrap().into();
} else {
consumer
.ft_balances_producer
.insert((producer_id.clone(), token.clone()), *amount);
}
}
FtPaymentType::ForAllProducers => {
let consumer = self
.consumers
.get_mut(consumer_id)
.expect("Consumer is not registered");

if let Some(ft_balance) = consumer.ft_balances.get_mut(token) {
*ft_balance = ft_balance.0.checked_add(amount.0).unwrap().into();
} else {
consumer.ft_balances.insert(token.clone(), *amount);
}
}
},
}
}

Expand All @@ -238,10 +346,22 @@ impl Oracle {
}
}
PrepaidFee::FungibleToken {
token: _,
amount: _,
token,
amount,
payment_type: _,
} => unimplemented!(),
} => {
if let Some(deposit_amount) =
amount.0.checked_sub(refund_amount.unwrap_or(U128(0)).0)
{
// TODO handle when the token is not registered in producer's account
ext_nep141::ext(token.clone())
.with_static_gas(Gas::from_tgas(10))
.with_attached_deposit(NearToken::from_yoctonear(1))
.ft_transfer(producer_id.clone(), deposit_amount.into(), None);
} else {
env::panic_str("Refund amount is greater than prepaid amount")
}
}
}
}
}
Loading
Loading