forked from storyprotocol/protocol-core
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d26b88
commit f7066f8
Showing
2 changed files
with
121 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// mockERC20 - mint, approveSpender, Allowance | ||
|
||
import hre from "hardhat" | ||
import { MockERC20 } from "../constants"; | ||
import { ethers } from "ethers"; | ||
|
||
// mockERC20 - approveSpender | ||
export async function approveSpender(spender: string, amount: bigint, singer: ethers.Wallet):Promise<string> { | ||
const contractAbi = [ | ||
{ | ||
type: "function", | ||
inputs: [ | ||
{ name: "spender", internalType: "address", type: "address" }, | ||
{ name: "amount", internalType: "uint256", type: "uint256" }, | ||
], | ||
name: "approve", | ||
outputs: [{ name: "", internalType: "bool", type: "bool" }], | ||
stateMutability: "nonpayable", | ||
} | ||
]; | ||
|
||
const contract = new hre.ethers.Contract(MockERC20, contractAbi, singer); | ||
|
||
// approveSpender | ||
try { | ||
const tx = await contract.approve( | ||
spender, | ||
amount | ||
); | ||
await tx.wait(); | ||
console.log("hash", tx.hash); | ||
console.log("approveSpender done"); | ||
return tx.hash; | ||
} catch (error) { | ||
console.error("Error approveSpender:", error); | ||
throw error; | ||
}; | ||
}; | ||
|
||
// mockERC20 - mint | ||
export async function mintAmount(toAddress: string, amount: bigint, singer: ethers.Wallet):Promise<string> { | ||
const contractAbi = [ | ||
{ | ||
type: "function", | ||
inputs: [ | ||
{ name: "to", internalType: "address", type: "address" }, | ||
{ name: "amount", internalType: "uint256", type: "uint256" }, | ||
], | ||
name: "mint", | ||
outputs: [{ name: "", internalType: "bool", type: "bool" }], | ||
stateMutability: "nonpayable", | ||
} | ||
]; | ||
|
||
const contract = new hre.ethers.Contract(MockERC20, contractAbi, singer); | ||
|
||
// mintAmount | ||
try { | ||
const tx = await contract.mint( | ||
toAddress, | ||
amount | ||
); | ||
await tx.wait(); | ||
console.log("hash", tx.hash); | ||
return tx.hash; | ||
} catch (error) { | ||
console.error("Error mintAmount:", error); | ||
throw error; | ||
}; | ||
}; | ||
|
||
// mockERC20 - Allowance | ||
export async function getAllowance(owner: string, spender: string, singer: ethers.Wallet):Promise<bigint> { | ||
const contractAbi = [ | ||
{ | ||
type: "function", | ||
inputs: [ | ||
{ name: "owner", internalType: "address", type: "address" }, | ||
{ name: "spender", internalType: "address", type: "address" }, | ||
], | ||
name: "allowance", | ||
outputs: [{ name: "", internalType: "uint256", type: "uint256" }], | ||
stateMutability: "view", | ||
} | ||
]; | ||
|
||
const contract = new hre.ethers.Contract(MockERC20, contractAbi, singer); | ||
|
||
// getAllowance | ||
try { | ||
const tx = await contract.allowance(owner, spender); | ||
console.log("Allowance", tx); | ||
return tx; | ||
} catch (error) { | ||
console.error("Error getAllowance:", error); | ||
throw error; | ||
}; | ||
}; | ||
|
||
export async function checkAndApproveSpender(owner: any, spender: any, amount: bigint) { | ||
const currentAllowance = await getAllowance(owner.address, spender, owner); | ||
if (currentAllowance < amount) { | ||
await mintAmount(owner.address, amount, owner); | ||
await approveSpender(spender, amount, owner); | ||
} | ||
}; |