Skip to content

Commit 5f8bce6

Browse files
author
d.kotwal
committed
fleet vehicle lending at the road incident site
1 parent 1c7e0eb commit 5f8bce6

File tree

256 files changed

+57928
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

256 files changed

+57928
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "fleet-lending"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.8.0" }
8+
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.8.0" }
9+
10+
[dev-dependencies]
11+
transaction = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.8.0" }
12+
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.8.0" }
13+
scrypto-unit = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.8.0" }
14+
15+
[profile.release]
16+
opt-level = 's' # Optimize for size.
17+
lto = true # Enable Link Time Optimization.
18+
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
19+
panic = 'abort' # Abort on panic.
20+
strip = "debuginfo" # Strip debug info.
21+
overflow-checks = true # Panic in the case of an overflow.
22+
23+
[lib]
24+
crate-type = ["cdylib", "lib"]
25+
26+
[workspace]
27+
# Set the package crate as its own empty workspace, to hide it from any potential ancestor workspace
28+
# Remove this [workspace] section if you intend the package to be part of a Cargo workspace
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Fleet Lending
2+
3+
Effective design, engineering and delivery of blockchain technology aided solution using Scrypto on Radix for citizens to lend vehicles or fleet services in case of road incidents.
4+
5+
Planning of fleet deployments or vehicles by using analytics managed in a decemtralized manner at the command and control center.
6+
7+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Emergency Loans for Citizens requiring Immediate Financial Assistance due to Incidents
2+
3+
We are developing the module by extending the Flash Loans example.
4+
5+
How to test:
6+
7+
## build flashloan component
8+
9+
1. `resim reset`
10+
1. `export xrd=030000000000000000000000000000000000000000000000000004`
11+
1. `cd flashloan`
12+
1. `scrypto build`
13+
1. `resim publish .` -> save package id into $fl_package
14+
1. `resim new-account` -> save address into $acc1 and private key into $priv1
15+
16+
Create the flashloan component with 900 000 XRD of funds and a 5% interest
17+
1. `resim call-function $fl_package FlashLoan new 900000,$xrd 5` -> save component address into $flashloan
18+
19+
## build caller component
20+
21+
1. `cd ../caller`
22+
1. `scrypto build`
23+
1. `resim publish .` -> save package id into $caller_package
24+
1. `resim call-function $caller_package Caller new $flashloan 10000,$xrd` -> save component address into $caller
25+
26+
## ask for a flashloan
27+
1. `resim call-method $caller call $caller` - We have to send the address of the caller component since we cannot yet get it from the Context
28+
29+
## Testing what happend when not returning fully owed amount
30+
Call the same command again, this time, the opportunity bucket contains 0 XRD so you are not able to pay for the loan !
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "caller"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.4.1" }
8+
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.4.1" }
9+
10+
[dev-dependencies]
11+
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.4.1" }
12+
13+
[profile.release]
14+
opt-level = 's' # Optimize for size.
15+
lto = true # Enable Link Time Optimization.
16+
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
17+
panic = 'abort' # Abort on panic.
18+
19+
[lib]
20+
crate-type = ["cdylib"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use scrypto::prelude::*;
2+
3+
/*
4+
* This is an example of a component that is requesting a
5+
* flash loan from another component to make more XRD and repay the loan
6+
*/
7+
8+
blueprint! {
9+
struct Caller {
10+
// Address of the flashloan component
11+
loaner_component: ComponentAddress,
12+
// Used to store payment for the opportunity
13+
hidden_vault: Vault,
14+
// Used to represent tokens that can be accessed if the user gets the loan
15+
opportunity: Vault
16+
}
17+
18+
impl Caller {
19+
pub fn new(loaner_component_address: ComponentAddress, opportunity: Bucket) -> ComponentAddress {
20+
assert!(opportunity.amount() > dec!("1000"), "The amount of the opportunity must be bigger than 1000");
21+
assert_eq!(opportunity.resource_address(), RADIX_TOKEN, "The tokens for the opportunity must be XRD");
22+
23+
Self {
24+
loaner_component: loaner_component_address,
25+
hidden_vault: Vault::new(RADIX_TOKEN),
26+
opportunity: Vault::with_bucket(opportunity)
27+
}
28+
.instantiate()
29+
.globalize()
30+
}
31+
32+
pub fn call(&self, this_component_address: ComponentAddress) -> Bucket {
33+
// Get a loan of 1000 XRD
34+
let amount:Decimal = dec!("1000");
35+
36+
// Call request loan and return the change back to the user
37+
borrow_component!(self.loaner_component).call::<Bucket>("request_loan", args![
38+
amount,
39+
this_component_address
40+
]).into()
41+
}
42+
43+
// Used to simulate an opportunity to make some XRD by paying 1000 XRD
44+
fn get_opportunity(&mut self, payment: Bucket) -> Bucket {
45+
assert!(payment.amount() >= 1000.into(), "You need to pay 1000 XRD !");
46+
self.hidden_vault.put(payment);
47+
48+
self.opportunity.take_all()
49+
}
50+
51+
pub fn execute(&mut self, loan: Bucket) -> Bucket {
52+
info!("Received {} from loaner !", loan.amount());
53+
info!("Found opportunity to make {} XRD !", self.opportunity.amount());
54+
55+
let opportunity_result = self.get_opportunity(loan);
56+
57+
// Repay the loan (you get the change back)
58+
opportunity_result
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "flashloan"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.4.1" }
8+
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.4.1" }
9+
10+
[dev-dependencies]
11+
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.4.1" }
12+
13+
[profile.release]
14+
opt-level = 's' # Optimize for size.
15+
lto = true # Enable Link Time Optimization.
16+
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
17+
panic = 'abort' # Abort on panic.
18+
19+
[lib]
20+
crate-type = ["cdylib"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use scrypto::prelude::*;
2+
3+
blueprint! {
4+
struct FlashLoan {
5+
interest: Decimal,
6+
vault: Vault
7+
}
8+
9+
impl FlashLoan {
10+
/*
11+
* Generate a FlashLoan component with a specified interest and a specified bucket as funds
12+
*/
13+
pub fn new(tokens: Bucket, interest: Decimal) -> ComponentAddress {
14+
Self {
15+
vault: Vault::with_bucket(tokens),
16+
interest: interest,
17+
}
18+
.instantiate()
19+
.globalize()
20+
}
21+
22+
pub fn request_loan(&mut self, amount: Decimal, component_address: ComponentAddress) -> Bucket {
23+
assert!(amount < self.vault.amount(), "Not enough funds to loan");
24+
25+
// Call the execute method at the specified component's address with the requested funds
26+
let args = args![self.vault.take(amount)];
27+
let mut returned_bucket: Bucket = borrow_component!(component_address).call::<Bucket>("execute", args).into();
28+
29+
// Make sure they repaid in loan in full
30+
let amount_to_take = amount * ((self.interest / 100) + 1);
31+
assert!(returned_bucket.amount() >= amount_to_take, "You have to return more than {}", amount_to_take);
32+
33+
self.vault.put(returned_bucket.take(amount_to_take));
34+
35+
// Return the change back to the component
36+
return returned_bucket;
37+
}
38+
}
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "donations"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.4.1" }
8+
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.4.1" }
9+
10+
[dev-dependencies]
11+
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.4.1" }
12+
13+
[profile.release]
14+
opt-level = 's' # Optimize for size.
15+
lto = true # Enable Link Time Optimization.
16+
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
17+
panic = 'abort' # Abort on panic.
18+
19+
[lib]
20+
crate-type = ["cdylib", "lib"]

0 commit comments

Comments
 (0)