Skip to content

Commit

Permalink
Fix codec
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed May 10, 2024
1 parent 52b04f7 commit c82e67a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 6 additions & 4 deletions packages/web3/src/codec/contract-codec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
LoadLocal,
LoadMutField,
Log5,
MethodSelector,
Return,
StoreLocal,
StoreMutField,
Expand Down Expand Up @@ -99,15 +100,15 @@ describe('Encode & decode contract', function () {
argsLength: 0,
localsLength: 0,
returnLength: 1,
instrs: [LoadImmField(3), Return]
instrs: [MethodSelector(-1137803467), LoadImmField(3), Return]
},
{
isPublic: true,
assetModifier: 0,
argsLength: 0,
localsLength: 0,
returnLength: 1,
instrs: [LoadImmField(0), Return]
instrs: [MethodSelector(167928762), LoadImmField(0), Return]
},
{
isPublic: false,
Expand All @@ -123,7 +124,7 @@ describe('Encode & decode contract', function () {
argsLength: 0,
localsLength: 0,
returnLength: 1,
instrs: [LoadImmField(2), Return]
instrs: [MethodSelector(1051502534), LoadImmField(2), Return]
}
]
)
Expand Down Expand Up @@ -234,6 +235,7 @@ describe('Encode & decode contract', function () {
localsLength: 9,
returnLength: 0,
instrs: [
MethodSelector(-50171758),
LoadLocal(0),
LoadLocal(2),
TokenRemaining,
Expand Down Expand Up @@ -321,7 +323,7 @@ describe('Encode & decode contract', function () {
const encoded = contractCodec.encode(decoded)

const decodedContract = contractCodec.decodeContract(Buffer.from(contractBytecode, 'hex'))
expect(decodedContract.fieldLength).toEqual(methods.length)
expect(decodedContract.methods.length).toEqual(methods.length)
expect(toHalfDecoded(decodedContract)).toEqual(decoded)
decodedContract.methods.map((decodedMethod, index) => {
expect(decodedMethod.isPublic).toEqual(methods[index].isPublic)
Expand Down
15 changes: 14 additions & 1 deletion packages/web3/src/codec/instr-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { compactUnsignedIntCodec, compactSignedIntCodec, DecodedCompactInt } fro
import { ByteString, byteStringCodec } from './bytestring-codec'
import { LockupScript, lockupScriptCodec } from './lockup-script-codec'
import { Codec } from './codec'
import { signedIntCodec } from './signed-int-codec'

const byteStringArrayCodec = new ArrayCodec(byteStringCodec)

Expand Down Expand Up @@ -192,6 +193,7 @@ export const AddModN: Instr = { code: 0x88, value: {} }
export const U256ToString: Instr = { code: 0x89, value: {} }
export const I256ToString: Instr = { code: 0x8a, value: {} }
export const BoolToString: Instr = { code: 0x8b, value: {} }
export const GroupOfAddress: Instr = { code: 0x8c, value: {} }
export const LoadMutField = (index: number): Instr => ({ code: 0xa0, value: { index } })
export const StoreMutField = (index: number): Instr => ({ code: 0xa1, value: { index } })
export const ApproveAlph: Instr = { code: 0xa2, value: {} }
Expand Down Expand Up @@ -246,6 +248,12 @@ export const CreateMapEntry: (immFields: number, mutFields: number) => Instr = (
immFields: number,
mutFields: number
) => ({ code: 0xd2, value: { immFields, mutFields } })
export const MethodSelector: (selector: number) => Instr = (selector: number) => {
return { code: 0xd3, value: { index: selector } }
}
export const CallExternalBySelector: (selector: number) => Instr = (selector: number) => {
return { code: 0xd4, value: { index: selector } }
}

export class InstrCodec implements Codec<Instr> {
parser = Parser.start()
Expand Down Expand Up @@ -393,6 +401,7 @@ export class InstrCodec implements Codec<Instr> {
[U256ToString.code]: Parser.start(),
[I256ToString.code]: Parser.start(),
[BoolToString.code]: Parser.start(),
[GroupOfAddress.code]: Parser.start(),
0xa0: Parser.start().uint8('index'),
0xa1: Parser.start().uint8('index'),
[ApproveAlph.code]: Parser.start(),
Expand Down Expand Up @@ -443,7 +452,9 @@ export class InstrCodec implements Codec<Instr> {
[LoadImmFieldByIndex.code]: Parser.start(),
[PayGasFee.code]: Parser.start(),
[MinimalContractDeposit.code]: Parser.start(),
0xd2: Parser.start().uint8('immFields').uint8('mutFields')
0xd2: Parser.start().uint8('immFields').uint8('mutFields'),
0xd3: Parser.start().int32('index'),
0xd4: Parser.start().int32('index')
}
})

Expand All @@ -465,6 +476,8 @@ export class InstrCodec implements Codec<Instr> {
} else if (instr.code === 0xd2) {
const value = instrValue as CreateMapEntryValue
result.push(value.immFields, value.mutFields)
} else if (instr.code === 0xd3 || instr.code === 0xd4) {
result.push(...signedIntCodec.encode((instrValue as InstrValueWithIndex).index))
}

return Buffer.from(result)
Expand Down

0 comments on commit c82e67a

Please sign in to comment.