-
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.
feat: update forc to
v0.46.1
(#1343)
* up v * add cs * format * cleanup sway files as part of upgrade * update * add AssetId * update abi * add * add tests * update check * adjust * adjust logs * add * catch invalid string types * fixes * update * change encoded length value * lint
- Loading branch information
Cameron Manavian
authored
Oct 20, 2023
1 parent
077893d
commit 3e03409
Showing
34 changed files
with
297 additions
and
80 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,5 @@ | ||
--- | ||
"@fuel-ts/forc": minor | ||
--- | ||
|
||
Updated forc version |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
contract; | ||
|
||
use std::{context::{msg_amount}}; | ||
use std::context::msg_amount; | ||
|
||
abi ReturnContext { | ||
#[payable] | ||
|
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,94 @@ | ||
import { AssetIdCoder } from './asset-id'; | ||
|
||
describe('AssetIdCoder', () => { | ||
const B256_DECODED = '0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'; | ||
const B256_ENCODED = new Uint8Array([ | ||
213, 87, 156, 70, 223, 204, 127, 24, 32, 112, 19, 230, 91, 68, 228, 203, 78, 44, 34, 152, 244, | ||
172, 69, 123, 168, 248, 39, 67, 243, 30, 147, 11, | ||
]); | ||
const B256_ZERO_DECODED = '0x0000000000000000000000000000000000000000000000000000000000000000'; | ||
const B256_ZERO_ENCODED = new Uint8Array(32); | ||
|
||
const coder = new AssetIdCoder(); | ||
|
||
it('should encode zero as a 256 bit hash string', () => { | ||
const expected = B256_ZERO_ENCODED; | ||
const actual = coder.encode(B256_ZERO_DECODED); | ||
|
||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
it('should encode a 256 bit hash string', () => { | ||
const expected = B256_ENCODED; | ||
const actual = coder.encode(B256_DECODED); | ||
|
||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
it('should decode zero as a 256 bit hash string', () => { | ||
const expectedValue = B256_ZERO_DECODED; | ||
const expectedLength = B256_ZERO_ENCODED.length; | ||
const [actualValue, actualLength] = coder.decode(B256_ZERO_ENCODED, 0); | ||
|
||
expect(actualValue).toStrictEqual(expectedValue); | ||
expect(actualLength).toBe(expectedLength); | ||
}); | ||
|
||
it('should decode a 256 bit hash string', () => { | ||
const expectedValue = B256_DECODED; | ||
const expectedLength = B256_ENCODED.length; | ||
const [actualValue, actualLength] = coder.decode(B256_ENCODED, 0); | ||
|
||
expect(actualValue).toStrictEqual(expectedValue); | ||
expect(actualLength).toBe(expectedLength); | ||
}); | ||
|
||
it('should throw an error when encoding a 256 bit hash string that is too short', () => { | ||
const invalidInput = B256_DECODED.slice(0, B256_DECODED.length - 1); | ||
|
||
expect(() => { | ||
coder.encode(invalidInput); | ||
}).toThrow('Invalid struct AssetId'); | ||
}); | ||
|
||
it('should throw an error when decoding an encoded 256 bit hash string that is too short', () => { | ||
const invalidInput = B256_ENCODED.slice(0, B256_ENCODED.length - 1); | ||
|
||
expect(() => { | ||
coder.decode(invalidInput, 0); | ||
}).toThrow(); | ||
}); | ||
|
||
it('should throw an error when encoding a 256 bit hash string that is too long', () => { | ||
const invalidInput = `${B256_DECODED}0`; | ||
|
||
expect(() => { | ||
coder.encode(invalidInput); | ||
}).toThrow('Invalid struct AssetId'); | ||
}); | ||
|
||
it('should throw an error when encoding a 512 bit hash string', () => { | ||
const B512 = | ||
'0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2'; | ||
|
||
expect(() => { | ||
coder.encode(B512); | ||
}).toThrow('Invalid struct AssetId'); | ||
}); | ||
|
||
it('should throw an error when decoding an encoded 256 bit hash string that is too long', () => { | ||
const invalidInput = new Uint8Array(Array.from(Array(32).keys())); | ||
|
||
expect(() => { | ||
coder.decode(invalidInput, 1); | ||
}).toThrow('Invalid size for AssetId'); | ||
}); | ||
|
||
it('should throw an error when encoding a 256 bit hash string that is not a hex string', () => { | ||
const invalidInput = 'not a hex string'; | ||
|
||
expect(() => { | ||
coder.encode(invalidInput); | ||
}).toThrow('Invalid struct AssetId'); | ||
}); | ||
}); |
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,36 @@ | ||
import { ErrorCode } from '@fuel-ts/errors'; | ||
import { bn, toHex } from '@fuel-ts/math'; | ||
import { getBytes } from 'ethers'; | ||
|
||
import { Coder } from './abstract-coder'; | ||
|
||
export class AssetIdCoder extends Coder<string, string> { | ||
constructor() { | ||
super('AssetId', 'struct AssetId', 32); | ||
} | ||
|
||
encode(value: string): Uint8Array { | ||
let encodedValue; | ||
try { | ||
encodedValue = getBytes(value); | ||
} catch (error) { | ||
this.throwError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`); | ||
} | ||
if (encodedValue.length !== 32) { | ||
this.throwError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`); | ||
} | ||
return encodedValue; | ||
} | ||
|
||
decode(data: Uint8Array, offset: number): [string, number] { | ||
let bytes = data.slice(offset, offset + 32); | ||
const decoded = bn(bytes); | ||
if (decoded.isZero()) { | ||
bytes = new Uint8Array(32); | ||
} | ||
if (bytes.length !== 32) { | ||
this.throwError(ErrorCode.DECODE_ERROR, `Invalid size for AssetId.`); | ||
} | ||
return [toHex(bytes, 32), offset + 32]; | ||
} | ||
} |
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,27 @@ | ||
import { AssetIdType } from './AssetIdType'; | ||
import { BoolType } from './BoolType'; | ||
|
||
describe('AssetIdType.ts', () => { | ||
test('should properly parse type attributes', () => { | ||
const b512 = new AssetIdType({ | ||
rawAbiType: { | ||
components: null, | ||
typeParameters: null, | ||
typeId: 1, | ||
type: AssetIdType.swayType, | ||
}, | ||
}); | ||
|
||
b512.parseComponentsAttributes({ types: [] }); | ||
|
||
const suitableForAssetId = AssetIdType.isSuitableFor({ type: AssetIdType.swayType }); | ||
const suitableForBool = AssetIdType.isSuitableFor({ type: BoolType.swayType }); | ||
|
||
expect(suitableForAssetId).toEqual(true); | ||
expect(suitableForBool).toEqual(false); | ||
|
||
expect(b512.attributes.inputLabel).toEqual('string'); | ||
expect(b512.attributes.outputLabel).toEqual('string'); | ||
expect(b512.requiredFuelsMembersImports).toStrictEqual([]); | ||
}); | ||
}); |
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,13 @@ | ||
import { B256Type } from './B256Type'; | ||
|
||
export class AssetIdType extends B256Type { | ||
public static swayType = 'struct AssetId'; | ||
|
||
public name = 'AssetId'; | ||
|
||
static MATCH_REGEX = /^struct AssetId$/m; | ||
|
||
static isSuitableFor(params: { type: string }) { | ||
return AssetIdType.MATCH_REGEX.test(params.type); | ||
} | ||
} |
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
Oops, something went wrong.