Skip to content

Commit

Permalink
feat(passport): add referral tracking
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Lahaye <[email protected]>
  • Loading branch information
ChrisLahaye committed Nov 23, 2023
1 parent 3d603d0 commit b135303
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
41 changes: 28 additions & 13 deletions contracts/passport/BaseV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ contract BaseV1 is ERC165Upgradeable, GatewayV1Axelar, GatewayV1Call, IBaseV1 {
uint256 public maxSupply;

uint256 private _tokenIdCounter;
mapping(address => uint256) private _tokens;

uint256 private _totalReservations;

mapping(address => uint256) private _referrals;
mapping(address => uint256) private _reservations;
mapping(address => uint256) private _tokens;

mapping(uint256 => address) private _owners;
mapping(uint256 => bytes32) private _networks;
Expand Down Expand Up @@ -59,7 +60,9 @@ contract BaseV1 is ERC165Upgradeable, GatewayV1Axelar, GatewayV1Call, IBaseV1 {
function grant(
Assignment[] calldata assignments
) public onlyRole(DEFAULT_ADMIN_ROLE) {
if (!_tryReserve(assignments)) {
(bool success, ) = _tryReserve(assignments);

if (!success) {
revert Forbidden();
}
}
Expand Down Expand Up @@ -90,6 +93,12 @@ contract BaseV1 is ERC165Upgradeable, GatewayV1Axelar, GatewayV1Call, IBaseV1 {
}
}

function referrals(
address referrer
) public view override returns (uint256) {
return _referrals[referrer];
}

function reservations(
address owner
) public view override returns (uint256) {
Expand Down Expand Up @@ -228,10 +237,6 @@ contract BaseV1 is ERC165Upgradeable, GatewayV1Axelar, GatewayV1Call, IBaseV1 {
(uint256 purchaseId, address sender, address payable referrer) = abi
.decode(params, (uint256, address, address));

if (_tokens[referrer] == 0) {
delete referrer;
}

bool success;
uint256 tokenId = _tokenIdCounter;

Expand All @@ -246,6 +251,14 @@ contract BaseV1 is ERC165Upgradeable, GatewayV1Axelar, GatewayV1Call, IBaseV1 {
}
}

if (_tokens[referrer] == 0) {
delete referrer;
} else if (success) {
unchecked {
++_referrals[referrer];
}
}

emit ExecutePurchase(
network,
purchaseId,
Expand All @@ -271,7 +284,11 @@ contract BaseV1 is ERC165Upgradeable, GatewayV1Axelar, GatewayV1Call, IBaseV1 {
Assignment[] memory assignments
) = abi.decode(params, (uint256, address, Assignment[]));

bool success = _tryReserve(assignments);
(bool success, uint256 count) = _tryReserve(assignments);

if (success) {
_referrals[sender] += count;
}

emit ExecuteReserve(network, paymentId, sender, assignments, success);

Expand Down Expand Up @@ -361,18 +378,16 @@ contract BaseV1 is ERC165Upgradeable, GatewayV1Axelar, GatewayV1Call, IBaseV1 {

function _tryReserve(
Assignment[] memory assignments
) internal returns (bool success) {
) internal returns (bool success, uint256) {
uint256 totalReservations_ = _totalReservations +
countAssignments(assignments);

if (totalReservations_ + totalSupply() > maxSupply) {
return false;
return (false, 0);
}

_totalReservations = totalReservations_;

_increaseReservations(assignments);

return true;
return (true, _increaseReservations(assignments));
}
}
2 changes: 2 additions & 0 deletions contracts/passport/IBaseV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ interface IBaseV1 is IERC165Upgradeable {

function ownerOf(uint256 tokenId) external view returns (address owner);

function referrals(address referrer) external view returns (uint256);

function reservations(address owner) external view returns (uint256);

function token(address owner) external view returns (uint256);
Expand Down
6 changes: 6 additions & 0 deletions test/Passport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ describe('Passport', () => {

expect(await baseV1.reservations(signers[5].address)).to.eq(0);
});

it('should have referrals', async () => {
const { signers, baseV1 } = await loadFixture(deployFixture);

expect(await baseV1.referrals(signers[5].address)).to.eq(2);
});
});

describe('signer 6', () => {
Expand Down

0 comments on commit b135303

Please sign in to comment.