Skip to content

Commit

Permalink
[apps]Fix bugs and upgrade twitter account binding v9
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar committed Feb 5, 2025
1 parent b27789b commit 943f23f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 26 deletions.
2 changes: 1 addition & 1 deletion apps/twitter_binding/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ MoveStdlib = { local = "../../frameworks/move-stdlib" }
MoveosStdlib = { local = "../../frameworks/moveos-stdlib" }
RoochFramework = { local = "../../frameworks/rooch-framework" }
BitcoinMove = { local = "../../frameworks/bitcoin-move" }
verity-move-oracles = { git = "https://github.com/usherlabs/verity-move-oracles", subdir = "rooch", rev = "main"}
verity-move-oracles = { git = "https://github.com/usherlabs/verity-move-oracles", subdir = "rooch", rev = "d08d06fe56ff2e6daae63555dc776454bb1e0405"}
app_admin = { local = "../../apps/app_admin" }

[addresses]
Expand Down
Binary file added apps/twitter_binding/released/9/package.rpd
Binary file not shown.
52 changes: 52 additions & 0 deletions apps/twitter_binding/sources/string_util.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module twitter_binding::string_util {

use std::vector;

friend twitter_binding::tweet_fetcher;
friend twitter_binding::twitter_account;

//TODO migrate to std::string::starts_with
public(friend) fun starts_with(haystack: &vector<u8>, needle: &vector<u8>): bool {
let haystack_len = vector::length(haystack);
let needle_len = vector::length(needle);

if (needle_len > haystack_len) {
return false
};

let i = 0;
while (i < needle_len) {
if (vector::borrow(haystack, i) != vector::borrow(needle, i)) {
return false
};
i = i + 1;
};

true
}

const MOVE_PREFIX: vector<u8> = b"b'";
// the ascii value of the single quote character "'"
const MOVE_SUFFIX: u8 = 39;

public(friend) fun try_remove_move_string_prefix(bytes: vector<u8>): vector<u8> {
let len = vector::length(&bytes);
if (starts_with(&bytes, &MOVE_PREFIX) && *vector::borrow(&bytes, len - 1) == MOVE_SUFFIX) {
vector::slice(&bytes, 2, len - 1)
} else {
bytes
}
}

#[test]
fun test_try_remove_move_string_prefix() {
let s2 = try_remove_move_string_prefix(b"b'hello'");
assert!(s2 == b"hello", 1000);
}

#[test]
fun test_try_remove_move_string_prefix_fail() {
let s2 = try_remove_move_string_prefix(b"hello");
assert!(s2 == b"hello", 1000);
}
}
22 changes: 17 additions & 5 deletions apps/twitter_binding/sources/tweet_fetcher.move
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module twitter_binding::tweet_fetcher {
use twitter_binding::twitter_account;
use twitter_binding::tweet_v2;
use app_admin::admin::{AdminCap};
use twitter_binding::string_util;

use verity::oracles;

Expand All @@ -23,7 +24,7 @@ module twitter_binding::tweet_fetcher {

const ORACLE_ADDRESS: address = @0x694cbe655b126e9e6a997e86aaab39e538abf30a8c78669ce23a98740b47b65d;

const MAX_BUFFER_QUEUE_SIZE: u64 = 50;
const MAX_BUFFER_QUEUE_SIZE: u64 = 20;

const ErrorInvalidRequestID: u64 = 1;
const ErrorInvalidResponse: u64 = 2;
Expand Down Expand Up @@ -274,12 +275,19 @@ module twitter_binding::tweet_fetcher {
let status = if (response_http_status == 200){
let response_opt = oracles::get_response(&request_id);
let response = option::destroy_some(response_opt);
// The response is a JSON string including the tweet json data
let json_str = json::from_json<String>(string::into_bytes(response));
let batch_tweet_data_opt = tweet_v2::parse_batch_tweet_data(string::into_bytes(json_str));
// The response of the old version is a JSON string including the tweet json data
let json_str_opt = json::from_json_option<String>(string::into_bytes(response));
let json_str = if(option::is_some(&json_str_opt)){
option::destroy_some(json_str_opt)
}else{
response
};
let json_str_bytes = string_util::try_remove_move_string_prefix(string::into_bytes(json_str));
let batch_tweet_data_opt = tweet_v2::parse_batch_tweet_data(json_str_bytes);
if (option::is_some(&batch_tweet_data_opt)){
let batch_tweet_data = option::destroy_some(batch_tweet_data_opt);
let tweet_datas = tweet_v2::unwrap_batch_tweet_data(batch_tweet_data);
let is_empty_data = vector::is_empty(&tweet_datas);
vector::for_each(tweet_datas, |tweet_data|{
let author_id = *tweet_v2::tweet_data_author_id(&tweet_data);
let tweet_id = *tweet_v2::tweet_data_id(&tweet_data);
Expand All @@ -294,7 +302,11 @@ module twitter_binding::tweet_fetcher {
tweet_v2::transfer_tweet_object_internal(tweet_obj, owner_address);
};
});
TWEET_STATUS_PROCESSED
if(is_empty_data){
TWEET_STATUS_PROCESS_FAILED
}else{
TWEET_STATUS_PROCESSED
}
}else{
TWEET_STATUS_PROCESS_FAILED
}
Expand Down
21 changes: 1 addition & 20 deletions apps/twitter_binding/sources/twitter_account.move
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module twitter_binding::twitter_account {
use rooch_framework::gas_coin::{RGas};

use twitter_binding::tweet_v2::{Self, Tweet};
use twitter_binding::string_util::{starts_with};
use app_admin::admin::{AdminCap};

const TWITTER_ACCOUNT_BINDING_MESSAGE_PREFIX: vector<u8> = b"BTC:";
Expand Down Expand Up @@ -341,26 +342,6 @@ module twitter_binding::twitter_account {
text_len
}

//TODO migrate to std::string::starts_with
fun starts_with(haystack: &vector<u8>, needle: &vector<u8>): bool {
let haystack_len = vector::length(haystack);
let needle_len = vector::length(needle);

if (needle_len > haystack_len) {
return false
};

let i = 0;
while (i < needle_len) {
if (vector::borrow(haystack, i) != vector::borrow(needle, i)) {
return false
};
i = i + 1;
};

true
}

fun borrow_twitter_rgas_faucet() : &TwitterRGasFaucet{
let twitter_rgas_faucet_obj_id = object::named_object_id<TwitterRGasFaucet>();
let twitter_rgas_faucet_obj = object::borrow_object<TwitterRGasFaucet>(twitter_rgas_faucet_obj_id);
Expand Down

0 comments on commit 943f23f

Please sign in to comment.