Skip to content

Commit

Permalink
Check if a voucher exists at a given address
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyjams committed Sep 10, 2024
1 parent 07d15fc commit efd08d7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

npx lint-staged
npm run doc
# TODO: Move generated solidity docs to a subfolder and only `git add`` this
# subfolder to the current commit
git add docs/
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## vNEXT
- Check if a voucher exists at a given address. (#42)
- Generate UML class diagram for contracts. (#41)
- Generate Solidity documentation. (#40)
- Add Bellecour poco address to config and harmonize deployment artifacts. (#39)
Expand Down
11 changes: 10 additions & 1 deletion contracts/VoucherHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,15 @@ contract VoucherHub is
return $._matchOrdersEligibility[voucherTypeId][asset];
}

/**
* Check if a voucher exists at a given address.
* @param account The address to be checked.
*/
function isVoucher(address account) external view returns (bool) {
VoucherHubStorage storage $ = _getVoucherHubStorage();
return $._isVoucher[account];
}

/**
* Get voucher address of a given account.
* Returns address(0) if voucher is not found.
Expand All @@ -337,7 +346,7 @@ contract VoucherHub is
_getCreate2Salt(account), // salt
$._voucherCreationCodeHash // bytecode hash
);
return voucherAddress.code.length > 0 ? voucherAddress : address(0);
return $._isVoucher[voucherAddress] ? voucherAddress : address(0);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions docs/VoucherHub.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,20 @@ Check if an asset is eligible to match orders sponsoring.
| voucherTypeId | uint256 | The ID of the voucher type. |
| asset | address | The address of the asset to check. |

### isVoucher

```solidity
function isVoucher(address account) external view returns (bool)
```

Check if a voucher exists at a given address.

#### Parameters

| Name | Type | Description |
| ---- | ---- | ----------- |
| account | address | The address to be checked. |

### getVoucher

```solidity
Expand Down
21 changes: 20 additions & 1 deletion test/VoucherHub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,10 +996,29 @@ describe('VoucherHub', function () {
});
});

describe('Is voucher', function () {
it('Should return true when account is a voucher', async function () {
const { voucherHub, voucherOwner1 } = await loadFixture(deployFixture);
await voucherHubAsManager
.createVoucherType(description, duration)
.then((tx) => tx.wait());
await voucherHubAsMinter
.createVoucher(voucherOwner1, voucherType, voucherValue)
.then((tx) => tx.wait());
const voucherAddress = await voucherHub.getVoucher(voucherOwner1);
expect(await voucherHub.isVoucher(voucherAddress)).to.be.true;
});

it('Should return false when account is not a voucher', async function () {
const { voucherHub } = await loadFixture(deployFixture);
expect(await voucherHub.isVoucher(random())).to.be.false;
});
});

describe('Get voucher', function () {
it('Should return address 0 when voucher is not created', async function () {
const { voucherHub, admin } = await loadFixture(deployFixture);
await expect(await voucherHub.getVoucher(admin)).to.be.equal(ethers.ZeroAddress);
expect(await voucherHub.getVoucher(admin)).to.be.equal(ethers.ZeroAddress);
});
});

Expand Down

0 comments on commit efd08d7

Please sign in to comment.