Skip to content

Commit

Permalink
feat(interaction): add .decode method (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrvk authored Apr 12, 2024
1 parent ec20e9d commit faba434
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/limit-order/interaction.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Interaction} from './interaction'
import {Address} from '../address'

describe('Interaction', () => {
it('should encode/decode', () => {
const interaction = new Interaction(
Address.fromBigInt(1337n),
'0xdeadbeef'
)

expect(Interaction.decode(interaction.encode())).toStrictEqual(
interaction
)
})
})
13 changes: 12 additions & 1 deletion src/limit-order/interaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isHexBytes, trim0x} from '@1inch/byte-utils'
import {BytesIter, isHexBytes, trim0x} from '@1inch/byte-utils'
import assert from 'assert'
import {Address} from '../address'

Expand All @@ -7,6 +7,17 @@ export class Interaction {
assert(isHexBytes(data), 'Interaction data must be valid hex bytes')
}

/**
* Create `Interaction` from bytes
*
* @param bytes Hex string with 0x. First 20 bytes are target, then data
*/
public static decode(bytes: string): Interaction {
const iter = BytesIter.String(bytes)

return new Interaction(new Address(iter.nextUint160()), iter.rest())
}

/**
* Hex string with 0x. First 20 bytes are target, then data
*/
Expand Down

0 comments on commit faba434

Please sign in to comment.