-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Enable amount/asset forwarding in tx params #167
Conversation
fuels-contract/src/contract.rs
Outdated
let script_len = 56; | ||
let script_data_offset = VM_TX_MEMORY + Transaction::script_offset() + script_len; | ||
let script_data_offset = script_data_offset as Immediate12; | ||
|
||
// The offset that locates the asset ID in the script data. | ||
// It goes from the beginning of `script_data` to `32`. | ||
let asset_id_offset = 32; | ||
|
||
// Script to call the contract. The offset that points to the `script_data` is loaded at the | ||
// register `0x10`. Note that we're picking `0x10` simply because it could be any | ||
// register `0x12`. Note that we're picking `0x12` simply because it could be any | ||
// non-reserved register. Then, we use the Opcode to call a contract: `CALL` pointing at the | ||
// register that we loaded the `script_data` at. | ||
|
||
// The `iter().collect()` does the Opcode->u8 conversion | ||
#[allow(clippy::iter_cloned_collect)] | ||
let script = vec![ | ||
Opcode::ADDI(0x10, REG_ZERO, script_data_offset), | ||
// @todo currently there's no way to forward an amount. | ||
// This would be done by programmatically changing | ||
// $rB (`REG_ZERO` right now) to actually point to an | ||
// amount. This would forward the amount in $rB using | ||
// $rC as the asset_id. | ||
Opcode::CALL(0x10, REG_ZERO, 0x10, REG_CGAS), | ||
// Setting `0x10` to point to the 32-byte asset ID of the amount to forward. | ||
Opcode::ADDI(0x10, REG_ZERO, asset_id_offset), | ||
// Setting `0x11` to hold the amount of of coins to forward. | ||
Opcode::ADDI(0x11, REG_ZERO, tx_parameters.amount as Immediate12), | ||
// Setting `0x12` to the `script_data`, defined down below but | ||
// we already know its length. | ||
Opcode::ADDI(0x12, REG_ZERO, script_data_offset), | ||
Opcode::CALL(0x12, 0x11, 0x10, REG_CGAS), | ||
Opcode::RET(REG_RET), | ||
Opcode::NOOP, | ||
] | ||
.iter() | ||
.copied() | ||
.collect::<Vec<u8>>(); | ||
|
||
assert_eq!(script.len(), script_len, "Script length *must* be 16"); | ||
assert_eq!( | ||
script.len(), | ||
script_len - asset_id_offset as usize, | ||
"Script length *must* be 24" | ||
); | ||
|
||
// `script_data` consists of: | ||
// 1. Contract ID (ContractID::LEN); | ||
// 2. Function selector (1 * WORD_SIZE); | ||
// 3. Calldata offset, if it has structs as input, | ||
// 1. Asset ID to be forwarded | ||
// 2. Contract ID (ContractID::LEN); | ||
// 3. Function selector (1 * WORD_SIZE); | ||
// 4. Calldata offset, if it has structs as input, | ||
// computed as `script_data_offset` + ContractId::LEN | ||
// + 2 * WORD_SIZE; | ||
// 4. Encoded arguments. | ||
// 5. Encoded arguments. | ||
let mut script_data: Vec<u8> = vec![]; | ||
|
||
script_data.extend(tx_parameters.asset_id.to_vec()); |
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.
@luizstacio @AlicanC This whole section here is what's needed on fuels-ts
to implement the same thing. Let me know if it's not clear enough and/or if you have more questions!
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.
The core changeset is good, but just to get into a good habit, could you break apart updating fuel-vm, updating tests, fixing unrelated tests, and fixing some constants all into different PRs?
Hmm... All the changes done in this PR are related to each other, I think?
|
Adds a
CallParameters
to support forwardingamount
andasset_id
through a new chain method:call_params()
.It also extends the generated
script
andscript_data
to enable this support.Example:
In Sway, this amount is accessible through the standard library method
msg_amount()
, e.g.:Closes #159.