Skip to content
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

docs: add docs examples for encoding / decoding tuples #460

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 107 additions & 30 deletions docs/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,40 @@ After the `data` is encoded, the object is ready to be stored in smart contracts

#### Examples

<details>
<summary>**Encode** a <code>Singleton</code> data key</summary>

```javascript title="encode a Singleton data key of valueContent Address"
import ERC725 from '@erc725/erc725.js';

const schemas = [
{
name: 'LSP1UniversalReceiverDelegate',
key: '0x0cfc51aec37c55a4d0b1a65c6255c4bf2fbdf6277f3cc0730c45b828b6db8b47',
keyType: 'Singleton',
valueType: 'address',
valueContent: 'Address',
},
];

const myErc725 = new ERC725(schemas);

myErc725.encodeData([
{
keyName: 'LSP1UniversalReceiverDelegate',
value: '0x1183790f29BE3cDfD0A102862fEA1a4a30b3AdAb',
},
]);
/**
{
keys: ['0x0cfc51aec37c55a4d0b1a65c6255c4bf2fbdf6277f3cc0730c45b828b6db8b47'],
values: ['0x1183790f29be3cdfd0a102862fea1a4a30b3adab'],
}
*/
```

</details>

<details>
<summary>Encode a <code>VerifiableURI</code> with JSON and uploaded URL</summary>

Expand Down Expand Up @@ -166,39 +200,10 @@ myErc725.encodeData([
*/
```

```javascript title="encode a Singleton data key of valueContent Address"
import ERC725 from '@erc725/erc725.js';

const schemas = [
{
name: 'LSP1UniversalReceiverDelegate',
key: '0x0cfc51aec37c55a4d0b1a65c6255c4bf2fbdf6277f3cc0730c45b828b6db8b47',
keyType: 'Singleton',
valueType: 'address',
valueContent: 'Address',
},
];

const myErc725 = new ERC725(schemas);

myErc725.encodeData([
{
keyName: 'LSP1UniversalReceiverDelegate',
value: '0x1183790f29BE3cDfD0A102862fEA1a4a30b3AdAb',
},
]);
/**
{
keys: ['0x0cfc51aec37c55a4d0b1a65c6255c4bf2fbdf6277f3cc0730c45b828b6db8b47'],
values: ['0x1183790f29be3cdfd0a102862fea1a4a30b3adab'],
}
*/
```

</details>

<details>
<summary>Encode a <code>VerifiableURI</code> with hash function, hash and uploaded URL</summary>
<summary>**Encode** a <code>VerifiableURI</code> with hash function, hash and uploaded URL</summary>

```javascript title="Encode a VerifiableURI with hash function, hash and uploaded URL"
import ERC725 from '@erc725/erc725.js';
Expand Down Expand Up @@ -463,6 +468,46 @@ myErc725.encodeData(

</details>

<details>
<summary>Encode a tuple for <code>valueType</code> / <code>valueContent</code></summary>

```javascript title="Encode a tuple for valueType / valueContent"
import ERC725 from '@erc725/erc725.js';

const schemas = [
{
name: 'LSP4CreatorsMap:<address>',
key: '0x6de85eaf5d982b4e5da00000<address>',
keyType: 'Mapping',
valueType: '(bytes4,uint128)',
valueContent: '(Bytes4,Number)',
},
];

ERC725.encodeData(
[
{
keyName: 'LSP4CreatorsMap:<address>',
dynamicKeyParts: "0xcafecafecafecafecafecafecafecafecafecafe"
value: ['0x24871b3d', '11'],
},
],
schemas,
);
/**
{
keys: [
'0xdf30dba06db6a30e65354d9a64c609861f089545ca58c6b4dbe31a5f338cb0e3', // -> data key for `LSP4CreatorsMap:0xcafecafecafecafecafecafecafecafecafecafe`
],
values: [
'0x24871b3d0000000000000000000000000000000b', // (bytes4,uint128)
],
}
*/
```

</details>

---

### encodeKeyName
Expand Down Expand Up @@ -888,6 +933,38 @@ myErc725.decodeData(

---

#### Tuple Example

```javascript title="Decode a key with tuple as valueType / valueContent"
import ERC725 from '@erc725/erc725.js';

const schemas = [
{
name: 'LSP4CreatorsMap:<address>',
key: '0x6de85eaf5d982b4e5da00000<address>',
keyType: 'Mapping',
valueType: '(bytes4,uint128)',
valueContent: '(Bytes4,Number)',
},
];

ERC725.decodeData(
{
keyName: 'LSP4CreatorsMap:<address>',
dynamicKeyParts: "0xcafecafecafecafecafecafecafecafecafecafe"
value: "0x24871b3d0000000000000000000000000000000b",
},
schemas,
);
/**
{
key: '0x6de85eaf5d982b4e5da00000cafecafecafecafecafecafecafecafecafecafe',
name: 'LSP4CreatorsMap:cafecafecafecafecafecafecafecafecafecafe',
value: ['0x24871b3d', 11]
}
*/
```

### decodeValueType

```js
Expand Down
7 changes: 7 additions & 0 deletions src/lib/decodeData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ describe('decodeData', () => {
valueType: 'bytes',
valueContent: 'VerifiableURI',
},
{
name: 'LSP4CreatorsMap:<address>',
key: '0x6de85eaf5d982b4e5da00000<address>',
keyType: 'Mapping',
valueType: '(bytes4,uint128)',
valueContent: '(Bytes4,Number)',
},
];

it('decodes each key', () => {
Expand Down
Loading