Example Integrations
This page is intended to provide code examples for common integrations with the Vi API.
X integration
This code example uses the X developer API to look up the user ID of an X account, then uses the Vi API to look up their Vi wallet addresses.
import { Client } from "twitter-api-sdk";
// Initialize X API client
const xClient = new Client("YOUR_X_API_TOKEN");
const VI_API_KEY = "YOUR_VI_API_KEY";
const VI_BASE_URL = "https://api.vi.social/v1";
async function getViWalletForXUser(username) {
try {
// First, fetch user data from X API to get user ID
const user = await xClient.users.findUserByUsername(username);
if (!user.data) {
throw new Error("User not found on X");
}
// Then fetch wallet info from Vi API
const viResponse = await fetch(`${VI_BASE_URL}/wallets/x/${userId}`, {
headers: {
Authorization: `Bearer ${VI_API_KEY}`,
"Content-Type": "application/json",
},
});
if (!viResponse.ok) {
throw new Error(`Vi API error: ${viResponse.statusText}`);
}
const walletData = await viResponse.json();
return {
username: username,
xUserId: user.data.id,
baseWallet: walletData.wallets.base.addresses[0],
solanaWallet: walletData.wallets.solana.addresses[0],
};
} catch (error) {
console.error("Error fetching wallet info:", error);
throw error;
}
}
// Example usage
async function main() {
try {
const walletInfo = await getViWalletForXUser("vitalikbuterin");
console.log("Wallet information:", walletInfo);
} catch (error) {
console.error("Main error:", error);
}
}
main();
Using Vi Social as a Chainlink Oracle
This example shows how to use Vi Social's API as a Chainlink oracle to fetch wallet addresses on-chain.
External Adapter
// vi-adapter.js
// vi-adapter.js
const { Requester, Validator } = require("@chainlink/external-adapter");
const customParams = {
platform: ["platform"],
username: ["username"],
};
const createRequest = (input, callback) => {
const validator = new Validator(callback, input, customParams);
const jobRunID = validator.validated.id;
const platform = validator.validated.data.platform;
const username = validator.validated.data.username;
const url = `https://api.vi.social/v1/wallets/${platform}/${username}`;
const config = {
url,
headers: {
Authorization: `Bearer ${process.env.VI_API_KEY}`,
},
};
Requester.request(config)
.then((response) => {
callback(response.status, {
jobRunID,
data: {
baseWallet: response.data.wallets.base.addresses[0],
solanaWallet: response.data.wallets.solana.addresses[0],
claimed:
response.data.wallets.base.claimed ||
response.data.wallets.solana.claimed,
},
result: response.data.wallets.base.addresses[0],
statusCode: response.status,
});
})
.catch((error) => {
callback(500, Requester.errored(jobRunID, error));
});
};
module.exports.createRequest = createRequest;
Consumer Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
import "@chainlink/contracts/src/v0.8/shared/access/ConfirmedOwner.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract ViSocialOracle is ChainlinkClient, ConfirmedOwner {
using Chainlink for Chainlink.Request;
event WalletAddressFetched(
string indexed platform,
string indexed username,
address baseWallet,
address solanaWallet,
bool claimed
);
address private immutable oracle;
bytes32 private immutable jobId;
uint256 private immutable fee;
// Request tracking
mapping(bytes32 => RequestData) public requests;
mapping(string => UserWallets) public userWallets;
struct RequestData {
string platform;
string username;
}
struct UserWallets {
address baseWallet;
address solanaWallet;
bool claimed;
uint256 lastUpdated;
}
constructor(
address _oracle,
bytes32 _jobId,
address _link
) ConfirmedOwner(msg.sender) {
setChainlinkToken(_link);
oracle = _oracle;
jobId = _jobId;
fee = (1 * LINK_DIVISIBILITY) / 10; // 0.1 LINK
}
function requestWalletAddress(
string memory platform,
string memory username
) public returns (bytes32) {
Chainlink.Request memory req = buildChainlinkRequest(
jobId,
address(this),
this.fulfill.selector
);
req.add("platform", platform);
req.add("username", username);
bytes32 requestId = sendChainlinkRequest(req, fee);
requests[requestId] = RequestData(platform, username);
return requestId;
}
function fulfill(
bytes32 _requestId,
address _baseWallet,
address _solanaWallet,
bool _claimed
) public recordChainlinkFulfillment(_requestId) {
RequestData memory reqData = requests[_requestId];
userWallets[reqData.username] = UserWallets({
baseWallet: _baseWallet,
solanaWallet: _solanaWallet,
claimed: _claimed,
lastUpdated: block.timestamp
});
emit WalletAddressFetched(
reqData.platform,
reqData.username,
_baseWallet,
_solanaWallet,
_claimed
);
delete requests[_requestId];
}
function getUserWallets(string memory username)
public
view
returns (UserWallets memory)
{
return userWallets[username];
}
function withdrawLink() public onlyOwner {
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
require(link.transfer(msg.sender, link.balanceOf(address(this))));
}
}
Setup Steps
Deploy External Adapter to a Chainlink node
Deploy contract on your network (example uses Polygon Mumbai)
Fund contract with LINK
Call
requestWalletAddress
with platform and usernameOracle will fetch and store the wallet address on-chain
Last updated