Skip to content

Commit

Permalink
Utils.IsDeployed
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Sep 3, 2024
1 parent 9989a3d commit 22f390d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Thirdweb.Tests/Thirdweb.Utils/Thirdweb.Utils.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,4 +584,33 @@ public async Task GetAddressFromENS_ReturnsAddress_WhenENSNameIsValid()
result = await Utils.GetAddressFromENS(this.Client, validENSName);
Assert.Equal(expectedAddress.ToChecksumAddress(), result);
}

/*
public static async Task<bool> IsDeployed(ThirdwebClient client, BigInteger chainId, string address)
{
var rpc = ThirdwebRPC.GetRpcInstance(client, chainId);
var code = await rpc.SendRequestAsync<string>("eth_getCode", address, "latest");
return code != "0x";
}
*/

[Fact(Timeout = 120000)]
public async Task IsDeployed_ReturnsTrue_WhenContractIsDeployed()
{
var chainId = new BigInteger(1);
var address = "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D";
var isDeployed = await Utils.IsDeployed(this.Client, chainId, address);

Assert.True(isDeployed);
}

[Fact(Timeout = 120000)]
public async Task IsDeployed_ReturnsFalse_WhenContractIsNotDeployed()
{
var chainId = new BigInteger(1);
var address = await Utils.GetAddressFromENS(this.Client, "vitalik.eth");
var isDeployed = await Utils.IsDeployed(this.Client, chainId, address);

Assert.False(isDeployed);
}
}
7 changes: 7 additions & 0 deletions Thirdweb/Thirdweb.Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -835,4 +835,11 @@ private static string NameHash(string name)
}
return node.EnsureHexPrefix();
}

public static async Task<bool> IsDeployed(ThirdwebClient client, BigInteger chainId, string address)
{
var rpc = ThirdwebRPC.GetRpcInstance(client, chainId);
var code = await rpc.SendRequestAsync<string>("eth_getCode", address, "latest");
return code != "0x";
}
}

0 comments on commit 22f390d

Please sign in to comment.