-
Notifications
You must be signed in to change notification settings - Fork 8
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(squid)!: squid object procurement #173
Open
Foivos
wants to merge
16
commits into
main
Choose a base branch
from
feat/squid-object-procurement
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8fbcb23
add versioned and better version control to squid
Foivos b29cd55
fixed tests
Foivos a7fc722
fix interfaces
Foivos 9ab5c92
addressed some comments
Foivos 63106d2
fix small error
Foivos 8adb9e4
fix tests
Foivos eec3780
fix interfaces
Foivos 0099397
some reorganizing using swaps.
Foivos 26c5b29
Merge remote-tracking branch 'origin/main' into feat/squid-update
Foivos 295ad5f
fix merge
Foivos 06634e6
add a couple of comments
Foivos 7093e06
fix interfaces
Foivos 5affc24
add object procurement.
Foivos 74909a2
fix error uppercasing
Foivos 8e4899f
add discovery
Foivos 48e58c6
format
Foivos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,182 @@ | ||||||||||||||
module squid::object_procurement; | ||||||||||||||
|
||||||||||||||
use relayer_discovery::transaction::{Self, MoveCall}; | ||||||||||||||
use squid::swap_info::SwapInfo; | ||||||||||||||
use std::ascii::{Self, String}; | ||||||||||||||
use std::type_name; | ||||||||||||||
use sui::bcs::{Self, BCS}; | ||||||||||||||
use sui::coin::Coin; | ||||||||||||||
|
||||||||||||||
const SWAP_TYPE: u8 = 4; | ||||||||||||||
|
||||||||||||||
// ------ | ||||||||||||||
// Errors | ||||||||||||||
// ------ | ||||||||||||||
#[error] | ||||||||||||||
const EWrongObject: vector<u8> = | ||||||||||||||
b"object passed did not match the requested object"; | ||||||||||||||
#[error] | ||||||||||||||
const ERemainingData: vector<u8> = b"remaining bcs data unexpected."; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
#[error] | ||||||||||||||
const EWrongCoinType: vector<u8> = b"coin type mismatch."; | ||||||||||||||
#[error] | ||||||||||||||
const EWrongSwapType: vector<u8> = b"swap type mismatch."; | ||||||||||||||
Comment on lines
+21
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
// ----- | ||||||||||||||
// Types | ||||||||||||||
// ----- | ||||||||||||||
public struct ObjectProcurementSwapData { | ||||||||||||||
swap_type: u8, | ||||||||||||||
coin_type: String, | ||||||||||||||
object_type: String, | ||||||||||||||
object_id: ID, | ||||||||||||||
recipient: address, | ||||||||||||||
price: u64, | ||||||||||||||
fallback: bool, | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public fun estimate<T>(swap_info: &mut SwapInfo) { | ||||||||||||||
let (data, fallback) = swap_info.get_data_estimating(); | ||||||||||||||
|
||||||||||||||
let swap_data = peel_swap_data(data, fallback); | ||||||||||||||
assert!(swap_data.swap_type == SWAP_TYPE, EWrongSwapType); | ||||||||||||||
assert!( | ||||||||||||||
&swap_data.coin_type == &type_name::get<T>().into_string(), | ||||||||||||||
EWrongCoinType, | ||||||||||||||
); | ||||||||||||||
|
||||||||||||||
if (!fallback) { | ||||||||||||||
swap_info.coin_bag().get_exact_estimate<T>(swap_data.price); | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
swap_data.destroy(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public fun loan_coins<T>( | ||||||||||||||
swap_info: &mut SwapInfo, | ||||||||||||||
ctx: &mut TxContext, | ||||||||||||||
): (ObjectProcurementSwapData, Option<Coin<T>>) { | ||||||||||||||
let (data, fallback) = swap_info.get_data_swapping(); | ||||||||||||||
|
||||||||||||||
let swap_data = peel_swap_data(data, fallback); | ||||||||||||||
assert!(swap_data.swap_type == SWAP_TYPE, EWrongSwapType); | ||||||||||||||
assert!( | ||||||||||||||
&swap_data.coin_type == &type_name::get<T>().into_string(), | ||||||||||||||
EWrongCoinType, | ||||||||||||||
); | ||||||||||||||
|
||||||||||||||
if (fallback) return (swap_data, option::none()); | ||||||||||||||
|
||||||||||||||
let balance = swap_info.coin_bag().get_exact_balance<T>(swap_data.price); | ||||||||||||||
let coin = balance.into_coin(ctx); | ||||||||||||||
(swap_data, option::some(coin)) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public fun return_object<T: key + store>( | ||||||||||||||
swap_data: ObjectProcurementSwapData, | ||||||||||||||
object_option: Option<T>, | ||||||||||||||
) { | ||||||||||||||
if (swap_data.fallback) { | ||||||||||||||
object_option.destroy_none(); | ||||||||||||||
} else { | ||||||||||||||
let object = object_option.destroy_some(); | ||||||||||||||
assert!(object::id(&object) == swap_data.object_id, EWrongObject); | ||||||||||||||
transfer::public_transfer(object, swap_data.recipient); | ||||||||||||||
}; | ||||||||||||||
swap_data.destroy(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public(package) fun get_estimate_move_call( | ||||||||||||||
package_id: address, | ||||||||||||||
mut bcs: BCS, | ||||||||||||||
swap_info_arg: vector<u8>, | ||||||||||||||
): MoveCall { | ||||||||||||||
let coin_type = ascii::string(bcs.peel_vec_u8()); | ||||||||||||||
|
||||||||||||||
transaction::new_move_call( | ||||||||||||||
transaction::new_function( | ||||||||||||||
package_id, | ||||||||||||||
ascii::string(b"object_procurement"), | ||||||||||||||
ascii::string(b"estimate"), | ||||||||||||||
), | ||||||||||||||
vector[swap_info_arg], | ||||||||||||||
vector[coin_type], | ||||||||||||||
) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public(package) fun get_procure_move_call( | ||||||||||||||
package_id: address, | ||||||||||||||
mut bcs: BCS, | ||||||||||||||
swap_info_arg: vector<u8>, | ||||||||||||||
index: u8, | ||||||||||||||
): vector<MoveCall> { | ||||||||||||||
let coin_type = ascii::string(bcs.peel_vec_u8()); | ||||||||||||||
let object_type = ascii::string(bcs.peel_vec_u8()); | ||||||||||||||
|
||||||||||||||
let swap_data_arg = vector[4, index, 0]; | ||||||||||||||
let object_option_arg = vector[4, index + 1, 0]; | ||||||||||||||
|
||||||||||||||
let loan_call = transaction::new_move_call( | ||||||||||||||
transaction::new_function( | ||||||||||||||
package_id, | ||||||||||||||
ascii::string(b"object_procurement"), | ||||||||||||||
ascii::string(b"loan_coins"), | ||||||||||||||
), | ||||||||||||||
vector[swap_info_arg], | ||||||||||||||
vector[coin_type], | ||||||||||||||
); | ||||||||||||||
|
||||||||||||||
let _object_id = bcs.peel_address(); | ||||||||||||||
let _recipient = bcs.peel_address(); | ||||||||||||||
let _price = bcs.peel_u64(); | ||||||||||||||
|
||||||||||||||
let purchase_call = transaction::new_move_call_from_bcs(&mut bcs); | ||||||||||||||
|
||||||||||||||
assert!(bcs.into_remainder_bytes().length() == 0, ERemainingData); | ||||||||||||||
|
||||||||||||||
let return_call = transaction::new_move_call( | ||||||||||||||
transaction::new_function( | ||||||||||||||
package_id, | ||||||||||||||
ascii::string(b"object_procurement"), | ||||||||||||||
ascii::string(b"return_object"), | ||||||||||||||
), | ||||||||||||||
vector[swap_data_arg, object_option_arg], | ||||||||||||||
vector[object_type], | ||||||||||||||
); | ||||||||||||||
vector[loan_call, purchase_call, return_call] | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// Store the fallback here too to be able to retreive in the `give_object` | ||||||||||||||
fun peel_swap_data( | ||||||||||||||
data: vector<u8>, | ||||||||||||||
fallback: bool, | ||||||||||||||
): ObjectProcurementSwapData { | ||||||||||||||
let mut bcs = bcs::new(data); | ||||||||||||||
let swap_data = ObjectProcurementSwapData { | ||||||||||||||
swap_type: bcs.peel_u8(), | ||||||||||||||
coin_type: ascii::string(bcs.peel_vec_u8()), | ||||||||||||||
object_type: ascii::string(bcs.peel_vec_u8()), | ||||||||||||||
object_id: object::id_from_address(bcs.peel_address()), | ||||||||||||||
recipient: bcs.peel_address(), | ||||||||||||||
price: bcs.peel_u64(), | ||||||||||||||
fallback, | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
transaction::new_move_call_from_bcs(&mut bcs); | ||||||||||||||
|
||||||||||||||
assert!(bcs.into_remainder_bytes().length() == 0, ERemainingData); | ||||||||||||||
|
||||||||||||||
swap_data | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
fun destroy(self: ObjectProcurementSwapData) { | ||||||||||||||
let ObjectProcurementSwapData { | ||||||||||||||
swap_type: _, | ||||||||||||||
object_id: _, | ||||||||||||||
recipient: _, | ||||||||||||||
coin_type: _, | ||||||||||||||
object_type: _, | ||||||||||||||
price: _, | ||||||||||||||
fallback: _, | ||||||||||||||
} = self; | ||||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not a fan of partial declarations. define swap type enum in a separate type module and use it where needed?