Integration

Here's an example of how a bounty can be established that will be paid out when sufficient replies to a user's twitter account are met by a user.

/*
 * This contract is to demonstrate some basic features of the fair witness.
 * In this example, a bounty is paid to a recipient once a single user has
 * replied to a set number of tweets.  So if the score is 20, once a user has
 * replied 20 times, the bounty is paid.
 */

contract ReplyBounty {
    // user to create the bounty for
    bytes32 targetUser;
    // amount of the bounty
    uint256 bounty;
    IAttestationOracle public oracle;
    // who will receive the bounty
    address payable recipient;

    bytes32 constant _dataType = "FAIR_WITNESS_TWEET_v0.1";

    mapping(bytes32 => mapping(bytes32 => bool)) private replies;
    mapping(bytes32 => bool) private validConversation;
    mapping(bytes32 => uint256) private userReplyScore;
    
    uint256 targetScore;
    
    constructor(bytes32 _targetUserId, IAttestationOracle _oracle, address payable _recipient, uint256 _targetScore) payable {
        targetUserId = _targetUserId;
        oracle = _oracle;
        bounty = msg.value;
        recipient = _recipient;
        targetScore = _targetScore;
    }

    function pushUserTweet(TweetLib.Tweet memory tweet, bytes memory attestation) external {
        require(oracle.verify(_dataType, tweet.asBytes(), attestation), "must be verified by oracle");

        // TODO: push to conversation mapping
        require(tweet.userId == targetUser);
        validConverstion[tweet.conversationId] = true;
    }

    function pushReply(TweetLib.Tweet memory tweet, bytes memory attestation) external {
        require(oracle.verify(_dataType, tweet.asBytes(), verificationData), "must be verified by oracle");

        require(validConversation[tweet.conversationId]);
        require(replies[tweet.userId][tweet.conversationId] == false, "tweet already indexed");
        replies[tweet.userId][tweet.conversationId] = true;
        userReplyScore += 1;
        
        if (getScore(tweet.userId) > targetScore) {
            deliverBounty();
        }
    }

    function getScore(bytes32 userId) external view returns(uint256) {
        return userReplyScore[userId];
    }
    
    function deliverBounty() public {
        recipient.call{value: bounty}("");
    }
}

Last updated