-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: contract operations in transaction summary (#3704)
* fix: contract operations in transaction summary * chore: minor changeset * chore: fix doc Co-authored-by: Peter Smith <peter@blueoceancomputing.co.uk> * chore: pr is not breaking * chore: remove unit test * chore: another test case * test: add multicall * chore: operations tests for getTransactionSumarry fn --------- Co-authored-by: Peter Smith <peter@blueoceancomputing.co.uk> Co-authored-by: Anderson Arboleya <anderson@arboleya.me>
- Loading branch information
1 parent
02503a6
commit 194e31f
Showing
8 changed files
with
389 additions
and
18 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@fuel-ts/account": patch | ||
"@fuel-ts/abi-coder": patch | ||
--- | ||
|
||
fix: contract operations in transaction summary |
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,52 @@ | ||
import type { BN } from '@fuel-ts/math'; | ||
|
||
import { Interface } from '../Interface'; | ||
import { B256Coder } from '../encoding/coders/B256Coder'; | ||
import { BigNumberCoder } from '../encoding/coders/BigNumberCoder'; | ||
import { StdStringCoder } from '../encoding/coders/StdStringCoder'; | ||
import type { JsonAbi } from '../types/JsonAbiNew'; | ||
|
||
import { WORD_SIZE } from './constants'; | ||
|
||
export type DecodedScriptData = { | ||
amount: BN; | ||
assetId: string; | ||
contractId: string; | ||
functionSelector: string; | ||
functionArgs: unknown[] | undefined; | ||
}; | ||
|
||
/** | ||
* Decodes the script data used when calling programs to deduce the amount, asset ID, | ||
* contract ID, function selector and function arguments passed. | ||
* | ||
* @param scriptData - the script data to decode. | ||
* @param abi - the abi used for the call, to deduce arguments (optional). | ||
* @returns the decoded script data. | ||
*/ | ||
export const decodeScriptData = (scriptData: Uint8Array, abi?: JsonAbi): DecodedScriptData => { | ||
// Slice all data sections at required offsets | ||
const [amount, amountOffset] = new BigNumberCoder('u64').decode(scriptData, 0); | ||
const [assetId, assetIdOffset] = new B256Coder().decode(scriptData, amountOffset); | ||
const [contractId, contractIdOffset] = new B256Coder().decode(scriptData, assetIdOffset); | ||
const [functionSelector, functionSelectorOffset] = new StdStringCoder().decode( | ||
scriptData, | ||
contractIdOffset + WORD_SIZE + WORD_SIZE | ||
); | ||
|
||
// Slice the function arguments after the function selector | ||
const functionArgsBytes = scriptData.slice(functionSelectorOffset); | ||
|
||
// Decode the function arguments using the function selector if we have the abi | ||
const functionArgs = abi | ||
? new Interface(abi).getFunction(functionSelector).decodeArguments(functionArgsBytes) | ||
: undefined; | ||
|
||
return { | ||
amount, | ||
assetId, | ||
contractId, | ||
functionSelector, | ||
functionArgs, | ||
}; | ||
}; |
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.