Skip to content

Commit

Permalink
Prefix legacy USDC constants with BRIDGED_*
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Feb 22, 2024
1 parent 6062a25 commit 5b425ca
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/config/config.local.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const CONFIG = { // eslint-disable-line no-unused-vars
ROOT_REDIRECT: 'https://wallet.nimiq-testnet.com',

POLYGON_CHAIN_ID: 80001,
USDC_CONTRACT_ADDRESS: '0x0FA8781a83E46826621b3BC094Ea2A0212e71B23',
USDC_HTLC_CONTRACT_ADDRESS: '0x2EB7cd7791b947A25d629219ead941fCd8f364BF',
BRIDGED_USDC_CONTRACT_ADDRESS: '0x0FA8781a83E46826621b3BC094Ea2A0212e71B23',
BRIDGED_USDC_HTLC_CONTRACT_ADDRESS: '0x2EB7cd7791b947A25d629219ead941fCd8f364BF',

NATIVE_USDC_CONTRACT_ADDRESS: '0x9999f7Fea5938fD3b1E26A12c3f2fb024e194f97',
NATIVE_USDC_TRANSFER_CONTRACT_ADDRESS: '0x5D101A320547f8D640c44fDfe5d1f35224f00B8B', // v1
Expand Down
4 changes: 2 additions & 2 deletions src/config/config.mainnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const CONFIG = { // eslint-disable-line no-unused-vars
ROOT_REDIRECT: 'https://wallet.nimiq.com',

POLYGON_CHAIN_ID: 137,
USDC_CONTRACT_ADDRESS: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',
USDC_HTLC_CONTRACT_ADDRESS: '0xF615bD7EA00C4Cc7F39Faad0895dB5f40891359f',
BRIDGED_USDC_CONTRACT_ADDRESS: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',
BRIDGED_USDC_HTLC_CONTRACT_ADDRESS: '0xF615bD7EA00C4Cc7F39Faad0895dB5f40891359f',

NATIVE_USDC_CONTRACT_ADDRESS: '0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359',
NATIVE_USDC_TRANSFER_CONTRACT_ADDRESS: '0x3157d422cd1be13AC4a7cb00957ed717e648DFf2', // v1
Expand Down
4 changes: 2 additions & 2 deletions src/config/config.testnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const CONFIG = { // eslint-disable-line no-unused-vars
ROOT_REDIRECT: 'https://wallet.nimiq-testnet.com',

POLYGON_CHAIN_ID: 80001,
USDC_CONTRACT_ADDRESS: '0x0FA8781a83E46826621b3BC094Ea2A0212e71B23',
USDC_HTLC_CONTRACT_ADDRESS: '0x2EB7cd7791b947A25d629219ead941fCd8f364BF',
BRIDGED_USDC_CONTRACT_ADDRESS: '0x0FA8781a83E46826621b3BC094Ea2A0212e71B23',
BRIDGED_USDC_HTLC_CONTRACT_ADDRESS: '0x2EB7cd7791b947A25d629219ead941fCd8f364BF',

NATIVE_USDC_CONTRACT_ADDRESS: '0x9999f7Fea5938fD3b1E26A12c3f2fb024e194f97',
NATIVE_USDC_TRANSFER_CONTRACT_ADDRESS: '0x5D101A320547f8D640c44fDfe5d1f35224f00B8B', // v1
Expand Down
4 changes: 2 additions & 2 deletions src/lib/polygon/PolygonContractABIs.full.js.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable max-len */
const PolygonContractABIsFull = { // eslint-disable-line no-unused-vars
USDC_CONTRACT_ABI: [
BRIDGED_USDC_CONTRACT_ABI: [
'event Approval(address indexed owner, address indexed spender, uint256 value)',
'event AuthorizationCanceled(address indexed authorizer, bytes32 indexed nonce)',
'event AuthorizationUsed(address indexed authorizer, bytes32 indexed nonce)',
Expand Down Expand Up @@ -74,7 +74,7 @@ const PolygonContractABIsFull = { // eslint-disable-line no-unused-vars
'function withdrawWithAuthorization(address owner, uint256 value, uint256 validAfter, uint256 validBefore, bytes32 nonce, uint8 v, bytes32 r, bytes32 s)',
],

USDC_HTLC_CONTRACT_ABI: [
BRIDGED_USDC_HTLC_CONTRACT_ABI: [
'constructor()',
'event DomainRegistered(bytes32 indexed domainSeparator, bytes domainValue)',
'event Open(bytes32 indexed id, address token, uint256 amount, address recipient, bytes32 hash, uint256 timeout)',
Expand Down
4 changes: 2 additions & 2 deletions src/lib/polygon/PolygonContractABIs.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable max-len */
const PolygonContractABIs = { // eslint-disable-line no-unused-vars
USDC_CONTRACT_ABI: [
BRIDGED_USDC_CONTRACT_ABI: [
'function approve(address spender, uint256 amount) returns (bool)',
],

USDC_HTLC_CONTRACT_ABI: [
BRIDGED_USDC_HTLC_CONTRACT_ABI: [
'function open(bytes32 id, address token, uint256 amount, address refundAddress, address recipientAddress, bytes32 hash, uint256 timeout, uint256 fee)',
'function openWithApproval(bytes32 id, address token, uint256 amount, address refundAddress, address recipientAddress, bytes32 hash, uint256 timeout, uint256 fee, uint256 approval, bytes32 sigR, bytes32 sigS, uint8 sigV)',
'function redeem(bytes32 id, address target, bytes32 secret, uint256 fee)',
Expand Down
2 changes: 1 addition & 1 deletion src/lib/polygon/PolygonKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class PolygonKey { // eslint-disable-line no-unused-vars
const domain = {
name: 'USD Coin (PoS)', // This is currently the same for testnet and mainnet
version: '1', // This is currently the same for testnet and mainnet
verifyingContract: CONFIG.USDC_CONTRACT_ADDRESS,
verifyingContract: CONFIG.BRIDGED_USDC_CONTRACT_ADDRESS,
salt: ethers.utils.hexZeroPad(ethers.utils.hexlify(CONFIG.POLYGON_CHAIN_ID), 32),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ class SignPolygonTransaction {
const { sigR, sigS, sigV } = await polygonKey.signUsdcApproval(
request.keyPath,
new ethers.Contract(
CONFIG.USDC_CONTRACT_ADDRESS,
PolygonContractABIs.USDC_CONTRACT_ABI,
CONFIG.BRIDGED_USDC_CONTRACT_ADDRESS,
PolygonContractABIs.BRIDGED_USDC_CONTRACT_ABI,
),
transferContract,
request.description.args.approval,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ class SignPolygonTransactionApi extends PolygonRequestParserMixin(TopLevelApi) {
if (!['transfer', 'transferWithPermit'].includes(description.name)) {
throw new Errors.InvalidRequestError('Requested Polygon contract method is invalid');
}
} else if (forwardRequest.to === CONFIG.USDC_HTLC_CONTRACT_ADDRESS) {
} else if (forwardRequest.to === CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS) {
const usdcHtlcContract = new ethers.Contract(
CONFIG.USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.USDC_HTLC_CONTRACT_ABI,
CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.BRIDGED_USDC_HTLC_CONTRACT_ABI,
);

/** @type {PolygonRefundDescription} */
Expand All @@ -122,7 +122,7 @@ class SignPolygonTransactionApi extends PolygonRequestParserMixin(TopLevelApi) {
value: forwardRequest.value,
}));

if (description.args.token !== CONFIG.USDC_CONTRACT_ADDRESS) {
if (description.args.token !== CONFIG.BRIDGED_USDC_CONTRACT_ADDRESS) {
throw new Errors.InvalidRequestError('Invalid USDC token contract in request data');
}

Expand Down
10 changes: 5 additions & 5 deletions src/request/sign-swap/SignSwap.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,19 +549,19 @@ class SignSwap {
const { sigR, sigS, sigV } = await polygonKey.signUsdcApproval(
request.fund.keyPath,
new ethers.Contract(
CONFIG.USDC_CONTRACT_ADDRESS,
PolygonContractABIs.USDC_CONTRACT_ABI,
CONFIG.BRIDGED_USDC_CONTRACT_ADDRESS,
PolygonContractABIs.BRIDGED_USDC_CONTRACT_ABI,
),
CONFIG.USDC_HTLC_CONTRACT_ADDRESS,
CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS,
request.fund.description.args.approval,
// Has been validated to be defined when function called is `openWithApproval`
/** @type {{ tokenNonce: number }} */ (request.fund.approval).tokenNonce,
request.fund.request.from,
);

const htlcContract = new ethers.Contract(
CONFIG.USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.USDC_HTLC_CONTRACT_ABI,
CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.BRIDGED_USDC_HTLC_CONTRACT_ABI,
);

request.fund.request.data = htlcContract.interface.encodeFunctionData(request.fund.description.name, [
Expand Down
8 changes: 4 additions & 4 deletions src/request/sign-swap/SignSwapApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,13 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
parseOpenGsnForwardRequest(request, allowedMethods) {
const forwardRequest = this.parseOpenGsnForwardRequestRoot(request.request);

if (forwardRequest.to !== CONFIG.USDC_HTLC_CONTRACT_ADDRESS) {
if (forwardRequest.to !== CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS) {
throw new Errors.InvalidRequestError('request.to address is not allowed');
}

const usdcHtlcContract = new ethers.Contract(
CONFIG.USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.USDC_HTLC_CONTRACT_ABI,
CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.BRIDGED_USDC_HTLC_CONTRACT_ABI,
);

// eslint-disable-next-line operator-linebreak
Expand All @@ -345,7 +345,7 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
}

if (description.name === 'open' || description.name === 'openWithApproval') {
if (description.args.token !== CONFIG.USDC_CONTRACT_ADDRESS) {
if (description.args.token !== CONFIG.BRIDGED_USDC_CONTRACT_ADDRESS) {
throw new Errors.InvalidRequestError('Invalid USDC token contract in request data');
}

Expand Down
46 changes: 27 additions & 19 deletions src/request/swap-iframe/SwapIFrameApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint
}
if (storedRawRequest.fund.type === 'USDC') {
const usdcHtlcContract = new ethers.Contract(
CONFIG.USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.USDC_HTLC_CONTRACT_ABI,
CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.BRIDGED_USDC_HTLC_CONTRACT_ABI,
);

storedRawRequest.fund.description = usdcHtlcContract.interface.parseTransaction({
Expand All @@ -102,8 +102,8 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint
}
if (storedRawRequest.redeem.type === 'USDC') {
const usdcHtlcContract = new ethers.Contract(
CONFIG.USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.USDC_HTLC_CONTRACT_ABI,
CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.BRIDGED_USDC_HTLC_CONTRACT_ABI,
);

storedRawRequest.redeem.description = usdcHtlcContract.interface.parseTransaction({
Expand Down Expand Up @@ -226,8 +226,8 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint

if (request.fund.type === 'USDC' && storedRequest.fund.type === 'USDC') {
const usdcHtlcContract = new ethers.Contract(
CONFIG.USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.USDC_HTLC_CONTRACT_ABI,
CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.BRIDGED_USDC_HTLC_CONTRACT_ABI,
);

const description = /** @type {PolygonOpenDescription} */ (usdcHtlcContract.interface.parseTransaction({
Expand All @@ -243,7 +243,7 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint
}

// Verify already known parts of the data
if (description.args.token !== CONFIG.USDC_CONTRACT_ADDRESS) {
if (description.args.token !== CONFIG.BRIDGED_USDC_CONTRACT_ADDRESS) {
throw new Errors.InvalidRequestError('Invalid USDC token contract in HTLC data');
}

Expand Down Expand Up @@ -546,8 +546,8 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint

if (parsedRequest.fund.type === 'USDC' && storedRequest.fund.type === 'USDC') {
const usdcHtlcContract = new ethers.Contract(
CONFIG.USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.USDC_HTLC_CONTRACT_ABI,
CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.BRIDGED_USDC_HTLC_CONTRACT_ABI,
);

// Place contract details into existing function call data
Expand All @@ -572,10 +572,14 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint
],
);

const typedData = new OpenGSN.TypedRequestData(CONFIG.POLYGON_CHAIN_ID, CONFIG.USDC_HTLC_CONTRACT_ADDRESS, {
request: storedRequest.fund.request,
relayData: storedRequest.fund.relayData,
});
const typedData = new OpenGSN.TypedRequestData(
CONFIG.POLYGON_CHAIN_ID,
CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS,
{
request: storedRequest.fund.request,
relayData: storedRequest.fund.relayData,
},
);

const { EIP712Domain, ...cleanedTypes } = typedData.types;

Expand Down Expand Up @@ -695,8 +699,8 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint

if (parsedRequest.redeem.type === 'USDC' && storedRequest.redeem.type === 'USDC') {
const usdcHtlcContract = new ethers.Contract(
CONFIG.USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.USDC_HTLC_CONTRACT_ABI,
CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS,
PolygonContractABIs.BRIDGED_USDC_HTLC_CONTRACT_ABI,
);

// Place contract details into existing function call data
Expand All @@ -712,10 +716,14 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint
],
);

const typedData = new OpenGSN.TypedRequestData(CONFIG.POLYGON_CHAIN_ID, CONFIG.USDC_HTLC_CONTRACT_ADDRESS, {
request: storedRequest.redeem.request,
relayData: storedRequest.redeem.relayData,
});
const typedData = new OpenGSN.TypedRequestData(
CONFIG.POLYGON_CHAIN_ID,
CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS,
{
request: storedRequest.redeem.request,
relayData: storedRequest.redeem.relayData,
},
);

const { EIP712Domain, ...cleanedTypes } = typedData.types;

Expand Down

0 comments on commit 5b425ca

Please sign in to comment.