Skip to content

Commit

Permalink
refactor(abi): clean up abi writer
Browse files Browse the repository at this point in the history
  • Loading branch information
milapsheth committed Jun 3, 2024
1 parent 84f1380 commit e35f7c6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 60 deletions.
80 changes: 32 additions & 48 deletions move/abi/sources/abi.move
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// This module implements ABI encoding/decoding methods for interoperability with EVM message format.
///
/// ABI Specification: https://docs.soliditylang.org/en/v0.8.26/abi-spec.html
module abi::abi {
// -----
// Types
Expand Down Expand Up @@ -54,19 +57,6 @@ module abi::abi {
bytes
}

public fun read_u256_from_slot(self: &AbiReader, pos: u64): u256 {
let mut var = 0u256;
let mut i = 0;

while (i < 32) {
var = var << 8;
var = var | (self.bytes[i + pos] as u256);
i = i + 1;
};

var
}

public fun read_u256(self: &mut AbiReader): u256 {
let mut var = 0u256;
let mut i = 0;
Expand All @@ -93,16 +83,16 @@ module abi::abi {
let offset = self.read_u256() as u64;
self.pos = self.head + offset;

let bytes = self.decode_bytes();
let var = self.decode_bytes();

// Move position to the next slot
self.pos = pos + 32;

bytes
var
}

public fun read_vector_u256(self: &mut AbiReader): vector<u256> {
let mut bytes = vector[];
let mut var = vector[];
let pos = self.pos;

// Move position to the start of the dynamic data
Expand All @@ -114,18 +104,18 @@ module abi::abi {
let mut i = 0;

while (i < length) {
bytes.push_back(self.read_u256());
var.push_back(self.read_u256());
i = i + 1;
};

self.pos = pos + 32;

bytes
var
}

/// Decode ABI-encoded 'bytes[]'
public fun read_vector_bytes(self: &mut AbiReader): vector<vector<u8>> {
let mut bytes = vector[];
let mut var = vector[];

let pos = self.pos;
let head = self.head;
Expand All @@ -140,7 +130,7 @@ module abi::abi {
let mut i = 0;

while (i < length) {
bytes.push_back(self.read_bytes());
var.push_back(self.read_bytes());

i = i + 1;
};
Expand All @@ -149,13 +139,22 @@ module abi::abi {
self.pos = pos + 32;
self.head = head;

bytes
var
}

public fun write_u256(self: &mut AbiWriter, var: u256): &mut AbiWriter {
let pos = self.pos;
self.encode_u256(pos, var);
self.pos = self.pos + 1;
let mut i = 0;

while (i < 32) {
let exp = ((31 - i) * 8 as u8);
let byte = (var >> exp & 255 as u8);
*self.bytes.borrow_mut(i + pos) = byte;
i = i + 1;
};

self.pos = pos + 32;

self
}

Expand All @@ -164,45 +163,41 @@ module abi::abi {
}

public fun write_bytes(self: &mut AbiWriter, var: vector<u8>): &mut AbiWriter {
let pos = self.pos;
let length = self.bytes.length() as u256;
self.encode_u256(pos, length);
let offset = self.bytes.length() as u256;
self.write_u256(offset);

// Write dynamic data length and bytes at the tail
self.append_u256(var.length() as u256);

self.append_bytes(var);
self.pos = self.pos + 1;

self
}

public fun write_vector_u256(self: &mut AbiWriter, var: vector<u256>): &mut AbiWriter {
let pos = self.pos;
let length = self.bytes.length();
self.encode_u256(pos, length as u256);
let offset = self.bytes.length() as u256;
self.write_u256(offset);

let length = var.length();
self.append_u256(length as u256);

let mut i = 0u64;
let mut i = 0;
while (i < length) {
self.append_u256(var[i]);
i = i + 1;
};

self.pos = self.pos + 1;
self
}

public fun write_vector_bytes(self: &mut AbiWriter, var: vector<vector<u8>>): &mut AbiWriter {
let pos = self.pos;
let length = self.bytes.length();
self.encode_u256(pos, length as u256);
let offset = self.bytes.length() as u256;
self.write_u256(offset);

let length = var.length();
self.append_u256(length as u256);

let mut i = 0;
let mut writer = new_writer(length);
let mut i = 0;

while (i < length) {
writer.write_bytes(var[i]);
Expand All @@ -211,24 +206,13 @@ module abi::abi {

self.append_bytes(writer.into_bytes());

self.pos = self.pos + 1;
self
}

// ------------------
// Internal Functions
// ------------------

fun encode_u256(self: &mut AbiWriter, pos: u64, var: u256) {
let mut i = 0;

while (i < 32) {
let exp = ((31 - i) * 8 as u8);
*self.bytes.borrow_mut(i + 32 * pos) = (var >> exp & 255 as u8);
i = i + 1;
};
}

fun append_u256(self: &mut AbiWriter, var: u256) {
let mut i = 0;
while (i < 32) {
Expand Down
26 changes: 14 additions & 12 deletions move/its/sources/service.move
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ module its::service {
let decimals = coin_info.decimals();
let mut writer = abi::new_writer(6);

writer.write_u256(MESSAGE_TYPE_DEPLOY_INTERCHAIN_TOKEN);
writer.write_u256(token_id.to_u256());
writer.write_bytes(*string::bytes(&name));
writer.write_bytes(*ascii::as_bytes(&symbol));
writer.write_u256((decimals as u256));
writer.write_bytes(vector::empty());
writer
.write_u256(MESSAGE_TYPE_DEPLOY_INTERCHAIN_TOKEN)
.write_u256(token_id.to_u256())
.write_bytes(*string::bytes(&name))
.write_bytes(*ascii::as_bytes(&symbol))
.write_u256((decimals as u256))
.write_bytes(vector::empty());

send_payload(self, destination_chain, writer.into_bytes());
}
Expand All @@ -89,12 +90,13 @@ module its::service {
let (_version, data) = its_utils::decode_metadata(metadata);
let mut writer = abi::new_writer(6);

writer.write_u256(MESSAGE_TYPE_INTERCHAIN_TRANSFER);
writer.write_u256(token_id.to_u256());
writer.write_bytes(address::to_bytes(ctx.sender()));
writer.write_bytes(destination_address);
writer.write_u256(amount);
writer.write_bytes(data);
writer
.write_u256(MESSAGE_TYPE_INTERCHAIN_TRANSFER)
.write_u256(token_id.to_u256())
.write_bytes(address::to_bytes(ctx.sender()))
.write_bytes(destination_address)
.write_u256(amount)
.write_bytes(data);

self.coin_management_mut(token_id)
.take_coin(coin);
Expand Down

0 comments on commit e35f7c6

Please sign in to comment.