Skip to content

Commit

Permalink
Add Loan initialization with collateral deposit and borrow from pool
Browse files Browse the repository at this point in the history
  • Loading branch information
Teolhyn committed Aug 8, 2024
1 parent b6499da commit bfc192a
Show file tree
Hide file tree
Showing 39 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion contracts/factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Deployer {
let token_contract_raw = Val::try_from_val(&env, &token_contract).unwrap();

// Construct the init_args
let init_args = vec![&env, token_wasm_hash_raw, token_contract_raw];
let init_args: soroban_sdk::Vec<Val> = vec![&env, token_wasm_hash_raw, token_contract_raw];

// Deploy the contract using the uploaded Wasm with given hash.
let deployed_address = env
Expand Down
26 changes: 24 additions & 2 deletions contracts/loans/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::storage_types::{self, extend_instance, LoansDataKey, INSTANCE_BUMP_AM
use crate::positions;

use soroban_sdk::{
contract, contractimpl, contractmeta, Address, BytesN, ConversionError, Env, IntoVal, TryFromVal, Val
contract, contractimpl, vec, Address, BytesN, ConversionError, Env, IntoVal, Symbol, TryFromVal, Val
};

pub trait LoansTrait {
Expand All @@ -16,6 +16,28 @@ struct LoansContract;
#[contractimpl]
impl LoansTrait for LoansContract {
fn initialize(e: Env, user: Address, borrowed: i128, borrowed_from: Address, collateral: i128, collateral_from: Address) {
positions::init_loan(&e, user, borrowed, borrowed_from, collateral, collateral_from);
// Require user authorization
user.require_auth();

// First thing should be to check if the collateral is enough => health factor large enough
// Create args for collateral deposit
let user_val: Val = Val::try_from_val(&e, &user).unwrap();
let collateral_val: Val = Val::try_from_val(&e, &collateral).unwrap();
let args: soroban_sdk::Vec<Val> = vec![&e, user_val, collateral_val];
// Function to be called
let func: Symbol = Symbol::new(&e, "deposit_collateral");
// Deposit collateral
let deposited_collateral: i128 = e.invoke_contract(&collateral_from, &func, args);

// Create args for borrow
let borrowed_val: Val = Val::try_from_val(&e, &borrowed).unwrap();
let args_borrow: soroban_sdk::Vec<Val> = vec![&e, user_val.clone(), borrowed_val];
// Function to be called
let func2: Symbol = Symbol::new(&e, "borrow");
// Borrow the funds
let borrowed_funds: i128 = e.invoke_contract(&borrowed_from, &func2, args_borrow);

// FIXME: Currently one can call initialize multiple times to change same addresses loan
positions::init_loan(&e, user, borrowed_funds, borrowed_from, deposited_collateral, collateral_from);
}
}
4 changes: 2 additions & 2 deletions packages/factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This library was automatically generated by Soroban CLI using a command similar
soroban contract bindings ts \
--rpc-url https://soroban-testnet.stellar.org:443 \
--network-passphrase "Test SDF Network ; September 2015" \
--contract-id CBF6IG2DMMGFGH7Q4AG45TDW3427D2C6XHKXRQKPHLSMAKQPT4P3QY3F \
--contract-id CDXRZ3XGTSSJVLTTVNFV7QA5JVVGLP7PXLE4C4AVF2MPZUGLKL5Y4L6X \
--output-dir ./path/to/factory
```

Expand All @@ -30,7 +30,7 @@ However, we've actually encountered [frustration](https://github.com/stellar/sor

```json
"scripts": {
"postinstall": "soroban contract bindings ts --rpc-url https://soroban-testnet.stellar.org:443 --network-passphrase \"Test SDF Network ; September 2015\" --id CBF6IG2DMMGFGH7Q4AG45TDW3427D2C6XHKXRQKPHLSMAKQPT4P3QY3F --name factory"
"postinstall": "soroban contract bindings ts --rpc-url https://soroban-testnet.stellar.org:443 --network-passphrase \"Test SDF Network ; September 2015\" --id CDXRZ3XGTSSJVLTTVNFV7QA5JVVGLP7PXLE4C4AVF2MPZUGLKL5Y4L6X --name factory"
}
```

Expand Down
2 changes: 1 addition & 1 deletion packages/factory/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export * as rpc from '@stellar/stellar-sdk/rpc';
export declare const networks: {
readonly testnet: {
readonly networkPassphrase: "Test SDF Network ; September 2015";
readonly contractId: "CBF6IG2DMMGFGH7Q4AG45TDW3427D2C6XHKXRQKPHLSMAKQPT4P3QY3F";
readonly contractId: "CDXRZ3XGTSSJVLTTVNFV7QA5JVVGLP7PXLE4C4AVF2MPZUGLKL5Y4L6X";
};
};
export declare const Errors: {};
Expand Down
2 changes: 1 addition & 1 deletion packages/factory/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (typeof window !== 'undefined') {
export const networks = {
testnet: {
networkPassphrase: "Test SDF Network ; September 2015",
contractId: "CBF6IG2DMMGFGH7Q4AG45TDW3427D2C6XHKXRQKPHLSMAKQPT4P3QY3F",
contractId: "CDXRZ3XGTSSJVLTTVNFV7QA5JVVGLP7PXLE4C4AVF2MPZUGLKL5Y4L6X",
}
};
export const Errors = {};
Expand Down
2 changes: 1 addition & 1 deletion packages/factory/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if (typeof window !== 'undefined') {
export const networks = {
testnet: {
networkPassphrase: "Test SDF Network ; September 2015",
contractId: "CBF6IG2DMMGFGH7Q4AG45TDW3427D2C6XHKXRQKPHLSMAKQPT4P3QY3F",
contractId: "CDXRZ3XGTSSJVLTTVNFV7QA5JVVGLP7PXLE4C4AVF2MPZUGLKL5Y4L6X",
}
} as const

Expand Down
4 changes: 2 additions & 2 deletions packages/loan_pool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This library was automatically generated by Soroban CLI using a command similar
soroban contract bindings ts \
--rpc-url https://soroban-testnet.stellar.org:443 \
--network-passphrase "Test SDF Network ; September 2015" \
--contract-id CDW6ATMFVGFMQNWQODMIOBNW3LVWDBPQZJ3MMXNH4AKGC7JFOHAJQIRV \
--contract-id CBN57ZMM4F6AFKCBISNIMNFKIWGRNP4AOGQ3MTJKVZPVB5FHVTUMKQ2D \
--output-dir ./path/to/loan_pool
```

Expand All @@ -30,7 +30,7 @@ However, we've actually encountered [frustration](https://github.com/stellar/sor

```json
"scripts": {
"postinstall": "soroban contract bindings ts --rpc-url https://soroban-testnet.stellar.org:443 --network-passphrase \"Test SDF Network ; September 2015\" --id CDW6ATMFVGFMQNWQODMIOBNW3LVWDBPQZJ3MMXNH4AKGC7JFOHAJQIRV --name loan_pool"
"postinstall": "soroban contract bindings ts --rpc-url https://soroban-testnet.stellar.org:443 --network-passphrase \"Test SDF Network ; September 2015\" --id CBN57ZMM4F6AFKCBISNIMNFKIWGRNP4AOGQ3MTJKVZPVB5FHVTUMKQ2D --name loan_pool"
}
```

Expand Down
2 changes: 1 addition & 1 deletion packages/loan_pool/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export * as rpc from '@stellar/stellar-sdk/rpc';
export declare const networks: {
readonly testnet: {
readonly networkPassphrase: "Test SDF Network ; September 2015";
readonly contractId: "CDW6ATMFVGFMQNWQODMIOBNW3LVWDBPQZJ3MMXNH4AKGC7JFOHAJQIRV";
readonly contractId: "CBN57ZMM4F6AFKCBISNIMNFKIWGRNP4AOGQ3MTJKVZPVB5FHVTUMKQ2D";
};
};
export interface PoolConfig {
Expand Down
2 changes: 1 addition & 1 deletion packages/loan_pool/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (typeof window !== 'undefined') {
export const networks = {
testnet: {
networkPassphrase: "Test SDF Network ; September 2015",
contractId: "CDW6ATMFVGFMQNWQODMIOBNW3LVWDBPQZJ3MMXNH4AKGC7JFOHAJQIRV",
contractId: "CBN57ZMM4F6AFKCBISNIMNFKIWGRNP4AOGQ3MTJKVZPVB5FHVTUMKQ2D",
}
};
export const Errors = {};
Expand Down
2 changes: 1 addition & 1 deletion packages/loan_pool/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if (typeof window !== 'undefined') {
export const networks = {
testnet: {
networkPassphrase: "Test SDF Network ; September 2015",
contractId: "CDW6ATMFVGFMQNWQODMIOBNW3LVWDBPQZJ3MMXNH4AKGC7JFOHAJQIRV",
contractId: "CBN57ZMM4F6AFKCBISNIMNFKIWGRNP4AOGQ3MTJKVZPVB5FHVTUMKQ2D",
}
} as const

Expand Down
4 changes: 2 additions & 2 deletions packages/usdc_pool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This library was automatically generated by Soroban CLI using a command similar
soroban contract bindings ts \
--rpc-url https://soroban-testnet.stellar.org:443 \
--network-passphrase "Test SDF Network ; September 2015" \
--contract-id CDQHVYHZLK5EMZMTTLESFB5EM3WMWU5MBWYLHXT4FRRGWIEEOM4OLR3W \
--contract-id CAJ3WSITSIT7HULWUACKT2IBMZLA7KFTM5RDJM5T2DH2RAMWNS7KKFAX \
--output-dir ./path/to/usdc_pool
```

Expand All @@ -30,7 +30,7 @@ However, we've actually encountered [frustration](https://github.com/stellar/sor

```json
"scripts": {
"postinstall": "soroban contract bindings ts --rpc-url https://soroban-testnet.stellar.org:443 --network-passphrase \"Test SDF Network ; September 2015\" --id CDQHVYHZLK5EMZMTTLESFB5EM3WMWU5MBWYLHXT4FRRGWIEEOM4OLR3W --name usdc_pool"
"postinstall": "soroban contract bindings ts --rpc-url https://soroban-testnet.stellar.org:443 --network-passphrase \"Test SDF Network ; September 2015\" --id CAJ3WSITSIT7HULWUACKT2IBMZLA7KFTM5RDJM5T2DH2RAMWNS7KKFAX --name usdc_pool"
}
```

Expand Down
2 changes: 1 addition & 1 deletion packages/usdc_pool/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export * as rpc from '@stellar/stellar-sdk/rpc';
export declare const networks: {
readonly testnet: {
readonly networkPassphrase: "Test SDF Network ; September 2015";
readonly contractId: "CDQHVYHZLK5EMZMTTLESFB5EM3WMWU5MBWYLHXT4FRRGWIEEOM4OLR3W";
readonly contractId: "CAJ3WSITSIT7HULWUACKT2IBMZLA7KFTM5RDJM5T2DH2RAMWNS7KKFAX";
};
};
export interface PoolConfig {
Expand Down
2 changes: 1 addition & 1 deletion packages/usdc_pool/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (typeof window !== 'undefined') {
export const networks = {
testnet: {
networkPassphrase: "Test SDF Network ; September 2015",
contractId: "CDQHVYHZLK5EMZMTTLESFB5EM3WMWU5MBWYLHXT4FRRGWIEEOM4OLR3W",
contractId: "CAJ3WSITSIT7HULWUACKT2IBMZLA7KFTM5RDJM5T2DH2RAMWNS7KKFAX",
}
};
export const Errors = {};
Expand Down
2 changes: 1 addition & 1 deletion packages/usdc_pool/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if (typeof window !== 'undefined') {
export const networks = {
testnet: {
networkPassphrase: "Test SDF Network ; September 2015",
contractId: "CDQHVYHZLK5EMZMTTLESFB5EM3WMWU5MBWYLHXT4FRRGWIEEOM4OLR3W",
contractId: "CAJ3WSITSIT7HULWUACKT2IBMZLA7KFTM5RDJM5T2DH2RAMWNS7KKFAX",
}
} as const

Expand Down
2 changes: 1 addition & 1 deletion target/.rustc_info.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"rustc_fingerprint":12136865326911447218,"outputs":{"14908675589988555557":{"success":true,"status":"","code":0,"stdout":"___.wasm\nlib___.rlib\n___.wasm\nlib___.a\n/home/teemu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\n___\ndebug_assertions\npanic=\"abort\"\nproc_macro\ntarget_arch=\"wasm32\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"wasm\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"unknown\"\ntarget_pointer_width=\"32\"\ntarget_vendor=\"unknown\"\n","stderr":"warning: dropping unsupported crate type `dylib` for target `wasm32-unknown-unknown`\n\nwarning: dropping unsupported crate type `proc-macro` for target `wasm32-unknown-unknown`\n\nwarning: 2 warnings emitted\n\n"},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/teemu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.76.0 (07dca489a 2024-02-04)\nbinary: rustc\ncommit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce\ncommit-date: 2024-02-04\nhost: x86_64-unknown-linux-gnu\nrelease: 1.76.0\nLLVM version: 17.0.6\n","stderr":""}},"successes":{}}
{"rustc_fingerprint":12136865326911447218,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.76.0 (07dca489a 2024-02-04)\nbinary: rustc\ncommit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce\ncommit-date: 2024-02-04\nhost: x86_64-unknown-linux-gnu\nrelease: 1.76.0\nLLVM version: 17.0.6\n","stderr":""},"14908675589988555557":{"success":true,"status":"","code":0,"stdout":"___.wasm\nlib___.rlib\n___.wasm\nlib___.a\n/home/teemu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\n___\ndebug_assertions\npanic=\"abort\"\nproc_macro\ntarget_arch=\"wasm32\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"wasm\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"unknown\"\ntarget_pointer_width=\"32\"\ntarget_vendor=\"unknown\"\n","stderr":"warning: dropping unsupported crate type `dylib` for target `wasm32-unknown-unknown`\n\nwarning: dropping unsupported crate type `proc-macro` for target `wasm32-unknown-unknown`\n\nwarning: 2 warnings emitted\n\n"},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/teemu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""}},"successes":{}}
Loading

0 comments on commit bfc192a

Please sign in to comment.